torch.narrow¶
- torch.narrow(input, dim, start, length) Tensor ¶
返回一個新張量,它是
input
張量的變窄版本。維度dim
是從start
輸入到start + length
。返回的張量和input
張量共享相同的底層儲存空間。- 參數
範例
>>> x = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) >>> torch.narrow(x, 0, 0, 2) tensor([[ 1, 2, 3], [ 4, 5, 6]]) >>> torch.narrow(x, 1, 1, 2) tensor([[ 2, 3], [ 5, 6], [ 8, 9]]) >>> torch.narrow(x, -1, torch.tensor(-1), 1) tensor([[3], [6], [9]])