gpt4 book ai didi

python - 等级不匹配 : Rank of labels (received 2) should equal rank of logits minus 1 (received 2)

转载 作者:太空狗 更新时间:2023-10-29 20:43:20 32 4
gpt4 key购买 nike

我正在构建 DNN 来预测对象是否存在于图像中。我的网络有两个隐藏层,最后一层看起来像这样:

  # Output layer
W_fc2 = weight_variable([2048, 1])
b_fc2 = bias_variable([1])

y = tf.matmul(h_fc1, W_fc2) + b_fc2

然后我有标签的占位符:

y_ = tf.placeholder(tf.float32, [None, 1], 'Output')

我分批进行训练(因此输出层形状中的第一个参数为无)。

我使用以下损失函数:

cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
y[:, :1], y_[:, :1], name='xentropy')
loss = tf.reduce_mean(cross_entropy, name='xentropy_mean')
predict_hand = tf.greater(y, 0.5)
correct_prediction = tf.equal(tf.to_float(predict_hand), y_)
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

但是在运行时我得到了以下错误:

Rank mismatch: Rank of labels (received 2) should equal rank of logits minus 1 (received 2).

我想我应该 reshape 标签层,但不确定它期望什么。我在 documentation 中查找它说:

logits: Unscaled log probabilities of rank r and shape [d_0, d_1, ..., d_{r-2}, num_classes] and dtype float32 or float64. labels: Tensor of shape [d_0, d_1, ..., d_{r-2}] and dtype int32 or int64. Each entry in labels must be an index in [0, num_classes).

如果我只有一个类,我的标签应该是什么样子(现在只有 0 或 1)?任何帮助表示赞赏

最佳答案

来自 tf.nn.sparse_softmax_cross_entropy_with_logits 的文档*:

"A common use case is to have logits of shape [batch_size, num_classes] and labels of shape [batch_size]. But higher dimensions are supported."

所以我想你的标签张量应该是 [None] 的形状。请注意,形状为 [None, 1] 或形状为 [None] 的给定张量将包含相同数量的元素。

具有具体虚拟值的示例输入:

>>> logits = np.array([[11, 22], [33, 44], [55, 66]])
>>> labels = np.array([1, 0, 1])

小批量中有 3 个示例,第一个示例的 logits 是 11 和 22,并且有 2 个类:0 和 1。

* https://www.tensorflow.org/versions/r0.11/api_docs/python/nn.html#sparse_softmax_cross_entropy_with_logits

关于python - 等级不匹配 : Rank of labels (received 2) should equal rank of logits minus 1 (received 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40350849/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com