Last active
February 22, 2021 13:01
-
-
Save sahandilshan/9068d8710d229c8034e13c64110ca533 to your computer and use it in GitHub Desktop.
A code snippet to train a model with MNIST dataset and compress it using pruning with PyTorch. Completed full code can be found on here https://github.com/sahandilshan/Simple-NN-Compression
This file contains hidden or 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
class Classifier(nn.Module): | |
def __init__(self): | |
super().__init__() | |
self.fc1 = nn.Linear(784, 256) | |
self.fc2 = nn.Linear(256, 128) | |
self.fc3 = nn.Linear(128, 64) | |
self.fc4 = nn.Linear(64, 10) | |
def forward(self, x): | |
# make sure input tensor is flattened | |
x = x.view(x.shape[0], -1) | |
x = F.relu(self.fc1(x)) | |
x = F.relu(self.fc2(x)) | |
x = F.relu(self.fc3(x)) | |
x = F.log_softmax(self.fc4(x), dim=1) | |
return x | |
model = Classifier() | |
criterion = nn.NLLLoss() | |
optimizer = optim.Adam(model.parameters(), lr=0.003) | |
epochs = 5 | |
for e in range(epochs): | |
running_loss = 0 | |
for images, labels in trainloader: | |
log_ps = model(images) | |
loss = criterion(log_ps, labels) | |
optimizer.zero_grad() | |
loss.backward() | |
optimizer.step() | |
running_loss += loss.item() | |
else: | |
print(f"Training loss: {running_loss/len(trainloader)}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment