我在使用反卷积神经网络进行预测时遇到了这个异常。秩和形状看起来是相同的,所以我不确定问题出在哪里。
File "/home/Workspace/image-recognition/app/model/per_pixel_deconv.py", line 141, in Model softmax = tf.nn.softmax(output, name=None)File "/home/anaconda2/envs/image-recognition/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 1396, in softmax result = _op_def_lib.apply_op("Softmax", logits=logits, name=name)File "/home/anaconda2/envs/image-recognition/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op op_def=op_def)File "/home/anaconda2/envs/image-recognition/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2319, in create_op set_shapes_for_outputs(ret)File "/home/anaconda2/envs/image-recognition/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1711, in set_shapes_for_outputs shapes = shape_func(op)File "/home/anaconda2/envs/image-recognition/lib/python2.7/site-packages/tensorflow/python/framework/common_shapes.py", line 45, in _ShapeFunction return [op.inputs[0].get_shape().with_rank(rank)]File "/home/anaconda2/envs/image-recognition/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.py", line 641, in with_rank raise ValueError("Shape %s must have rank %d" % (self, rank))ValueError: Shape (?, 128, 128, 2) must have rank 2
回答:
形状(?, 128, 128, 2)
是一个秩4的张量,但softmax操作符显然期望的是一个秩2的张量。
看起来你在使用的是一个旧版本的TensorFlow。在0.10版本中,tf.nn.softmax
要求输入必须是秩2:https://www.tensorflow.org/versions/r0.10/api_docs/python/nn/classification#softmax
在当前版本的TensorFlow中,tf.nn.softmax
接受任何秩的输入:https://www.tensorflow.org/api_docs/python/nn/classification#softmax
尝试使用更新版本的TensorFlow。
希望这对你有帮助!