質問編集履歴

1

全体のコードを追加しました

2019/02/12 10:49

投稿

vaitarika
vaitarika

スコア29

test CHANGED
File without changes
test CHANGED
@@ -64,6 +64,356 @@
64
64
 
65
65
  ```
66
66
 
67
+ 下記が全体のコードです
68
+
69
+ ```python
70
+
71
+ import sys
72
+
73
+ import os
74
+
75
+ import numpy as np
76
+
77
+ import tensorflow as tf
78
+
79
+ import tensorflow.python.platform
80
+
81
+
82
+
83
+ from tensorflow.examples.tutorials.mnist import input_data
84
+
85
+ mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
86
+
87
+
88
+
89
+ NUM_CLASSES = 10
90
+
91
+ IMAGE_SIZE = 28
92
+
93
+ IMAGE_PIXELS = IMAGE_SIZE*IMAGE_SIZE
94
+
95
+
96
+
97
+ flags = tf.app.flags
98
+
99
+ FLAGS = flags.FLAGS
100
+
101
+ flags.DEFINE_integer('max_steps', 20, 'Number of steps to run trainer.')
102
+
103
+ flags.DEFINE_integer('batch_size', 1024, 'Batch size'
104
+
105
+ 'Must divide evenly into the dataset sizes.')
106
+
107
+ flags.DEFINE_float('learning_rate', 1e-4, 'Initial learning rate.')
108
+
109
+
110
+
111
+ def inference(images_placeholder, keep_prob):
112
+
113
+
114
+
115
+ def weight_variable(shape):
116
+
117
+ initial = tf.truncated_normal(shape, stddev=0.1)
118
+
119
+ return tf.Variable(initial)
120
+
121
+
122
+
123
+ def bias_variable(shape):
124
+
125
+ initial = tf.constant(0.1, shape=shape)
126
+
127
+ return tf.Variable(initial)
128
+
129
+
130
+
131
+ def conv2d(x, W):
132
+
133
+ return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
134
+
135
+
136
+
137
+ def max_pool_2x2(x):
138
+
139
+ return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
140
+
141
+ strides=[1, 2, 2, 1], padding='SAME')
142
+
143
+
144
+
145
+ x_image = tf.reshape(images_placeholder, [-1, IMAGE_SIZE, IMAGE_SIZE, 1])
146
+
147
+ print(x_image.shape)
148
+
149
+
150
+
151
+
152
+
153
+ # 畳み込み層1の作成
154
+
155
+ with tf.name_scope('conv1') as scope:
156
+
157
+ W_conv1 = weight_variable([5, 5, 1, 32])
158
+
159
+ b_conv1 = bias_variable([32])
160
+
161
+ h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
162
+
163
+
164
+
165
+ # プーリング層1の作成
166
+
167
+ with tf.name_scope('pool1') as scope:
168
+
169
+ h_pool1 = max_pool_2x2(h_conv1)
170
+
171
+
172
+
173
+ # 畳み込み層2の作成
174
+
175
+ with tf.name_scope('conv2') as scope:
176
+
177
+ W_conv2 = weight_variable([5, 5, 32, 64])
178
+
179
+ b_conv2 = bias_variable([64])
180
+
181
+ h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
182
+
183
+
184
+
185
+ # プーリング層2の作成
186
+
187
+ with tf.name_scope('pool2') as scope:
188
+
189
+ h_pool2 = max_pool_2x2(h_conv2)
190
+
191
+
192
+
193
+ # 畳み込み層3の作成
194
+
195
+ with tf.name_scope('conv3') as scope:
196
+
197
+ W_conv3 = weight_variable([5, 5, 64, 128])
198
+
199
+ b_conv3 = bias_variable([128])
200
+
201
+ h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3)
202
+
203
+
204
+
205
+ # プーリング層3の作成
206
+
207
+ with tf.name_scope('pool3') as scope:
208
+
209
+ h_pool3 = max_pool_2x2(h_conv3)
210
+
211
+
212
+
213
+ # 全結合層1の作成
214
+
215
+ with tf.name_scope('fc1') as scope:
216
+
217
+ W_fc1 = weight_variable([7*7*128, 1024])
218
+
219
+ b_fc1 = bias_variable([1024])
220
+
221
+ h_pool3_flat = tf.reshape(h_pool3, [-1, 7*7*128])
222
+
223
+ h_fc1 = tf.nn.relu(tf.matmul(h_pool3_flat, W_fc1) + b_fc1)
224
+
225
+ h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
226
+
227
+
228
+
229
+ # 全結合層2の作成
230
+
231
+ with tf.name_scope('fc2') as scope:
232
+
233
+ W_fc2 = weight_variable([1024, NUM_CLASSES])
234
+
235
+ b_fc2 = bias_variable([NUM_CLASSES])
236
+
237
+
238
+
239
+ with tf.name_scope('softmax') as scope:
240
+
241
+ y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
242
+
243
+
244
+
245
+ return y_conv
246
+
247
+
248
+
249
+ def loss(logits, labels):
250
+
251
+
252
+
253
+ cross_entropy = -tf.reduce_sum(labels*tf.log(logits))
254
+
255
+ tf.summary.scalar("cross_entropy", cross_entropy)
256
+
257
+ return cross_entropy
258
+
259
+
260
+
261
+ def training(loss, learning_rate):
262
+
263
+
264
+
265
+ train_step = tf.train.AdamOptimizer(learning_rate).minimize(loss)
266
+
267
+ return train_step
268
+
269
+
270
+
271
+ def accuracy(logits, labels):
272
+
273
+
274
+
275
+ correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
276
+
277
+ accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
278
+
279
+ tf.summary.scalar("accuracy", accuracy)
280
+
281
+ return accuracy
282
+
283
+
284
+
285
+ if __name__ == '__main__':
286
+
287
+
288
+
289
+ with tf.Graph().as_default():
290
+
291
+ images_placeholder = tf.placeholder("float", shape=(None, IMAGE_PIXELS))
292
+
293
+
294
+
295
+ labels_placeholder = tf.placeholder("float", shape=(None, NUM_CLASSES))
296
+
297
+
298
+
299
+ keep_prob = tf.placeholder("float")
300
+
301
+
302
+
303
+
304
+
305
+ logits = inference(images_placeholder, keep_prob)
306
+
307
+
308
+
309
+ loss_value = loss(logits, labels_placeholder)
310
+
311
+
312
+
313
+ train_op = training(loss_value, FLAGS.learning_rate)
314
+
315
+
316
+
317
+ acc = accuracy(logits, labels_placeholder)
318
+
319
+
320
+
321
+ saver = tf.train.Saver()
322
+
323
+
324
+
325
+ sess = tf.Session()
326
+
327
+
328
+
329
+ sess.run(tf.global_variables_initializer())
330
+
331
+
332
+
333
+ summary_op = tf.summary.merge_all()
334
+
335
+ summary_writer = tf.summary.FileWriter('./logs', sess.graph)
336
+
337
+
338
+
339
+ for step in range(FLAGS.max_steps):
340
+
341
+ for i in range(int(len(mnist.train.images)/FLAGS.batch_size)):
342
+
343
+
344
+
345
+ batch = FLAGS.batch_size * i
346
+
347
+
348
+
349
+ sess.run(train_op, feed_dict={
350
+
351
+ images_placeholder: mnist.train.images[batch:batch+FLAGS.batch_size],
352
+
353
+ labels_placeholder: mnist.train.labels[batch:batch+FLAGS.batch_size],
354
+
355
+ keep_prob: 0.5})
356
+
357
+
358
+
359
+ train_accuracy = sess.run(acc, feed_dict={
360
+
361
+ images_placeholder: mnist.train.images,
362
+
363
+ labels_placeholder: mnist.train.labels,
364
+
365
+ keep_prob: 1.0})
366
+
367
+ print ("step {}, training accuracy {}".format(step, train_accuracy))
368
+
369
+
370
+
371
+ summary_str = sess.run(summary_op, feed_dict={
372
+
373
+ images_placeholder: mnist.train.images,
374
+
375
+ labels_placeholder: mnist.train.labels,
376
+
377
+ keep_prob: 1.0})
378
+
379
+ summary_writer.add_summary(summary_str, step)
380
+
381
+
382
+
383
+ print ("test accuracy {}".format(sess.run(acc, feed_dict={
384
+
385
+ images_placeholder: mnist.test.images,
386
+
387
+ labels_placeholder: mnist.test.labels,
388
+
389
+ keep_prob: 1.0})))
390
+
391
+
392
+
393
+ summary_str_1 = sess.run(summary_op, feed_dict={
394
+
395
+ images_placeholder: mnist.test.images,
396
+
397
+ labels_placeholder: mnist.test.labels,
398
+
399
+ keep_prob: 1.0})
400
+
401
+ summary_writer.add_summary(summary_str_1, step)
402
+
403
+
404
+
405
+ cwd = os.getcwd()
406
+
407
+ save_path = saver.save(sess, cwd + "//model.ckpt")
408
+
409
+ sess = tf.InteractiveSession()
410
+
411
+ saver.restore(sess, "model.ckpt")
412
+
413
+
414
+
415
+ ```
416
+
67
417
  ### 補足情報
68
418
 
69
419
  開発環境