mygrad.zeros_like#

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

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

This docstring was adapted from numpy.zeros_like [1]

Parameters
otherArrayLike

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[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.

Integer-type tensors must be constant.

Returns
Tensor

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

See also

empty_like

Return an empty tensor with shape and type of input.

ones_like

Return an tensor of ones with shape and type of input.

full_like

Return a new tensor with shape of input filled with value.

zeros

Return a new tensor setting values to zero.

References

1

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

Examples

>>> import mygrad as mg
>>> x = mg.arange(6).reshape((2, 3))
>>> x
Tensor([[0, 1, 2],
        [3, 4, 5]])
>>> mg.zeros_like(x, constant=True)  # tensor will not back-propagate a gradient
Tensor([[0, 0, 0],
        [0, 0, 0]])
>>> y = mg.arange(3, dtype=float)
>>> y
Tensor([ 0.,  1.,  2.])
>>> mg.zeros_like(y)
Tensor([ 0.,  0.,  0.])