to#

metatensor.to(tensor: TensorMap, backend: str | None = None, dtype: dtype | None = None, device: device | str | None = None, requires_grad: bool | None = None) TensorMap[source]#

Converts a TensorMap to a different backend. Currently only supports converting to and from numpy- or torch-based tensors.

Parameters:
  • tensor (TensorMap) – input TensorMap.

  • backend (str | None) – str indicating the backend to convert to. Currently only supports "numpy" or "torch". If not provided, the backend of the input tensor will be used.

  • dtype (dtype | None) – the dtype of the data in the resulting TensorMap. This is passed directly to numpy/torch, so can be specified as a variety of objects, such as (but not limited to) numpy.dtype, torch.dtype, str, or type.

  • device (device | str | None) – only applicable if backend is "torch". The device on which the torch.Tensor objects of the resulting TensorMap should be stored. Can be specified as a variety of objects such as (but not limited to) torch.device or str.

  • requires_grad (bool | None) – only applicable if backend is "torch". A bool indicating whether or not to use torch’s autograd to record operations on this block’s data. If not specified (i.e. requires_grad=None), in the case that the input tensor is already torch-based, the value of requires_grad will be preserved at its current setting. In the case that tensor is numpy-based, upon conversion to a torch tensor, torch will by default set requires_grad to False.

Returns:

a TensorMap converted to the specified backend, data type, and/or device.

Return type:

TensorMap

metatensor.block_to(block: TensorBlock, backend: str | None = None, dtype: dtype | None = None, device: device | str | None = None, requires_grad: bool | None = None) TensorBlock[source]#

Converts a TensorBlock to a different backend. Currently only supports converting to and from numpy- or torch-based tensors.

Parameters:
  • block (TensorBlock) – input TensorBlock.

  • backend (str | None) – str, the backend to convert to. Currently only supports "numpy" or "torch". If not specified, the backend is set to match the current backend of the input block.

  • dtype (dtype | None) – the dtype of the data in the resulting TensorBlock. This is passed directly to numpy/torch, so can be specified as a variety of objects, such as (but not limited to) numpy.dtype, torch.dtype, str, or type.

  • device (device | str | None) – only applicable if backend is "torch". The device on which the torch.Tensor of the resulting TensorBlock should be stored. Can be specified as a variety of objects such as (but not limited to) torch.device or str.

  • requires_grad (bool | None) – only applicable if backend is "torch". A bool indicating whether or not to use torch’s autograd to record operations on this block’s data. If not specified (i.e. None), in the case that the input block is already torch-based, the value of requires_grad will be preserved. In the case that block is numpy-based, upon conversion to a torch tensor, torch will by default set requires_grad to False.

Returns:

a TensorBlock converted to the specified backend, data type, and/or device.

Return type:

TensorBlock