SHARE
TWEET

Untitled

a guest Dec 9th, 2018 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Net(nn.Module):
  2.     def __init__(self):
  3.         super(Net, self).__init__()
  4.  
  5.         self.act_max = nn.Parameter(torch.Tensor([0]), requires_grad=True)
  6.  
  7.         self.conv1 = nn.Conv2d(3, 32, kernel_size=5)
  8.         self.conv2 = nn.Conv2d(32, 64, kernel_size=5)
  9.         self.pool = nn.MaxPool2d(2, 2)
  10.         self.relu = nn.ReLU()
  11.         self.linear = nn.Linear(64 * 5 * 5, 10)
  12.  
  13.     def forward(self, input):
  14.         conv1 = self.conv1(input)
  15.         pool1 = self.pool(conv1)
  16.         relu1 = self.relu(pool1)
  17.  
  18.         relu1[relu1 > self.act_max] = self.act_max
  19.  
  20.         conv2 = self.conv2(relu1)
  21.         pool2 = self.pool(conv2)
  22.         relu2 = self.relu(pool2)
  23.         relu2 = relu2.view(relu2.size(0), -1)
  24.         linear = self.linear(relu2)
  25.         return linear
  26.  
  27.  
  28. model = Net()
  29. torch.nn.init.kaiming_normal_(model.parameters)
  30. nn.init.constant(model.act_max, 1.0)
  31. model = model.cuda()
  32. optimizer = torch.optim.SGD(model.parameters(), lr=0.001)
  33. for epoch in range(100):
  34.     for i in range(1000):
  35.         output = model(input)
  36.         loss = nn.CrossEntropyLoss()(output, label)
  37.         optimizer.zero_grad()
  38.         loss.backward()
  39.         optimizer.step()
  40.         model.act_max.data = model.act_max.data - 0.001 * model.act_max.grad.data
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top