Pad a tensor along a given dimension. Parameters ---------- data : Tensor The input tensor. dim : int The dimension to pad. pad_len : float The padding length. value : int, optional The value to pad with. Returns ------- Tensor
(data: Tensor, dim: int, pad_len: float, value: float = 0)
| 4 | |
| 5 | |
| 6 | def pad_dim(data: Tensor, dim: int, pad_len: float, value: float = 0) -> Tensor: |
| 7 | """Pad a tensor along a given dimension. |
| 8 | |
| 9 | Parameters |
| 10 | ---------- |
| 11 | data : Tensor |
| 12 | The input tensor. |
| 13 | dim : int |
| 14 | The dimension to pad. |
| 15 | pad_len : float |
| 16 | The padding length. |
| 17 | value : int, optional |
| 18 | The value to pad with. |
| 19 | |
| 20 | Returns |
| 21 | ------- |
| 22 | Tensor |
| 23 | The padded tensor. |
| 24 | |
| 25 | """ |
| 26 | if pad_len == 0: |
| 27 | return data |
| 28 | |
| 29 | total_dims = len(data.shape) |
| 30 | padding = [0] * (2 * (total_dims - dim)) |
| 31 | padding[2 * (total_dims - 1 - dim) + 1] = pad_len |
| 32 | return pad(data, tuple(padding), value=value) |
| 33 | |
| 34 | |
| 35 | def pad_to_max(data: list[Tensor], value: float = 0) -> tuple[Tensor, Tensor]: |
no outgoing calls
no test coverage detected