0

The code cannot run properly 我是深度学习和python的初学者,这是我的卷积神经网络的代码。我无法理解这个错误,它看起来没有任何语法错误。在Anaconda上使用TensorFlow实现CNN

#!/usr/bin/env python3 
# -*- coding: utf-8 -*- 
""" 
Created on Thu Apr 20 17:23:07 2017 

@author: gengyoung 
""" 

from tensorflow.examples.tutorials.mnist import input_data 
import tensorflow as tf 
mnist = input_data.read_data_sets("MNIST_data/",one_hot=True) 
sess = tf.InteractiveSession() 
def weights(shape): 
    return tf.Variable(tf.truncated_normal(shape,stddev=0.1)) 
def biases(shape): 
    return tf.Variable(tf.constant(0.1,shape=shape)) 
def conv2d(X,W): 
    return tf.nn.conv2d(X,W,[1,1,1,1],padding='SAME') 
def max_pool_2x2(X): 
    return tf.nn.max_pool(X,[1,2,2,1],[1,1,1,1],padding='SAME') 
X = tf.placeholder(dtype=tf.float32,shape=[None,784]) 
y = tf.placeholder(dtype=tf.float32,shape=[None,10]) 
keep_prob = tf.placeholder(tf.float32) 
X_train = tf.reshape(X,[-1,28,28,1]) 
w1 = weights([5,5,1,32]) 
b1 =biases([32]) 
conv1 = tf.nn.relu(conv2d(X_train,w1)+b1) 
pool1 = max_pool_2x2(conv1) 
w2 = weights([5,5,32,64]) 
b2 = biases([64]) 
conv2 = tf.nn.relu(conv2d(pool1,w2)+b2) 
pool2 = max_pool_2x2(conv2) 
f_w1 = weights([7*7*64,1024]) 
f_b1 = biases([1024]) 
f_w2 = weights([1024,10]) 
f_b2 = biases([10]) 
flatten_pool2 = tf.reshape(pool2,[-1,7*7*64]) 
h1 = tf.nn.relu(tf.matmul(flatten_pool2,f_w1)+f_b1) 
h1_drop = tf.nn.dropout(h1,keep_prob) 
predict_y = tf.nn.softmax(tf.matmul(h1_drop,f_w2)+f_b2) 

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y*tf.log(predict_y),reduction_indices=[1])) 
train_step = tf.train.AdamOptimizer(0.0001).minimize(cross_entropy) 
corrct_prediction = tf.equal(tf.argmax(predict_y,1),tf.argmax(y,1)) 
accuracy = tf.reduce_mean(tf.cast(corrct_prediction,tf.float32)) 
tf.global_variables_initializer().run() 
for i in range(2000): 
    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:%04d accuracy:%.9f"%(i,train_accuracy)) 
    train_step.run(feed_dict={X:batch[0],y:batch[1],keep_prob:0.5}) 
print("Test accuracy: %.9f"%accuracy.eval(feed_dict={X:mnist.test.images,y:mnist.test.labels, 
              keep_prob:1.0})) 

接下来的ERR信息:

Python 3.6.0 |Anaconda custom (x86_64)| (default, Dec 23 2016, 13:19:00) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> runfile('/Users/gengyoung/CNN.py', wdir='/Users/gengyoung') 
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 
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. 
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. 
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. 
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations. 
W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations. 
Traceback (most recent call last): 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1022, in _do_call 
    return fn(*args) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1004, in _run_fn 
    status, run_metadata) 
    File "/anaconda/lib/python3.6/contextlib.py", line 89, in __exit__ 
    next(self.gen) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status 
    pywrap_tensorflow.TF_GetCode(status)) 
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [800] 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 "<stdin>", line 1, in <module> 
    File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile 
    execfile(filename, namespace) 
    File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile 
    exec(compile(f.read(), filename, 'exec'), namespace) 
    File "/Users/gengyoung/CNN.py", line 50, in <module> 
    train_accuracy = accuracy.eval(feed_dict={X:batch[0],y:batch[1],keep_prob:1.0}) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 567, in eval 
    return _eval_using_default_session(self, feed_dict, self.graph, session) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 3729, in _eval_using_default_session 
    return session.run(tensors, feed_dict) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 767, in run 
    run_metadata_ptr) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 965, in _run 
    feed_dict_string, options, run_metadata) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1015, in _do_run 
    target_list, options, run_metadata) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1035, in _do_call 
    raise type(e)(node_def, op, message) 
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [800] 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 "<stdin>", line 1, in <module> 
    File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile 
    execfile(filename, namespace) 
    File "/anaconda/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile 
    exec(compile(f.read(), filename, 'exec'), namespace) 
    File "/Users/gengyoung/CNN.py", line 44, in <module> 
    corrct_prediction = tf.equal(tf.argmax(predict_y,1),tf.argmax(y,1)) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/ops/gen_math_ops.py", line 721, in equal 
    result = _op_def_lib.apply_op("Equal", x=x, y=y, name=name) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 763, in apply_op 
    op_def=op_def) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 2327, in create_op 
    original_op=self._default_original_op, op_def=op_def) 
    File "/anaconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1226, in __init__ 
    self._traceback = _extract_stack() 

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

欢迎来到SO,您可能需要[tour](https://stackoverflow.com/tour),阅读[如何问](http://stackoverflow.com/help/how-to-ask )并提供[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。如果你详细说明,人们会更愿意帮助你(现在我们缺少太多的信息)。 – Nuageux

+0

此外 - 不要将您的代码发布为照片。复制并粘贴到您的文章。 – asongtoruin

+0

非常感谢! –

回答

0

你的最大池功能的滑动窗口的步伐回到你不同的形状张量相乘下来的预言给你的错误。将其更改为

def max_pool_2x2(X): 
     return tf.nn.max_pool(X,[1,2,2,1],[1,2,2,1],padding='SAME') 

要与您的代码保持一致。

我还建议您检查一些关于卷积和最大池的解释和实现,如this以了解如何为您的代码进行更改。

+0

谢谢!我之前犯过这个错误,并再次犯下了错误。但我认为这已经刻在了我的心上! –