54 lines
1.3 KiB
Python
54 lines
1.3 KiB
Python
from torch import nn
|
|
import torch.nn.functional as F
|
|
|
|
|
|
class ModelMNISTv1(nn.Module):
|
|
|
|
def __init__(self, config):
|
|
super().__init__()
|
|
|
|
# mnist images are (1, 28, 28) (channels, width, height)
|
|
self.layer_1 = nn.Linear(28 * 28, 128)
|
|
self.layer_2 = nn.Linear(128, 256)
|
|
self.layer_3 = nn.Linear(256, 10)
|
|
|
|
def forward(self, x):
|
|
batch_size, channels, width, height = x.size()
|
|
|
|
# (b, 1, 28, 28) -> (b, 1*28*28)
|
|
x = x.view(batch_size, -1)
|
|
x = self.layer_1(x)
|
|
x = F.relu(x)
|
|
x = self.layer_2(x)
|
|
x = F.relu(x)
|
|
x = self.layer_3(x)
|
|
|
|
return F.log_softmax(x, dim=1)
|
|
|
|
|
|
class ModelMNISTv2(nn.Module):
|
|
|
|
def __init__(self, config):
|
|
super().__init__()
|
|
|
|
# mnist images are (1, 28, 28) (channels, width, height)
|
|
self.layer_1 = nn.Linear(28 * 28, 256)
|
|
self.layer_2 = nn.Linear(256, 128)
|
|
self.layer_3 = nn.Linear(128, 64)
|
|
self.layer_4 = nn.Linear(64, 10)
|
|
|
|
def forward(self, x):
|
|
batch_size, channels, width, height = x.size()
|
|
|
|
# (b, 1, 28, 28) -> (b, 1*28*28)
|
|
x = x.view(batch_size, -1)
|
|
x = self.layer_1(x)
|
|
x = F.relu(x)
|
|
x = self.layer_2(x)
|
|
x = F.relu(x)
|
|
x = self.layer_3(x)
|
|
x = F.relu(x)
|
|
x = self.layer_4(x)
|
|
|
|
return F.log_softmax(x, dim=1)
|