使用sparse_categorical_crossentropy时如何解决形状不匹配错误

时间:2020-03-02 05:46:51

标签: python tensorflow keras

我想知道为什么在输入具有相同形状和相同内容的两个张量时,调用sparse_categorical_crossentropy损失函数时出现错误。 我有以下代码:

labels = np.array([0,1,2])
logits = np.array([0,1,2])
tf.keras.losses.sparse_categorical_crossentropy(labels, logits, from_logits=True)

我收到以下错误:

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/losses.py in sparse_categorical_crossentropy(y_true, y_pred, from_logits, axis)
    976 def sparse_categorical_crossentropy(y_true, y_pred, from_logits=False, axis=-1):
    977   return K.sparse_categorical_crossentropy(
--> 978       y_true, y_pred, from_logits=from_logits, axis=axis)
    979 
    980 

/tensorflow-2.1.0/python3.6/tensorflow_core/python/keras/backend.py in sparse_categorical_crossentropy(target, output, from_logits, axis)
   4574   else:
   4575     res = nn.sparse_softmax_cross_entropy_with_logits_v2(
-> 4576         labels=target, logits=output)
   4577 
   4578   if update_shape and output_rank >= 3:

/tensorflow-2.1.0/python3.6/tensorflow_core/python/ops/nn_ops.py in sparse_softmax_cross_entropy_with_logits_v2(labels, logits, name)
   3535   """
   3536   return sparse_softmax_cross_entropy_with_logits(
-> 3537       labels=labels, logits=logits, name=name)
   3538 
   3539 

/tensorflow-2.1.0/python3.6/tensorflow_core/python/ops/nn_ops.py in sparse_softmax_cross_entropy_with_logits(_sentinel, labels, logits, name)
   3451                        "should equal the shape of logits except for the last "
   3452                        "dimension (received %s)." % (labels_static_shape,
-> 3453                                                      logits.get_shape()))
   3454     # Check if no reshapes are required.
   3455     if logits.get_shape().ndims == 2:

ValueError: Shape mismatch: The shape of labels (received (3,)) should equal the shape of logits except for the last dimension (received (1, 3)).

1 个答案:

答案 0 :(得分:0)

labels = tf.convert_to_tensor(np.asarray([0,1,2]))
logits = tf.convert_to_tensor(np.asarray([[1,0,0],[0,1,0],[0,0,1]]), dtype=tf.float32)
tf.keras.losses.sparse_categorical_crossentropy(labels, logits)
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([2.3841855e-07, 2.3841855e-07, 2.3841855e-07], dtype=float32)>

sparse意味着您在labels中提供的整数实际上表示类标签,这意味着sparse_categorical_crossentropy_loss会将它们转换为一种热编码(如logits)。不知道from_logits代表什么,对不起。

注意:必须tf.convert_to_tensor的事实是为了避免tensorflow中当前存在的错误。