3 from __future__
import absolute_import
4 from __future__
import division
5 from __future__
import print_function
6 from __future__
import unicode_literals
8 from caffe2.python
import schema
9 from caffe2.python.layers.layers
import (
16 _known_types = [
'FLOAT',
'ID_LIST']
18 def __init__(self, model, input_record, input_specs,
19 name='sparse_to_dense', **kwargs):
21 `input_specs` follows the format of FeatureSpec from schema. To be more 22 precise it's a namedtuple that should have: 23 'feature_type', 'feature_names', 'feature_ids' 25 super(SparseToDense, self).
__init__(model, name,
26 input_record, **kwargs)
32 assert len(feature_specs.feature_names) ==\
33 len(feature_specs.feature_ids)
34 if feature_specs.feature_type ==
'FLOAT':
38 (np.float32, (len(feature_specs.feature_ids), )),
39 model.net.NextScopedBlob(name +
'_' + field +
'_output')
42 elif feature_specs.feature_type ==
'ID_LIST':
50 (len(feature_specs.feature_ids), 2)
52 model.net.NextScopedBlob(
53 name +
'_' + field +
'_ranges')
56 (
'values', input_record[field].values.items),
59 elif feature_specs.feature_type ==
'ID_SCORE_LIST':
67 (len(feature_specs.feature_ids), 2)
69 model.net.NextScopedBlob(
70 name +
'_' + field +
'_ranges')
73 (
'ids', input_record[field].values.keys),
74 (
'scores', input_record[field].values.values),
79 "Unsupported input type: {0}".
80 format(feature_specs.feature_type))
95 for field, feature_specs
in input_specs:
99 feature_specs=feature_specs)
101 self.
zero = model.global_constants[
'ZERO']
102 self.
zero_range = model.global_constants[
'ZERO_RANGE']
105 def add_ops(self, net):
108 if feature_specs.feature_type ==
'FLOAT':
109 net.SparseToDenseMask(
111 record[field].keys(),
112 record[field].values(),
114 record[field].lengths(),
119 mask=feature_specs.feature_ids,
121 elif feature_specs.feature_type ==
'ID_LIST':
122 id_list_ranges = net.LengthsToRanges(
123 record[field].values.lengths(),
124 net.NextScopedBlob(
'id_list_ranges')
126 net.SparseToDenseMask(
128 record[field].keys(), id_list_ranges, self.
zero_range,
129 record[field].lengths()
132 mask=feature_specs.feature_ids,
134 elif feature_specs.feature_type ==
'ID_SCORE_LIST':
136 id_list_ranges = net.LengthsToRanges(
137 record[field].values.lengths(),
138 net.NextScopedBlob(
'id_score_list_ranges')
140 net.SparseToDenseMask(
142 record[field].keys(), id_list_ranges, self.
zero_range,
143 record[field].lengths()
146 mask=feature_specs.feature_ids,
149 def get_metadata(self):
155 'type': feature_specs.feature_type,
156 'names': feature_specs.feature_names,
157 'ids': feature_specs.feature_ids,
163 if feature_specs.feature_type ==
'FLOAT':
164 metadata[-1][0][
'cardinality'] = 1
def attach_metadata_to_scalars(field, metadata)
def __init__(self, model, input_record, input_specs, name='sparse_to_dense', kwargs)