Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions Pytorch_MNIST.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Net.forward refactored with the following changes:


train_losses = []
train_counter = []
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function main refactored with the following changes:


transform=transforms.Compose([
transforms.ToTensor(),
Expand Down
26 changes: 10 additions & 16 deletions Pytorch_ResNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ResNet.__init__ refactored with the following changes:

super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(in_dim, 64, kernel_size=7, stride=2, padding=3,
bias=False)
Expand Down Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function ResNet._make_layer refactored with the following changes:

return nn.Sequential(*layers)

def forward(self, x):
Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function resnet18 refactored with the following changes:


torch.manual_seed(RANDOM_SEED)

Expand All @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function compute_accuracy refactored with the following changes:

features = features.to(device)
targets = targets.to(device)

Expand Down