Caffe2 - Python API
A deep learning, cross platform ML framework
reservoir_sampling.py
1 # Copyright (c) 2016-present, Facebook, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ##############################################################################
15 
16 ## @package reservoir_sampling
17 # Module caffe2.python.layers.reservoir_sampling
18 from __future__ import absolute_import
19 from __future__ import division
20 from __future__ import print_function
21 from __future__ import unicode_literals
22 
23 from caffe2.python import core, schema
24 from caffe2.python.layers.layers import ModelLayer
25 
26 
28  """
29  Collect samples from input record w/ reservoir sampling. If you have complex
30  data, use PackRecords to pack it before using this layer.
31 
32  This layer is not thread safe.
33  """
34 
35  def __init__(self, model, input_record, num_to_collect,
36  name='reservoir_sampling', **kwargs):
37  super(ReservoirSampling, self).__init__(
38  model, name, input_record, **kwargs)
39  assert num_to_collect > 0
40  self.num_to_collect = num_to_collect
41 
42  self.reservoir = self.create_param(
43  param_name='reservoir',
44  shape=[0],
45  initializer=('ConstantFill',),
46  optimizer=model.NoOptim,
47  )
48  self.num_visited_blob = self.create_param(
49  param_name='num_visited',
50  shape=[],
51  initializer=('ConstantFill', {
52  'value': 0,
53  'dtype': core.DataType.INT64,
54  }),
55  optimizer=model.NoOptim,
56  )
57  self.mutex = self.create_param(
58  param_name='mutex',
59  shape=None,
60  initializer=('CreateMutex',),
61  optimizer=model.NoOptim,
62  )
63 
64  self.extra_input_blobs = []
65  self.extra_output_blobs = []
66  if 'object_id' in input_record:
67  object_to_pos = self.create_param(
68  param_name='object_to_pos',
69  initializer=('CreateMap', {
70  'key_dtype': core.DataType.INT64,
71  'valued_dtype': core.DataType.INT32,
72  }),
73  optimizer=model.NoOptim,
74  )
75  pos_to_object = self.create_param(
76  param_name='pos_to_object',
77  shape=[0],
78  initializer=('ConstantFill', {
79  'value': 0,
80  'dtype': core.DataType.INT64,
81  }),
82  optimizer=model.NoOptim,
83  )
84  self.extra_input_blobs.append(input_record.object_id())
85  self.extra_input_blobs.extend([object_to_pos, pos_to_object])
86  self.extra_output_blobs.extend([object_to_pos, pos_to_object])
87 
89  (
90  'reservoir',
91  schema.from_blob_list(input_record.data, [self.reservoir])
92  ),
93  ('num_visited', schema.Scalar(blob=self.num_visited_blob)),
94  ('mutex', schema.Scalar(blob=self.mutex)),
95  )
96 
97  def add_ops(self, net):
98  net.ReservoirSampling(
99  [self.reservoir, self.num_visited_blob, self.input_record.data(),
100  self.mutex] + self.extra_input_blobs,
101  [self.reservoir, self.num_visited_blob] + self.extra_output_blobs,
102  num_to_collect=self.num_to_collect,
103  )
def create_param(self, param_name, shape, initializer, optimizer, ps_param=None, regularizer=None)
Definition: layers.py:337