tensorflow和python不兼容_Tensorflow值错误:形状(?)?,1)和?,)是不兼容的...

在尝试使用3层LSTM模型时,遇到了TensorFlow的形状不兼容错误。具体表现为:Shapes (?, 1)和 (?,) 不兼容。错误出现在计算mean_squared_error损失函数时。代码中设置了batch_size、embedding_size、lstmUnits和MAX_SEQUENCE_LENGTH等参数,并使用了预训练的词向量。问题可能出在动态RNN的输出和标签形状不匹配。" 108540752,7756739,VLPT虚拟大视场相机标定:小目标棋盘格组合应用,"['计算机视觉', '图像处理', '标定技术', 'OpenCV库', 'Matlab编程']
摘要由CSDN通过智能技术生成

当我用3层lstm运行我的代码时,我面临着这个错误。不知道怎么修。有人能帮忙吗。这里MAX_SEQUENCE_LENGTH=250。在运行cost函数之后,我得到错误的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)

值错误:形状(?)?,1)和?,)不兼容

完整错误消息如下。---------------------------------------------------------------------------

ValueError Traceback (most recent call last)

in ()

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值