I have been working on writing a keras model using a tensorflow loss (sparse_softmax_cross_entropy_with_logits
) and I ran into this issue. For this model, the true values should be a tensor with shape (batch_size), and the output of the model will have shape (batch_size, num_classes). I have verified that the output of the model is of shape (?, num_classes), and I have created a target tensor for the true values but that does not seem to address the issue. Does anybody have any ideas on how to fix it? Is there something I am missing? Below is the relevant code.
def tf_loss(y_true, y_pred):
return tf.nn.sparse_softmax_cross_entropy_with_logits(labels=y_true, logits=y_pred)
pred = tf.placeholder(dtype=tf.int32, shape=[None])
model.compile(optimizer='adam', loss=tf_loss, target_tensor=pred)
When I look inside the loss function, I find that y_true has shape (?, num_classes) and y_pred has shape (?, ?).