Lanka
Lanka

Reputation: 357

Pytorch tensor reshape 2d to 3d without loosing data

i need convert pytorch 2D tensor to 3D, suppose x is(torch.Size([16, 1024])) and need to convert torch.Size([16, 1024, 1024]) by copying reshaping tensor without loosing x data.

Upvotes: 1

Views: 3100

Answers (1)

jomavera
jomavera

Reputation: 171

One way would be by expanding dimension and repeating its elements

x_3D = x.unsqueeze(2)
x_3D = x_3D.repeat(1,1,1024).reshape(16,1024,1024)

checking if the elements are the same and at the same position

torch.all(x[0,:] == xt_expand[0,0,:])

Upvotes: 1

Related Questions