Skip to content

Commit 2fc9455

Browse files
committed
Adjust for library updates
1 parent 22673f4 commit 2fc9455

17 files changed

+199
-47
lines changed

n0_network.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def fit(self, truthed_data, nEpochs=5000):
3030
summary_str = result[0]
3131

3232
self._writer.add_summary(summary_str, i)
33-
train_accuracy = self._accuracy.eval(feed)
33+
train_accuracy = result[1]
3434
if train_accuracy <= (1.0 - 1e-5 ):
3535
perfect_count=10;
3636
else:
@@ -39,7 +39,7 @@ def fit(self, truthed_data, nEpochs=5000):
3939
break;
4040

4141
print ("step %d, training accuracy %g"%(i, train_accuracy),flush=True)
42-
self._train_step.run(feed_dict=feed)
42+
self._sess.run(self._train_step,feed_dict=feed)
4343

4444

4545

n1_2cnv1fc.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ def max_pool_2x2(x):
105105
with tf.name_scope("reshape_x_image") as scope:
106106
self._x_image = tf.reshape(self._ph.image, [-1,self._nCols,self._nRows,1])
107107

108-
image_summ = tf.image_summary("x_image", self._x_image)
108+
image_summ = tf.summary.image("x_image", self._x_image)
109109

110110
"""# ==============================================================================
111111
@@ -156,7 +156,9 @@ def max_pool_2x2(x):
156156

157157
# append the features, the 2nd on, that go directly to the fully connected layer
158158
for i in range(2,truthed_features.num_features ):
159-
h_pool2_flat = tf.concat(1, [h_pool2_flat, self._ph[i]])
159+
print(i)
160+
print(self._ph[i])
161+
h_pool2_flat = tf.concat([h_pool2_flat, self._ph[i]],1)
160162
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
161163

162164
"""# ==============================================================================
@@ -184,7 +186,7 @@ def max_pool_2x2(x):
184186

185187
# 1e-8 added to eliminate the crash of training when taking log of 0
186188
cross_entropy = -tf.reduce_sum(self._ph[0]*tf.log(y_conv+ 1e-8 ))
187-
ce_summ = tf.scalar_summary("cross entropy", cross_entropy)
189+
ce_summ = tf.summary.scalar("cross entropy", cross_entropy)
188190

189191
with tf.name_scope("train") as scope:
190192
self._train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
@@ -194,21 +196,22 @@ def max_pool_2x2(x):
194196
self._prediction = tf.argmax(y_conv,1)
195197

196198
self._accuracy = tf.reduce_mean(tf.cast(self._correct_prediction, dtype))
197-
accuracy_summary = tf.scalar_summary("accuracy", self._accuracy)
199+
accuracy_summary = tf.summary.scalar("accuracy", self._accuracy)
200+
weight_summary = tf.summary.histogram("weights", W_fc2)
198201
"""# ==============================================================================
199202
200203
Start TensorFlow Interactive Session
201204
202205
"""# ==============================================================================
203206

204207
self._sess.run(tf.initialize_all_variables())
205-
self._merged = tf.merge_all_summaries()
208+
self._merged = tf.summary.merge_all()
206209
tm = ""
207210
tp = datetime.datetime.now().timetuple()
208211
for i in range(4):
209212
tm += str(tp[i])+'-'
210213
tm += str(tp[4])
211-
self._writer = tf.train.SummaryWriter("/tmp/ds_logs/"+ tm, self._sess.graph)
214+
self._writer = tf.summary.FileWriter("/tmp/ds_logs/"+ tm, self._sess.graph)
212215

213216
def computeSize(s,tens):
214217
sumC = 1

n1_2cnv2fc.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def max_pool_2x2(x):
107107
with tf.name_scope("reshape_x_image") as scope:
108108
self._x_image = tf.reshape(self._ph.image, [-1,self._nCols,self._nRows,1])
109109

110-
image_summ = tf.image_summary("x_image", self._x_image)
110+
image_summ = tf.summary.image("x_image", self._x_image)
111111

