简体   繁体   English

Tensorflow ValueError:形状(?,1)和(?,)不兼容

[英]Tensorflow ValueError: Shapes (?, 1) and (?,) are incompatible

I'm facing this error when running my code with 3 lstm layers. 运行带有3个lstm图层的代码时,我遇到了这个错误。 Not sure how to fix it. 不知道如何解决它。 Can anyone help. 谁能帮忙。 Here MAX_SEQUENCE_LENGTH=250. 这里MAX_SEQUENCE_LENGTH = 250。 After running the cost function, i get the error 'ValueError: Shapes (?, 1) and (?,) are incompatible' 运行成本函数后,我得到错误'ValueError:Shapes(?,1)和(?,)不兼容'

# 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: Shapes (?, 1) and (?,) are incompatible full error message as below. ValueError:Shapes(?,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

I know this question is a month-old. 我知道这个问题是一个月大了。

I was facing this issue some days ago. 几天前我正面对这个问题。 It was a well-known bug even though they solved only for that specific case. 这是一个众所周知的错误,即使他们只解决了这个特定情况。

In your case, the only working solution I found is to modify: 在您的情况下,我发现唯一可行的解​​决方案是修改:

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

in: 在:

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

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

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