What exactly is a tensor?

I am new to machine learning, and I am taking an online class to develop an AI. I would like to understand how it all works, rather than just copying the code so I can implement my own AIs in the future, but my first question is what exactly is a tensor? From what I can find online, it is basically a multi-dimensional array.

For instance, a tensor with size [2, 2, 2] means that there is there is a 3-d array with size 2x2x2. If I were to extend that to [2, 2, 2, 2], each dimension of the 3-d object would have an additional dimension. Is that correct? So a [2, 2, 2, 2] array would hold 16 objects (2x2x2x2 = 16)?

If that is the case, what is the benefit of using a tensor object versus simply a numpy array? Can a tensor hold additional data or are there things that can be done on a tensor that cannot be done in a numpy array?

Thanks to all who aid me in the beginning of my adventure into machine learning!

2 Likes

A tensor of size (2, 2, 2, 2) is four-dimensional. Yes, it can hold 2 * 2 * 2 * 2 = 16 elements.

A tensor is a generic n-dimensional array, very similar to numpy arrays. The nice thing about using pytorch tensors in general is that you can easily put them on the gpu (as opposed to numpy arrays, which only exist on the cpu).

3 Likes

Awesome, thank you so much for the quick response! A question related to size then:

torch.unsqueeze(tensor, 0) adds a 1 dimensional object to the beginning of the tensor?

So tensor_test = (2, 2, 2)
tensor_test.unsqueeze(0) then makes tensor_test = (1, 2, 2, 2)?

Yes, torch.unsqueeze(tensor, 0) adds a dimension to the beginning of the tensor.

Dear Richard sir,
can we use tensors for extracting visual features as i have training and testing data in tensors now i want to extract the visual features from them so please is it possible and how can i do it?

Am working on movielens dataset of 10M and i have its data testing training in tenors form (numpy ) file can we extract visual features from the tensors.

Could you give an example of a visual feature, please?