Hello, I am new to Pytorch. I tried to implement LeNet architecture in PyTorch. But my result is bad when compared to the tutorial i followed.
from torch import nn
class LeNet5(nn.Module):
def init(self):
super().__init__()
#initialize the layers
# Layer 1 which is convolution layer
self.conv1 = nn.Conv2d(in_channels = 1,
out_channels = 6,
kernel_size = 5,
stride = 1,
padding = 2
) #28 X 28 -----> 32 X 32 ----> 28 X 28
# The reason we are adding padding beacause, the image size we have is 28 X 28.
# But in the original paper we have input size as 32 X 32.
#Layer 2 which is Average Pooling
self.pool1 = nn.AvgPool2d(kernel_size = 2,
stride = 2) # 14 X 14
# Layer 3 which is Convolution
self.conv2 = nn.Conv2d(in_channels = 6,
out_channels = 16,
kernel_size = 5,
stride = 1,
) # 10 X 10
#Layer 4 AVG Pooling
self.pool2 = nn.AvgPool2d(kernel_size = 2,
stride = 2
) # 5 X 5
#Layer 5 Fully connected
self.fc1 = nn.Linear(in_features = 16 * 5 * 5,
out_features = 120
)
#As the image size for this layer is 16 X 5 X 5 , the input features will
#400 (multiply). Paper used 120 as output features
# Layer 6 Fully connected
self.fc2 = nn.Linear(in_features = 120,
out_features = 84)
# Layer 7 Fully connected
self.fc3 = nn.Linear(in_features = 84,
out_features = 10)
def forward(self,input):
# conv2D -> TanH -> AVGpool
tanh = nn.Tanh()
c1 = tanh(self.conv1(input))
#In PyTorch, nn.Tanh is a module that applies the hyperbolic tangent function
#element-wise to the input tensor. It does not take any arguments during instantiation.
#Instead, you should call it directly on the input tensor.
p1 = self.pool1(c1)
# conv2D -> TanH -> AVGpool
c2 = tanh(self.conv2(p1))
p2 = self.pool2(c2)
# AVGpool -> FC1 -> Fc2 ->Fc3
f1 = tanh(self.fc1(torch.flatten(p2,1)))
f2 = tanh(self.fc2(f1))
f3 = self.fc3(f2)
# return the output
return f3
This is my code. I am not sure what i did wrong