forked from lazyprogrammer/machine_learning_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_with_save.py
151 lines (115 loc) · 4.23 KB
/
tf_with_save.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
# https://deeplearningcourses.com/c/data-science-deep-learning-in-theano-tensorflow
# https://www.udemy.com/data-science-deep-learning-in-theano-tensorflow
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import json
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from util import get_normalized_data, y2indicator
def error_rate(p, t):
return np.mean(p != t)
class TFLogistic:
def __init__(self, savefile, D=None, K=None):
self.savefile = savefile
if D and K:
# we can define some parts in the model to be able to make predictions
self.build(D, K)
def build(self, D, K):
W0 = np.random.randn(D, K) * np.sqrt(2.0 / D)
b0 = np.zeros(K)
# define variables and expressions
self.inputs = tf.placeholder(tf.float32, shape=(None, D), name='inputs')
self.targets = tf.placeholder(tf.int64, shape=(None,), name='targets')
self.W = tf.Variable(W0.astype(np.float32), name='W')
self.b = tf.Variable(b0.astype(np.float32), name='b')
# variables must exist when calling this
# try putting this line in the constructor and see what happens
self.saver = tf.train.Saver({'W': self.W, 'b': self.b})
logits = tf.matmul(self.inputs, self.W) + self.b
cost = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(
logits=logits,
labels=self.targets
)
)
self.predict_op = tf.argmax(logits, 1)
return cost
def fit(self, X, Y, Xtest, Ytest):
N, D = X.shape
K = len(set(Y))
# hyperparams
max_iter = 30
lr = 1e-3
mu = 0.9
regularization = 1e-1
batch_sz = 100
n_batches = N // batch_sz
cost = self.build(D, K)
l2_penalty = regularization*tf.reduce_mean(self.W**2) / 2
cost += l2_penalty
train_op = tf.train.MomentumOptimizer(lr, momentum=mu).minimize(cost)
costs = []
init = tf.global_variables_initializer()
with tf.Session() as session:
session.run(init)
for i in range(max_iter):
for j in range(n_batches):
Xbatch = X[j*batch_sz:(j*batch_sz + batch_sz),]
Ybatch = Y[j*batch_sz:(j*batch_sz + batch_sz),]
session.run(train_op, feed_dict={self.inputs: Xbatch, self.targets: Ybatch})
if j % 100 == 0:
test_cost = session.run(cost, feed_dict={self.inputs: Xtest, self.targets: Ytest})
Ptest = session.run(self.predict_op, feed_dict={self.inputs: Xtest})
err = error_rate(Ptest, Ytest)
print("Cost / err at iteration i=%d, j=%d: %.3f / %.3f" % (i, j, test_cost, err))
costs.append(test_cost)
# save the model
self.saver.save(session, self.savefile)
# save dimensions for later
self.D = D
self.K = K
plt.plot(costs)
plt.show()
def predict(self, X):
with tf.Session() as session:
# restore the model
self.saver.restore(session, self.savefile)
P = session.run(self.predict_op, feed_dict={self.inputs: X})
return P
def score(self, X, Y):
return 1 - error_rate(self.predict(X), Y)
def save(self, filename):
j = {
'D': self.D,
'K': self.K,
'model': self.savefile
}
with open(filename, 'w') as f:
json.dump(j, f)
@staticmethod
def load(filename):
with open(filename) as f:
j = json.load(f)
return TFLogistic(j['model'], j['D'], j['K'])
def main():
X, Y = get_normalized_data()
Xtrain = X[:-1000,]
Ytrain = Y[:-1000]
Xtest = X[-1000:,]
Ytest = Y[-1000:]
model = TFLogistic("tf.model")
model.fit(Xtrain, Ytrain, Xtest, Ytest)
# test out restoring the model via the predict function
print("final train accuracy:", model.score(Xtrain, Ytrain))
print("final test accuracy:", model.score(Xtest, Ytest))
# save the model
model.save("my_trained_model.json")
# load and score again
model = TFLogistic.load("my_trained_model.json")
print("final train accuracy (after reload):", model.score(Xtrain, Ytrain))
print("final test accuracy (after reload):", model.score(Xtest, Ytest))
if __name__ == '__main__':
main()