-
Notifications
You must be signed in to change notification settings - Fork 74k
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
Support RaggedTensors in Keras losses (at least MSE, CE, SCE) #45403
Comments
If you are looking for a workaround, the following seems to work for me for 2D tensors shape: (batch, [var len]).
For 3d tensors where the ragged dimension is the sequence length and the features dimension is constant I use a user defined loss such as.
|
@pedro-r-marques Thanks! Once your PRs go though, I plan to use something like But I think basic losses like MSE or CE should support RaggedTensors anyway :-) |
@foxik I agree with the desirability of adding support in the keras library itself. In terms of the feature request, it would be nice if keras.losses.Loss could understand 2-D RaggedTensors and perform the mean per-batch and then across batches. And also if it could understand N-D RaggedTensors where the 2nd dimension is the ragged dimension and use |
@pedro-r-marques Personally I would prefer the other behaviour, i.e., average across all "words" (assuming the batch examples are sequences of words). If you consider that the gradients in the network are caused "by every word" (because the individual losses are some cross entropies of every word), then to estimate correct second and first moments, I believe you should not average across sentences first. If you look at it from the metrics point of view, in NLP you usually want to compute accuracy "per word", not average of "sentence-level" accuracies (BTW, this seems to be exactly what the current implementation of |
@foxik I believe it would make sense to close this issue. Other, less common metrics, can be added on demand when the need arises. |
@pedro-r-marques Thanks, closing. |
System information
Describe the feature and the current behavior/state.
Currently, RaggedTensors can be passed as Keras model inputs. Soon, it will be possible to use them as targets (#45060 and #45015). Therefore, it would be great if they could also be passed to standard losses.
I therefore propose to extend the losses in
tf.keras.losses
to support RaggedTensors as inputs. That is currently (TF 2.4.0rc3) not possible:fails with an exception
TypeError: object of type 'RaggedTensor' has no len()
because it usestensorflow/tensorflow/python/keras/losses.py
Line 1196 in c37b2f1
(sparse_)categorical_crossentropy
,binary_crossentropy
etc.Will this change the current api? How?
RaggedTensors will be supported as arguments of
tf.keras.losses
methods and classes. The change is backward compatible.Who will benefit with this feature?
Anyone wanting to use RaggedTensors as outputs. There seem to be demand for it, see for example #44988, #44112, #43591, #43093, #42320, #41810.
Any Other info.
Note that RaggedTensors can be passed already to
tf.keras.metrics
, soreturns correctly
1/3
.The text was updated successfully, but these errors were encountered: