Create a feature vectorizer from input features. This returns a 2-tuple ``(spec, num_dimension)`` for a feature vectorizer that puts everything into a single array with a length equal to the total size of all the input features. Parameters ---------- input_features: [list o
(input_features, output_feature_name, known_size_map={})
| 12 | |
| 13 | |
| 14 | def create_feature_vectorizer(input_features, output_feature_name, known_size_map={}): |
| 15 | """ |
| 16 | Create a feature vectorizer from input features. This returns a 2-tuple |
| 17 | ``(spec, num_dimension)`` for a feature vectorizer that puts everything into a |
| 18 | single array with a length equal to the total size of all the input features. |
| 19 | |
| 20 | Parameters |
| 21 | ---------- |
| 22 | input_features: [list of 2-tuples] |
| 23 | Name(s) of the input features, given as a list of ``('name', datatype)`` |
| 24 | tuples. The datatypes entry is one of the data types defined in the |
| 25 | ``datatypes`` module. Allowed ``datatypes`` are ``datatype.Int64``, |
| 26 | ``datatype.Double``, ``datatypes.Dictionary``, and ``datatype.Array``. |
| 27 | |
| 28 | If the feature is a dictionary type, then the dictionary must have integer |
| 29 | keys, and the number of dimensions to expand it into must be provided by |
| 30 | ``known_size_map``. |
| 31 | |
| 32 | Feature indices in the final array are counted sequentially from the |
| 33 | from 0 through the total number of features. |
| 34 | |
| 35 | |
| 36 | output_feature_name: str |
| 37 | The name of the output feature. The type is an Array |
| 38 | List of the output features of the network. |
| 39 | |
| 40 | known_size_map: |
| 41 | A dictionary mapping the feature name to the expanded size in the final |
| 42 | array. This is most useful for specifying the size of sparse vectors |
| 43 | given as dictionaries of index to value. |
| 44 | |
| 45 | """ |
| 46 | |
| 47 | spec = proto.Model_pb2.Model() |
| 48 | spec.specificationVersion = SPECIFICATION_VERSION |
| 49 | |
| 50 | input_features = process_or_validate_features(input_features) |
| 51 | |
| 52 | feature_vectorizer = spec.featureVectorizer |
| 53 | |
| 54 | num_output_dimensions = 0 |
| 55 | |
| 56 | for n, ft in input_features: |
| 57 | if n in known_size_map: |
| 58 | dim = known_size_map[n] |
| 59 | |
| 60 | if ft.num_elements is not None: |
| 61 | if dim != ft.num_elements: |
| 62 | raise ValueError( |
| 63 | "In feature {}, override size {} not compatible with inherent " |
| 64 | "value size {}.".format(n, dim, ft.num_elements) |
| 65 | ) |
| 66 | else: |
| 67 | if ft.num_elements is None: |
| 68 | raise ValueError( |
| 69 | "In feature {}, inherent size unknown so must be manually supplied.".format( |
| 70 | n |
| 71 | ) |
no test coverage detected