Utils¶
These utils functions only support the following basic Python types: tuple, list and dict.

poutyne.
torch_to_numpy
(obj, copy=False)[source]¶ Convert to Numpy arrays all tensors inside a Python object composed of the supported types.
 Parameters
obj – The Python object to convert.
copy (bool) – Whether to copy the memory. By default, if a tensor is already on CPU, the Numpy array will be a view of the tensor.
 Returns
A new Python object with the same structure as obj but where the tensors are now Numpy arrays. Not supported type are left as reference in the new object.
Example
>>> from poutyne import torch_to_numpy >>> torch_to_numpy({ ... 'first': torch.tensor([1, 2, 3]), ... 'second':[torch.tensor([4,5,6]), torch.tensor([7,8,9])], ... 'third': 34 ... }) { 'first': array([1, 2, 3]), 'second': [array([4, 5, 6]), array([7, 8, 9])], 'third': 34 }
 See:
torch_apply()
for supported types.

poutyne.
numpy_to_torch
(obj)[source]¶ Convert to tensors all Numpy arrays inside a Python object composed of the supported types.
 Parameters
obj – The Python object to convert.
 Returns
A new Python object with the same structure as obj but where the Numpy arrays are now tensors. Not supported type are left as reference in the new object.
Example
>>> from poutyne import numpy_to_torch >>> numpy_to_torch({ ... 'first': np.array([1, 2, 3]), ... 'second':[np.array([4,5,6]), np.array([7,8,9])], ... 'third': 34 ... }) { 'first': tensor([1, 2, 3]), 'second': [tensor([4, 5, 6]), tensor([7, 8, 9])], 'third': 34 }

poutyne.
torch_apply
(obj, func)[source]¶ Apply a function to all tensors inside a Python object composed of the supported types.
Supported types are: list, tuple and dict.
 Parameters
obj – The Python object to convert.
func – The function to apply.
 Returns
A new Python object with the same structure as obj but where the tensors have been applied the function func. Not supported type are left as reference in the new object.