You would have to initialize W as an nn.Parameter or set it requires_grad attribute to True in the creation.
Also don’t wrap outputs in new tensors, as this will detach the computation graph.
This code snippet should work:
x=[torch.tensor([0,1]),torch.tensor([[2,3],[0,1]]),torch.tensor([4,5])]
W=torch.tensor([0.1,0.2,0.3,0.4,0.5,0.6], requires_grad=True)
optimizer = torch.optim.Adam([W], lr=0.001) #, weight_decay=5e-4)
y=torch.tensor([0.,1.,1.])
for epoch in range(3):
    pred= torch.stack([torch.sigmoid(torch.sum(W[i])) for i in x])
    loss=torch.nn.BCELoss()(pred,y)
    loss.backward()
    optimizer.step()
    print(W)