mygrad.ones_like#

mygrad.ones_like(other: ArrayLike, dtype: DTypeLikeReals | None = None, shape: int | Sequence[int] | None = None, *, constant: bool | None = None) Tensor[source]#

Return a Tensor of the same shape and type as the given, filled with ones.

This docstring was adapted from numpy.ones_like [1]

Parameters:
otherarray_like

The Tensor or array whose shape and datatype should be mirrored.

dtypeOptional[DTypeLikeReals]

Override the data type of the returned Tensor with this value, or None to not override.

shapeOptional[Union[int, Sequence[int]]]

If specified, overrides the shape of the result

constantOptional[bool]

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

Inferred from other, if other is a tensor Defaults to False for float-type data. Defaults to True for integer-type data.

Returns:
Tensor

A Tensor of ones whose shape and data type match other.

References

Examples

>>> import mygrad as mg
>>> x = mg.arange(6).reshape((2, 3))
>>> x
Tensor([[0, 1, 2],
        [3, 4, 5]])
>>> mg.ones_like(x)
Tensor([[1, 1, 1],
        [1, 1, 1]])
>>> y = mg.arange(3, dtype=float)
>>> y
Tensor([ 0.,  1.,  2.])
>>> mg.ones_like(y)
Tensor([ 1.,  1.,  1.])