-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneuralnet.py
90 lines (60 loc) · 2.38 KB
/
neuralnet.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
import tensorflow as tf
import pickle
n_nodes_h1 = 64
n_classes = 2
batch_size = 10
x = tf.placeholder("float", [None, 4096])
y = tf.placeholder("float")
def neural_net(data):
hidden_layer_1 = {"weights": tf.Variable(tf.random_normal([4096, n_nodes_h1])),
"biases": tf.Variable(tf.random_normal([n_nodes_h1]))}
output_layer = {"weights": tf.Variable(tf.random_normal([n_nodes_h1, n_classes])),
"biases": tf.Variable(tf.random_normal([n_classes]))}
l1 = tf.add(tf.matmul(data, hidden_layer_1["weights"]), hidden_layer_1["biases"])
l1 = tf.nn.relu(l1)
output = tf.add(tf.matmul(l1, output_layer["weights"]), output_layer["biases"])
return output
def train_neural_net(x):
prediction = neural_net(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
optimizer = tf.train.AdamOptimizer().minimize(cost)
no_of_epochs = 5
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
start = 0
end = start + batch_size
for epoch in range(no_of_epochs):
epoch_loss = 0
for i in range(int(len(x_train_data)/batch_size)):
epoch_x = x_train_data[start:end]
epoch_y = y_train_data[start:end]
_, c = sess.run([optimizer, cost], feed_dict={x: epoch_x, y: epoch_y})
epoch_loss += c
end = end + batch_size
print("Epoch ", epoch, " Completed out of ", no_of_epochs, " Loss: ", epoch_loss)
correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct, "float"))
print("Accuracy ", accuracy.eval({x: x_test_data, y: y_test_data}))
def get_training_xy():
pickle_file = open("training_obj.pickle", "rb")
data = pickle.load(pickle_file)
x = []
y = []
for example in data:
x.append(example["features"])
for answers in data:
y.append([answers["label"]])
return x, y
def get_test_xy():
pickle_file = open("test_obj.pickle", "rb")
data = pickle.load(pickle_file)
x = []
y = []
for example in data:
x.append(example["features"])
for answers in data:
y.append([answers["label"]])
return x, y
x_train_data, y_train_data = get_training_xy()
x_test_data, y_test_data = get_test_xy()
train_neural_net(x)