Ok I written too fast, it’s expand and not expand_as.
I tested this on my machin and it run without any bug:
 x = torch.floor(torch.rand(3,7,5)*30).long()
 w = 1-2*torch.rand(30,4,1,1,1)
 idx = torch.cat([x.unsqueeze(0)]*4,0).unsqueeze(0)
 x_emb = w.expand(30,4,3,7,5).gather(0,idx).squeeze()

With the bug you have, it looks you tried with batch_size = 1, while you did not change the batch value in the gather line