espnet2.torch_utils.recursive_op.recursive_divide
Less than 1 minute
espnet2.torch_utils.recursive_op.recursive_divide
espnet2.torch_utils.recursive_op.recursive_divide(a, b: Tensor)
Divides elements of a given object by a specified tensor recursively.
This function supports various types of input structures including tuples, lists, dictionaries, and PyTorch tensors. The function will recursively divide each element of the input a by the tensor b. It raises a ValueError if the shapes of a and b do not match.
espnet2.torch_utils.recursive_op.None
- Parameters:
- a (Union *[*torch.Tensor , list , tuple , dict , None ]) – The object to be divided.
- b (torch.Tensor) – The tensor by which to divide each element of a.
- Returns: A new object with each element divided by b. The structure of the returned object matches that of the input a.
- Return type: Union[torch.Tensor, list, tuple, dict, None]
- Raises:
- ValueError – If the types of a are unsupported or if the sizes of
- a –
Examples
>>> import torch
>>> a = torch.tensor([4.0, 8.0, 16.0])
>>> b = torch.tensor([2.0, 4.0, 8.0])
>>> result = recursive_divide(a, b)
>>> print(result)
tensor([2.0, 2.0, 2.0])
>>> a_list = [torch.tensor([4.0]), torch.tensor([8.0])]
>>> b_tensor = torch.tensor([2.0])
>>> result_list = recursive_divide(a_list, b_tensor)
>>> print(result_list)
[tensor([2.0]), tensor([4.0])]
>>> a_dict = {'x': torch.tensor([4.0]), 'y': torch.tensor([8.0])}
>>> result_dict = recursive_divide(a_dict, b_tensor)
>>> print(result_dict)
{'x': tensor([2.0]), 'y': tensor([4.0])}
>>> result_none = recursive_divide(None, b_tensor)
>>> print(result_none)
None