-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add limited SoftmaxCrossEntropyLoss support
Signed-off-by: Jonathan Sparling <[email protected]>
- Loading branch information
Jonathan Sparling
committed
Sep 1, 2022
1 parent
f9ebc35
commit b876a41
Showing
2 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import tensorflow as tf | ||
|
||
from onnx_tf.handlers.backend_handler import BackendHandler | ||
from onnx_tf.handlers.handler import onnx_op | ||
from onnx_tf.handlers.handler import tf_func | ||
|
||
|
||
@onnx_op("SoftmaxCrossEntropyLoss") | ||
@tf_func(tf.nn.sparse_softmax_cross_entropy_with_logits) | ||
class SoftmaxCrossEntropyLoss(BackendHandler): | ||
@classmethod | ||
def _common(cls, node, **kwargs): | ||
logits = kwargs["tensor_dict"][node.inputs[0]] | ||
labels = kwargs["tensor_dict"][node.inputs[1]] | ||
|
||
labels_shape = tf.shape(labels) | ||
if labels_shape.shape[0] > 1: | ||
raise NotImplementedError( | ||
"SoftmaxCrossEntropyLoss support is limited to rank 1 label tensors." | ||
.format(spatial_size)) | ||
|
||
return [ | ||
tf.nn.sparse_softmax_cross_entropy_with_logits(labels=labels, logits=logits) | ||
] | ||
|
||
@classmethod | ||
def version_12(cls, node, **kwargs): | ||
return cls._common(node, **kwargs) | ||
|
||
@classmethod | ||
def version_13(cls, node, **kwargs): | ||
return cls._common(node, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters