| 48 | |
| 49 | |
| 50 | class MnistNet(Module): |
| 51 | def __init__(self, has_bn=True): |
| 52 | super().__init__() |
| 53 | self.conv0 = Conv2d(1, 20, kernel_size=5, bias=True) |
| 54 | self.pool0 = AvgPool2d(2) |
| 55 | self.conv1 = Conv2d(20, 20, kernel_size=5, bias=True) |
| 56 | self.pool1 = AvgPool2d(2) |
| 57 | self.fc0 = Linear(20 * 4 * 4, 500, bias=True) |
| 58 | self.fc1 = Linear(500, 10, bias=True) |
| 59 | self.bn0 = None |
| 60 | self.bn1 = None |
| 61 | if has_bn: |
| 62 | self.bn0 = BatchNorm2d(20) |
| 63 | self.bn1 = BatchNorm2d(20) |
| 64 | |
| 65 | def forward(self, x): |
| 66 | x = self.conv0(x) |
| 67 | if self.bn0: |
| 68 | x = self.bn0(x) |
| 69 | x = F.relu(x) |
| 70 | x = self.pool0(x) |
| 71 | x = self.conv1(x) |
| 72 | if self.bn1: |
| 73 | x = self.bn1(x) |
| 74 | x = F.relu(x) |
| 75 | x = self.pool1(x) |
| 76 | x = F.flatten(x, 1) |
| 77 | x = self.fc0(x) |
| 78 | x = F.relu(x) |
| 79 | x = self.fc1(x) |
| 80 | return x |
| 81 | |
| 82 | |
| 83 | def train(data, label, net, opt, gm): |
no outgoing calls