mygrad.identity#

mygrad.identity(n: int, dtype: ~mygrad.typing._dtype_like.DTypeLikeReals = <class 'float'>, *, constant: bool | None = None) Tensor[source]#

Return the identity Tensor; a square Tensor with 1s on the main diagonal and 0s elsewhere.

This docstring was adapted from numpy.identity [1]

Parameters:
nint

The number of rows and columns in the output Tensor.

dtypedata-type, optional (default=numpy.float32)

The data type of the output Tensor.

constantOptional[bool]

If True, this tensor is a constant, and thus does not facilitate back propagation.

Defaults to False for float-type data. Defaults to True for integer-type data.

Integer-type tensors must be constant.

Returns:
Tensor

A square Tensor whose main diagonal is 1 and all other elements are 0.

References

Examples

>>> import mygrad as mg
>>> mg.identity(3)
Tensor([[ 1.,  0.,  0.],
        [ 0.,  1.,  0.],
        [ 0.,  0.,  1.]])