| import random |
| import numpy as np |
| import torch as tr |
|
|
| seed = 4312130 |
| random.seed(seed) |
| np.random.seed(seed) |
| tr.manual_seed(seed) |
|
|
| class DummyNet(tr.nn.Module): |
| |
| def __init__(self): |
| super().__init__() |
| self.linear1 = tr.nn.Linear(784, 128, bias=True) |
| self.linear2 = tr.nn.Linear(128, 64, bias=False) |
| self.linear3 = tr.nn.Linear(64, 32, bias=True) |
| self.linear4 = tr.nn.Linear(32, 10, bias=False) |
| |
| |
| self.init_weights() |
|
|
| def init_weights(self): |
| tr.nn.init.normal_(self.linear1.weight, mean=0.0, std=0.01) |
| tr.nn.init.normal_(self.linear2.weight, mean=0.0, std=0.01) |
| tr.nn.init.normal_(self.linear3.weight, mean=0.0, std=0.01) |
| tr.nn.init.normal_(self.linear4.weight, mean=0.0, std=0.01) |
|
|
| tr.nn.init.zeros_(self.linear1.bias) |
| tr.nn.init.zeros_(self.linear3.bias) |
|
|
| |
|
|
| |
| |
| def forward(self, x): |
| x = self.linear1(x) |
| x = self.linear2(x) |
| x = self.linear3(x) |
| x = self.linear4(x) |
| |
| return x |
| |
| model = DummyNet() |
| optimizer = tr.optim.SGD(model.parameters(), lr=0.01, maximize=False) |
|
|
|
|
| loss_fn = tr.nn.CrossEntropyLoss() |