New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RaggedTensor support for sparse_categorical_crossentropy. #47092
Conversation
tensorflow/python/keras/losses.py
Outdated
When used by SparseCategoricalCrossentropy() with the default reduction | ||
(SUM_OVER_BATCH_SIZE), the reduction averages the loss over the | ||
number of elements independent of the batch. E.g. if the RaggedTensor | ||
has 2 batches with [2, 1] values respectivly the resulting loss is |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a typo - respectively; and I believe there should be a comma after respectively.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
Independently to the It seems that such a scenario works with sce_obj = tf.losses.SparseCategoricalCrossentropy()
y_true = tf.ragged.constant([[1, 1], [0]])
y_pred = tf.ragged.constant([[[0.1, 0.9], [0.1, 0.9]], [[0.9, 0.1]]])
print(sce_obj(y_true, y_pred)) but after adding another ragged dimension y_true = tf.ragged.constant([[[1, 1]], [[0]]])
y_pred = tf.ragged.constant([[[[0.1, 0.9], [0.1, 0.9]]], [[[0.9, 0.1]]]]) # both without and with `inner_shape=(2,)`
print(sce_obj(y_true, y_pred)) it crashes with It seems to be connected to |
8c4a2b4
to
a83cf2d
Compare
@foxit Added a test with the example you provided and changed the approach a bit to avoid the warning generated by the previous code. |
A gentle bump after two weeks -- we would be grateful for a review. Thanks & cheers. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
@pedro-r-marques can you please check build failures ? |
a83cf2d
to
22ce5f3
Compare
@rthadur Fixed. It was a pylint error for an unused import. |
@rthadur Can you please check why the CL doesn't built internally to google ? |
@pedro-r-marques seeing this error internally
|
22ce5f3
to
8fc297c
Compare
@rthadur
The flag y_pred_extra_dim is only set when calculating losses for sparse categorical cross entropy since we expect the shape of y_pred to have one extra dimension. This if statement had an extra check which is both unnecessary and incorrect. I would appreciate it if you could trigger the google internal CI and check that the patch passes the tests. |
Attempt to support RaggedTensors in
sparse_categorical_crossentropy
loss.