tf.one_ How to use hot ()

Tensorflow study notes
tf.one_hot
This paper only serves as a personal learning record, please refer to tensorFlow Chinese official website TF Chinese official website
Call format
tf.one_hot(indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None)
Parameters that
Indices: Tensors of an index.
· depth: a scalar quantity defined in one_hot dimension
· on_value: a set of indices[j] = I (default: 1)
· off_value: a set of indices[j]! = I (default: 0)
· axis: axis to be filled (default: -1, a new innermost axis)
· dtype: the data type of the output tensor.
· name: the name of the operation (optional).
The output
A one_hot tensor
A possible mistake

    TypeError
    whether on_value or off_value does not match dtype. TypeError
    whether on_value and off_value do not match each other

prompt

    indices location value is on_value, while the other location is off_value. The on_value and OFF_value data types must match. If dType has a value, they must take the same value as the type displayed by DTYPE.
    if on_value has no value, the default value is 1, and the output is dtype.
    if off_value has no value, the default value is 0, and the output is dtype.
    if the indices have N dimensions, the output is N+1. If the indices are scalars, the output is a vector with a length of depth. If the indices are tensor with features, the output will be:
    . If the indices are indices with batch size [batch, features], the output will be:
    . If the indices are RaggedTensor, the axis must be positive and form an axis which is hard to form. The output is equivalent to the output of one_hot applied with a value of an irregular shape, and a new irregular shape is generated from the result.
    if dtype has no value, it will try to assume that the data type is on_value or off_value if one or both pass. If on_value, off_value, dtype have no value, dtpye will default to be tf.float32.
    ** note: if output of non-numeric types (such as tf.string, tf.bool, etc), on_value, off_value all need to have a value.

example

Read More: