3 from __future__
import absolute_import, division, print_function, unicode_literals
12 self, model, input_record, input_specs, name=
"feature_sparse_to_dense", **kwargs
15 `input_specs` follows the format of FeatureSpec from schema. To be more 16 precise it's a namedtuple that should have: 17 'feature_type', 'feature_names', 'feature_ids' 19 super(FeatureSparseToDense, self).
__init__(model, name, input_record, **kwargs)
25 assert len(feature_specs.feature_names) == len(feature_specs.feature_ids)
26 if feature_specs.feature_type ==
"FLOAT":
31 (np.float32, (len(feature_specs.feature_ids),)),
36 elif feature_specs.feature_type ==
"ID_LIST":
44 (np.int32, (len(feature_specs.feature_ids), 2)),
58 elif feature_specs.feature_type ==
"ID_SCORE_LIST":
66 (np.int32, (len(feature_specs.feature_ids), 2)),
87 elif feature_specs.feature_type ==
"EMBEDDING":
98 (np.int32, (len(feature_specs.feature_ids), 2)),
112 elif feature_specs.feature_type ==
"GENERIC_FEATURE":
126 (np.int32, (len(feature_specs.feature_ids), 2)),
142 "Unsupported input type: {0}".format(feature_specs.feature_type)
156 for field, feature_specs
in input_specs:
157 schema.attach_metadata_to_scalars(
160 self.
zero = model.global_constants[
"ZERO"]
161 self.
zero_range = model.global_constants[
"ZERO_RANGE"]
164 def add_ops(self, net):
167 if feature_specs.feature_type ==
"FLOAT":
168 net.SparseToDenseMask(
170 record[field].keys(),
171 record[field].values(),
173 record[field].lengths(),
176 mask=feature_specs.feature_ids,
178 elif feature_specs.feature_type ==
"ID_LIST":
179 id_list_ranges = net.LengthsToRanges(
180 record[field].values.lengths(), net.NextScopedBlob(
"id_list_ranges")
182 net.SparseToDenseMask(
184 record[field].keys(),
187 record[field].lengths(),
190 mask=feature_specs.feature_ids,
198 record[field].values.items(), self.
output_schema[field].values()
200 elif feature_specs.feature_type ==
"ID_SCORE_LIST":
202 id_list_ranges = net.LengthsToRanges(
203 record[field].values.lengths(),
204 net.NextScopedBlob(
"id_score_list_ranges"),
206 net.SparseToDenseMask(
208 record[field].keys(),
211 record[field].lengths(),
214 mask=feature_specs.feature_ids,
221 net.Alias(record[field].values.keys(), self.
output_schema[field].ids())
223 record[field].values.values(), self.
output_schema[field].scores()
225 elif feature_specs.feature_type ==
"EMBEDDING":
226 ranges = net.LengthsToRanges(
227 record[field].values.lengths(),
228 net.NextScopedBlob(
"embeddings_ranges"),
230 net.SparseToDenseMask(
232 record[field].keys(),
235 record[field].lengths(),
238 mask=feature_specs.feature_ids,
246 record[field].values.items(), self.
output_schema[field].values()
248 elif feature_specs.feature_type ==
"GENERIC_FEATURE":
250 feature_lengths_blob,
254 ) = net.ParseGeneric(
256 [
"feature_lengths",
"feature_ids",
"value_lengths",
"value_values"],
265 ranges = net.LengthsToRanges(
266 value_lengths_blob, net.NextScopedBlob(
"generics_ranges")
268 net.SparseToDenseMask(
269 [feature_ids_blob, ranges, self.
zero_range, feature_lengths_blob],
271 mask=feature_specs.feature_ids,
278 net.Alias(value_values_blob, self.
output_schema[field].values())
280 def get_metadata(self):
286 "type": feature_specs.feature_type,
287 "names": feature_specs.feature_names,
288 "ids": feature_specs.feature_ids,
294 if feature_specs.feature_type ==
"FLOAT":
295 metadata[-1][0][
"cardinality"] = 1
def get_next_blob_reference(self, name)
def __init__(self, model, input_record, input_specs, name="feature_sparse_to_dense", kwargs)