CNN代码运行报错
来源:4-23 动手实现CNN卷积神经网络(五)
限量版灬型男
2018-03-21
老师好,我按照视频打了CNN手写识别的代码,但是执行的时候报
TypeError: Fetch argument None has invalid type <class 'NoneType'>
错误,代码在第一次执行打印精确度是没有问题,但之后就报了这个错误
以下是我的代码:
import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('mnist_data', one_hot=True) input_x = tf.placeholder(dtype=tf.float32, shape=[None, 28*28]) / 255 output_y = tf.placeholder(dtype=tf.int32, shape=[None, 10]) input_x_images = tf.reshape(input_x, [-1, 28, 28, 1]) test_x = mnist.test.images[:3000] test_y = mnist.test.labels[:3000] # # print(test_x[0]) # print(test_y[0]) conv1 = tf.layers.conv2d(inputs=input_x_images, filters=32, kernel_size=[5, 5], strides=1, padding='same', activation=tf.nn.relu) pool1 = tf.layers.max_pooling2d(inputs=conv1, pool_size=[2, 2], strides=2) conv2 = tf.layers.conv2d(inputs=pool1, filters=64, kernel_size=[5, 5], strides=1, padding='same', activation=tf.nn.relu) pool2 = tf.layers.max_pooling2d(inputs=conv2, pool_size=[2, 2], strides=2) flat = tf.reshape(pool2, [-1, 7*7*64]) dense = tf.layers.dense(inputs=flat, units=1024, activation=tf.nn.relu) dropout = tf.layers.dropout(inputs=dense, rate=0.5) logits = tf.layers.dense(inputs=dropout, units=10) loss = tf.losses.softmax_cross_entropy(onehot_labels=output_y, logits=logits) train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss) accuracy = tf.metrics.accuracy(labels=tf.argmax(output_y, axis=1), predictions=tf.argmax(logits, axis=1))[1] sess = tf.Session() init = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) sess.run(init) for i in range(20000): batch = mnist.train.next_batch(50) train_loss, train_op = sess.run([loss, train_op], {input_x: batch[0], output_y: batch[1]}) if i % 100 == 0: test_accuracy = sess.run(accuracy, {input_x: test_x, output_y: test_y}) print("step=%d, Train loss=%.4f, [Test accuracy=%.2f]"%(i, train_loss, test_accuracy)) test_output = sess.run(logits, {input_x: test_x[:20]}) inferenced_y = np.argmax(test_output, 1) print(inferenced_y, 'Inference numbers') print(np.argmax(test_y[:20], 1), 'real numbers')
以下是执行结果:
Extracting mnist_data\train-images-idx3-ubyte.gz Extracting mnist_data\train-labels-idx1-ubyte.gz Extracting mnist_data\t10k-images-idx3-ubyte.gz Extracting mnist_data\t10k-labels-idx1-ubyte.gz 2018-03-21 23:46:23.802475: I D:\Build\tensorflow\tensorflow-r1.4\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX step=0, Train loss=2.3221, [Test accuracy=0.19] Traceback (most recent call last): File "E:/python学习/tensorflow/CNN_Mnist.py", line 41, in <module> train_loss, train_op = sess.run([loss, train_op], {input_x: batch[0], output_y: batch[1]}) File "H:\virtualenvs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 889, in run run_metadata_ptr) File "H:\virtualenvs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1105, in _run self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles) File "H:\virtualenvs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 414, in __init__ self._fetch_mapper = _FetchMapper.for_fetch(fetches) File "H:\virtualenvs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 234, in for_fetch return _ListFetchMapper(fetch) File "H:\virtualenvs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 341, in __init__ self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] File "H:\virtualenvs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 341, in <listcomp> self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches] File "H:\virtualenvs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 231, in for_fetch (fetch, type(fetch))) TypeError: Fetch argument None has invalid type <class 'NoneType'> Process finished with exit code 1
写回答
1回答
-
问题是在 46 行的地方,那个应该是 train_op_, 和之前我们在 34 行定义的 train_op 不是一个哦:
train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
它多一个下划线(_)。
所以只要将 46 行这句
train_loss, train_op = sess.run([loss, train_op], {input_x: batch[0], output_y: batch[1]})
改为
train_loss, train_op_ = sess.run([loss, train_op], {input_x: batch[0], output_y: batch[1]})
即可。当然了,这个 train_op_ 我们后面没有用到,所以你随便取什么名字都行。
112018-03-22
相似问题
运行出错
回答 2
怎么卷积神经网络报错 啊
回答 1