在Anaconda上使用TensorFlow实现CNN

时间:2017-05-26 08:14:31

标签: python machine-learning tensorflow computer-vision deep-learning

The code cannot run properly 我是深度学习和python的初学者,这是我的卷积神经网络的代码。我根本无法理解错误,看起来语法没有任何问题。

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

接下来的错误信息:

        <tr class="day-row">
            <td class="center" rowspan="4">1</td>
            <td class="center" rowspan="2">projekt</td>
            <td>ActivityGroupName object</td>
            <td>None</td>
            <td>0.8</td>
            <td class="center" rowspan="4">1.6</td>
            <td class="center" rowspan="4"></td>
            <td><a>Upravit</a></td>
        </tr>
        <tr class="day-row">
            <td></td>
            <td></td>
            <td></td>
            <td><a>Nový</a></td>
        </tr>
        <tr class="day-row">
            <td class="center" rowspan="2">frontend</td>
            <td>ActivityGroupName object</td>
            <td>None</td>
            <td>0.8</td>
            <td><a>Upravit</a></td>
        </tr>
        <tr class="day-row">
            <td></td>
            <td></td>
            <td></td>
            <td><a>Nový</a></td>
        </tr>


        <tr class="day-row">
            <td class="center" rowspan="4">2</td>
            <td class="center" rowspan="2">projekt</td>
            <td>ActivityGroupName object</td>
            <td>None</td>
            <td>0.8</td>
            <td class="center" rowspan="4">HERE IS BORDER-BOTTOM SHOWN</td>
            <td class="center" rowspan="4"></td>
            <td><a>Upravit</a></td>
        </tr>
        <tr class="day-row">
            <td></td>
            <td></td>
            <td></td>
            <td><a>Nový</a></td>
        </tr>
        <tr class="day-row">
            <td class="center" rowspan="2">frontend</td>
            <td>ActivityGroupName object</td>
            <td>None</td>
            <td>0.8</td>
            <td><a>Upravit</a></td>
        </tr>
        <tr class="day-row">
            <td></td>
            <td></td>
            <td></td>
            <td><a>Nový</a></td>
        </tr>


        <tr class="day-row">
            <td class="center" rowspan="4">3</td>
            <td class="center" rowspan="2">projekt</td>
            <td>ActivityGroupName object</td>
            <td>None</td>
            <td>0.8</td>
            <td class="center" rowspan="4">1.6</td>
            <td class="center" rowspan="4"></td>
            <td><a>Upravit</a></td>
        </tr>
        <tr class="day-row">
            <td></td>
            <td></td>
            <td></td>
            <td><a>Nový</a></td>
        </tr>
        <tr class="day-row">
            <td class="center" rowspan="2">frontend</td>
            <td>ActivityGroupName object</td>
            <td>None</td>
            <td>0.8</td>
            <td><a>Upravit</a></td>
        </tr>
        <tr class="day-row">
            <td></td>
            <td></td>
            <td></td>
            <td><a>Nový</a></td>
        </tr>

        </tbody>
    </table>

1 个答案:

答案 0 :(得分:0)

最大合并功能的滑动窗口的步幅使您返回一个不同的形状张量,乘以预测会给出错误。将其更改为

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

与您的代码保持一致。

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