pytorch/caffe2/python/layers/dropout.py
Jiyan Yang a8695178aa Adding parameter sharing API to Dper2
Summary:
To achive this, I modified the blob name scheme defined in a layer.
Before it was scope/fc_w and scope/fc_w_auto_0 (if there is another fc
    within the same scope).
Now I change it to scope/fc/w and scope/fc_auto_0/w.
That is, we rely on the uniqueness of the scoped layer name to define
names for blobs.

I also overwrote the create_param method in LayerModelHelper to let it
use the resolved name for blobs given the sharingparameter context.

There are some details such as making the initializer more structured
that I need to finalize.

Reviewed By: kennyhorror

Differential Revision: D5435132

fbshipit-source-id: a0525f5ea0977e255dd5ea765b38913f5951d455
2017-08-03 00:33:18 -07:00

49 lines
1.4 KiB
Python

# Module caffe2.python.layers.dropout
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
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,
**kwargs):
super(Dropout, self).__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.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=True)
def add_ops(self, net):
self.add_eval_ops(net)