【发布时间】: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