Skip to content
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

Bug fix and change of defaults #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 9 additions & 12 deletions net.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
from __future__ import print_function

import torch
import torch.nn as nn
import torch.nn.functional as F
import math

from torch.optim import lr_scheduler
from torch.autograd import Variable


def squash(x):
Expand Down Expand Up @@ -165,14 +162,14 @@ def forward(self, lengths, targets, size_average=True):

# Training settings
parser = argparse.ArgumentParser(description='CapsNet with MNIST')
parser.add_argument('--batch-size', type=int, default=128, metavar='N',
parser.add_argument('--batch-size', type=int, default=64, metavar='N',
help='input batch size for training (default: 64)')
parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N',
help='input batch size for testing (default: 1000)')
parser.add_argument('--epochs', type=int, default=250, metavar='N',
parser.add_argument('--test-batch-size', type=int, default=64, metavar='N',
help='input batch size for testing (default: 64)')
parser.add_argument('--epochs', type=int, default=10, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--lr', type=float, default=0.001, metavar='LR',
help='learning rate (default: 0.01)')
help='learning rate (default: 0.001)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
Expand Down Expand Up @@ -241,7 +238,7 @@ def train(epoch):
if batch_idx % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.data[0]))
100. * batch_idx / len(train_loader), loss.item()))

def test():
model.eval()
Expand All @@ -254,12 +251,12 @@ def test():

if args.with_reconstruction:
output, probs = model(data, target)
reconstruction_loss = F.mse_loss(output, data.view(-1, 784), size_average=False).data[0]
test_loss += loss_fn(probs, target, size_average=False).data[0]
reconstruction_loss = F.mse_loss(output, data.view(-1, 784), size_average=False).item()
test_loss += loss_fn(probs, target, size_average=False).item()
test_loss += reconstruction_alpha * reconstruction_loss
else:
output, probs = model(data)
test_loss += loss_fn(probs, target, size_average=False).data[0]
test_loss += loss_fn(probs, target, size_average=False).item()

pred = probs.data.max(1, keepdim=True)[1] # get the index of the max probability
correct += pred.eq(target.data.view_as(pred)).cpu().sum()
Expand Down