utils.torch_utils

Utility functions for manipulation and introspection of ``torch.tensor``s.

borch.utils.torch_utils.detach_copy_tensors(tensors)
Returns a new list where .detach().clone() have been called on all

the elements of the list.

Parameters

tensors – List where the elements are torch.tensor’s

Returns

list

Example

>>> import torch
>>> x = torch.randn(10, requires_grad = True)
>>> x.requires_grad
True
>>> new_x = new_tensor_list = detach_copy_tensors([x])[0]
>>> new_x.requires_grad
False
borch.utils.torch_utils.detach_tensor_dict(tensor_dict)

Returns a new dictionary where .detach() have been called on all the value elements of the dict.

Parameters

tensor_dict – Dictionary where the values are torch.tensor’s

Returns

Dictionary

Example

>>> import torch
>>> tensor_dict = {str(ii): torch.randn(1, requires_grad=True)
...     for ii in range(3)}
>>> new_tensor_dict = detach_tensor_dict(tensor_dict)
>>> tensor_dict['1'].requires_grad
True
>>> new_tensor_dict['1'].requires_grad
False
borch.utils.torch_utils.dict_values_to_tensor(dictionary)

Converts all values in the dictionary that is an instance of numbers.Number to a tensor. If some of the values are allready a torch.tensor then all of the new tensors will be moved to the same device. In the case there are several torch.tensors on different devices, then it will be placed on a random device.

Parameters

dictionary (dict) – a dictionary with values to convert to tensors

Returns

dictionary where all values that inherited from numbers.Number are converted to torch.tensors.

Examples

>>> import torch
>>> temp = {'a': 1., 'b': torch.tensor(1.), 'c': 2}
>>> dict_values_to_tensor(temp)
{'a': tensor(1.), 'b': tensor(1.), 'c': tensor(2.)}
borch.utils.torch_utils.get_device()

Get current device, cuda if available else CPU

borch.utils.torch_utils.grads_to_none(params)

Set the gardients to None for all params :param params: iterable with torch.Tensors :type params: iterable

Notes

Warning, this is an in place operation

borch.utils.torch_utils.hessian(output, inputs, out=None, allow_unused=False, create_graph=False)

Compute the Hessian of output with respect to inputs hessian((x * y).sum(), [x, y])

Parameters
  • output – the output to calcualte the hessian against

  • inputs – the tensors to calculate the hessian with respect to

  • allow_unused (bool) – allow input tensors not to be used in the grpah

  • create_graph (bool) – create a graph that can be used in the auto diff

Example

>>> x = torch.tensor([1.5, 2.5], requires_grad=True)
>>> h = hessian(x.pow(2).prod(), x, create_graph=True)
borch.utils.torch_utils.is_numeric(val)

Check if it a numeric value

borch.utils.torch_utils.is_optimizable_leaf_tensor(tensor)

Checks if the tensor is a leaf node and requires grad :param tensor: the tensor that ig going to be checked :type tensor: torch.tensor

Returns

Boolean, True if the tensor is a leaf node and requires grad

Examples

>>> var = torch.randn(3, requires_grad=True)
>>> is_optimizable_leaf_tensor(var)
True
borch.utils.torch_utils.jacobian(outputs, inputs, create_graph=False)

Compute the Jacobian of outputs with respect to inputs jacobian(x, x) jacobian(x * y, [x, y]) jacobian([x * y, x.sqrt()], [x, y])

Parameters
  • output – the output to calcualte the jacobainagainst

  • inputs – the tensors to calculate the jacobian with respect to

  • allow_unused (bool) – allow input tensors not to be used in the grpah

  • create_graph (bool) – create a graph that can be used in the auto diff

Example

>>> x = torch.randn(2,2, requires_grad=True)
>>> y = torch.tensor([5.5, -4.], requires_grad=True)
>>> j = jacobian(x.pow(y), [x, y])
borch.utils.torch_utils.one_hot(labels: torch.LongTensor, n_classes: int) → torch.Tensor

Given a tensor of class encodings ..math::X \in \{0, ..., C-1\}^N where ..math::C is the number of classes, and ..math::N is the number of datapoints, convert it to a ‘one-hot’ tensor ..math::Y \in \{0, 1\}^{N \times C} where ..math::Y_{i,j} = \delta_{X_i,j-1}.

Parameters
  • labels – A tensor of class encodings.

  • n_classes – Total number of classes encoded (e.g. 10 for MNIST).

Returns

A one-hot encoded tensor of shape [len(labels), n_classes].

Example

>>> a = torch.LongTensor([0, 1, 1, 3])
>>> one_hot(a, n_classes=5)
tensor([[1., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0.],
        [0., 1., 0., 0., 0.],
        [0., 0., 0., 1., 0.]])
borch.utils.torch_utils.seed(seed)

Seed the random number generator.

Parameters

seed (int) – Seed number to use.

borch.utils.torch_utils.update_tensor_data(tensor_list, data_list)

Updates the data of the tensor’s in the tensor list with values of the data_list. Where element one of the tensor_list is updated with elemnt one of the data_list etc.

Parameters
  • tensor_list – List with torch.tenor’s

  • data_list – List with torch.tenor’s

Returns

None

Examples

>>> import torch
>>> tensor_list = [torch.randn(1, requires_grad=True)]
>>> data_list = [torch.tensor([1.2])]
>>> update_tensor_data(tensor_list, data_list)
>>> tensor_list[0].data
tensor([1.2000])
>>> data_list[0]
tensor([1.2000])