This repository has been archived by the owner on Jun 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
49 lines (42 loc) · 1.72 KB
/
train.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
from __future__ import division, print_function
import tensorflow as tf
from sklearn.preprocessing import LabelBinarizer
from data import read_dataset
import model
def get_accuracy(x, y, acc, session):
dataset_accuracy = 0
# TODO: Compute accuracy on the dataset
return dataset_accuracy
def train():
tr, va, te = read_dataset('data/mnist.pkl.gz')
binarizer = LabelBinarizer().fit(range(10))
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
keep_prob = tf.placeholder(tf.float32)
preds = model.inference(x, keep_prob)
loss, total_loss = model.loss(preds, y)
acc = model.evaluation(preds, y)
# learning rate: 0.1
train_op = model.training(total_loss, 0.1)
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
for i in xrange(10000):
batch_xs, batch_ys = tr.next_batch(50)
if i % 100 == 0:
train_acc = acc.eval(feed_dict={
x:batch_xs, y:binarizer.transform(batch_ys),
keep_prob: 1.0}, session=sess)
print("step: {0}, training accuracy : {1}".format(i, train_acc))
validation_acc = get_accuracy(va.data[0],
binarizer.transform(va.data[1]),
acc, sess)
print("Validation accuracy : {0}".format(validation_acc))
train_op.run(feed_dict={
x:batch_xs, y:binarizer.transform(batch_ys), keep_prob: 0.5},
session=sess)
test_accuracy = get_accuracy(te.data[0], binarizer.transform(te.data[1]),
acc, sess)
print("Test accuracy : ", test_accuracy)
if __name__ == '__main__':
train()