mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Rewrite Python built-in class `super()` calls. Only non-semantic changes should be applied. - #94587 - #94588 - #94592 Also, methods with only a `super()` call are removed: ```diff class MyModule(nn.Module): - def __init__(self): - super().__init__() - def forward(self, ...): ... ``` Some cases that change the semantics should be kept unchanged. E.g.:f152a79be9/caffe2/python/net_printer.py (L184-L190)f152a79be9/test/test_jit_fuser_te.py (L2628-L2635)Pull Request resolved: https://github.com/pytorch/pytorch/pull/94587 Approved by: https://github.com/ezyang
49 lines
1.4 KiB
Python
49 lines
1.4 KiB
Python
## @package batch_sigmoid_cross_entropy_loss
|
|
# Module caffe2.python.layers.batch_sigmoid_cross_entropy_loss
|
|
|
|
|
|
|
|
|
|
|
|
from caffe2.python import schema
|
|
from caffe2.python.layers.layers import ModelLayer
|
|
from caffe2.python.layers.tags import Tags
|
|
import numpy as np
|
|
|
|
|
|
class BatchSigmoidCrossEntropyLoss(ModelLayer):
|
|
def __init__(
|
|
self,
|
|
model,
|
|
input_record,
|
|
name='batch_sigmoid_cross_entropy_loss',
|
|
**kwargs
|
|
):
|
|
super().__init__(model, name, input_record, **kwargs)
|
|
|
|
assert schema.is_schema_subset(
|
|
schema.Struct(
|
|
('label', schema.Scalar(np.float32)),
|
|
('prediction', schema.Scalar(np.float32)),
|
|
),
|
|
input_record
|
|
)
|
|
assert input_record.prediction.field_type().shape == \
|
|
input_record.label.field_type().shape, \
|
|
"prediction and label must have the same shape"
|
|
|
|
self.tags.update([Tags.EXCLUDE_FROM_PREDICTION])
|
|
|
|
self.output_schema = schema.Scalar(
|
|
(np.float32, tuple()), self.get_next_blob_reference('loss')
|
|
)
|
|
|
|
def add_ops(self, net):
|
|
sigmoid_cross_entropy = net.SigmoidCrossEntropyWithLogits(
|
|
[self.input_record.prediction(), self.input_record.label()],
|
|
net.NextScopedBlob('sigmoid_cross_entropy')
|
|
)
|
|
|
|
net.AveragedLoss(
|
|
sigmoid_cross_entropy, self.output_schema.field_blobs())
|