News for hackers. Stuff that matters.
Re:The Illustrated Stable Diffusion by Waterluvian on Tuesday October 04, 2022 @02:56PM (#33085021)
Closer. But I still get lost when words like “tensor” are used. “structured lists of numbers” really doesn’t seem to explain it usefully.

This reminds me that explaining seemingly complex things in simple terms is one of the most valuable and rarest skills in engineering. Most people just can’t. And often because they no-longer remember what’s not general knowledge. You end up with a recursive Feynmannian “now explain what that means” situation.

This is probably why I admire a whole bunch of engineering YouTubers and other engineering “PR” people for their brilliance at making complex stuff seem very very simple.

Re:The Illustrated Stable Diffusion by netruk44 on Tuesday October 04, 2022 @03:14PM (#33085291)
If it helps you to understand at all, assuming you have a CS background, any time you see the word "tensor" you can replace it with "array" and you'll be 95% of the way to understanding it. Or "matrix" if you have a mathematical background.

Whereas CS arrays tend to be 1 dimensional, and sometimes 2 dimensional, tensors can be as many dimensions as you need. A 256x256 photo with RGB channels would be stored as a [256 x 256 x 3] tensor/array. If you want to store a bunch of them? Add a dimension to store each image. Want rows and columns of images? Make the dimensions [width x height x channels x rows x columns].

Re:The Illustrated Stable Diffusion by minimaxir on Tuesday October 04, 2022 @03:23PM (#33085415)
A more practical example of the added dimensionality of tensors is the addition of a batch dimension, so a 8 image batch per training step would be a (8, 256, 256, 3) tensor.

Tools such as PyTorch's DataLoader can efficiently collate multiple inputs into a batch.

Re:The Illustrated Stable Diffusion by pmoriarty on Wednesday October 05, 2022 @10:31AM (#33096045)
"Whereas CS arrays tend to be 1 dimensional, and sometimes 2 dimensional, tensors can be as many dimensions as you need."

You can have arrays of as many dimensions as you need in many (most?) programming languages.

Is there some other difference between tensors and arrays?

Or is it just the math term for multidimensional array?

Re:The Illustrated Stable Diffusion by netruk44 on Wednesday October 05, 2022 @02:43PM (#33099609)
I didn't mean to imply you couldn't have an n-dimensional array in many programming languages. Just that it's usually not what's done when designing programs. I think many would prefer to make some kind of object to encapsulate the data type (an RGB struct for my example) instead of making arrays of arrays of arrays of (double/float/uint8).

For the literal definition between a mathematical 'tensor' and a (e.g.) PyTorch 'tensor' (which, from reading the other comments here, are not the same thing), I leave that to other commenters. I'm just someone learning PyTorch and sharing my knowledge.

The practical engineering difference between a PyTorch tensor and Python array is that PyTorch comes with tons of math & utility functions that support n-dimensions and are differentiable, whereas the builtin Python arrays and math utilities don't do much, if any, of that.

Re:The Illustrated Stable Diffusion by nh23423fefe on Wednesday October 05, 2022 @01:31PM (#33098580)
Essentially the same difference between vectors and arrays. A vector is an element of a vector space. A vector can be represented as a coordinate array in a basis, but it is not the coord array. Consider the vector 1 + x + x^2 you might use the canonical basis for polynomials {1, x, xx, xxx, ...} and then the coordinate vector is [1 1 1]. The coordinate and the basis represent the polynomial. But the polynomial isn't a list of numbers.

A matrix is the coefficient list of a linear transformation, but it is not the transformation. The derivative operator is a linear transformation. It's representation as a matrix is just coordinates. But the operator is clearly more than that.

A tensor can be written down and you can compute with the components, but the tensor itself is the basis independent transformation. Not a list of components.

Re:The Illustrated Stable Diffusion by Waterluvian on Tuesday October 04, 2022 @04:09PM (#33086005)
This helps. Thank you. Any advice on where to look to understand why the word tensor was used?
Re:The Illustrated Stable Diffusion by pvarangot on Tuesday October 04, 2022 @04:56PM (#33086723)
A Tensor is a mathematical object for symbolic manipulation of relationships between other objects that belong in conceptually similar universes or spaces. Literature on Deep Learning, like Goodfellow, call for the CS-minded reader to just assume it's a fancy word for a matrix of more than two dimensions. That makes matters confusing because mathematically you could have scalar or vectorial tensors. The classic mathematical definition puts more restrictions on the "shape" of the "matrix" by requiring certain properties so that it can create a symbolic language for a tensor calculus. Then you can study how the relationships change as variables on the universes or spaces change.

Understanding the inertia tensor on classical mechanics or the stress tensor may illustrate where tensors come from, and I understand that GR also makes use of a lot of tensor calculus that came to be as mathematics developed manipulating and talking about tensors. I have a kinda firm grasp on some very rudimentary tensor calculus from trying to leanr GR, and a pretty solid grasp on classical mechanics. I've had hour long conversations with deep learning popes and thought leaders in varying states of mind and after that my understanding is that they use the word tensor in an overreaching fashion as like you could call a solar panel a nuclear fission reactor power source. This thought leaders include people with books and 1M+ view Youtube videos on the subject that use the word tensor and I'm not saying their names because they off-the-record admitted that it's a poor choice of term but it harms the ego of many Google engineers to publicly admit that.

Re:The Illustrated Stable Diffusion by amelius on Tuesday October 04, 2022 @06:46PM (#33088167)
How are sum-types handled in deep learning?

E.g., a type that holds "a 3x2 tensor OR a 4x6x2x5 tensor".

Re:The Illustrated Stable Diffusion by nicd on Tuesday October 04, 2022 @07:52PM (#33088888)
I'm not aware of tensor shape type safety (in general) being widely used in deep learning, let alone sum-types. I believe Pytorch and TensorFlow lack support for tensor shape type checking (looks like there is a Pytorch issue open: https://github.com/pytorch/pytorch/issues/26889).
Re:The Illustrated Stable Diffusion by ntr-- on Tuesday October 04, 2022 @11:02PM (#33090601)
https://docs.rs/ndarray/0.15.6/ndarray/

Rust to the rescue, as it always is for safety. (I don't know if this directly translates for tensors, I used it for manipulating point clouds and iirc it allows for arbitrary dimension containers)

Re:The Illustrated Stable Diffusion by survirtual on Wednesday October 05, 2022 @04:50PM (#33101102)
I use this crate extensively and while I have gotten good results, it is not ergonomic and it is not equivalent to numpy ndarray. There are a lot of issues and work still to be done.

Just a word of caution for anyone interested in using it.

Things like windowing, slicing, views are all “supported” but have many limitations and are non intuitive. In particular, I needed an “axis shift” fn and I ended up spending hours scouring docs just to find a way to shift along an axis. When I mean by shift is a roll in numpy, ala: https://github.com/rust-ndarray/ndarray/issues/281

Answer: you can’t with this crate. I implemented a dynamic n-dim solution myself but it uses views of integer indices that get copied to a new array, which have indexes to another flattened array in order to avoid duplication of possibly massive amounts of n-dimensional data; using the crate alone, copying all the array data would be unavoidable.

Ultimately I’ve had to make my own axis shifting and windowing mechanisms. But the crate is still a useful lib and continuing effort.

While I don’t mind getting into the weeds, these kinds of side efforts can really impact context focus so it’s just something to be aware of.

Re:The Illustrated Stable Diffusion by gpderetta on Wednesday October 05, 2022 @04:12AM (#33092679)
How does this help with typechecking tensor types in python?
Re:The Illustrated Stable Diffusion by mxkopy on Wednesday October 05, 2022 @03:10AM (#33092228)
In my experience (CV) you typically want 4 dims - (height, width, channels, batch). In Julia you can do element-wise operations on two arrays if their lengths (i.e. size after flattening) are the same. Obviously dimension-wise that sort of thing won't work (what's the dot product of a 3-vector by a 6-vector? Undefined!)

But really, the whitepaper should include this sort of detail, and you should be following the white paper.

Re:The Illustrated Stable Diffusion by jayalammar on Tuesday October 04, 2022 @04:49PM (#33086607)
I updated the post to say "multi-dimensional array".

In a context like this, we use tensor because it allows for any number of dimensions (while vector/ array is only one, matrix is two). When you get into ML libraries, both popular packages PyTorch and TensorFlow use the "tensor" terminology.

It's a good point. Hope it's clearer for devs with "array" terminology.

Re:The Illustrated Stable Diffusion by zestyping on Tuesday October 04, 2022 @06:34PM (#33088050)
> we use tensor because it allows for any number of dimensions

"Vector" implies one dimension and "matrix" strongly implies two. But an array can have any number of dimensions, so "array" is the best word.

We don't need the word "tensor"; when the context is programming, "tensor" is only confusing and doesn't really add any useful meaning.

Re:The Illustrated Stable Diffusion by OkayPhysicist on Tuesday October 04, 2022 @07:15PM (#33088469)
Tensor does imply a set of operations that are expected. Multiplying two arrays together is an ambiguous operation; multiplying two tensors together is well-defined.

And really, the context is math, not programming. The programming side of DL is approximately trivial, the interesting bits are all represented as mathematical operations on a set of tensors.

Re:The Illustrated Stable Diffusion by pmoriarty on Wednesday October 05, 2022 @10:38AM (#33096133)
"Tensor does imply a set of operations that are expected. Multiplying two arrays together is an ambiguous operation; multiplying two tensors together is well-defined."

In programming too, multiplying two arrays of arbitrary types is often undefined, and many languages allow their programmers to specify what they want to happen when arrays of specific types are multiplied (or may have built-in defined behavior for certain specific types of arrays).

I'd love to learn if there are any actual differences between tensors and arrays commonly used in programming, but so far it doesn't sound like it from reading through this HN thread.

Re:The Illustrated Stable Diffusion by OkayPhysicist on Wednesday October 05, 2022 @06:42PM (#33102144)
I think you misunderstood what I meant. If I say "multiply this 3x3 matrix by this 3x1 vector", then I know know to expect the particular form of multiplication that multiplies each column of the matrix by it's associated value in the vector, then adds them up to produce a 3x1 vector. Likewise, multiplying a 3x3x3 tensor by a 3x1x1 tensor will produce a 3x3x1 result.

If I say "multiply this 3x3 array by this 3x3 array", then it's an ambiguous operation. There is not a canonical multiplication for arrays. One ecosystem might treat it as a matrix-matrix multiplication, another might do an element-wise multiplication, others might just throw an error. None is more correct than another, because array just implies the structure of the data.

Re:The Illustrated Stable Diffusion by kgwgk on Wednesday October 05, 2022 @03:17AM (#33092279)
Interestingly the Wikipedia page

Tensor (disambiguation)

https://en.wikipedia.org/wiki/Tensor_(disambiguation)

has an entry

Tensor type (computing)

which is just a redirection to

Array (data type)

https://en.wikipedia.org/wiki/Array_(data_type)

Doesn’t seem to support the relevance of “tensor” as something distinct from “array”.

Re:The Illustrated Stable Diffusion by leereeves on Tuesday October 04, 2022 @09:01PM (#33089604)
It's especially confusing as math, because the standard meaning of "tensor" in math includes a lot of baggage that's not used in deep learning (related to physics and differential geometry).
Re:The Illustrated Stable Diffusion by zestyping on Wednesday October 05, 2022 @05:05PM (#33101239)
Exactly.
Re:The Illustrated Stable Diffusion by FeepingCreature on Wednesday October 05, 2022 @12:46AM (#33091364)
"Array" implies one dimension to me. Maybe n-array?
Re:The Illustrated Stable Diffusion by kgwgk on Wednesday October 05, 2022 @02:43AM (#33092082)
Array may imply 1d to you, or 2d to others, but it is more general that that: https://en.wikipedia.org/wiki/Array_(data_type)
Re:The Illustrated Stable Diffusion by auggierose on Wednesday October 05, 2022 @03:57AM (#33092574)
I think tensor is much shorter than "multi-dimensional array", and it is OK to use it. You could just mention that in the context of DL tensor is just a shorter way to say "multi-dimensional array", and everyone should be happy. On top of that, if they read another text about DL that uses the word tensor, there is no need to be scared anymore.
Re:The Illustrated Stable Diffusion by kgwgk on Wednesday October 05, 2022 @02:55AM (#33092152)
It’s a cool name from the good ol’ “multi-dimensional arrays with a uniform type”

https://www.tensorflow.org/guide/tensor

Re:The Illustrated Stable Diffusion by sva_ on Tuesday October 04, 2022 @04:44PM (#33086529)
It should be noted that it doesn't have all to much to do with the rigorous mathematical definition of a Tensor.
Re:The Illustrated Stable Diffusion by jamessb on Tuesday October 04, 2022 @05:18PM (#33087064)
Very loosely, a number/vector/matrix/tensor can be considered to be objects where specifying the values of 0/1/2/3 indexes will give a number.

(A mathematician might object to this on several grounds, such as that vectors/matrices/tensors are geometric objects which need not be expressed numerically as coordinates in any coordinate system)

Re:The Illustrated Stable Diffusion by amelius on Tuesday October 04, 2022 @06:43PM (#33088135)
A tensor can take any number of dimensions (not just 3).
Re:The Illustrated Stable Diffusion by avereveard on Tuesday October 04, 2022 @04:35PM (#33086416)
To reduce it a little, matrix holds numbers, tensor holds whatever. Numbers. Vectors. Operations.
Re:The Illustrated Stable Diffusion by yreg on Tuesday October 04, 2022 @06:30PM (#33087998)
So it is math lingo for array.
Re:The Illustrated Stable Diffusion by squeaky-clean on Wednesday October 05, 2022 @04:53AM (#33092948)
In math tensor has a more specific meaning than just an array, but in machine learning lingo, tensor just means an array or multidimensional array. That base is true in math as well, but it can also be a single value (rank zero tensor, aka a scalar. Sometimes ML will call this a tensor as well) and has to obey certain rules about linearity for some operations applied to it.
Re:The Illustrated Stable Diffusion by brundolf on Thursday October 06, 2022 @11:00AM (#33109078)
This is something that infuriates me about formal mathematics (and CS, for that matter): they don't just use the words people already know! They have to make up new words, sometimes even entirely new symbols, that often just mean the same thing as something else. And then the literature becomes entirely inaccessible unless you've learned this secret code. The bottleneck becomes translation, instead of understanding.
Re:The Illustrated Stable Diffusion by Breza on Friday October 14, 2022 @02:08PM (#33206864)
I agree with you. Fields like graph theory and set theory are fun once you wrap your head around the symbols, but I remember having a lot of trouble with both as a teenager.
Re:The Illustrated Stable Diffusion by beernet on Wednesday October 05, 2022 @04:22AM (#33092733)
I think this post explains tensors fairly well: https://www.kdnuggets.com/2018/05/wtf-tensor.html

Quote: "A tensor is a container which can house data in N dimensions. Often and erroneously used interchangeably with the matrix (which is specifically a 2-dimensional tensor), tensors are generalizations of matrices to N-dimensional space."

Re:The Illustrated Stable Diffusion by pdntspa on Tuesday October 04, 2022 @08:43PM (#33089414)
I got lost at the word 'tensor' too, but then I just googled it and skilled up...

But simply put, a 'tensor' is a 3+-dimensional array of floats. Or a stack of matrices.

Re:The Illustrated Stable Diffusion by TigeriusKirk on Tuesday October 04, 2022 @05:40PM (#33087397)
One of my favorite moments in Geoffrey Hinton's otherwise pretty info-dense Coursera neural network class was when he said-

"To deal with a 14-dimensional space, visualize a 3-D space and say 'fourteen' to yourself very loudly. Everyone does it."

Re:The Illustrated Stable Diffusion by TuringTest on Wednesday October 05, 2022 @04:26AM (#33092757)
Kudos to this. It also helps to think of a three-dimensional space bounded by the sides of a box, and to think of another 11 boxes stacked on top of each other.

Then, you can visualize orthogonal on dimensions higher than 3 by throwing wires between equivalent points in two boxes.

Re:The Illustrated Stable Diffusion by pmoriarty on Wednesday October 05, 2022 @11:06AM (#33096504)
"It also helps to think of a three-dimensional space bounded by the sides of a box, and to think of another 11 boxes stacked on top of each other."

Of course the problem with that is that you're still thinking three-dimensionally because "on top of" is a description of a 2D or 3D relationship.

Re:The Illustrated Stable Diffusion by TuringTest on Wednesday October 05, 2022 @11:11AM (#33096570)
Yes, but by boxing each sub-space, you can think of the pile of boxes as a dimension separate from the Z axis.

It's like painting a house in isometric style. You draw each floor above the one below it, and put a "perpendicular" slanted axis to represent the third dimension in 2D.

You could put a 'slanted' fourth axis in a 3D origin of coordinates, but I find it easier to think of the lower corner of each box as the origin of coordinates displaced along that fourth axis.

Re:The Illustrated Stable Diffusion by pmoriarty on Wednesday October 05, 2022 @11:43AM (#33097026)
"You could put a 'slanted' fourth axis in a 3D origin of coordinates"

Even with that the same problem occurs: "slanted" is a 3D relationship, so at best it's an analogy.

It's like "translating" the color red to some shade of gray to a person who can only see in black and white.

Or trying to describe through written words what music is like to someone who can't hear.

These are all analogies which might allow us to reason and get interesting/useful results when we use such analogies... but we should not under the illusion that we really know what those things for which we have no senses are like.

Also, there are probably all sorts of interesting/useful connections, relationships or conclusions that a being who really could sense 4D objects would find obvious or easy to make that we may never arrive at because our way of thinking of them is so limited in comparison.

That's not to mention trying to think about even higher dimensions or various other mathematical constructions that have no obvious translation to things in our ordinary experience we can relate to.

Re:The Illustrated Stable Diffusion by TuringTest on Friday October 07, 2022 @04:50AM (#33118853)
> Even with that the same problem occurs: "slanted" is a 3D relationship, so at best it's an analogy.

Yeah, but it's the same analogy you use to represent 3D figures on a 2D plane, where slanted is a 2D relationship; and we are very well versed on it and know how it works as a projection of a 3D reality.

> These are all analogies which might allow us to reason and get interesting/useful results when we use such analogies... but we should not under the illusion that we really know what those things for which we have no senses are like.

Is this some reflections about Plato's cave? You don't need much insight to understand the basis of moving over an extra dimension - if you can understand what it's like to move over parallel planes from a 2D view, you can understand the same by moving over superposed volumes from a 3D view. A hyper-sphere can be visualized as a linear collection of increasingly small spheres glued together, in the same way that you can view a sphere as a linear collection of increasingly small circles over an axis orthogonal to its center.

> Also, there are probably all sorts of interesting/useful connections, relationships or conclusions that a being who really could sense 4D objects would find obvious or easy to make that we may never arrive at because our way of thinking of them is so limited in comparison.

Yeah, and some people are really bad at 3D visualization so they have no hope to arrive at conclusions that are trivial to an architect or engineer. That doesn't prevent higher-dimension visualization techniques from being useful for the insights that it can provide, even if they can't give omniscience. For all the other insights, we have formal methods and logic reasoning, which is how 4D space was originated to begin with.

Re:The Illustrated Stable Diffusion by dr_dshiv on Wednesday October 05, 2022 @09:33AM (#33095355)
Or imagine a spreadsheet with 14 columns, amiright?
Re:The Illustrated Stable Diffusion by TuringTest on Wednesday October 05, 2022 @09:42AM (#33095470)
Technically true, but you're leaving out any possible visual insights if you do that.
Re:The Illustrated Stable Diffusion by dr_dshiv on Friday October 07, 2022 @01:13AM (#33117676)
I don’t know. I feel that, for most people, it is the most visual way to see in many dimensions. That is, it’s an insight—rather than having to imagine a 5-d cube or whatever, it’s just a spreadsheet, no big deal.
Re:The Illustrated Stable Diffusion by TuringTest on Friday October 07, 2022 @05:01AM (#33118937)
Yes, but they see it as a data dump, without perceiving any spacial connections between the vectors represented by each row. The point of spatial reasoning is using our minds inner eye to visualize those connections intuitively, and the tabular data dump has nothing of it.
Re:The Illustrated Stable Diffusion by dr_dshiv on Friday October 07, 2022 @02:45PM (#33125354)
But I’d argue a 5d cube has even less use, as a mental model. Most people don’t know that each additional column is another dimension. People can imagine the sense of distance between varying points/entries in a table easier than a 5d cube, for instance. (Actually, that’s an empirical question)
Re:The Illustrated Stable Diffusion by Waterluvian on Tuesday October 04, 2022 @06:13PM (#33087804)
I should have added that the images/figures really help. I think I’m about there.
Re:The Illustrated Stable Diffusion by 6gvONxR4sf7o on Tuesday October 04, 2022 @04:23PM (#33086232)
You’re talking like using jargon makes something a bad explanation, but maybe you just aren’t the audience? Why not use words like that if it’s a super basic concept to your intended audience?
Re:The Illustrated Stable Diffusion by Waterluvian on Tuesday October 04, 2022 @05:48PM (#33087498)
I saw the scientific term, “Text Understander” and wrongly thought I was the audience.