【Week3 动手任务跑程序的错误无法解决】


#1

代码如下: import tensorflow as tf import input_data

mnist=input_data.read_data_sets(‘MNIST_data’,one_hot=True) sess=tf.InteractiveSession()

x=tf.placeholder(tf.float32,shape=[None,784]) y=tf.placeholder(tf.float32,shape=[None,10])

def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) return tf.Variable(initial)

def bias_variable(shape): initial = tf.constant(0.1, shape=shape) return tf.Variable(initial)

def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding=‘SAME’)

def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding=‘SAME’)

x_image = tf.reshape(x,[-1,28,28,1])

with tf.name_scope(‘conv1’): W_conv1 = weight_variable([5, 5, 1, 6]) b_conv1 = bias_variable([6]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)

with tf.name_scope(‘pool1’): h_pool1 = max_pool_2x2(h_conv1)

with tf.name_scope(‘conv2’): W_conv2 = weight_variable([5, 5, 6, 16]) b_conv2 = bias_variable([16]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)

with tf.name_scope(‘pool2’): h_pool2 = max_pool_2x2(h_conv2)

with tf.name_scope(‘fc1’): h_pool2_flat = tf.reshape(h_pool2, [-1, 5 * 5 * 16]) W_fc1 = weight_variable([5 * 5 * 16, 120]) b_fc1 = bias_variable([120]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

with tf.name_scope(‘fc2’): W_fc2 = weight_variable([120, 84]) b_fc2 = bias_variable([84]) h_fc2 = tf.nn.relu(tf.matmul(h_fc1, W_fc2) + b_fc2)

keep_prob = tf.placeholder(“float”) h_fc1_drop = tf.nn.dropout(h_fc2, keep_prob)

with tf.name_scope(‘softmax’): W_fc3 = weight_variable([84, 10]) b_fc3 = bias_variable([10]) y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc3) + b_fc3)

cross_entropy = -tf.reduce_sum(y*tf.log(y_conv)) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(y,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, “float”)) sess.run(tf.global_variables_initializer()) for i in range(20000): batch = mnist.train.next_batch(50) if i%100 == 0: train_accuracy = accuracy.eval(feed_dict={ x: batch[0], y: batch[1], keep_prob: 1.0}) print(“step %d, training accuracy %g”%(i, train_accuracy)) train_step.run(feed_dict={x: batch[0], y: batch[1],keep_prob:0.5})

print(“test accuracy %g”%accuracy.eval(feed_dict={ x: mnist.test.images, y: mnist.test.labels,keep_prob:1.0}))

在运行时出现错误,请问大神们是什么问题,错误代码如下: Traceback (most recent call last): File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 1021, in _do_call return fn(*args) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 1003, in _run_fn status, run_metadata) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\contextlib.py”, line 66, in exit next(self.gen) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py”, line 469, in raise_exception_on_not_ok_status pywrap_tensorflow.TF_GetCode(status)) tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [98] vs. [50] [[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/cpu:0"](ArgMax, ArgMax_1)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File “F:/python/Tensorflow相关程序/LeNet5Minit.py”, line 71, in x: batch[0], y: batch[1], keep_prob: 1.0}) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py”, line 575, in eval return _eval_using_default_session(self, feed_dict, self.graph, session) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py”, line 3633, in _eval_using_default_session return session.run(tensors, feed_dict) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 766, in run run_metadata_ptr) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 964, in _run feed_dict_string, options, run_metadata) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 1014, in _do_run target_list, options, run_metadata) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py”, line 1034, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [98] vs. [50] [[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/cpu:0"](ArgMax, ArgMax_1)]]

Caused by op ‘Equal’, defined at: File “F:/python/Tensorflow相关程序/LeNet5Minit.py”, line 64, in correct_prediction = tf.equal(tf.argmax(y_conv,1),tf.argmax(y,1)) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_math_ops.py”, line 728, in equal result = _op_def_lib.apply_op(“Equal”, x=x, y=y, name=name) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py”, line 759, in apply_op op_def=op_def) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py”, line 2240, in create_op original_op=self._default_original_op, op_def=op_def) File “C:\Users\Administrator\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py”, line 1128, in init self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Incompatible shapes: [98] vs. [50] [[Node: Equal = Equal[T=DT_INT64, _device="/job:localhost/replica:0/task:0/cpu:0"](ArgMax, ArgMax_1)]]

Process finished with exit code 1


#2

检查一下这一块的逻辑,是否有错误?


#3

这句话写的没错啊…逻辑错误是指上下文么


#4

改好了,是第二层padding的问题:joy: