Hi everyone, thanks for the great community. I was wondering whether could someone provide some insights into how to construct a NN such as those in the examples on the github page but on its simplest form. Let me explain with an example.
For instance in the github page of examples one can find the following.
import torch.nn as nn
import torch.nn.functional as F
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 6, 5) # 1 input image channel, 6 output channels, 5x5 square convolution kernel
self.conv2 = nn.Conv2d(6, 16, 5)
self.fc1 = nn.Linear(16*5*5, 120) # an affine operation: y = Wx + b
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2)) # Max pooling over a (2, 2) window
x = F.max_pool2d(F.relu(self.conv2(x)), 2) # If the size is a square you can only specify a single number
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = self.fc3(x)
return x
net = Net()
The problem is that I want to achieve the same thing but without classes and inheritance. I could do that with the nn.Module
from torch
either the sequential
or funcitonal
.
There are a couple of things here that are being obfuscated and might create confusion for newcomers.
Correct me if I’m wrong but for instance inside the function forward
the self.conv1(x) == nn.Conv2d(1,6, 5)(x)?
right?
I also don’t know how to use the nn.Module
besides the context of the inheritance introduced in the examples.
Is it possible for someone to translate that into a simple minimal example using only as sequence of conv
->relu
->pool
only?
Thanks.