zeros_like

Documentation

treetensor.torch.zeros_like(input, *args, **kwargs)[source]

In treetensor, you can use zeros_like to create a tree of tensors with all zeros like another tree.

Example:

>>> import torch
>>> import treetensor.torch as ttorch
>>> ttorch.zeros_like(torch.randn(2, 3))  # the same as torch.zeros_like(torch.randn(2, 3))
tensor([[0., 0., 0.],
        [0., 0., 0.]])

>>> ttorch.zeros_like({
...    'a': torch.randn(2, 3),
...    'b': {'x': torch.randn(4, )},
... })
<Tensor 0x7ff363bb6128>
├── a --> tensor([[0., 0., 0.],
│                 [0., 0., 0.]])
└── b --> <Tensor 0x7ff363bb6080>
    └── x --> tensor([0., 0., 0., 0.])

Torch Version Related

This documentation is based on torch.zeros_like in torch v2.0.1+cu117. Its arguments’ arrangements depend on the version of pytorch you installed.

If some arguments listed here are not working properly, please check your pytorch’s version with the following command and find its documentation.

1
python -c 'import torch;print(torch.__version__)'

The arguments and keyword arguments supported in torch v2.0.1+cu117 is listed below.

Description From Torch v2.0.1+cu117

torch.zeros_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format)Tensor

Returns a tensor filled with the scalar value 0, with the same size as input. torch.zeros_like(input) is equivalent to torch.zeros(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).

Warning

As of 0.4, this function does not support an out keyword. As an alternative, the old torch.zeros_like(input, out=output) is equivalent to torch.zeros(input.size(), out=output).

Args:

input (Tensor): the size of input will determine size of the output tensor.

Keyword args:
dtype (torch.dtype, optional): the desired data type of returned Tensor.

Default: if None, defaults to the dtype of input.

layout (torch.layout, optional): the desired layout of returned tensor.

Default: if None, defaults to the layout of input.

device (torch.device, optional): the desired device of returned tensor.

Default: if None, defaults to the device of input.

requires_grad (bool, optional): If autograd should record operations on the

returned tensor. Default: False.

memory_format (torch.memory_format, optional): the desired memory format of

returned Tensor. Default: torch.preserve_format.

Example:

>>> input = torch.empty(2, 3)
>>> torch.zeros_like(input)
tensor([[ 0.,  0.,  0.],
        [ 0.,  0.,  0.]])