Conversion: numpy#

This module contains safe functions for numpy and pytorch interconversion.

tad_mctc.convert.numpy.numpy_to_tensor(x, device=None, dtype=None)[source]#

Convert a numpy array to a PyTorch tensor.

Parameters:
  • x (NDArray[Any]) – Array to convert.

  • device (torch.device | None, optional) – Device to store the tensor on. Defaults to None.

  • dtype (torch.dtype | None, optional) – Data type of the tensor. Defaults to None.

Returns:

Converted PyTorch tensor.

Return type:

Tensor

tad_mctc.convert.numpy.tensor_to_numpy(x, dtype=None)[source]#

Convert a PyTorch tensor to a numpy array.

Parameters:
  • x (Tensor) – Tensor to convert.

  • dtype (np.dtype, optional) – Data type of the array. Defaults to np.dtype(np.float64).

Returns:

Converted numpy array.

Return type:

np.ndarray