112112
"""# ==============================================================================
113113
@@ -219,7 +219,7 @@ def max_pool_2x2(x):
219219

220220
# 1e-8 added to eliminate the crash of training when taking log of 0
221221
cross_entropy = -tf.reduce_sum(self._ph[0]*tf.log(y_conv+ 1e-8 ))
222-
ce_summ = tf.scalar_summary("cross entropy", cross_entropy)
222+
ce_summ = tf.summary.scalar("cross entropy", cross_entropy)
223223

224224
with tf.name_scope("train") as scope:
225225
self._train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
@@ -229,21 +229,21 @@ def max_pool_2x2(x):
229229
self._prediction = tf.argmax(y_conv,1)
230230

231231
self._accuracy = tf.reduce_mean(tf.cast(self._correct_prediction, dtype))
232-
accuracy_summary = tf.scalar_summary("accuracy", self._accuracy)
232+
accuracy_summary = tf.summary.scalar("accuracy", self._accuracy)
233233
"""# ==============================================================================
234234
235235
Start TensorFlow Interactive Session
236236
237237
"""# ==============================================================================
238238

239239
self._sess.run(tf.initialize_all_variables())
240-
self._merged = tf.merge_all_summaries()
240+
self._merged = tf.summary.merge_all()
241241
tm = ""
242242
tp = datetime.datetime.now().timetuple()
243243
for i in range(4):
244244
tm += str(tp[i])+'-'
245245
tm += str(tp[4])
246-
self._writer = tf.train.SummaryWriter("/tmp/ds_logs/"+ tm, self._sess.graph)
246+
self._writer = tf.summary.FileWriter("/tmp/ds_logs/"+ tm, self._sess.graph)
247247

248248
def computeSize(s,tens):
249249
sumC = 1

n1_baseTensorNN.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -141,11 +141,11 @@ def max_pool_2x2(x):
141141
with tf.name_scope("xent") as scope:
142142

143143
# 1e-8 added to eliminate the crash of training when taking log of 0
144-
cross_entropy = -tf.reduce_sum(self._ph[0]*tf.log(y_conv+ 1e-8 ))
145-
ce_summ = tf.scalar_summary("cross entropy", cross_entropy)
144+
self._cross_entropy = -tf.reduce_sum(self._ph[0]*tf.log(y_conv+ 1e-8 ))
145+
ce_summ = tf.scalar_summary("cross entropy", self._cross_entropy)
146146

147147
with tf.name_scope("train") as scope:
148-
self._train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
148+
self._train_step = tf.train.AdamOptimizer(1e-4).minimize(self._cross_entropy)
149149

150150
with tf.name_scope("test") as scope:
151151
self._correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(self._ph[0],1))
@@ -155,10 +155,10 @@ def max_pool_2x2(x):
155155
accuracy_summary = tf.scalar_summary("accuracy", self._accuracy)
156156
"""# ==============================================================================
157157
158-
Start TensorFlow Interactive Session
158+
Start TensorFlow Session
159159
160160
"""# ==============================================================================
161-
self._sess = tf.InteractiveSession()
161+
self._sess = tf.Session()
162162
self._sess.run(tf.initialize_all_variables())
163163
self._merged = tf.merge_all_summaries()
164164
tm = ""
@@ -215,7 +215,7 @@ def fit(self, truthed_data, nEpochs=5000):
215215
summary_str = result[0]
216216
#acc = result[1]
217217
self._writer.add_summary(summary_str, i)
218-
train_accuracy = self._accuracy.eval(feed)
218+
train_accuracy = result[1]
219219
if train_accuracy <= (1.0 - 1e-5 ):
220220
perfect_count=10;
221221
else:
@@ -224,7 +224,7 @@ def fit(self, truthed_data, nEpochs=5000):
224224
break;
225225

226226
print ("step %d, training accuracy %g"%(i, train_accuracy),flush=True)
227-
self._train_step.run(feed_dict=feed)
227+
self._sess.run(self._train_step.run,feed_dict=feed)
228228

229229

230230

n1_image_to_image.py

+41-9
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def max_pool_2x2(x):
107107
with tf.name_scope("reshape_x_image") as scope:
108108
self._x_image = tf.reshape(self._ph.image, [-1,self._nCols,self._nRows,1])
109109

110-
image_summ = tf.image_summary("x_image", self._x_image)
110+
image_summ = tf.summary.image("x_image", self._x_image)
111111

112112
"""# ==============================================================================
113113
@@ -218,40 +218,40 @@ def max_pool_2x2(x):
218218
with tf.name_scope("xent") as scope:
219219

220220
# 1e-8 added to eliminate the crash of training when taking log of 0
221-
cross_entropy = -tf.reduce_sum(self._ph[0]*tf.log(y_conv+ 1e-8 ))
221+
self._cross_entropy = -tf.reduce_sum(self._ph[0]*tf.log(y_conv+ 1e-8 ))
222222
#cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
223223
# logits, labels, name='xentropy')
224-
ce_summ = tf.scalar_summary("cross entropy", cross_entropy)
224+
ce_summ = tf.summary.scalar("cross entropy", self._cross_entropy)
225225

226226
with tf.name_scope("reshape_x_image2") as scope:
227227
self._x_image2 = tf.reshape(self._ph[0], [-1,int(self._nCols/2),int(self._nRows/2),1])
228228

