empty_like¶
Documentation¶
-
treetensor.torch.
empty_like
(input, *args, **kwargs)[source]¶ In
treetensor
, you can useones_like
to create a tree of tensors with all the uninitialized values of like another tree.Example:
>>> import torch >>> import treetensor.torch as ttorch >>> ttorch.empty_like(torch.randn(2, 3)) # the same as torch.empty_like(torch.randn(2, 3), 2.3) tensor([[-3.6515e+14, 4.5900e-41, -1.3266e-36], [ 3.0802e-41, 4.4842e-44, 0.0000e+00]]) >>> ttorch.empty_like({ ... 'a': torch.randn(2, 3), ... 'b': {'x': torch.randn(4, )}, ... }) <Tensor 0x7ff363bbc780> ├── a --> tensor([[-3.6515e+14, 4.5900e-41, -3.6515e+14], │ [ 4.5900e-41, 1.1592e-41, 0.0000e+00]]) └── b --> <Tensor 0x7ff3d6f3cb38> └── x --> tensor([-1.3267e-36, 3.0802e-41, -3.8049e-38, 3.0802e-41])
Torch Version Related
This documentation is based on torch.empty_like in torch v2.4.1+cu121. 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.4.1+cu121 is listed below.
Description From Torch v2.4.1+cu121¶
-
torch.
empty_like
(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) → Tensor¶ Returns an uninitialized tensor with the same size as
input
.torch.empty_like(input)
is equivalent totorch.empty(input.size(), dtype=input.dtype, layout=input.layout, device=input.device)
.- 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 ofinput
.- layout (
torch.layout
, optional): the desired layout of returned tensor. Default: if
None
, defaults to the layout ofinput
.- device (
torch.device
, optional): the desired device of returned tensor. Default: if
None
, defaults to the device ofinput
.- 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
.
- dtype (
Example:
>>> torch.empty((2,3), dtype=torch.int64) tensor([[ 9.4064e+13, 2.8000e+01, 9.3493e+13], [ 7.5751e+18, 7.1428e+18, 7.5955e+18]])