-
Notifications
You must be signed in to change notification settings - Fork 0
/
TensorFlow.py
153 lines (109 loc) · 4.14 KB
/
TensorFlow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Tensorflow combined with scikitlearn
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import requests
from sklearn import datasets
from sklearn.preprocessing import normalize
from tensorflow.python.framework import ops
'''
# 2d array:-
with tf.compat.v1.Session() as sess:
l = tf.constant([[1, 2, 3], [4, 5, 6]])
d = np.array([[1, 2, 3], [4, 5, 6]])
tf.constant(d)
d = tf.compat.v1.linspace(start=2.0, stop=10.0, num=20)
o = sess.run(d)
print(o)
'''
# -------------------------------------------------------------------------------------------------------------X
'''
#Variables:-
init_vals = tf.compat.v1.random_normal((1,3),8,7)
print(init_vals.shape)
var = tf.Variable(init_vals)
init = tf.compat.v1.global_variables_initializer()
sess.run(init)
print(sess.run(var))
'''
# -------------------------------------------------------------------------------------------------------------X
'''
#Placeholders:-
ph = tf.compat.v1.placeholder(tf.int16)
d = tf.multiply(ph, 2)
print(sess.run(d, feed_dict={ph: 5}))
print(sess.run(d, feed_dict={ph: 15}))
'''
# --------------------------------------------------------------------------------------------------------------X
ops.reset_default_graph()
sess = tf.compat.v1.Session()
bc = datasets.load_breast_cancer()
print("\n")
print(bc.data.shape)
print("\n")
print(bc.data)
print("\n")
print(bc.target)
print("\n")
print(bc.feature_names)
# Splitting the data into training and testing sets
X_vals = bc.data
y_vals = bc.target
train_indices = np.random.choice(len(X_vals), round(len(X_vals)*0.8), replace=False)
test_indices = np.array(list(set(range(len(X_vals))) - set(train_indices)))
print("\n")
print(test_indices)
print("\n")
print(train_indices)
print("\n")
x_vals_train = X_vals[train_indices]
x_vals_test = X_vals[test_indices]
y_vals_train = y_vals[train_indices]
y_vals_test = y_vals[test_indices]
# Creating placeholders for input data and target data
batch_size = 25
tf.compat.v1.disable_eager_execution() # To disable immediate execution
x_data = tf.compat.v1.placeholder(shape=[None,30], dtype=tf.float32)
y_target = tf.compat.v1.placeholder(shape=[None,1], dtype=tf.float32)
A = tf.Variable(tf.compat.v1.random_normal(shape=[30,1], dtype=tf.float32))
B = tf.Variable(tf.compat.v1.random_normal(shape=[1,1]))
model_output = tf.add(tf.matmul(x_data, A), B)
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=model_output, labels=y_target))
init = tf.compat.v1.global_variables_initializer()
sess.run(init)
my_opt = tf.compat.v1.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)
prediction = tf.round(tf.nn.sigmoid(model_output))
prediction_correct = tf.cast(tf.equal(prediction, y_target), tf.float32)
accuracy = tf.reduce_mean(prediction_correct)
# Train Model
loss_vec = []
train_acc =[]
test_acc = []
for i in range(1000):
rand_index = np.random.choice(len(x_vals_train), size=batch_size)
rand_x = x_vals_train[rand_index]
rand_y = np.transpose([y_vals_train[rand_index]])
sess.run(train_step, feed_dict={x_data: rand_x, y_target: rand_y})
temp_loss = sess.run(loss, feed_dict={x_data: rand_x, y_target: rand_y})
loss_vec.append(temp_loss)
temp_acc_train = sess.run(accuracy, feed_dict={x_data: x_vals_train, y_target: np.transpose([y_vals_train])})
train_acc.append(temp_acc_train)
temp_acc_test = sess.run(accuracy, feed_dict={x_data: x_vals_test, y_target: np.transpose([y_vals_test])})
test_acc.append(temp_acc_test)
print(test_acc)
print("\n")
print(train_acc)
# Plot graph
plt.plot(loss_vec, 'k-')
plt.title('Cross Entropy Loss per Generation')
plt.xlabel('Generation')
plt.ylabel('Cross Entropy Loss')
plt.show()
plt.plot(train_acc, 'k-', label='Train set accuracy')
plt.plot(test_acc, 'r--', label='Test set accuracy')
plt.title('Train and Test Accuracy')
plt.xlabel('Generation')
plt.ylabel('Accuracy')
plt.legend(loc='lower right')
plt.show()