I have 2 tensors:
outputs: torch.Size([4, 27, 161]) pred: torch.Size([4, 30, 161])
I want to cut pred
(from the end) so that it'll have the same dimensions as outputs
.
What's the best way to do it with PyTorch?
Slicing a 3D Tensor Slicing: Slicing means selecting the elements present in the tensor by using “:” slice operator. We can slice the elements by using the index of that particular element. Parameters: tensor_position_start: Specifies the Tensor to start iterating.
squeeze(input). It squeezes (removes) the size 1 and returns a tensor with all other dimensions of the input tensor. Compute torch. unsqueeze(input, dim). It inserts a new dimension of size 1 at the given dim and returns the tensor.
Constant padding is implemented for arbitrary dimensions. Replicate and reflection padding are implemented for padding the last 3 dimensions of a 4D or 5D input tensor, the last 2 dimensions of a 3D or 4D input tensor, or the last dimension of a 2D or 3D input tensor.
You can use Narrow
e.g:
a = torch.randn(4,30,161)
a.size() # torch.Size([4, 30, 161])
a.narrow(1,0,27).size() # torch.Size([4, 27, 161])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With