one_hot#
- ivy.one_hot(indices, depth, /, *, on_value=None, off_value=None, axis=None, dtype=None, device=None, out=None)[source]#
Return a one-hot array. The locations represented by indices in the parameter indices take value on_value, while all other locations take value off_value.
- Parameters:
indices (
Union[Array,NativeArray]) – Indices for where the ones should be scattered [batch_shape, dim]depth (
int) – Scalar defining the depth of the one-hot dimension.on_value (
Optional[Number], default:None) – Scalar defining the value to fill in output when indices[j] == i. Default:1.off_value (
Optional[Number], default:None) – Scalar defining the value to fill in output when indices[j] != i. Default:0.axis (
Optional[int], default:None) – Axis to scatter on. The default is-1, a new inner-most axis is created.dtype (
Optional[Union[Dtype,NativeDtype]], default:None) – The data type of the output tensor.device (
Optional[Union[Device,NativeDevice]], default:None) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. Same as x if None.out (
Optional[Array], default:None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
- Returns:
ret – Tensor of zeros with the same shape and type as a, unless dtype provided which overrides.
Examples
With
ivy.Arrayinputs:>>> x = ivy.array([3, 1]) >>> y = 5 >>> z = x.one_hot(5) >>> print(z) ivy.array([[0., 0., 0., 1., 0.], ... [0., 1., 0., 0., 0.]])
>>> x = ivy.array([0]) >>> y = 5 >>> ivy.one_hot(x, y) ivy.array([[1., 0., 0., 0., 0.]])
>>> x = ivy.array([0]) >>> y = 5 >>> ivy.one_hot(x, 5, out=z) ivy.array([[1., 0., 0., 0., 0.]]) >>> print(z) ivy.array([[1., 0., 0., 0., 0.]])
With
ivy.Containerinput:>>> x = ivy.Container(a=ivy.array([1, 2]), b=ivy.array([3, 1]), c=ivy.array([2, 3])) >>> y = 5 >>> z = x.one_hot(y) >>> print(z) { a: ivy.array([[0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.]]), b: ivy.array([[0., 0., 0., 1., 0.], [0., 1., 0., 0., 0.]]), c: ivy.array([[0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.]]) }
>>> x = ivy.Container(a=ivy.array([2]), b=ivy.array([], dtype=ivy.int32), c=ivy.native_array([4])) >>> y = 7 >>> z = x.one_hot(y) >>> print(z) { a: ivy.array([[0., 0., 1., 0., 0., 0., 0.]]), b: ivy.array([], shape=(0, 7)), c: ivy.array([[0., 0., 0., 0., 1., 0., 0.]]) }
- Array.one_hot(self, depth, /, *, on_value=None, off_value=None, axis=None, dtype=None, device=None, out=None)[source]#
ivy.Array instance method variant of ivy.one_hot. This method simply wraps the function, and so the docstring for ivy.one_hot also applies to this method with minimal changes.
- Parameters:
self (
Array) – input array containing the indices for which the ones should be scattereddepth (
int) – Scalar defining the depth of the one-hot dimension.on_value (
Optional[Number], default:None) – Value to fill in output whenindices[j] == i. Default 1.off_value (
Optional[Number], default:None) – Value to fill in output whenindices[j] != i. Default 0.axis (
Optional[int], default:None) – The axis to scatter on. The default is-1which is the last axis.dtype (
Optional[Union[Dtype,NativeDtype]], default:None) – The data type of the output array. If None, the data type of the on_value is used, or if that is None, the data type of the off_value is used. Default float32.device (
Optional[Union[Device,NativeDevice]], default:None) – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. Same as x if None.out (
Optional[Array], default:None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Array- Returns:
ret – Tensor of zeros with the same shape and type as a, unless dtype provided which overrides.
Examples
With
ivy.Arrayinputs:>>> x = ivy.array([3, 1]) >>> y = 5 >>> z = x.one_hot(5) >>> print(z) ivy.array([[0., 0., 0., 1., 0.], ... [0., 1., 0., 0., 0.]])
>>> x = ivy.array([0]) >>> y = 5 >>> ivy.one_hot(x, y) ivy.array([[1., 0., 0., 0., 0.]])
>>> x = ivy.array([0]) >>> y = 5 >>> ivy.one_hot(x, 5, out=z) ivy.array([[1., 0., 0., 0., 0.]]) >>> print(z) ivy.array([[1., 0., 0., 0., 0.]])
- Container.one_hot(self, depth, /, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, *, on_value=None, off_value=None, axis=None, dtype=None, device=None, out=None)[source]#
ivy.Container instance method variant of ivy.one_hot. This method simply wraps the function, and so the docstring for ivy.one_hot also applies to this method with minimal changes.
- Parameters:
self (
Container) – Indices for where the ones should be scattered [batch_shape, dim]depth (
Union[int,Container]) – Scalar defining the depth of the one-hot dimension.on_value (
Optional[Union[Number,Container]], default:None) – Value to fill in output when indices[j] == i. If None, defaults to 1.off_value (
Optional[Union[Number,Container]], default:None) – Value to fill in output when indices[j] != i. If None, defaults to 0.axis (
Optional[Union[int,Container]], default:None) – Axis to scatter on. The default is-1, a new inner-most axis is created.dtype (
Optional[Union[Dtype,NativeDtype,Container]], default:None) – The dtype of the returned tensor. If None, defaults to the on_value dtype or the off_value dtype. If both are None, defaults to float32.key_chains (
Optional[Union[List[str],Dict[str,str],Container]], default:None) – The key-chains to apply or not apply the method to. Default isNone.to_apply (
Union[bool,Container], default:True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue.prune_unapplied (
Union[bool,Container], default:False) – Whether to prune key_chains for which the function was not applied. Default is False.map_sequences (
Union[bool,Container], default:False) – Whether to also map method to sequences (lists, tuples). Default isFalse.out (
Optional[Container], default:None) – optional output container, for writing the result to. It must have a shape that the inputs broadcast to.
- Return type:
Container- Returns:
ret – container with tensors of zeros with the same shape and type as the inputs, unless dtype provided which overrides.
Examples
With
ivy.Containerinput:>>> x = ivy.Container(a=ivy.array([1, 2]), b=ivy.array([3, 1]), c=ivy.array([2, 3])) >>> y = 5 >>> z = x.one_hot(y) >>> print(z) { a: ivy.array([[0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.]]), b: ivy.array([[0., 0., 0., 1., 0.], [0., 1., 0., 0., 0.]]), c: ivy.array([[0., 0., 1., 0., 0.], [0., 0., 0., 1., 0.]]) }
>>> x = ivy.Container(a=ivy.array([1, 2]), b=ivy.array([]), c=ivy.native_array([4])) >>> y = 5 >>> z = x.one_hot(y) >>> print(z) { a: ivy.array([[0., 1., 0., 0., 0.], [0., 0., 1., 0., 0.]]), b: ivy.array([], shape=(0, 5)), c: ivy.array([[0., 0., 0., 0., 1.]]) }