Jun Lee
Jun Lee

Reputation: 45

torch tensor reshape from torch.Size([1, 16384, 3]) to torch.Size([1, 128, 128, 3])

I have a torch tensor shaped, torch.Size([1, 16384, 3]) and I want it to be torch.Size([1, 128, 128, 3]).

How can I do that?

Upvotes: 0

Views: 1136

Answers (2)

SarthakJain
SarthakJain

Reputation: 1686

You can use .view method, but make sure you don't truncate or extropolate dimension as you will get a Runtime Error.

I suggest:


# Your tensor
a = torch.ones((1, 16384, 3))
a = a.view((1, 128, 128, 3))

# Test if it works
print(a.size())
>> torch.Size([1, 128, 128, 3])

Optional: Also you can used up to 1 inferred size value like this

a = torch.ones((1, 16384, 3))
# Place -1 wherever you want an inferred value but make sure only one -1. 
a = a.view(1, -1, 128, 3))

# or 

a = a.view(1, 128, 128, -1))

Sarthak Jain

Upvotes: 1

ke qi
ke qi

Reputation: 44

I am a new fish, and maybe you can choose "view()" method in Pytorch. Like:

variable.view(128,128)

then done.

Upvotes: 0

Related Questions