2
$\begingroup$

I keep hearing people say something like:

lets say you have a 1 dimensional vector of a person that just has his age. Then you add another dimension which is his height, so you have a 2 dimensional vector

But I also heard things like

A 2d vector is a rank 2 tensor

Can someone clarify which is the correct one?

Is it

[1, 1]

or

[[1,1],
 [1,1]]
$\endgroup$

3 Answers 3

1
$\begingroup$

"Tensor" is a very general term, meaning N-dimensional array. A tensor can be 0-dimensional (a scalar), 1-dimensional (a vector), 2-dimensional (a matrix), and so on... Tensor is a very encompassing term, meaning all of the things listed above. Sometimes, people use "tensor" to refer to high dimensional objects, as explained here:

While, technically, all of the above constructs are valid tensors, colloquially when we speak of tensors we are generally speaking of the generalization of the concept of a matrix to N ≥ 3 dimensions. We would, then, normally refer only to tensors of 3 dimensions or more as tensors, in order to avoid confusion [...]

But please keep in mind that this is just a practice, and N-dimensional array is technically a tensor.

Coming to your question, if a tensor is more like:

[1, 1]

or

[[1,1],
 [1,1]]

First, they look like Python list and list of lists, respectively. Python lists are not mathematical objects, strictly speaking. Provided you imported numpy, it would be more correct to describe them as:

numpy.array([ 1, 1 ])

and

numpy.array([[ 1, 1 ],
             [ 1, 1 ]])

Now these objects have the mathematical properties of vectors-matrices-tensors. I think they could both be thought as tensors of different kinds and ranks. The first is of rank 1, the other of rank 2.


Other useful resources on tensors are this question, and this explanation.

$\endgroup$
6
  • $\begingroup$ Thanks for the effort and reply. It seems maybe I wasn't clear in the question. As I do understand about what tensors ranks mean, I wasn't sure if a 2d vector meant a 2x2 matrix or a vector with 2 elements. $\endgroup$
    – katiex7
    Commented Jan 8, 2020 at 8:56
  • 2
    $\begingroup$ 2D vector = 2D matrix = rank 2 tensor. 2x2 is a size, not a rank. 2-dimensional and 2x2 are different concepts. Sorry for not being clear about this. $\endgroup$
    – Leevo
    Commented Jan 8, 2020 at 8:59
  • $\begingroup$ Ah sorry! I knew that! 2x2 or 2x3 are still rank 2 tensors. Okay, so when someone says to me, i have 3 dimensions in my feature vector because we have age, height, and name. Is he wrong for saying that? $\endgroup$
    – katiex7
    Commented Jan 8, 2020 at 9:05
  • 1
    $\begingroup$ A vector with three variables: age, height, name is a 3D vector, i.e. a tensor of rank 3. Or a three-dimensional matrix, they are equivalent expressions. $\endgroup$
    – Leevo
    Commented Jan 8, 2020 at 10:38
  • $\begingroup$ And what is the shape of that 3d vector? If it was 3x1x1 or 1x1x3 that will make sense, but usually we store it as shape of 3 $\endgroup$
    – katiex7
    Commented Jan 8, 2020 at 17:41
1
$\begingroup$

@Leevo has already explained it well. Alternatively, just ask Pytorch:

import torch
x = torch.tensor([1, 1])
y = torch.tensor([[ 1, 1 ], [ 1, 1 ]])
print("x dimension: {}".format(x.dim()))
print("y dimension: {}".format(y.dim()))

gives:

x dimension: 1
y dimension: 2

So your first vector/tensor with age and height of one single(!) person has only one dimension and your second vector/tensor has two dimensions.

$\endgroup$
1
  • $\begingroup$ So this is the way I thought too, however please read the latest comment on that answer $\endgroup$
    – katiex7
    Commented Jan 8, 2020 at 17:42
1
$\begingroup$

In Python, you can think of vector dimensionality as list-of-lists-of-lists-of....

So,

  1. list = 1D
  2. list-of-lists = 2D
  3. list-of-lists-of-list = 3D
  4. and so on...
[1,1] # 1D

[[1,1],
 [1,1]] # 2D

[[[1,1],
  [1,1]],
 [[1,1],
  [1,1]]] # 3D

Then you mentioned:

Then you add another dimension which is his height, so you have a 2 dimensional vector.

I think this can be interpreted in two ways:

[<age>] # 1D of size 1x1


# You can add height and make it 2D
[[<age>],
 [<height>]] # 2D of size 2x1

# or, add height and keep it 1D
[<age>, <height>] # 1D of size 1x2
$\endgroup$
2
  • $\begingroup$ Ah yes, this is the way I thought as well. However if you wanted to say we have four dimensions since we have age height weight and id, then itll literally be a 1x1x1x4 or 4x1x1x1 tensor which seems unneccesary. $\endgroup$
    – katiex7
    Commented Jan 8, 2020 at 17:45
  • $\begingroup$ I guess then you can either have a 4x1 or a 1x4. You don't need to keep nesting like you did. More generally you could have 4xn or nx4 where n=number of samples. Then you are always at 2D. $\endgroup$ Commented Jan 9, 2020 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.