4

I have a simple question regarding the shapes of 2 different tensors - tensor_1 and tensor_2.

  1. tensor_1.shape outputs torch.Size([784, 1]);
  2. tensor_2.shape outputs torch.Size([784]).

I understand that the first one is rank-2 tensor, whereas the second is rank-1. What's hard for me is to conceptualize the difference between shape [784, 1] and [784].

Is it correct to think that tensor_1 has 784 rows and 1 column with a scalar inside each place? If so, why can't we call it simply a column vector (which is, in fact, rank-1 tensor), which also has values displayed vertically?

Similarly, can the shape of the second tensor ([784]) be imagined as 784 values inside a horizontal vector?

2
  • 2
    You’re a bit too hopeful about mapping Pytorch concepts onto mathematical concepts. There isn’t a distinction in Pytorch between row vectors and column vectors, for instance. There’s just rank-1 vectors. Commented Feb 17, 2021 at 18:32
  • @AryaMcCarthy, thank you! I needed this clarification. :) Commented Feb 17, 2021 at 18:42

1 Answer 1

3

You cant call tensor_1 as column vector because of its dimension . indexing that particular tensor is done in 2D
eg . tensor_1[1,1]

Coming to tensor_2 , its a scalar tensor having only one dimension.
And of course you can make it have a shape of tensor_1, just do

tensor_2 = tensor_2.unsqueeze(1)   #This method will make tensor_2 have a shape of tensor_1
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Prajot. It makes sense. Maybe this is a dumb question, but what is there a specific reason behind having a tensor_1 of shape [784, 1] instead of just [784] (same as tensor_2)? We can easily reshape [784, 1] into [784] by tensor_1.view(784) and reduce the dimensionality.
there are many reason for having shape of [784, 1] instaed of [784]. One reason would be batches.when you have single value inputs, & you want to send it through a batches,and your loss function expects your value in 2D shape rather than 1D shape.

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.