229-
image_summ2 = tf.image_summary("x_image2", self._x_image2)
229+
image_summ2 = tf.summary.image("x_image2", self._x_image2)
230230

231231
with tf.name_scope("train") as scope:
232-
self._train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
232+
self._train_step = tf.train.AdamOptimizer(1e-4).minimize(self._cross_entropy)
233233
#self._train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
234234

235235
with tf.name_scope("test") as scope:
236236
self._correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(self._ph[0],1))
237237
self._prediction = tf.argmax(y_conv,1)
238238

239239
self._accuracy = tf.reduce_mean(tf.cast(self._correct_prediction, dtype))
240-
accuracy_summary = tf.scalar_summary("accuracy", self._accuracy)
240+
accuracy_summary = tf.summary.scalar("accuracy", self._accuracy)
241241
"""# ==============================================================================
242242
243-
Start TensorFlow Interactive Session
243+
Start TensorFlow Session
244244
245245
"""# ==============================================================================
246246

247247
self._sess.run(tf.initialize_all_variables())
248-
self._merged = tf.merge_all_summaries()
248+
self._merged = tf.summary.merge_all()
249249
tm = ""
250250
tp = datetime.datetime.now().timetuple()
251251
for i in range(4):
252252
tm += str(tp[i])+'-'
253253
tm += str(tp[4])
254-
self._writer = tf.train.SummaryWriter("/tmp/ds_logs/"+ tm, self._sess.graph)
254+
self._writer = tf.summary.FileWriter("/tmp/ds_logs/"+ tm, self._sess.graph)
255255

256256
def computeSize(s,tens):
257257
sumC = 1
@@ -319,4 +319,36 @@ def test2(self, truthed_data, title = ''):
319319
ocr_utils.montage(output_images,title='TensorFlow Output Images')
320320
ocr_utils.montage(input_images,title='TensorFlow Input Images')
321321

322+
def fit_entropy(self, truthed_data, nEpochs=5000):
323+
324+
perfect_count=10
325+
for i in range(nEpochs):
326+
327+
batch = truthed_data.next_batch(100)
328+
# assign feature data to each placeholder
329+
# the batch list is returned in the same order as the features requested
330+
feed = {self._keep_prob: 0.5}
331+
for j in range(truthed_data.num_features):
332+
feed[self._ph[j]] = batch[j]
333+
334+
if i%100 == 0:
335+
336+
feed[self._keep_prob] = 1.0
337+
result = self._sess.run([self._merged, self._cross_entropy ], feed_dict=feed)
338+
summary_str = result[0]
339+
340+
self._writer.add_summary(summary_str, i)
341+
train_entropy = result[1]
342+
if train_entropy >= (2000 ):
343+
perfect_count=10;
344+
else:
345+
perfect_count -= 1
346+
if perfect_count==0:
347+
break;
348+
349+
print ("step %d, training entropy %g"%(i, train_entropy),flush=True)
350+
self._sess.run(self._train_step,feed_dict=feed)
351+
352+
353+
322354

n1_residual3x4.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def computeSize(s,tens):
169169
with tf.name_scope("xent") as scope:
170170
# 1e-8 added to eliminate the crash of training when taking log of 0
171171
cross_entropy = -tf.reduce_sum(self._ph[0]*tf.log(y_conv+1e-8))
172-
ce_summ = tf.scalar_summary("cross entropy", cross_entropy)
172+
ce_summ = tf.summary.scalar("cross entropy", cross_entropy)
173173

174174
with tf.name_scope("train") as scope:
175175
self._train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
@@ -179,7 +179,7 @@ def computeSize(s,tens):
179179
self._prediction = tf.argmax(y_conv,1)
180180

181181
self._accuracy = tf.reduce_mean(tf.cast(self._correct_prediction, dtype))
182-
accuracy_summary = tf.scalar_summary("accuracy", self._accuracy)
182+
accuracy_summary = tf.summary.scalar("accuracy", self._accuracy)
183183

184184
"""# ==============================================================================
185185
@@ -188,7 +188,7 @@ def computeSize(s,tens):
188188
"""# ==============================================================================
189189

190190
self._sess.run(tf.initialize_all_variables())
191-
self._merged = tf.merge_all_summaries()
191+
self._merged = tf.summary.merge_all()
192192
tm = ""
193193
tp = datetime.datetime.now().timetuple()
194194
for i in range(4):
@@ -200,7 +200,7 @@ def computeSize(s,tens):
200200
# tensorboard --logdir '/tmp/ds_logs/'
201201
# See results on localhost:6006
202202

