Expand documentation on broadcasting#2084
Conversation
0af086f to
69e4e7f
Compare
69e4e7f to
cf513f9
Compare
jessegrabowski
left a comment
There was a problem hiding this comment.
looks nice, i nitpicked it a bit
| shape), resulting in ``(1, 2)``, and then broadcast just like the | ||
| row in the figure. | ||
|
|
||
| Unlike NumPy, which does broadcasting dynamically, PyTensor needs |
There was a problem hiding this comment.
is this accurate? We have basically the same broadcasting rules as numpy, but we're more flexible because we don't require you to tell us the shapes.
There was a problem hiding this comment.
This is accurate we do strictly less broadcasting than Numpy in the sense numpy will never fail with zeros(3, 1) + zeros(3, 3), whereas we will absolutely refuse if we didn't know that 1 was going to be a 1 in advance.
| shape), resulting in ``(1, 2)``, and then broadcast just like the | ||
| row in the figure. | ||
|
|
||
| Unlike NumPy, which does broadcasting dynamically, PyTensor needs |
There was a problem hiding this comment.
Is this the right framing? We're the same as numpy, just more flexible because sometimes we don't know the shape of things, so there's a requirements to be more like numpy to get broadcasting to work
There was a problem hiding this comment.
We are less flexible. Numpy is eager, it always knows everything it needs. No symbolic/deferred stuff
| >>> x_matrix = pt.matrix("x_matrix") | ||
| >>> x_matrix.type.shape | ||
| (None, None) | ||
| >>> y_col = pt.col("y_col") |
There was a problem hiding this comment.
consider pt.tensor('y_col', shape=(None, 1)) to make it super explicit? That's all col is, i wouldn't want the example to give the impression this is the way you hanve to do it
There was a problem hiding this comment.
I show that below. Here I wanted to just show broadcast working. pt.col is not bad per se. I agree we never promote it. In this case it makes the document a bit easier to follow. Otherwise the solution to the gotcha below is just the working case above. Let me thing
|
|
||
| >>> x_matrix = pt.matrix("x_matrix") | ||
| >>> y_matrix = pt.matrix("y_matrix") | ||
| >>> y_col = pt.specify_shape(y_matrix, (None, 1)) |
There was a problem hiding this comment.
illustrate the intermediate result case you mentioned as motivation
There was a problem hiding this comment.
WDYM? I don't want to complicate the example with an arbitrary Op that happens to lose static shape just to make the point. If that's what you had in mind....
| >>> y_vector = pt.vector("y_vector") | ||
| >>> out = x_matrix + pt.expand_dims(y_vector, 1) # or x_matrix + y_vector[:, None] |
There was a problem hiding this comment.
Where is the "drop the axis" part here?
There was a problem hiding this comment.
we went for a vector instead of a matrix
There was a problem hiding this comment.
It's a drop before you even start. I can try to get a better title
It would be good to have a place to link to for runtime broadcasting gotcha. Not sure this is the right place (tutorial?), but it's what showed up if I googled pytensor + broadcasting
Preview page: https://pytensor--2084.org.readthedocs.build/en/2084/tutorial/broadcasting.html