-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Add LeNet Implementation in PyTorch #7070
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3af2ebd
add torch to requirements
ishandutta0098 d41a827
add lenet architecture in pytorch
ishandutta0098 dbfa015
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2248180
add type hints
ishandutta0098 0d14ebb
remove file
ishandutta0098 72f6316
Merge branch 'master' of https://github.com/ishandutta0098/Python
ishandutta0098 a6121d6
add type hints
ishandutta0098 eced5e0
Merge branch 'TheAlgorithms:master' into master
ishandutta0098 b08a3c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ad84dc9
update variable name
ishandutta0098 43d2128
add fail test
ishandutta0098 6bf6e91
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f2a2e78
add newline
ishandutta0098 a5227d9
reformatting
ishandutta0098 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
""" | ||
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(image_tensor: torch.tensor) -> bool: | ||
""" | ||
Test the model on an input batch of 64 images | ||
|
||
Args: | ||
image_tensor (torch.tensor): Batch of Images for the model | ||
|
||
>>> test_model(torch.randn(64, 1, 32, 32)) | ||
True | ||
|
||
""" | ||
try: | ||
model = LeNet() | ||
output = model(image_tensor) | ||
except RuntimeError: | ||
return False | ||
|
||
return output.shape == torch.zeros([64, 10]).shape | ||
|
||
|
||
if __name__ == "__main__": | ||
random_image_1 = torch.randn(64, 1, 32, 32) | ||
random_image_2 = torch.randn(1, 32, 32) | ||
|
||
print(f"random_image_1 Model Passed: {test_model(random_image_1)}") | ||
print(f"\nrandom_image_2 Model Passed: {test_model(random_image_2)}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ statsmodels | |
sympy | ||
tensorflow | ||
texttable | ||
torch | ||
tweepy | ||
xgboost | ||
yulewalker |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pytorch/pytorch#86566
There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, after it is compatible this PR will be automatically merged?