randomal
randomal

Reputation: 6612

Pytorch - select region of a tensor using torch function

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'

Upvotes: 0

Views: 1015

Answers (1)

Shai
Shai

Reputation: 114866

What's wrong with regular slicing?

import torch

A = torch.randn([1,3,64,64])
B = A[..., 16:32, 16:32]

Upvotes: 1

Related Questions