Reputation: 438
I'm trying to give my keras neural network categorical crossentropy loss with from_logits=True. However, I'm not sure how to pass this into the code, as it asks me to specify the target and output.
Normally I can use:
network.compile(sgd, loss='categorical_crossentropy'),
but now I'm having to try this:
network.compile(sgd, loss=categorical_crossentropy(from_logits=True))
which gives me an error:
TypeError: categorical_crossentropy() missing 2 required positional arguments: 'target' and 'output'
The best I can come up with is:
network.compile(sgd, loss=categorical_crossentropy(y_true, network.output, from_logits=True))
I don't have any idea what to put for y_true however as this is not part of the network. I've had a look around online but haven't come across anything which specifies how to do this, including, weirdly, the keras documentation.
Upvotes: 1
Views: 1104
Reputation: 86630
Keras losses need strictly two arguments: y_true
(ground truth data) and y_pred
(model's output).
If you want to use a function with a different signature, you must wrap it to follow the correct signature.
import keras.backend as K
def cc_from_logits(y_true, y_pred):
return K.categorical_crossentropy(y_true, y_pred, from_logits=True, axis=-1)
model.compile(loss=cc_from_logits)
I'm quite convinced that cc_with_logits
brings the exact same results as softmax + 'categorical_crossentropy'
.
Upvotes: 2