ding.torch_utils.reshape_helper¶
ding.torch_utils.reshape_helper
¶
fold_batch(x, nonbatch_ndims=1)
¶
Overview
:math:(T, B, X) \leftarrow (T*B, X) Fold the first (ndim - nonbatch_ndims) dimensions of a tensor as batch dimension. This operation is similar to torch.flatten but provides an inverse function
unfold_batch to restore the folded dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- x (
|
obj: |
required | |
- nonbatch_ndims (
|
obj: |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Size
|
|
Examples:
>>> x = torch.ones(10, 20, 5, 4, 8)
>>> x, batch_dim = fold_batch(x, 2)
>>> x.shape == (1000, 4, 8)
>>> batch_dim == (10, 20, 5)
unfold_batch(x, batch_dims)
¶
Overview
Unfold the batch dimension of a tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- x (
|
obj: |
required | |
- batch_dims (
|
obj: |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Examples:
>>> x = torch.ones(10, 20, 5, 4, 8)
>>> x, batch_dim = fold_batch(x, 2)
>>> x.shape == (1000, 4, 8)
>>> batch_dim == (10, 20, 5)
>>> x = unfold_batch(x, batch_dim)
>>> x.shape == (10, 20, 5, 4, 8)
unsqueeze_repeat(x, repeat_times, unsqueeze_dim=0)
¶
Overview
Squeeze the tensor on unsqueeze_dim and then repeat in this dimension for repeat_times times. This is useful for preproprocessing the input to an model ensemble.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
- x (
|
obj: |
required | |
- repeat_times (
|
obj: |
required | |
- unsqueeze_dim (
|
obj: |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
|
Examples:
>>> x = torch.ones(64, 6)
>>> x = unsqueeze_repeat(x, 4)
>>> x.shape == (4, 64, 6)
>>> x = torch.ones(64, 6)
>>> x = unsqueeze_repeat(x, 4, -1)
>>> x.shape == (64, 6, 4)
Full Source Code
../ding/torch_utils/reshape_helper.py