Hi I am new to pytorch , I just wanted to convert 1 dimensional tensor to 2 dimensional tensor but when I use tensor.view() it my code still throws a dimensionality problem.please help
You can’t change the number of elements in the tensor, so you’re likely calculating the dimensions wrong. If you have a 100 element tensor, you can’t view it to x.view(20,10) because that would require you to have 20*10=200 elements. Try just taking a 100 element tensor and running x.view(1,100,1) and you’ll see how you can arbitrarily add dimensions.
If it’s a tensor and not a variable you can also use unsqueeze to add a dummy dimension
I have done like this before:
x_train = torch.linspace(-1, 1, 101) # 1D tensor
x_train = x_train.view(101, 1) # convert to 2D tensor
# torch.Size([101, 1])