質問編集履歴
1
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -13,3 +13,193 @@
|
|
13
13
|
というエラーメッセージが出てしまいます。
|
14
14
|
|
15
15
|
なかなか解決策が見つからないのでご教授お願いします。
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
追記
|
20
|
+
|
21
|
+
http://qiita.com/toyolab/items/bccd03d4cb7795112ab6
|
22
|
+
|
23
|
+
上記のサイトを参考に環境を構築しました
|
24
|
+
|
25
|
+
実行コードは以下の通りです。
|
26
|
+
|
27
|
+
```python
|
28
|
+
|
29
|
+
from tensorflow.examples.tutorials.mnist import input_data
|
30
|
+
|
31
|
+
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
import tensorflow as tf
|
36
|
+
|
37
|
+
sess = tf.InteractiveSession()
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
x = tf.placeholder(tf.float32, shape=[None, 784])
|
42
|
+
|
43
|
+
y_ = tf.placeholder(tf.float32, shape=[None, 10])
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
W = tf.Variable(tf.zeros([784,10]))
|
48
|
+
|
49
|
+
b = tf.Variable(tf.zeros([10]))
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
sess.run(tf.initialize_all_variables())
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
y = tf.nn.softmax(tf.matmul(x,W) + b)
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
for i in range(1000):
|
70
|
+
|
71
|
+
batch = mnist.train.next_batch(50)
|
72
|
+
|
73
|
+
train_step.run(feed_dict={x: batch[0], y_: batch[1]})
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
print(accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
def weight_variable(shape):
|
90
|
+
|
91
|
+
initial = tf.truncated_normal(shape, stddev=0.1)
|
92
|
+
|
93
|
+
return tf.Variable(initial)
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
def bias_variable(shape):
|
98
|
+
|
99
|
+
initial = tf.constant(0.1, shape=shape)
|
100
|
+
|
101
|
+
return tf.Variable(initial)
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
def conv2d(x, W):
|
106
|
+
|
107
|
+
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
def max_pool_2x2(x):
|
112
|
+
|
113
|
+
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
|
114
|
+
|
115
|
+
strides=[1, 2, 2, 1], padding='SAME')
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
W_conv1 = weight_variable([5, 5, 1, 32])
|
120
|
+
|
121
|
+
b_conv1 = bias_variable([32])
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
x_image = tf.reshape(x, [-1,28,28,1])
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
|
130
|
+
|
131
|
+
h_pool1 = max_pool_2x2(h_conv1)
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
W_conv2 = weight_variable([5, 5, 32, 64])
|
136
|
+
|
137
|
+
b_conv2 = bias_variable([64])
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
|
142
|
+
|
143
|
+
h_pool2 = max_pool_2x2(h_conv2)
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
W_fc1 = weight_variable([7 * 7 * 64, 1024])
|
148
|
+
|
149
|
+
b_fc1 = bias_variable([1024])
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
|
154
|
+
|
155
|
+
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
keep_prob = tf.placeholder(tf.float32)
|
160
|
+
|
161
|
+
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
W_fc2 = weight_variable([1024, 10])
|
166
|
+
|
167
|
+
b_fc2 = bias_variable([10])
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
|
176
|
+
|
177
|
+
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
|
178
|
+
|
179
|
+
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
|
180
|
+
|
181
|
+
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
|
182
|
+
|
183
|
+
sess.run(tf.initialize_all_variables())
|
184
|
+
|
185
|
+
for i in range(20000):
|
186
|
+
|
187
|
+
batch = mnist.train.next_batch(50)
|
188
|
+
|
189
|
+
if i%100 == 0:
|
190
|
+
|
191
|
+
train_accuracy = accuracy.eval(feed_dict={
|
192
|
+
|
193
|
+
x:batch[0], y_: batch[1], keep_prob: 1.0})
|
194
|
+
|
195
|
+
print("step %d, training accuracy %g"%(i, train_accuracy))
|
196
|
+
|
197
|
+
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
print("test accuracy %g"%accuracy.eval(feed_dict={
|
202
|
+
|
203
|
+
x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
|
204
|
+
|
205
|
+
```
|