Module models.femnist
CNN model for FEMNIST Dataset.
Expand source code
#!/usr/bin/env python3
"""
CNN model for FEMNIST Dataset.
"""
from torch import nn
# Import PyTorch layers, activations and more
import torch.nn.functional as F
class FEMNIST(nn.Module):
def __init__(self, channel_1=32, channel_2=64, num_classes=62):
super(FEMNIST, self).__init__()
self.conv1 = nn.Conv2d(1, channel_1, (5, 5))
self.conv2 = nn.Conv2d(channel_1, channel_2, (5, 5))
# Fully connected layer from 16 * channel_2 to num_classes units
self.fc = nn.Linear(16 * channel_2, num_classes)
def forward(self, x):
out = F.relu(F.max_pool2d(self.conv1(x), 2))
out = F.relu(F.max_pool2d(self.conv2(out), 2))
out = self.fc(nn.Flatten(out))
return out
# TODO: Any way we can actually have an useful pretrained argument here?
def femnist(pretrained=False, num_classes=62):
return FEMNIST(num_classes=num_classes)
def minifemnist(pretrained=False, num_classes=62):
return FEMNIST(num_classes=num_classes, channel_1=10, channel_2=20)
Functions
def femnist(pretrained=False, num_classes=62)
-
Expand source code
def femnist(pretrained=False, num_classes=62): return FEMNIST(num_classes=num_classes)
def minifemnist(pretrained=False, num_classes=62)
-
Expand source code
def minifemnist(pretrained=False, num_classes=62): return FEMNIST(num_classes=num_classes, channel_1=10, channel_2=20)
Classes
class FEMNIST (channel_1=32, channel_2=64, num_classes=62)
-
Base class for all neural network modules.
Your models should also subclass this class.
Modules can also contain other Modules, allowing to nest them in a tree structure. You can assign the submodules as regular attributes::
import torch.nn as nn import torch.nn.functional as F class Model(nn.Module): def __init__(self): super(Model, self).__init__() self.conv1 = nn.Conv2d(1, 20, 5) self.conv2 = nn.Conv2d(20, 20, 5) def forward(self, x): x = F.relu(self.conv1(x)) return F.relu(self.conv2(x))
Submodules assigned in this way will be registered, and will have their parameters converted too when you call :meth:
to
, etc.:ivar training: Boolean represents whether this module is in training or evaluation mode. :vartype training: bool
Initializes internal Module state, shared by both nn.Module and ScriptModule.
Expand source code
class FEMNIST(nn.Module): def __init__(self, channel_1=32, channel_2=64, num_classes=62): super(FEMNIST, self).__init__() self.conv1 = nn.Conv2d(1, channel_1, (5, 5)) self.conv2 = nn.Conv2d(channel_1, channel_2, (5, 5)) # Fully connected layer from 16 * channel_2 to num_classes units self.fc = nn.Linear(16 * channel_2, num_classes) def forward(self, x): out = F.relu(F.max_pool2d(self.conv1(x), 2)) out = F.relu(F.max_pool2d(self.conv2(out), 2)) out = self.fc(nn.Flatten(out)) return out
Ancestors
- torch.nn.modules.module.Module
Class variables
var dump_patches : bool
var training : bool
Methods
def forward(self, x) ‑> Callable[..., Any]
-
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the :class:
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.Expand source code
def forward(self, x): out = F.relu(F.max_pool2d(self.conv1(x), 2)) out = F.relu(F.max_pool2d(self.conv2(out), 2)) out = self.fc(nn.Flatten(out)) return out