Conv3d:InvalidArgumentError(参见上面的回溯):重塑的输入是一个张量

时间:2018-03-09 22:24:35

标签: python tensorflow reshape convolution

 inp = tf.expand_dims(inp, -1)
    conv = tf.layers.conv3d(inputs=inp, filters=64, kernel_size=[5, 5, 5], padding='same', activation=tf.nn.relu)
    print 'conv size', conv.get_shape().as_list()
    pool = tf.layers.max_pooling3d(inputs=conv, pool_size=[2, 2, 2], strides=2)
    print 'pool size', pool.get_shape().as_list()
    conv2 = tf.layers.conv3d(inputs=pool, filters=32, kernel_size=[5, 5, 5], padding='same', activation=tf.nn.relu)
    print 'conv size', conv2.get_shape().as_list()
    pool2 = tf.layers.max_pooling3d(inputs=conv2, pool_size=[2, 2, 2], strides=5)
    print 'pool size', pool2.get_shape().as_list()
    flattened = tf.reshape(pool2, [-1, 10, 1 * 30 * 32])
    print 'flattened size', flattened.get_shape().as_list()

打印

conv size [None, 10, 10, 300, 64]
pool size [None, 5, 5, 150, 64]
conv size [None, 5, 5, 150, 32]
pool size [None, 1, 1, 30, 32]
flattened size [None, 10, 960]

但我收到了错误

  ARGS.func(ARGS)
  File "cnn_rnn.py", line 274, in train
model.fit(session, saver, train, test)
  File "/Users//Desktop/Coterm/Winter2018/Psych209/Final/neural-transposition-corrector/CNN/rnn_model.py", line 110, in fit
score = self.run_epoch(sess, train, dev)
  File "/Users//Desktop/Coterm/Winter2018/Psych209/Final/neural-transposition-corrector/CNN/rnn_model.py", line 69, in run_epoch
loss = self.train_on_batch(sess, *batch)
  File "cnn_rnn.py", line 211, in train_on_batch
_, loss = sess.run([self.train_op, self.loss], feed_dict=feed)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 889, in run
run_metadata_ptr)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1120, in _run
feed_dict_tensor, options, run_metadata)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1317, in _do_run
options, run_metadata)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1336, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input to reshape is a tensor with 30720 values, but the requested shape requires a multiple of 9600
     [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](max_pooling3d_2/MaxPool3D, Reshape_1/shape)]]

Caused by op u'Reshape_1', defined at:
  File "cnn_rnn.py", line 335, in <module>
ARGS.func(ARGS)
  File "cnn_rnn.py", line 266, in train
model = CNN_RNN(config, embeddings)
  File "cnn_rnn.py", line 225, in __init__
self.build()
  File "/Users//Desktop/Coterm/Winter2018/Psych209/Final/neural-transposition-corrector/CNN/model.py", line 81, in build
  File "cnn_rnn.py", line 109, in add_prediction_op
x = self.convolve(x)
  File "cnn_rnn.py", line 96, in convolve
flattened = tf.reshape(pool2, [-1, 10, 1 * 30 * 32])
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 3938, in reshape
"Reshape", tensor=tensor, shape=shape, name=name)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2956, in create_op
op_def=op_def)
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1470, in __init__
self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 30720 values, but the requested shape requires a multiple of 9600
     [[Node: Reshape_1 = Reshape[T=DT_FLOAT, Tshape=DT_INT32, _device="/job:localhost/replica:0/task:0/device:CPU:0"](max_pooling3d_2/MaxPool3D, Reshape_1/shape)]]

我理解调整重塑的尺寸有问题,但是从打印前面步骤的尺寸看起来应该没问题。这是我第一次使用conv3d,所以我很可能犯了一个愚蠢的错误。

有谁知道维度应该是什么?

谢谢!

1 个答案:

答案 0 :(得分:0)

我解决了!问题是重塑调用应该是扁平的= tf.reshape(pool2,[-1,1,1 * 30 * 32])