MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _EmbeddingResult

Function _EmbeddingResult

tensorflow/python/kernel_tests/embedding_ops_test.py:187–241  ·  view source on GitHub ↗
(params,
                     id_vals,
                     num_shards,
                     vocab_size,
                     partition_strategy="mod",
                     weight_vals=None)

Source from the content-addressed store, hash-verified

185
186
187def _EmbeddingResult(params,
188 id_vals,
189 num_shards,
190 vocab_size,
191 partition_strategy="mod",
192 weight_vals=None):
193 if weight_vals is None:
194 weight_vals = np.copy(id_vals)
195 weight_vals.fill(1)
196 values = []
197 weights = []
198 weights_squared = []
199 for ids, wts in zip(id_vals, weight_vals):
200 value_aggregation = None
201 weight_aggregation = None
202 squared_weight_aggregation = None
203 if isinstance(ids, compat.integral_types):
204 ids = [ids]
205 wts = [wts]
206 for i, weight_value in zip(ids, wts):
207 if partition_strategy == "mod":
208 val = np.copy(params[_PName(i % num_shards) + ":0"][
209 i // num_shards, :]) * weight_value
210 elif partition_strategy == "div":
211 ids_per_partition, extras = divmod(vocab_size, num_shards)
212 threshold = extras * (ids_per_partition + 1)
213 if i < threshold:
214 partition = i // (ids_per_partition + 1)
215 offset = i % (ids_per_partition + 1)
216 else:
217 partition = extras + (i - threshold) // ids_per_partition
218 offset = (i - threshold) % ids_per_partition
219 val = np.copy(
220 params[_PName(partition) + ":0"][offset, :]) * weight_value
221 else:
222 assert False
223 if value_aggregation is None:
224 assert weight_aggregation is None
225 assert squared_weight_aggregation is None
226 value_aggregation = val
227 weight_aggregation = weight_value
228 squared_weight_aggregation = weight_value * weight_value
229 else:
230 assert weight_aggregation is not None
231 assert squared_weight_aggregation is not None
232 value_aggregation += val
233 weight_aggregation += weight_value
234 squared_weight_aggregation += weight_value * weight_value
235 values.append(value_aggregation)
236 weights.append(weight_aggregation)
237 weights_squared.append(squared_weight_aggregation)
238 values = np.array(values).astype(np.float32)
239 weights = np.array(weights).astype(np.float32)
240 weights_squared = np.array(weights_squared).astype(np.float32)
241 return values, weights, weights_squared
242
243
244class EmbeddingLookupTest(test.TestCase):

Calls 5

divmodFunction · 0.85
fillMethod · 0.80
_PNameFunction · 0.70
copyMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected