繁体   English   中英

在Anaconda上使用TensorFlow实施CNN

[英]Implementing CNN with TensorFlow on Anaconda

代码无法正常运行我是深度学习和python的初学者,这是我的卷积神经网络代码。 我根本无法理解该错误,并且看起来语法上没有任何问题。

#!/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}))

接下来的错误信息:

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)]]

max pooling函数的滑动窗口的步幅向您返回了一个不同的形状张量,该张量乘以预测值即可得出误差。 更改为

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

与您的代码保持一致。

我还建议您像这样检查一些有关卷积和最大池的解释和实现,以了解如何为代码更改它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM