torchoutil.nn.functional.transform module¶
- as_tensor(
- data: Sequence[Never],
- dtype: Literal[None] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[Never]],
- dtype: Literal[None] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[Sequence[Never]]],
- dtype: Literal[None] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: bool,
- dtype: Literal[None, 'bool'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[bool],
- dtype: Literal[None, 'bool'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[bool]],
- dtype: Literal[None, 'bool'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[Sequence[bool]]],
- dtype: Literal[None, 'bool'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: int,
- dtype: Literal[None, 'int64', 'long'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[int],
- dtype: Literal[None, 'int64', 'long'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[int]],
- dtype: Literal[None, 'int64', 'long'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[Sequence[int]]],
- dtype: Literal[None, 'int64', 'long'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: float,
- dtype: Literal[None, 'float32', 'float'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[float],
- dtype: Literal[None, 'float32', 'float'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[float]],
- dtype: Literal[None, 'float32', 'float'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[Sequence[float]]],
- dtype: Literal[None, 'float32', 'float'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: complex,
- dtype: Literal[None, 'complex64', 'cfloat'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[complex],
- dtype: Literal[None, 'complex64', 'cfloat'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[complex]],
- dtype: Literal[None, 'complex64', 'cfloat'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[Sequence[complex]]],
- dtype: Literal[None, 'complex64', 'cfloat'] = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: bool | int | float | complex,
- dtype: dtype | None | Literal['default'] | str | DTypeEnum = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[bool | int | float | complex],
- dtype: dtype | None | Literal['default'] | str | DTypeEnum = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[bool | int | float | complex]],
- dtype: dtype | None | Literal['default'] | str | DTypeEnum = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Sequence[Sequence[Sequence[bool | int | float | complex]]],
- dtype: dtype | None | Literal['default'] | str | DTypeEnum = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
- as_tensor(
- data: Any,
- dtype: dtype | None | Literal['default'] | str | DTypeEnum = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
Convert arbitrary data to tensor.
Unlike torch.as_tensor, it works recursively and stack sequences like List[Tensor]. It also accept python generator objects.
- Args:
data: Data to convert to tensor. Can be Tensor, np.ndarray, list, tuple or any number-like object. dtype: Target torch dtype. defaults to None. device: Target torch device. defaults to None.
- Returns:
PyTorch tensor created from data.
- flatten( ) Tensor1D[source]¶
- flatten( ) ndarray
- flatten( ) List[T_BuiltinScalar]
- flatten( ) List[T_BuiltinScalar]
- move_to_rec( ) Dict[T, U][source]¶
- move_to_rec( ) T
Move all modules and tensors recursively to a specific dtype or device.
- pad_and_crop_dim(
- x: Tensor,
- target_length: int,
- *,
- align: Literal['left', 'right', 'center', 'random'] = 'left',
- pad_value: int | float | bool | Callable[[Tensor], int | float | bool] = 0.0,
- dim: int = -1,
- mode: Literal['constant', 'reflect', 'replicate', 'circular'] = 'constant',
- generator: Generator | None | Literal['default'] | int = None,
Pad and crop along the specified dimension.
- repeat_interleave_nd( ) Tensor[source]¶
Generalized version of torch.repeat_interleave for N >= 1 dimensions. The output size will be (…, D*repeats, …), where D is the size of the dimension of the dim argument.
- Args:
x: Any tensor of shape (…, D, …) with at least 1 dim. repeats: Number of repeats. dim: The dimension to repeat. defaults to 0.
Examples::¶
>>> x = torch.as_tensor([[0, 1, 2, 3], [4, 5, 6, 7]]) >>> repeat_interleave_nd(x, n=2, dim=0) tensor([[0, 1, 2, 3], [0, 1, 2, 3], [4, 5, 6, 7], [4, 5, 6, 7]])
- resample_nearest_freqs(x: ~torch.Tensor, orig_freq: int, new_freq: int, *, dims: int | ~typing.Iterable[int] = -1, round_fn: ~typing.Callable[[~torch.Tensor], ~torch.Tensor] = <built-in method floor of type object>) Tensor[source]¶
Nearest neigbour resampling using tensor slices.
- Args:
x: Input tensor. orig_freq: Source sampling rate. new_freq: Target sampling rate. dims: Dimensions to apply resampling. defaults to -1. round_fn: Rounding function to compute sub-indices. defaults to torch.floor.
- resample_nearest_rates(x: ~torch.Tensor, rates: float | ~typing.Iterable[float], *, dims: int | ~typing.Iterable[int] = -1, round_fn: ~typing.Callable[[~torch.Tensor], ~torch.Tensor] = <built-in method floor of type object>) Tensor[source]¶
Nearest neigbour resampling using tensor slices.
- Args:
x: Input tensor. rate: The reduction factor of each axis, e.g. a factor of 0.5 will divide the input axes by 2. dims: Dimensions to apply resampling. defaults to -1. round_fn: Rounding function to compute sub-indices. defaults to torch.floor.
- resample_nearest_steps(x: ~torch.Tensor, steps: float | ~typing.Iterable[float], *, dims: int | ~typing.Iterable[int] = -1, round_fn: ~typing.Callable[[~torch.Tensor], ~torch.Tensor] = <built-in method floor of type object>) Tensor[source]¶
Nearest neigbour resampling using tensor slices.
- Args:
x: Input tensor. steps: Floating step for resampling each value. dims: Dimensions to apply resampling. defaults to -1. round_fn: Rounding function to compute sub-indices. defaults to torch.floor.
- shuffled(
- x: Tensor,
- dims: int | Iterable[int] = -1,
- generator: Generator | None | Literal['default'] | int = None,
Returns a shuffled version of the input tensor along specific dimension(s).
- squeeze(
- x: T_TensorOrArray,
- dim: None | int | Iterable[int] = None,
- mode: Literal['view_if_possible', 'view', 'copy', 'inplace'] = 'view_if_possible',
- to_item(
- x: T_BuiltinScalar,
- to_item(
- x: Tensor | ndarray | SupportsIterLen,
Convert scalar value to the closest built-in type.
- to_tensor(
- data: Any,
- dtype: dtype | None | Literal['default'] | str | DTypeEnum = None,
- device: device | None | Literal['default', 'cuda_if_available'] | str | int = None,
Convert arbitrary data to tensor.
Unlike torch.as_tensor, it works recursively and stack sequences like List[Tensor]. It also accept python generator objects.
- Args:
data: Data to convert to tensor. Can be Tensor, np.ndarray, list, tuple or any number-like object. dtype: Target torch dtype. defaults to None. device: Target torch device. defaults to None.
- Returns:
PyTorch tensor created from data.
- top_k(
- x: T_Tensor,
- k: int,
- dim: int = -1,
- largest: bool = True,
- sorted: bool = True,
- *,
- return_values: bool = True,
- return_indices: bool = True,
- top_p(
- x: Tensor,
- p: float,
- dim: int = -1,
- largest: bool = True,
- *,
- return_values: Literal[True] = True,
- return_indices: Literal[True] = True,
- top_p(
- x: T_Tensor,
- p: float,
- dim: int = -1,
- largest: bool = True,
- *,
- return_values: Literal[True] = True,
- return_indices: Literal[False],
- top_p(
- x: Tensor,
- p: float,
- dim: int = -1,
- largest: bool = True,
- *,
- return_values: Literal[False],
- return_indices: Literal[True] = True,
- top_p(
- x: T_Tensor,
- p: float,
- dim: int = -1,
- largest: bool = True,
- *,
- return_values: bool = True,
- return_indices: bool = True,
- topk(
- x: Tensor,
- k: int,
- dim: int = -1,
- largest: bool = True,
- sorted: bool = True,
- *,
- return_values: Literal[True] = True,
- return_indices: Literal[True] = True,
- topk(
- x: T_Tensor,
- k: int,
- dim: int = -1,
- largest: bool = True,
- sorted: bool = True,
- *,
- return_values: Literal[True] = True,
- return_indices: Literal[False],
- topk(
- x: Tensor,
- k: int,
- dim: int = -1,
- largest: bool = True,
- sorted: bool = True,
- *,
- return_values: Literal[False],
- return_indices: Literal[True] = True,
- topk(
- x: T_Tensor,
- k: int,
- dim: int = -1,
- largest: bool = True,
- sorted: bool = True,
- *,
- return_values: bool = True,
- return_indices: bool = True,
- transform_drop(
- transform: Callable[[T], T],
- x: T,
- p: float,
- *,
- generator: Generator | None | Literal['default'] | int = None,
Apply a transform on a tensor with a probability of p.
- Args:
transform: Transform to apply. x: Argument of the transform. p: Probability p to apply the transform. Cannot be negative.
If > 1, it will apply the transform floor(p) times and apply a last time with a probability of p - floor(p).
- unsqueeze(
- x: T_TensorOrArray,
- dim: int | Iterable[int],
- mode: Literal['view_if_possible', 'view', 'copy', 'inplace'] = 'view_if_possible',