mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/17549 Currently Dropout is only enabled in training, we enable the option of having dropout in Eval. This is to follow [1]. This functionality would be used for uncertainty estimation in exploration project. [1] Gal, Yarin, and Zoubin Ghahramani. "Dropout as a bayesian approximation: Representing model uncertainty in deep learning." international conference on machine learning. 2016. Reviewed By: Wakeupbuddy Differential Revision: D14216216 fbshipit-source-id: 87c8c9cc522a82df467b685805f0775c86923d8b
51 lines
1.5 KiB
Python
51 lines
1.5 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,
|
|
dropout_for_eval=False,
|
|
**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.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)
|