What does the & operator do in PyTorch and why does it change the shape?
I have code that contains x and y, both of the type torch.autograd.variable.Variable. Their shape is

torch.Size([30, 1, 9])
torch.Size([1, 9, 9])


What I don't understand is, why the following results in a different size/shape

z = x &amp; y
print(z.shape)


which outputs

torch.Size([30, 9, 9])


Why is the shape of z 30*9*9, after x &amp; y? The shape of x is 30*1*9, and the shape of y is 1*9*9, what does &amp; do in x &amp; y?

This has nothing to do with the &amp; operator, but with how broadcasting works in Python. To quote Eric Wieser's excellent documentation on broadcasting in NumPy:


  In order to broadcast, the size of the trailing axes for both arrays in an operation must either be the same size or one of them must be one. 


See the following image from the quoted page as an example:



This translates to your problem as follows:


a has the shape 30 x 1 x 9
b has the shape  1 x 9 x 9


Therefore the result is created like this:


result1 is a1, because a1 &gt; b1
result2 is b2, because a2 &lt; b2
result3 is both a3 and b3, because a3 = b3


Therefore result has the shape 30 x 9 x 9.

Please also note that the &amp; operator implements logical conjunction on a binary encoding of the tensors' items.
