mygrad.eye#

mygrad.eye(N: int, M: ~typing.Optional[int] = None, k: int = 0, dtype: ~mygrad.typing._dtype_like.DTypeLikeReals = <class 'float'>, *, constant: ~typing.Optional[bool] = None) Tensor[source]#

Return a 2D Tensor with ones on the diagonal and zeros elsewhere.

This docstring was adapted from numpy.eye [1]

Parameters
Nint

The number of rows in the output Tensor.

Mint, optional (default=None)

The number of columns in the output, or None to match rows.

kint, optional (default=0)

The index of the diagonal. 0 is the main diagonal; a positive value is the upper diagonal, while a negative value refers to the lower diagonal.

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 tensor whose elements are 0, except for the \(k\)-th diagonal, whose values are 1.

References

1

Retrieved from https://numpy.org/doc/stable/reference/generated/numpy.eye.html

Examples

>>> import mygrad as mg
>>> mg.eye(2, dtype=int)
Tensor([[1, 0],
        [0, 1]])
>>> mg.eye(3, k=1)
Tensor([[ 0.,  1.,  0.],
        [ 0.,  0.,  1.],
        [ 0.,  0.,  0.]])