-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Add LeNet Implementation in PyTorch #7070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
3af2ebd
d41a827
dbfa015
2248180
0d14ebb
72f6316
a6121d6
eced5e0
b08a3c8
ad84dc9
43d2128
6bf6e91
f2a2e78
a5227d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
LeNet Network | ||
|
||
Paper: http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf | ||
""" | ||
|
||
import numpy | ||
import torch | ||
import torch.nn as nn | ||
|
||
|
||
class LeNet(nn.Module): | ||
def __init__(self) -> None: | ||
super().__init__() | ||
|
||
self.tanh = nn.Tanh() | ||
self.avgpool = nn.AvgPool2d(kernel_size=2, stride=2) | ||
|
||
self.conv1 = nn.Conv2d( | ||
in_channels=1, | ||
out_channels=6, | ||
kernel_size=(5, 5), | ||
stride=(1, 1), | ||
padding=(0, 0), | ||
) | ||
self.conv2 = nn.Conv2d( | ||
in_channels=6, | ||
out_channels=16, | ||
kernel_size=(5, 5), | ||
stride=(1, 1), | ||
padding=(0, 0), | ||
) | ||
self.conv3 = nn.Conv2d( | ||
in_channels=16, | ||
out_channels=120, | ||
kernel_size=(5, 5), | ||
stride=(1, 1), | ||
padding=(0, 0), | ||
) | ||
|
||
self.linear1 = nn.Linear(120, 84) | ||
self.linear2 = nn.Linear(84, 10) | ||
|
||
def forward(self, image_array: numpy.ndarray) -> numpy.ndarray: | ||
image_array = self.tanh(self.conv1(image_array)) | ||
image_array = self.avgpool(image_array) | ||
image_array = self.tanh(self.conv2(image_array)) | ||
image_array = self.avgpool(image_array) | ||
image_array = self.tanh(self.conv3(image_array)) | ||
|
||
image_array = image_array.reshape(image_array.shape[0], -1) | ||
image_array = self.tanh(self.linear1(image_array)) | ||
image_array = self.linear2(image_array) | ||
return image_array | ||
|
||
|
||
def test_model() -> bool: | ||
""" | ||
Test the model on a random input of size [64, 1, 32, 32] | ||
|
||
>>> test_model() | ||
True | ||
|
||
""" | ||
random_image = torch.randn(64, 1, 32, 32) | ||
model = LeNet() | ||
output = model(random_image) | ||
|
||
return output.shape == torch.zeros([64, 10]).shape | ||
|
||
|
||
if __name__ == "__main__": | ||
print(f"Model Passed: {test_model()}") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ statsmodels | |
sympy | ||
tensorflow | ||
texttable | ||
torch | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do I need to do anything about this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The only thing we can do is wait until torch is compatible with the current version of Python. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, after it is compatible this PR will be automatically merged? |
||
tweepy | ||
xgboost | ||
yulewalker |
Uh oh!
There was an error while loading. Please reload this page.