前提・実現したいこと
機械学習を勉強中の学生です。
CNNを使ってMNISTの0〜9の画像を認識しようとしています。
該当のソースコード
python
1import tensorflow as tf 2from tensorflow.examples.tutorials.mnist import input_data 3 4mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) 5tf_x = tf.placeholder(tf.float32, [None, 784], name="tf_x") 6tf_y = tf.placeholder(tf.float32, [None, 10], name="tf_y") 7 8def cnn(): 9 x = tf.reshape(tf_x, [-1, 28, 28, 1], name="x") 10 conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu) 11 conv1 = tf.layers.max_pooling2d(conv1, 2, 2) 12 conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu) 13 conv2 = tf.layers.max_pooling2d(conv2, 2, 2) 14 fc1 = tf.contrib.layers.flatten(conv2) 15 fc1 = tf.layers.dense(fc1, 1024) 16 fc1 = tf.layers.dropout(fc1, 0.3) 17 out = tf.layers.dense(fc1, 10) 18 return out 19 20 21y = cnn() 22 23loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y, labels=tf_y)) 24 25 26lr = 0.25 27train_step = tf.train.AdamOptimizer(lr).minimize(loss) 28 29 30correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(tf_y, 1)) 31accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 32 33 34sess = tf.InteractiveSession() 35sess.run(tf.global_variables_initializer()) 36 37for i in range(1000): 38 batch_xs, batch_ys = mnist.train.next_batch(100) 39 _, acc = sess.run([train_step, accuracy], feed_dict={ tf_x: batch_xs, tf_y: batch_ys}) 40 print(acc) 41 42 43acc = sess.run(accuracy, feed_dict={tf_x: mnist.test.images, tf_y: mnist.test.labels}) 44print(acc) 45
問題
このコードを実行して、正解率を確認してみると、0.07〜0.13ぐらいの間を行ったり来たりしています。つまり、全く学習していないという事だと思います。このコードの問題はどこになりますでしょうか。
回答のほど、よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/04/25 11:55