【问题标题】:Tensorflow Estimator logits and labels must have the same shapeTensorflow Estimator logits 和标签必须具有相同的形状
【发布时间】:2018-06-29 00:14:53
【问题描述】:

仅供参考...这不是已报告和修复的错误 #4715。

print('Tensorflow version {} is loaded.'.format(tf.__version__))
#Tensorflow version 1.4.0 is loaded.

我已经组合了一个只有 2 个特征和一个二元分类的自定义 Estimator。下面的代码有效,但确实收敛于一些有用的东西。

input_layer = tf.feature_column.input_layer(features, feature_columns)
h1 = tf.layers.Dense(h1_size, activation=tf.nn.relu)(input_layer)
h2 = tf.layers.Dense(h2_size, activation=tf.nn.relu)(h1)
logits = tf.layers.Dense(NUM_CLASSES)(h2)
labels = tf.squeeze(labels, 1)
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)

为了改进模型,我想改变计算损失的方式。具体来说,我宁愿不使用 softmax,而是使用 sigmoid_cross_entropy。因此,我将损失更改为;

loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=labels, logits=logits)

并得到这些错误;

ValueError: Shapes (?,) and (?, 2) must have the same rank
....
During handling of the above exception, another exception occurred:
....
ValueError: Shapes (?,) and (?, 2) are not compatible
....
During handling of the above exception, another exception occurred:
....
ValueError: logits and labels must have the same shape ((?, 2) vs (?,))

由于我真的不明白为什么需要挤压(),所以我将其删除并得到了这个;

ValueError: Dimensions 1 and 2 are not compatible
....
During handling of the above exception, another exception occurred:
....
ValueError: logits and labels must have the same shape ((?, 2) vs (?, 1))

这让我觉得我可以将 one_hot 传递给损失计算来解决形状问题。我尝试了这个,以及 one_hot() 之前的squeeze()。但是,错误变得更加复杂,表明我可能走错了路。 (作为记录,one_hot 为矩阵添加了一个维度,因此没有挤压,张量变为 (?, 1, 2)。

loss = tf.nn.sigmoid_cross_entropy_with_logits(labels=tf.one_hot(labels, depth=2), logits=logits)
InvalidArgumentError (see above for traceback): Input to reshape is a tensor with 288 values, but the requested shape has 1

有人可以帮助我理解为什么我的标签和 logits 适合 sparse_softmax_cross_entropy。但不适用于 sigmoid_cross_entropy_with_logits? 我可以做些什么来重塑张量以允许使用 sigmoid 损失函数?

【问题讨论】:

  • 您是否尝试过挤压然后在标签上使用 one_hot ?最后标签和logits应该是相同的形状和类型(这里使用one_hot是正确的想法)
  • 我将更改问题以反映 one_hot 函数的添加是与挤压()。感谢您的回复。
  • 您是否尝试过重塑标签?在将它们传递给网络之前“np.reshape(labels, [batch_size, 1])”或网络中的“tf.reshape(labels, [labels.get_shape()[0], 1])”
  • labels 的初始形状是什么? labels 的形状在传递给 sigmoid_cross_entropy_with_logits 时应该是 [batch_size, NUM_CLASSES]
  • 在挤压后最初的 Tensor("IteratorGetNext:2", shape=(?, 1), dtype=int32, device=/device:CPU:0) Tensor("Squeeze:0", shape= (?,), dtype=int32) 在 onehot Tensor("one_hot:0", shape=(?, 2), dtype=float32) 之后

标签: python tensorflow


【解决方案1】:

答案是先创建一个 one_hot,然后再挤压() 得到的张量。

这适用于损失计算。

loss = tf.losses.sigmoid_cross_entropy(
        multi_class_labels=tf.squeeze(tf.one_hot(labels, depth=2), axis=1),
        logits=logits)

sigmoid_cross_entropy 继续生成 sigmoid_cross_entropy_with_logits。在搜索过程中,我最终切换到仅仅是因为懒惰而发布的内容。

【讨论】:

    猜你喜欢
    • 2021-09-16
    • 2021-10-22
    • 2019-02-23
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2021-07-19
    相关资源
    最近更新 更多