tf.contrib.lookup.string_to_index

View source on GitHub

Maps tensor of strings into int64 indices based on mapping. (deprecated)

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]

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).

The mapped indices. It has the same shape and tensor type (dense or sparse) as tensor.