【问题标题】:Tensorflow ValueError: Shapes (?, 1) and (?,) are incompatibleTensorflow ValueError:形状(?,1)和(?,)不兼容
【发布时间】:2018-05-05 01:38:42
【问题描述】:

我在使用 3 个 lstm 层运行我的代码时遇到了这个错误。不知道如何修复它。任何人都可以帮忙。这里 MAX_SEQUENCE_LENGTH=250。运行成本函数后,我收到错误“ValueError: Shapes (?, 1) and (?,) are incompatible'

# Generate a Tensorflow Graph
tf.reset_default_graph()
batch_size = 25
embedding_size = 50
lstmUnits = 64
max_label = 2

x = tf.placeholder(tf.int32, [None, MAX_SEQUENCE_LENGTH])
y = tf.placeholder(tf.int32, [None])

number_of_layers=3

#  Embeddings to represent words
saved_embeddings = np.load('wordVectors.npy')
embeddings = tf.nn.embedding_lookup(saved_embeddings, x)

def lstm_cell():
  return tf.contrib.rnn.BasicLSTMCell(lstmUnits,reuse=tf.get_variable_scope().reuse)

lstmCell = tf.contrib.rnn.MultiRNNCell([lstm_cell() for _ in range(number_of_layers)])

lstmCell = tf.contrib.rnn.DropoutWrapper(cell=lstmCell, output_keep_prob=0.75)

outputs, final_state = tf.nn.dynamic_rnn(lstmCell, embeddings, dtype=tf.float32)

predictions = tf.contrib.layers.fully_connected(outputs[:, -1], 1, activation_fn=tf.sigmoid)

cost = tf.losses.mean_squared_error(y, predictions)

ValueError:形状 (?, 1) 和 (?,) 不兼容 完整的错误信息如下。

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-f261b46a6f62> in <module>()
1 # Try 3
----> 2 cost = tf.losses.mean_squared_error(y, predictions)
3 cost
4 #y.shape
5 #y.reshape[]

/home/lavared/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/losses/losses_impl.py in mean_squared_error(labels, predictions, weights, scope, loss_collection, reduction)
564     predictions = math_ops.to_float(predictions)
565     labels = math_ops.to_float(labels)
--> 566     predictions.get_shape().assert_is_compatible_with(labels.get_shape())
567     losses = math_ops.squared_difference(predictions, labels)
568     return compute_weighted_loss(

/home/lavared/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py in assert_is_compatible_with(self, other)
751     """
752     if not self.is_compatible_with(other):
--> 753       raise ValueError("Shapes %s and %s are incompatible" % (self, other))
754 
755   def most_specific_compatible_shape(self, other):

ValueError: Shapes (?, 1) and (?,) are incompatible

【问题讨论】:

  • 请给出错误的完整堆栈跟踪以及找出行号的方法
  • 你在cost = ...之前试过cost = tf.losses.mean_squared_error(y, predictions[:, 0])或y = tf.expand_dims(y, axis=1)吗?
  • 如上更新了完整的错误堆栈。
  • y = tf.expand_dims(y, axis=1) 对我有帮助。

标签: python tensorflow lstm


【解决方案1】:

我知道这个问题已经有一个月了。

几天前我遇到了这个问题。这是一个众所周知的bug,尽管他们只解决了那个特定的案例。

在你的情况下,我找到的唯一可行的解​​决方案是修改:

y = tf.placeholder(tf.int32, [None])

在:

y = tf.placeholder(tf.int32, [None, 1])

【讨论】:

  • 我的Tensorflow代码也有类似的问题,在我看来问题是对输出维度的理解不是输出数组的维度而是输出的可能性。以 MNIST 0-9 笔迹为例,可能性为 10。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-12
  • 2021-05-29
  • 2021-02-23
  • 2020-07-30
  • 2020-08-27
  • 2021-09-24
  • 1970-01-01
相关资源
最近更新 更多