Can't understand about pytorch tensor broadcast
I have the following code:
import torch
d = 2
n = 50
X = torch.randn(n,d)
z = torch.tensor([[-1.0], [2.0]])
y = X @ z
X.size()
z.size()
y.size()

The output is:
torch.Size([50, 2])
torch.Size([2, 1])
torch.Size([50, 1])

My question is, why after broadcasting, the size of the result y is [50,1] rather than [50,2], I think it should be [50,2], am I correct?

The @ is not broadcasting but multiplication.

In python 3.5, the @ operator was introduced for matrix
multiplication, following PEP465. This is implemented e.g. in numpy
as the matmul operator.

So the size of y is fine.
multiply a matrix of size [50,2] with a vector of size [2,1] will output a vector of size [50,1].
An example showing it more clearly is:
import torch
xx = torch.ones(3, 2)
zz = torch.tensor([[-1.0], [2.0]])
yy = xx @ zz

print(xx)
print(zz)
print(yy)


# tensor([[1., 1.],
#         [1., 1.],
#         [1., 1.]])
# tensor([[-1.],
#         [ 2.]])
# tensor([[1.],
#         [1.],
#         [1.]])

As you can see the third output is indeed just the multiplication of the 2 tensors.
If you wish to do broadcasting I recommend that you will refer to https://medium.com/ai%C2%B3-theory-practice-business/understanding-broadcasting-in-pytorch-ca9e9533f05f
