-
Notifications
You must be signed in to change notification settings - Fork 0
add simple neural network and ResNet for Pytorch (Sourcery refactored) #2
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
base: benchmarks-Python
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -31,8 +31,7 @@ def forward(self, x): | |
| x = F.relu(x) | ||
| x = self.dropout2(x) | ||
| x = self.fc2(x) | ||
| output = F.log_softmax(x, dim=1) | ||
| return output | ||
| return F.log_softmax(x, dim=1) | ||
|
|
||
| train_losses = [] | ||
| train_counter = [] | ||
|
|
@@ -120,8 +119,8 @@ def main(): | |
| cuda_kwargs = {'num_workers': 1, | ||
| 'pin_memory': True, | ||
| 'shuffle': True} | ||
| train_kwargs.update(cuda_kwargs) | ||
| test_kwargs.update(cuda_kwargs) | ||
| train_kwargs |= cuda_kwargs | ||
| test_kwargs |= cuda_kwargs | ||
|
Author
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. Function
|
||
|
|
||
| transform=transforms.Compose([ | ||
| transforms.ToTensor(), | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,10 +107,7 @@ class ResNet(nn.Module): | |
|
|
||
| def __init__(self, block, layers, num_classes, grayscale): | ||
| self.inplanes = 64 | ||
| if grayscale: | ||
| in_dim = 1 | ||
| else: | ||
| in_dim = 3 | ||
| in_dim = 1 if grayscale else 3 | ||
|
Author
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. Function
|
||
| super(ResNet, self).__init__() | ||
| self.conv1 = nn.Conv2d(in_dim, 64, kernel_size=7, stride=2, padding=3, | ||
| bias=False) | ||
|
|
@@ -141,12 +138,9 @@ def _make_layer(self, block, planes, blocks, stride=1): | |
| nn.BatchNorm2d(planes * block.expansion), | ||
| ) | ||
|
|
||
| layers = [] | ||
| layers.append(block(self.inplanes, planes, stride, downsample)) | ||
| layers = [block(self.inplanes, planes, stride, downsample)] | ||
| self.inplanes = planes * block.expansion | ||
| for i in range(1, blocks): | ||
| layers.append(block(self.inplanes, planes)) | ||
|
|
||
| layers.extend(block(self.inplanes, planes) for _ in range(1, blocks)) | ||
|
Comment on lines
-144
to
+143
Author
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. Function
|
||
| return nn.Sequential(*layers) | ||
|
|
||
| def forward(self, x): | ||
|
|
@@ -170,11 +164,12 @@ def forward(self, x): | |
|
|
||
| def resnet18(num_classes): | ||
| """Constructs a ResNet-18 model.""" | ||
| model = ResNet(block=BasicBlock, | ||
| layers=[2, 2, 2, 2], | ||
| num_classes=NUM_CLASSES, | ||
| grayscale=GRAYSCALE) | ||
| return model | ||
| return ResNet( | ||
| block=BasicBlock, | ||
| layers=[2, 2, 2, 2], | ||
| num_classes=NUM_CLASSES, | ||
| grayscale=GRAYSCALE, | ||
| ) | ||
|
Comment on lines
-173
to
+172
Author
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. Function
|
||
|
|
||
| torch.manual_seed(RANDOM_SEED) | ||
|
|
||
|
|
@@ -186,8 +181,7 @@ def resnet18(num_classes): | |
|
|
||
| def compute_accuracy(model, data_loader, device): | ||
| correct_pred, num_examples = 0, 0 | ||
| for i, (features, targets) in enumerate(data_loader): | ||
|
|
||
| for features, targets in data_loader: | ||
|
Comment on lines
-189
to
+184
Author
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. Function
|
||
| features = features.to(device) | ||
| targets = targets.to(device) | ||
|
|
||
|
|
||
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.
Function
Net.forwardrefactored with the following changes:inline-immediately-returned-variable)