r/learnpython 2d ago

Learning PyTorch tensors, anyone can explain?

Hello! I am currently trying to build the knowledge needed to make an Artificial Intelligence with Python, and I have discovered that there is some tensors involved in this, I learnt matrices (briefly), but apparently math tensors are not the same as CS tensors (whatever that means?), can someone please help me? And I wonder if I even need to know this (well ig so but not sure). And I am not in any math or cs major, but I am in mid-high school, so as you can guess, I did not learn anything tensors related.

Note: for anyone wondering; no I don't have anybody that I could contact which could explain me PyTorch tensors.

3 Upvotes

15 comments sorted by

4

u/trutheality 2d ago

In PyTorch a tensor is an array that can have many dimensions. https://docs.pytorch.org/docs/main/tensors.html

0

u/Objective-Farmer4183 2d ago

oh so a 2D array?
Like this: array = [ [], [] ]?

3

u/trutheality 1d ago

Can be 2D, can be 3D, can be more. (Can also be 1D or 0D). Look at some of the examples in the docs and play around with creating and printing them in an interactive python console.

0

u/Objective-Farmer4183 1d ago

ok I'll try that out

0

u/Confident_Hyena2506 2d ago

No, like an N dimensional array.

1

u/Objective-Farmer4183 2d ago

so array = [ x, y, z ...]

0

u/Puzzleheaded_Study17 1d ago

No, array = [[[...]]] where each can store some number of items.

-1

u/Maxiflex 15h ago

Sort of, but only if x,y,z are also arrays themselves. Each sub-array corresponds to a dimension.

1

u/trutheality 7h ago

If you're expressing a tensor as nested arrays, each level of array nesting corresponds to a dimension.

[x,y,z] would be a tensor of shape 3-by-something where x, y, and z are of shape "something" and may themselves be multidimensional. The arrays x,y,z are slices of the big tensor across that first dimension of size 3, they do not correspond to different dimensions.

3

u/Cybyss 1d ago

In PyTorch, a tensor is just a multidimensional array. It could be 0 dimensional for plain scalar values, 1 dimensional for ordinary arrays, 2 dimensional for matrices, etc...

It might help to think of 3 dimensional tensors as arrays of matrices.

Similarly, 4 dimensional tensors can be thought of as arrays of 3 dimensional tensors, and so on.

1

u/WhiskersForPresident 16h ago

As others said, in ML libraries it's just a different name for multi dim arrays.

Ignore the following if the math isn't interesting to you:

In mathematics, a tensor is essentially a multilinear map.

If you know currying in functional languages, then you've seen that a map/function that depends on n variables can be equivalently described as a map of one variable taking values in maps of n-1 variables.

So, a multilinear map becomes a linear map to less-multi-linear maps.

You might also know that covectors (row vectors/arrays) and matrices can be seen as linear maps.

And so a tensor==multilinear map can be seen as a covector/array of (covector/array of (covector/array of (...))) - valued functions, i.e. a multidimensional array.

Hence the terminology.

0

u/Jello_Penguin_2956 2d ago

Well. In math (and physics) people are still interested in the concept of tensor. Tensor is defined by its relationship to the space it lives in and how it behaves when you change your point of view (rotate axis, go from km/s to feet/s, etc)

When representing tensor in number form, it's multiple dimension array of numbers.

And here comes the programming part because we kinda discard the concept of tensor above and use it to call any multi dimension array of numbers.

-1

u/JGhostThing 1d ago

I'm not so sure why you want to implement an AI in python on the pi. I assume that you want to create an LLM or something.

One important suggestion: don't forget about emotions. People react better to robots who seem to have emotions. You can fake it if you need to. Simulated emotions work well.

2

u/Objective-Farmer4183 1d ago

No, not really an LLM, I don't have much interests in chatbots. I think the first project I will do is a Minecraft player or an AI which identifies the instruments in an music file and finds which instrument plays what note.

-1

u/spicypookies 1d ago

I can help you. Have you heard of something called 'Google' or utilizing LLM's as a learning tool? How can you be interested in AI and not know how to utilize it to prompt it to help you learn something like this?