mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01: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
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
# Module caffe2.python.layers.dropout
|
|
|
|
|
|
|
|
|
|
|
|
from caffe2.python import schema
|
|
from caffe2.python.layers.layers import ModelLayer
|
|
|
|
|
|
class Dropout(ModelLayer):
|
|
|
|
def __init__(
|
|
self,
|
|
model,
|
|
input_record,
|
|
name='dropout',
|
|
ratio=0.5,
|
|
dropout_for_eval=False,
|
|
**kwargs):
|
|
|
|
super().__init__(model, name, input_record, **kwargs)
|
|
assert isinstance(input_record, schema.Scalar), "Incorrect input type"
|
|
assert (ratio >= 0 and ratio < 1.0), \
|
|
"Expected 0 <= ratio < 1, but got ratio of %s" % ratio
|
|
|
|
self.output_schema = input_record.clone_schema()
|
|
self.output_schema.set_value(self.get_next_blob_reference('output'))
|
|
self.dropout_for_eval = dropout_for_eval
|
|
|
|
self.ratio = ratio
|
|
|
|
def _add_ops(self, net, is_test):
|
|
input_blob = self.input_record.field_blobs()
|
|
output_blobs = self.output_schema.field_blobs() \
|
|
+ [net.NextScopedBlob('d_mask')]
|
|
|
|
net.Dropout(input_blob,
|
|
output_blobs,
|
|
ratio=self.ratio,
|
|
is_test=is_test)
|
|
|
|
def add_train_ops(self, net):
|
|
self._add_ops(net, is_test=False)
|
|
|
|
def add_eval_ops(self, net):
|
|
self._add_ops(net, is_test=(not self.dropout_for_eval))
|
|
|
|
def add_ops(self, net):
|
|
self.add_eval_ops(net)
|