ptyrad.io.adapter

ptyrad.io.adapter#

Translates between torch.tensor and np.array

Functions

ndarrays_to_tensors(data[, device])

Recursively convert all numpy.ndarray instances in any nested structure (including lists, dicts, and tuples) into torch.Tensor on the specified device.

tensors_to_ndarrays(data)

Recursively convert all torch.Tensor instances in any nested structure (including lists, dicts, and tuples) into numpy.ndarray.

ptyrad.io.adapter.tensors_to_ndarrays(data)[source]#

Recursively convert all torch.Tensor instances in any nested structure (including lists, dicts, and tuples) into numpy.ndarray.

This function supports both single objects and arbitrarily nested container types. Non-tensor types are returned unchanged.

Parameters:

data – Input data which can be nested dict, list, tuple, torch.Tensor, or other.

Returns:

The same structure with torch.Tensor replaced by numpy.ndarray.

ptyrad.io.adapter.ndarrays_to_tensors(data, device='cuda')[source]#

Recursively convert all numpy.ndarray instances in any nested structure (including lists, dicts, and tuples) into torch.Tensor on the specified device.

This function supports both single objects and arbitrarily nested container types. Non-array types are returned unchanged.

Parameters:
  • data – Input data which can be nested dict, list, tuple, np.ndarray, or other.

  • device (str) – Device on which to place the tensors. Default is ‘cuda’.

Returns:

The same structure with numpy.ndarray replaced by torch.Tensor.