就您而言,我找到的唯一可行的解​​决方案是修改: y = tf.placeholder(tf.int32,[无]) 在: y = tf.placeholder(tf.int32,[无,1]) I'm facing this error when running my code with 3 lstm layers. Not sure how to fix it. Can anyone help. Here MAX_SEQUENCE_LENGTH=250. After running the cost function, i get the error 'ValueError: Shapes (?, 1) and (?,) are incompatible'# Generate a Tensorflow Graphtf.reset_default_graph()batch_size = 25embedding_size = 50lstmUnits = 64max_label = 2x = tf.placeholder(tf.int32, [None, MAX_SEQUENCE_LENGTH])y = tf.placeholder(tf.int32, [None])number_of_layers=3# Embeddings to represent wordssaved_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 incompatiblefull error message as below.---------------------------------------------------------------------------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 cost4 #y.shape5 #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))754755 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]) 这篇关于Tensorflow ValueError:形状(?,1)和(?,)不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-23 14:58