The locations represented by indices in indices take value on_value,
while all other locations take value off_value.
on_value and off_value must have matching data types. If dtype is also
provided, they must be the same data type as specified by dtype.
If on_value is not provided, it will default to the value 1 with type
dtype
If off_value is not provided, it will default to the value 0 with type
dtype
If the input indices is rank N, the output will have rank N+1. The
new axis is created at dimension axis (default: the new axis is appended
at the end).
If indices is a scalar the output shape will be a vector of length depth
If indices is a vector of length features, the output shape will be:
featuresxdepthifaxis==-1depthxfeaturesifaxis==0
If indices is a matrix (batch) with shape [batch, features], the output
shape will be:
If indices is a RaggedTensor, the 'axis' argument must be positive and refer
to a non-ragged axis. The output will be equivalent to applying 'one_hot' on
the values of the RaggedTensor, and creating a new RaggedTensor from the
result.
If dtype is not provided, it will attempt to assume the data type of
on_value or off_value, if one or both are passed in. If none of
on_value, off_value, or dtype are provided, dtype will default to the
value tf.float32.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.one_hot\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/array_ops.py#L3954-L4114) |\n\nReturns a one-hot tensor.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.one_hot`](https://www.tensorflow.org/api_docs/python/tf/one_hot)\n\n\u003cbr /\u003e\n\n tf.one_hot(\n indices,\n depth,\n on_value=None,\n off_value=None,\n axis=None,\n dtype=None,\n name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [Import a JAX model using JAX2TF](https://www.tensorflow.org/guide/jax2tf) - [Quickstart for the TensorFlow Core APIs](https://www.tensorflow.org/guide/core/quickstart_core) - [Training \\& evaluation with the built-in methods](https://www.tensorflow.org/guide/keras/training_with_built_in_methods) | - [Adversarial example using FGSM](https://www.tensorflow.org/tutorials/generative/adversarial_fgsm) - [Custom Federated Algorithms, Part 2: Implementing Federated Averaging](https://www.tensorflow.org/federated/tutorials/custom_federated_algorithms_2) - [Federated Learning for Image Classification](https://www.tensorflow.org/federated/tutorials/federated_learning_for_image_classification) - [Client-efficient large-model federated learning via \\`federated_select\\` and sparse aggregation](https://www.tensorflow.org/federated/tutorials/sparse_federated_learning) - [Training with Orbit](https://www.tensorflow.org/tfmodels/orbit/index) |\n\nSee also [`tf.fill`](../tf/fill), [`tf.eye`](../tf/eye).\n\nThe locations represented by indices in `indices` take value `on_value`,\nwhile all other locations take value `off_value`.\n\n`on_value` and `off_value` must have matching data types. If `dtype` is also\nprovided, they must be the same data type as specified by `dtype`.\n\nIf `on_value` is not provided, it will default to the value `1` with type\n`dtype`\n\nIf `off_value` is not provided, it will default to the value `0` with type\n`dtype`\n\nIf the input `indices` is rank `N`, the output will have rank `N+1`. The\nnew axis is created at dimension `axis` (default: the new axis is appended\nat the end).\n\nIf `indices` is a scalar the output shape will be a vector of length `depth`\n\nIf `indices` is a vector of length `features`, the output shape will be: \n\n features x depth if axis == -1\n depth x features if axis == 0\n\nIf `indices` is a matrix (batch) with shape `[batch, features]`, the output\nshape will be: \n\n batch x features x depth if axis == -1\n batch x depth x features if axis == 1\n depth x batch x features if axis == 0\n\nIf `indices` is a RaggedTensor, the 'axis' argument must be positive and refer\nto a non-ragged axis. The output will be equivalent to applying 'one_hot' on\nthe values of the RaggedTensor, and creating a new RaggedTensor from the\nresult.\n\nIf `dtype` is not provided, it will attempt to assume the data type of\n`on_value` or `off_value`, if one or both are passed in. If none of\n`on_value`, `off_value`, or `dtype` are provided, `dtype` will default to the\nvalue [`tf.float32`](../tf#float32).\n| **Note:** If a non-numeric data type output is desired ([`tf.string`](../tf#string), [`tf.bool`](../tf#bool), etc.), both `on_value` and `off_value` *must* be provided to `one_hot`.\n\n#### For example:\n\n indices = [0, 1, 2]\n depth = 3\n tf.one_hot(indices, depth) # output: [3 x 3]\n # [[1., 0., 0.],\n # [0., 1., 0.],\n # [0., 0., 1.]]\n\n indices = [0, 2, -1, 1]\n depth = 3\n tf.one_hot(indices, depth,\n on_value=5.0, off_value=0.0,\n axis=-1) # output: [4 x 3]\n # [[5.0, 0.0, 0.0], # one_hot(0)\n # [0.0, 0.0, 5.0], # one_hot(2)\n # [0.0, 0.0, 0.0], # one_hot(-1)\n # [0.0, 5.0, 0.0]] # one_hot(1)\n\n indices = [[0, 2], [1, -1]]\n depth = 3\n tf.one_hot(indices, depth,\n on_value=1.0, off_value=0.0,\n axis=-1) # output: [2 x 2 x 3]\n # [[[1.0, 0.0, 0.0], # one_hot(0)\n # [0.0, 0.0, 1.0]], # one_hot(2)\n # [[0.0, 1.0, 0.0], # one_hot(1)\n # [0.0, 0.0, 0.0]]] # one_hot(-1)\n\n indices = tf.ragged.constant([[0, 1], [2]])\n depth = 3\n tf.one_hot(indices, depth) # output: [2 x None x 3]\n # [[[1., 0., 0.],\n # [0., 1., 0.]],\n # [[0., 0., 1.]]]\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-------------|------------------------------------------------------------------------------------|\n| `indices` | A `Tensor` of indices. |\n| `depth` | A scalar defining the depth of the one hot dimension. |\n| `on_value` | A scalar defining the value to fill in output when `indices[j] = i`. (default: 1) |\n| `off_value` | A scalar defining the value to fill in output when `indices[j] != i`. (default: 0) |\n| `axis` | The axis to fill (default: -1, a new inner-most axis). |\n| `dtype` | The data type of the output tensor. |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|----------|---------------------|\n| `output` | The one-hot tensor. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|------------------------------------------------------------------|\n| `TypeError` | If dtype of either `on_value` or `off_value` don't match `dtype` |\n| `TypeError` | If dtype of `on_value` and `off_value` don't match one another |\n\n\u003cbr /\u003e"]]