/
mnist.py
138 lines (95 loc) · 3.68 KB
/
mnist.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
# base imports
import sys
import getopt
# numerical packages / tensorflow
import random
import pandas as pd
import numpy as np
import tensorflow as tf
# user defined code imports
from data import Data
from models import get_models
from tf_utils import *
verbose = False
def usage():
print('Usage')
print('python %s : runs the set of models' % sys.argv[0])
print('python %s -v|--verbose : runs the set of models with verbose output' % sys.argv[0])
print('python %s -h : prints this help and exits' % sys.argv[0])
def run_model(train, accuracy, kp, x, y, vbatch, dtrain):
global verbose
# create a validation feed dictionary for this model run in the ensemble
v_feed_dict = { x: vbatch[0], y: vbatch[1] }
# iterate over batches for training
for i in range(5000):
# get a batch
batch = dtrain.get_batch()
# create the training feed dict
tr_feed_dict = { x: batch[0], y: batch[1] }
# report accuracy if i modulo 1000 is zero - i.e. every 1000th step - includes first step
if i % 1000 == 0 and verbose:
# set a value to feed kp if it has been set
if kp is not None:
tr_feed_dict[kp] = 1.0
train_acc = accuracy.eval(feed_dict=tr_feed_dict)
print('step %d, training accuracy %g' % (i, train_acc))
# set a value to feed kp if it has been set
if kp is not None:
v_feed_dict[kp] = 1.0
valid_acc = accuracy.eval(feed_dict=v_feed_dict)
print('Validation accuracy %g' % valid_acc)
# set a value to feed kp if it has been set
if kp is not None:
tr_feed_dict[kp] = 0.5
# run a training step
train.run(feed_dict=tr_feed_dict)
# get the final accuracy
# set a value to feed kp if it has been set
if kp is not None:
v_feed_dict[kp] = 1.0
valid_acc = accuracy.eval(feed_dict=v_feed_dict)
if verbose:
print('\nFinal Validation accuracy: %g\n' % valid_acc)
return valid_acc
def run_ensemble(sess, m, x, y, vbatch, dtrain):
# print the model description to screen
print('======================')
print(m[0])
# get the accuracy
accuracy = m[2]
# get the trainer
train = m[1]
# get the keep probability
kp = m[3]
acc = []
ensemble_count = 3
for i in range(ensemble_count):
# initialise global variables in the graph
sess.run(tf.global_variables_initializer())
acc.append(run_model(train, accuracy, kp, x, y, vbatch, dtrain))
print(acc)
print('\n\nEnsemble average validation accuracy: %g\n\n' % (sum(acc)/len(acc)))
print('======================')
if __name__ == '__main__':
# get command line options
optlist, args = getopt.getopt(sys.argv[1:], 'vh', ['verbose', 'help'])
for (k, v) in optlist:
if k in ('-v', '--verbose'):
verbose = True
elif k in ('-h', '--help'):
usage()
sys.exit(0)
# create an instance of the Data class
dtrain = Data('datasets/train-exploration.csv')
dvalid = Data('datasets/validation-exploration.csv')
# create placeholders for x and y
x = tf.placeholder(tf.float32, shape=[None, 28, 28, 1])
y = tf.placeholder(tf.float32, shape=[None, 10])
# build the neural network
models = get_models(x, y)
with tf.Session() as sess:
# get a validation batch
vbatch = (dvalid.features, dvalid.labels) # get_batch(4207)
# iterate over all models
for m in models:
run_ensemble(sess, m, x, y, vbatch, dtrain)