tf.contrib.lookup.string_to_index
Maps tensor of strings into int64 indices based on mapping. (deprecated)
tf.contrib.lookup.string_to_index(
tensor, mapping, default_value=-1, name=None
)
This operation converts tensor of strings into int64 indices. The mapping is initialized from a string mapping tensor where each element is a key and corresponding index within the tensor is the value.
Any entry in the input which does not have a corresponding entry in 'mapping' (an out-of-vocabulary entry) is assigned the default_value
Elements in mapping cannot be duplicated, otherwise the initialization will throw a FailedPreconditionError.
The underlying table must be initialized by calling session.run(tf.compat.v1.tables_initializer) once.
For example:
mapping_strings = tf.constant(["emerson", "lake", "palmer"])
feats = tf.constant(["emerson", "lake", "and", "palmer"])
ids = tf.contrib.lookup.string_to_index(
feats, mapping=mapping_strings, default_value=-1)
...
tf.compat.v1.tables_initializer().run()
ids.eval() ==> [0, 1, -1, 2]
| Args | |
|---|---|
tensor | A 1-D input Tensor with the strings to map to indices. |
mapping | A 1-D string Tensor that specifies the mapping of strings to indices. |
default_value | The int64 value to use for out-of-vocabulary strings. Defaults to -1. |
name | A name for this op (optional). |
| Returns | |
|---|---|
The mapped indices. It has the same shape and tensor type (dense or sparse) as tensor. |
© 2020 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 3.0.
Code samples licensed under the Apache 2.0 License.
https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/contrib/lookup/string_to_index