MCPcopy Create free account
hub / github.com/pytorch/pytorch / __init__

Method __init__

caffe2/python/layers/sparse_lookup.py:131–261  ·  view source on GitHub ↗
(self, model, input_record, inner_shape, reducer,
                 weight_init=None, weight_optim=None,
                 name='sparse_lookup', regularizer=None, use_external_weights=False,
                 uniform_weight_init_scale_numerator=1.0, **kwargs)

Source from the content-addressed store, hash-verified

129 ]
130
131 def __init__(self, model, input_record, inner_shape, reducer,
132 weight_init=None, weight_optim=None,
133 name='sparse_lookup', regularizer=None, use_external_weights=False,
134 uniform_weight_init_scale_numerator=1.0, **kwargs):
135
136 super().__init__(model, name, input_record, **kwargs)
137
138 self.sparse_key = get_key(self.input_record)()
139 logger.info("Setup the sparse lookup layer for " + self.sparse_key)
140
141 # TODO Add some asserts about input type
142 if isinstance(inner_shape, int):
143 inner_shape = [inner_shape]
144 assert isinstance(inner_shape, list) or isinstance(inner_shape, tuple),\
145 "Unexpected type for inner_shape, expected list or tuple, got {0} for {1}".\
146 format(type(inner_shape), self.sparse_key)
147
148 if reducer == "PositionWeighted":
149 assert _is_id_score_list(self.input_record), (
150 "PositionWeighted only support IdScoreList, but got {} for {}"
151 + "please use PositionWeighted layer to convert IdList "
152 + "to IdScoreList"
153 ).format(repr(self.input_record), self.sparse_key)
154 self.external_weights = self.input_record.values()
155
156 elif reducer == "RecencyWeighted":
157 assert _is_id_score_list(self.input_record), (
158 "RecencyWeighted only supports IdScoreList, "
159 "while the sparse feature {} is not.".format(self.sparse_key)
160 )
161 self.external_weights = self.input_record.values()
162 # TODO: create a new type of reducer with external weights to wrap
163 # this and the above two cases since essentially their input formats
164 # are the same.
165 elif use_external_weights:
166 assert _is_id_score_list(self.input_record), (
167 "Use_external_weights only supports IdScoreList, "
168 "while the sparse feature {} is not.".format(self.sparse_key)
169 )
170 assert reducer in ["Sum", "WeightedSum"], (
171 "Use_external_weights only supports Sum reducer, "
172 "while the reducer is {}.".format(reducer)
173 )
174 self.external_weights = self.input_record.values()
175 self.reducer = reducer
176 self.use_external_weights = use_external_weights
177
178 input_dim = get_categorical_limit(self.input_record)
179 assert input_dim > 0, "{} should have categorical limit > 0, but got {}".format(
180 self.sparse_key, input_dim
181 )
182
183 self.input_dim = input_dim
184 self.shape = [input_dim] + inner_shape
185
186 self.trainer_version = get_trainer_version_based_on_optim(
187 weight_optim
188 )

Callers

nothing calls this directly

Calls 11

_get_default_init_opMethod · 0.95
get_keyFunction · 0.90
get_categorical_limitFunction · 0.90
isinstanceFunction · 0.85
_is_id_score_listFunction · 0.85
infoMethod · 0.80
formatMethod · 0.45
valuesMethod · 0.45
create_paramMethod · 0.45

Tested by

no test coverage detected