I want to add random gaussian noise to my network weights, for every forward pass. When backpropagating, I want to calculate gradients in respect to distorted weights, then update the original weights using those gradients. Am I doing it right in the example below?
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.linear = nn.Linear(784, 10)
self.gaussian = Normal(loc=0, scale=torch.ones_like(self.linear.weight))
def forward(self, x):
orig_weight = self.linear.weight.clone()
noise = self.gaussian.sample()
self.linear.weight.data = self.linear.weight.data + noise
x = self.linear(x)
self.linear.weight.data = orig_weight.data
return x
model = Net()
optimizer = torch.optim.SGD(model.parameters(), lr=0.001)
model.train()
for epoch in range(10):
for i in range(100):
output = model(input)
loss = nn.CrossEntropyLoss()(output, label)
optimizer.zero_grad()
loss.backward()
optimizer.step()