mlverse / torch

R Interface to Torch
https://torch.mlverse.org
Other
490 stars 64 forks source link

A possible mistake in document "GET TECHNICAL" on the broadcasting #1148

Open tabe opened 5 months ago

tabe commented 5 months ago

I appreciate your "GET TECHNICAL" article in torch.mlverse.org for its clarity and efficiency to explain the training process of neural networks from scratch, which is very helpful. Meanwhile, in its Tensor section the example of broadcasting reads:

We align array shapes, starting from the right.

Say we have two tensors, one of size 8x1x6x1, the other of size 7x1x5.

Here they are, right-aligned:

# t1, shape:     8  1  6  1
# t2, shape:        7  1  5

Starting to look from the right, the sizes along aligned axes either have to match exactly, or one of them has to be equal to 1: in which case the latter is broadcast to the larger one.

In the above example, this is the case for the second-from-last dimension. This now gives

# t1, shape:     8  1  6  1
# t2, shape:        7  6  5

But the broadcasting should start from the rightmost dimension, so

# t1, shape:     8  1  6  5
# t2, shape:        7  1  5

and then

# t1, shape:     8  1  6  5
# t2, shape:        7  6  5

In fact, in R

x <- torch_randn(c(8,1,6,1))
y <- torch_randn(c(7,1,5))
z <- x + y
z$size()

returns

[1] 8 7 6 5