203-
self._writer = tf.train.SummaryWriter("/tmp/ds_logs/"+ tm, self._sess.graph)
203+
self._writer = tf.summary.FileWriter("/tmp/ds_logs/"+ tm, self._sess.graph)
204204

205205
def computeSize(s,tens):
206206
sumC = 1

o3_top_secret_python_box.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@
7070
from sklearn.metrics import accuracy_score
7171
from sklearn.decomposition import PCA
7272
from sklearn.metrics import accuracy_score
73-
from sklearn.lda import LDA
73+
#from sklearn.model_selection
74+
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
7475
from sklearn.linear_model import LogisticRegression
7576
from sklearn.cross_validation import train_test_split
7677

@@ -233,7 +234,7 @@ def find_min_max(sums):
233234

234235

235236
n_components = 2
236-
lda = LDA(n_components=n_components)
237+
lda = LinearDiscriminantAnalysis(n_components=n_components)
237238

238239
X_train_lda = lda.fit_transform(X_train, y_train)
239240
X_test_lda = lda.transform(X_test)

o4_image_to_image.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
#output_feature_list = ['font_one_hot','image','italic','aspect_ratio','upper_case']
6969

7070
# train the digits 0-9 for all fonts
71-
input_filters_dict = {'m_label': range(48,58),'italic':0,'strength':.4}
71+
input_filters_dict = {'m_label': [43]+list(range(48,58)),'italic':0,'strength':.4}
7272
#input_filters_dict = {'font':'BANKGOTHIC','m_label': list(range(48,58)),'italic':0,'strength':.7}
7373
#input_filters_dict = {}
7474
output_feature_list = ['low_pass_image','image']
@@ -83,7 +83,7 @@
8383
test_size = .2,
8484
engine_type='tensorflow',dtype=dtype)
8585
nn = nnetwork.network(ds.train)
86-
nn.fit( ds.train, nEpochs=5000)
86+
nn.fit_entropy( ds.train, nEpochs=5000)
8787
nn.test2(ds.test)
8888

8989
# train_a_font(input_filters_dict, output_feature_list, nEpochs = 50000)

p115_l1_l2_regularization.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100

101101
def weight_graph(regularization = 'l1'):
102102
weights, params = [], []
103-
for c in np.arange(-4, 6):
103+
for c in np.arange(0, 6):
104104
lr = LogisticRegression(penalty=regularization, C=10**c, random_state=0)
105105
lr.fit(X_train_std, y_train)
106106
weights.append(lr.coef_[1])

p119_squential_backward_selection.py

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ def __init__(self, estimator, k_features,
8080
self.k_features = k_features
8181
self.test_size = test_size
8282
self.random_state = random_state
83+
8384
def fit(self, X, y):
8485
X_train, X_test, y_train, y_test = \
8586
train_test_split(X, y, test_size=self.test_size,

p141_linear_descriminant_analsys.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@
5757
import numpy as np
5858
import ocr_utils
5959
import matplotlib.pyplot as plt
60-
from sklearn.lda import LDA
61-
60+
#from sklearn.lda import LDA
61+
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
6262
print_limit = 20
6363
chars_to_train = range(48,58)
6464
columnsXY=range(0,20)
@@ -115,7 +115,7 @@
115115
print('Within-class scatter matrix: {}x{}'.format(S_W.shape[0], S_W.shape[1]))
116116

117117
print('Class label distribution: %s'
118-
% np.bincount(np.array(y_train,dtype='int32'))[min(y_train):])
118+
% np.bincount(np.array(y_train,dtype='int32'))[int(min(y_train)):])
119119

120120
d = S_W.shape[1] # number of features
121121
S_W = np.zeros((d, d))

p177_k_fold_cross_validation.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
import matplotlib.pyplot as plt
5050
import numpy as np
5151
from sklearn.cross_validation import StratifiedKFold
52-
from sklearn.lda import LDA
52+
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis as LDA
5353

5454
if __name__ == '__main__':
5555
#charsToTrain=range(48,58)

p356_neural_net.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def _encode_labels(self, y, k):
178178
"""
179179
onehot = np.zeros((k, y.shape[0]))
180180
for idx, val in enumerate(y):
181-
onehot[val, idx] = 1.0
181+
onehot[int(val), idx] = 1.0
182182
return onehot
183183

184184
def _initialize_weights(self):
@@ -587,7 +587,7 @@ def _encode_labels(self, y, k):
587587
"""
588588
onehot = np.zeros((k, y.shape[0]))
589589
for idx, val in enumerate(y):
590-
onehot[val, idx] = 1.0
590+
onehot[int(val), idx] = 1.0
591591
return onehot
592592

593593
def _initialize_weights(self):

0 commit comments

Comments
 (0)