fastdev.utils.tensor ==================== .. py:module:: fastdev.utils.tensor Module Contents --------------- .. py:function:: to_numpy(x: torch.Tensor, preserve_list: bool = ...) -> numpy.ndarray to_numpy(x: numpy.ndarray, preserve_list: bool = ...) -> numpy.ndarray to_numpy(x: numpy.typing.ArrayLike, preserve_list: bool = ...) -> numpy.ndarray to_numpy(x: None, preserve_list: bool = ...) -> None to_numpy(x: Dict[Any, Any], preserve_list: bool = ...) -> Dict[Any, numpy.ndarray] to_numpy(x: List[Any], preserve_list: Literal[True]) -> List[numpy.ndarray] to_numpy(x: List[Any], preserve_list: Literal[False] = ...) -> numpy.ndarray to_numpy(x: Tuple[Any, Ellipsis], preserve_list: Literal[True]) -> Tuple[numpy.ndarray, Ellipsis] to_numpy(x: Tuple[Any, Ellipsis], preserve_list: Literal[False] = ...) -> numpy.ndarray Convert input to numpy array. :param x: Input to be converted. :type x: Any :param preserve_list: Whether to preserve list or convert to numpy array. Defaults to True. :type preserve_list: bool, optional .. py:function:: to_torch(x: numpy.ndarray, preserve_list: bool = ...) -> torch.Tensor to_torch(x: torch.Tensor, preserve_list: bool = ...) -> torch.Tensor to_torch(x: None, preserve_list: bool = ...) -> None to_torch(x: Dict[Any, Any], preserve_list: bool = ...) -> Dict[Any, torch.Tensor] to_torch(x: List[Any], preserve_list: Literal[True]) -> List[torch.Tensor] to_torch(x: List[Any], preserve_list: Literal[False] = ...) -> torch.Tensor to_torch(x: Tuple[Any, Ellipsis], preserve_list: Literal[True]) -> Tuple[torch.Tensor, Ellipsis] to_torch(x: Tuple[Any, Ellipsis], preserve_list: Literal[False] = ...) -> torch.Tensor Convert input to torch tensor. :param x: Input to be converted. :type x: Any :param preserve_list: Whether to preserve list or convert to torch tensor. Defaults to True. :type preserve_list: bool, optional .. py:function:: to_number(x: None) -> None to_number(x: int) -> int to_number(x: float) -> float to_number(x: numpy.ndarray) -> Union[int, float] to_number(x: torch.Tensor) -> Union[int, float] Convert input to number. :param x: Input to be converted. :type x: Any .. py:function:: auto_cast(fn: Optional[Callable] = None, return_type: Literal['by_input', 'by_func', 'pt', 'np'] = 'by_input') -> Callable Automatically cast input and output of a function to numpy or torch tensors. Since the function simply converts the input and output to numpy or torch tensors, it may introduce overhead. It is recommended to use this function for functions that are not performance critical. :param fn: Function to be wrapped. :type fn: Callable :param return_type: Type of return value. Defaults to "by_input". - "by_input": Return type is determined by the input argument type, first found array/tensor type is used. - "by_func": Return type is determined by the orginal function. - "pt": Return type is torch.Tensor. - "np": Return type is np.ndarray. :type return_type: Literal["by_input", "by_func", "pt", "np"], optional :returns: Wrapped function. :rtype: Callable .. py:function:: atleast_nd(tensor: None, expected_ndim: int) -> None atleast_nd(tensor: numpy.ndarray, expected_ndim: int) -> numpy.ndarray atleast_nd(tensor: torch.Tensor, expected_ndim: int) -> torch.Tensor Convert input to at least nD tensor. .. note:: Differs from `np.atleast_nd` and `torch.atleast_nd`, this function can add dimensions to the front or back of the tensor.