MCPcopy Create free account
hub / github.com/apple/ml-pointersect / tensorfun

Function tensorfun

cdslib/core/utils/tensor_utils.py:10–42  ·  view source on GitHub ↗

Apply `func(tensor)` to any tensors in the list or dict. Args: func: function that takes a tensor and outputs a tensor arr: list of tensor, dict of tensor Returns: list or dict containing the output tensors

(
    func: T.Callable[[torch.Tensor], torch.Tensor],
    arr: T.Union[torch.Tensor, T.List[torch.Tensor], T.Dict[T.Any, torch.Tensor]],
)

Source from the content-addressed store, hash-verified

8
9
10def tensorfun(
11 func: T.Callable[[torch.Tensor], torch.Tensor],
12 arr: T.Union[torch.Tensor, T.List[torch.Tensor], T.Dict[T.Any, torch.Tensor]],
13) -> T.Union[torch.Tensor, T.List[torch.Tensor], T.Dict[T.Any, torch.Tensor], None]:
14 """
15 Apply `func(tensor)` to any tensors in the list or dict.
16
17 Args:
18 func:
19 function that takes a tensor and outputs a tensor
20 arr:
21 list of tensor, dict of tensor
22
23 Returns:
24 list or dict containing the output tensors
25 """
26
27 if arr is None:
28 return None
29
30 if isinstance(arr, torch.Tensor):
31 return func(arr)
32
33 if isinstance(arr, (list, tuple)):
34 return [tensorfun(func, a) for a in arr]
35
36 if isinstance(arr, dict):
37 out_dict = dict()
38 for key in arr:
39 out_dict[key] = tensorfun(func, arr[key])
40 return out_dict
41
42 raise RuntimeError(f'Not supported type: {type(arr)}, {arr}')
43
44
45def random_crop(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected