zeros_like¶
- metatensor.zeros_like(tensor: TensorMap, gradients: List[str] | str | None = None, requires_grad: bool = False) TensorMap [source]¶
Return a new
TensorMap
with the same metadata as tensor, and all values equal to zero.- Parameters:
tensor (TensorMap) – Input tensor from which the metadata is taken.
gradients (List[str] | str | None) – Which gradients should be present in the output. If this is
None
(default) all gradient oftensor
are present in the newTensorMap
. If this is an empty list[]
, no gradients information is copied.requires_grad (bool) – If autograd should record operations for the returned tensor. This option is only relevant for torch.
- Return type:
>>> import numpy as np >>> import metatensor >>> from metatensor import TensorBlock, TensorMap, Labels >>> np.random.seed(1)
First we create a
TensorMap
with just one block with two gradients, namedalpha
andbeta
, containing random data:>>> block = TensorBlock( ... values=np.random.rand(4, 3), ... samples=Labels.range("sample", 4), ... components=[], ... properties=Labels.range("property", 3), ... ) >>> block.add_gradient( ... parameter="alpha", ... gradient=TensorBlock( ... values=np.random.rand(2, 3, 3), ... samples=Labels(["sample", "atom"], np.array([[0, 0], [0, 2]])), ... components=[Labels.range("component", 3)], ... properties=block.properties, ... ), ... ) >>> block.add_gradient( ... parameter="beta", ... gradient=TensorBlock( ... values=np.random.rand(1, 3), ... samples=Labels(["sample"], np.array([[0]])), ... components=[], ... properties=block.properties, ... ), ... ) >>> keys = Labels(names=["key"], values=np.array([[0]])) >>> tensor = TensorMap(keys, [block]) >>> print(tensor.block(0)) TensorBlock samples (4): ['sample'] components (): [] properties (3): ['property'] gradients: ['alpha', 'beta']
Then we use
zeros_like
to create aTensorMap
with the same metadata astensor
, but with all values set to 0.>>> tensor_zeros = metatensor.zeros_like(tensor) >>> print(tensor_zeros.block(0)) TensorBlock samples (4): ['sample'] components (): [] properties (3): ['property'] gradients: ['alpha', 'beta'] >>> print(tensor_zeros.block(0).values) [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] >>> print(tensor_zeros.block(0).gradient("alpha").values) [[[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]] [[0. 0. 0.] [0. 0. 0.] [0. 0. 0.]]]
Note that if we copy just the gradient
alpha
,beta
is no longer available.>>> tensor_zeros = metatensor.zeros_like(tensor, gradients="alpha") >>> print(tensor_zeros.block(0).gradients_list()) ['alpha']
- metatensor.zeros_like_block(block: TensorBlock, gradients: List[str] | str | None = None, requires_grad: bool = False) TensorBlock [source]¶
Return a new
TensorBlock
with the same metadata as block, and all values equal to zero.- Parameters:
block (TensorBlock) – Input block from which the metadata is taken.
gradients (List[str] | str | None) – Which gradients should be present in the output. If this is
None
(default) all gradient ofblock
are present in the newTensorBlock
. If this is an empty list[]
, no gradients information is copied.requires_grad (bool) – If autograd should record operations for the returned tensor. This option is only relevant for torch.
- Return type: