In order to add noise to the XNOR-Net, I need to modify the trained weights which contains only 1 and -1. So I think the problem is how to generate a tensor with random number of 1 and -1, and then multiply this tensor with the trained weights. The solution of mine is following:

```
def add_noise_to_weights(m):
s = m.data.size()
n = m.data.nelement()
r = round(n*0.01) #0.01 is the noise ratio
mask = -torch.ones([1, r]).type_as(m.data)
neg_mask = torch.ones([1, n-r]).type_as(m.data)
t = torch.cat((mask, neg_mask),1).reshape(s)
idx = torch.randperm(t.nelement())
t = t.view(-1)[idx].view(t.size())
m.data = m.data.mul(t)
return m
```

Despite it works, the code is too complicated, so could you guys have some simply solutions?