random_like#
- metatensor.random_uniform_like(tensor: TensorMap, gradients: List[str] | str | None = None, requires_grad: bool = False) TensorMap[source]#
- Return a new - TensorMapwith the same metadata as tensor, and all values randomly sampled from the uniform distribution between 0 and 1.- 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 of- tensorare present in the new- TensorMap. 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 - TensorMapwith just one block with two gradients, named- alphaand- beta, 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 - random_uniform_liketo create a- TensorMapwith the same metadata as- tensor, but with all values randomly sampled from a uniform distribution.- >>> tensor_random = metatensor.random_uniform_like(tensor) >>> print(tensor_random.block(0)) TensorBlock samples (4): ['sample'] components (): [] properties (3): ['property'] gradients: ['alpha', 'beta'] >>> print(tensor_random.block(0).values) [[0.53316528 0.69187711 0.31551563] [0.68650093 0.83462567 0.01828828] [0.75014431 0.98886109 0.74816565] [0.28044399 0.78927933 0.10322601]] >>> print(tensor_random.block(0).gradient("alpha").values) [[[0.44789353 0.9085955 0.29361415] [0.28777534 0.13002857 0.01936696] [0.67883553 0.21162812 0.26554666]] [[0.49157316 0.05336255 0.57411761] [0.14672857 0.58930554 0.69975836] [0.10233443 0.41405599 0.69440016]]] - Note that if we copy just the gradient - alpha,- betais no longer available.- >>> tensor_random = metatensor.random_uniform_like(tensor, gradients="alpha") >>> print(tensor_random.block(0).gradients_list()) ['alpha'] 
- metatensor.random_uniform_like_block(block: TensorBlock, gradients: List[str] | str | None = None, requires_grad: bool = False) TensorBlock[source]#
- Return a new - TensorBlockwith the same metadata as block, and all values randomly sampled from the uniform distribution between 0 and 1.- 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 of- blockare present in the new- TensorBlock. 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: