0

I am looking for a way to select a region of a PyTorch tensor using a torch function (without using numpy). Do you have suggestions on how to proceed?

In other words, I'm looking for a way to crop a region of a matrix. Using numpy, it would be something like

import numpy as np
A = np.random.rand(16,16)
B = A[0:8, 0:8]

The approach I am trying is the following:

from torchvision import transforms
A = torch.randn([1,3,64,64])
B = torch.split(A, [16,32,16], dim =2)
C = torch.split(B, [16,32,16], dim =3)

Which gives the error

'tuple' object has no attribute 'split'

1 Answer 1

1

What's wrong with regular slicing?

import torch

A = torch.randn([1,3,64,64])
B = A[..., 16:32, 16:32]
Sign up to request clarification or add additional context in comments.

2 Comments

For some weird reason I have to avoid that... I ended up using torch.nn.functional.pad, like A = torch.nn.functional.pad(D, (0,-32,0,-32)).
@albus_c this is indeed a twisted workaround. If you can explain what prevents you from using regular indices, we might find a better/simpler solution. pytorch is quite flexible (I think as flexible as numpy in this case).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.