Skip to content

Instantly share code, notes, and snippets.

@xmfbit
Last active March 4, 2023 19:45
Show Gist options
  • Select an option

  • Save xmfbit/b27cdbff68870418bdb8cefa86a2d558 to your computer and use it in GitHub Desktop.

Select an option

Save xmfbit/b27cdbff68870418bdb8cefa86a2d558 to your computer and use it in GitHub Desktop.

Revisions

  1. xmfbit revised this gist May 12, 2018. 1 changed file with 11 additions and 8 deletions.
    19 changes: 11 additions & 8 deletions pytorch_mnist.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    import os
    import torch
    import torch.nn as nn
    from torch.autograd import Variable
    @@ -9,11 +10,13 @@
    use_cuda = torch.cuda.is_available()

    root = './data'
    download = False # download MNIST dataset or not

    if not os.path.exists(root):
    os.mkdir(root)

    trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))])
    train_set = dset.MNIST(root=root, train=True, transform=trans, download=download)
    test_set = dset.MNIST(root=root, train=False, transform=trans)
    # if not exist, download mnist dataset
    train_set = dset.MNIST(root=root, train=True, transform=trans, download=True)
    test_set = dset.MNIST(root=root, train=False, transform=trans, download=True)

    batch_size = 100

    @@ -75,7 +78,7 @@ def name(self):

    optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

    ceriation = nn.CrossEntropyLoss()
    criterion = nn.CrossEntropyLoss()

    for epoch in xrange(10):
    # trainning
    @@ -86,7 +89,7 @@ def name(self):
    x, target = x.cuda(), target.cuda()
    x, target = Variable(x), Variable(target)
    out = model(x)
    loss = ceriation(out, target)
    loss = criterion(out, target)
    ave_loss = ave_loss * 0.9 + loss.data[0] * 0.1
    loss.backward()
    optimizer.step()
    @@ -98,10 +101,10 @@ def name(self):
    total_cnt = 0
    for batch_idx, (x, target) in enumerate(test_loader):
    if use_cuda:
    x, targe = x.cuda(), target.cuda()
    x, target = x.cuda(), target.cuda()
    x, target = Variable(x, volatile=True), Variable(target, volatile=True)
    out = model(x)
    loss = ceriation(out, target)
    loss = criterion(out, target)
    _, pred_label = torch.max(out.data, 1)
    total_cnt += x.data.size()[0]
    correct_cnt += (pred_label == target.data).sum()
  2. xmfbit revised this gist Jan 27, 2018. 1 changed file with 54 additions and 33 deletions.
    87 changes: 54 additions & 33 deletions pytorch_mnist.py
    Original file line number Diff line number Diff line change
    @@ -6,22 +6,25 @@
    import torch.nn.functional as F
    import torch.optim as optim
    ## load mnist dataset
    use_cuda = torch.cuda.is_available()

    root = './data'
    download = False
    download = False # download MNIST dataset or not

    trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))])
    train_set = dset.MNIST(root=root, train=True, transform=trans, download=download)
    test_set = dset.MNIST(root=root, train=False, transform=trans)

    batch_size = 128
    kwargs = {'num_workers': 1, 'pin_memory': True}
    batch_size = 100

    train_loader = torch.utils.data.DataLoader(
    dataset=train_set,
    batch_size=batch_size,
    shuffle=True, **kwargs)
    shuffle=True)
    test_loader = torch.utils.data.DataLoader(
    dataset=test_set,
    batch_size=batch_size,
    shuffle=False, **kwargs)
    shuffle=False)

    print '==>>> total trainning batch number: {}'.format(len(train_loader))
    print '==>>> total testing batch number: {}'.format(len(test_loader))
    @@ -33,16 +36,15 @@ def __init__(self):
    self.fc1 = nn.Linear(28*28, 500)
    self.fc2 = nn.Linear(500, 256)
    self.fc3 = nn.Linear(256, 10)
    self.ceriation = nn.CrossEntropyLoss()
    def forward(self, x, target):
    def forward(self, x):
    x = x.view(-1, 28*28)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = F.relu(self.fc3(x))
    loss = self.ceriation(x, target)
    return x, loss
    x = self.fc3(x)
    return x

    def name(self):
    return 'mlpnet'
    return "MLP"

    class LeNet(nn.Module):
    def __init__(self):
    @@ -51,45 +53,64 @@ def __init__(self):
    self.conv2 = nn.Conv2d(20, 50, 5, 1)
    self.fc1 = nn.Linear(4*4*50, 500)
    self.fc2 = nn.Linear(500, 10)
    self.ceriation = nn.CrossEntropyLoss()
    def forward(self, x, target):
    x = self.conv1(x)

    def forward(self, x):
    x = F.relu(self.conv1(x))
    x = F.max_pool2d(x, 2, 2)
    x = F.relu(x)
    x = self.conv2(x)
    x = F.relu(self.conv2(x))
    x = F.max_pool2d(x, 2, 2)
    x = F.relu(x)
    x = x.view(-1, 4*4*50)
    x = self.fc1(x)
    x = F.relu(self.fc1(x))
    x = self.fc2(x)
    loss = self.ceriation(x, target)
    return x, loss
    return x

    def name(self):
    return 'lenet'
    return "LeNet"

    ## training
    model = MLPNet().cuda()
    model = LeNet()

    if use_cuda:
    model = model.cuda()

    optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

    ceriation = nn.CrossEntropyLoss()

    for epoch in xrange(10):
    # trainning
    ave_loss = 0
    for batch_idx, (x, target) in enumerate(train_loader):
    optimizer.zero_grad()
    x, target = Variable(x.cuda()), Variable(target.cuda())
    _, loss = model(x, target)
    if use_cuda:
    x, target = x.cuda(), target.cuda()
    x, target = Variable(x), Variable(target)
    out = model(x)
    loss = ceriation(out, target)
    ave_loss = ave_loss * 0.9 + loss.data[0] * 0.1
    loss.backward()
    optimizer.step()
    if batch_idx % 100 == 0:
    print '==>>> epoch: {}, batch index: {}, train loss: {:.6f}'.format(epoch, batch_idx, loss.data[0])
    if (batch_idx+1) % 100 == 0 or (batch_idx+1) == len(train_loader):
    print '==>>> epoch: {}, batch index: {}, train loss: {:.6f}'.format(
    epoch, batch_idx+1, ave_loss)
    # testing
    correct_cnt, ave_loss = 0, 0
    total_cnt = 0
    for batch_idx, (x, target) in enumerate(test_loader):
    x, target = Variable(x.cuda(), volatile=True), Variable(target.cuda(), volatile=True)
    score, loss = model(x, target)
    _, pred_label = torch.max(score.data, 1)
    if use_cuda:
    x, targe = x.cuda(), target.cuda()
    x, target = Variable(x, volatile=True), Variable(target, volatile=True)
    out = model(x)
    loss = ceriation(out, target)
    _, pred_label = torch.max(out.data, 1)
    total_cnt += x.data.size()[0]
    correct_cnt += (pred_label == target.data).sum()
    ave_loss += loss.data[0]
    accuracy = correct_cnt*1.0/len(test_loader)/batch_size
    ave_loss /= len(test_loader)
    print '==>>> epoch: {}, test loss: {:.6f}, accuracy: {:.4f}'.format(epoch, ave_loss, accuracy)
    # smooth average
    ave_loss = ave_loss * 0.9 + loss.data[0] * 0.1

    if(batch_idx+1) % 100 == 0 or (batch_idx+1) == len(test_loader):
    print '==>>> epoch: {}, batch index: {}, test loss: {:.6f}, acc: {:.3f}'.format(
    epoch, batch_idx+1, ave_loss, correct_cnt * 1.0 / total_cnt)

    torch.save(model.state_dict(), model.name())

  3. xmfbit created this gist Mar 4, 2017.
    95 changes: 95 additions & 0 deletions pytorch_mnist.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    import torch
    import torch.nn as nn
    from torch.autograd import Variable
    import torchvision.datasets as dset
    import torchvision.transforms as transforms
    import torch.nn.functional as F
    import torch.optim as optim
    ## load mnist dataset
    root = './data'
    download = False
    trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))])
    train_set = dset.MNIST(root=root, train=True, transform=trans, download=download)
    test_set = dset.MNIST(root=root, train=False, transform=trans)

    batch_size = 128
    kwargs = {'num_workers': 1, 'pin_memory': True}
    train_loader = torch.utils.data.DataLoader(
    dataset=train_set,
    batch_size=batch_size,
    shuffle=True, **kwargs)
    test_loader = torch.utils.data.DataLoader(
    dataset=test_set,
    batch_size=batch_size,
    shuffle=False, **kwargs)

    print '==>>> total trainning batch number: {}'.format(len(train_loader))
    print '==>>> total testing batch number: {}'.format(len(test_loader))

    ## network
    class MLPNet(nn.Module):
    def __init__(self):
    super(MLPNet, self).__init__()
    self.fc1 = nn.Linear(28*28, 500)
    self.fc2 = nn.Linear(500, 256)
    self.fc3 = nn.Linear(256, 10)
    self.ceriation = nn.CrossEntropyLoss()
    def forward(self, x, target):
    x = x.view(-1, 28*28)
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = F.relu(self.fc3(x))
    loss = self.ceriation(x, target)
    return x, loss
    def name(self):
    return 'mlpnet'

    class LeNet(nn.Module):
    def __init__(self):
    super(LeNet, self).__init__()
    self.conv1 = nn.Conv2d(1, 20, 5, 1)
    self.conv2 = nn.Conv2d(20, 50, 5, 1)
    self.fc1 = nn.Linear(4*4*50, 500)
    self.fc2 = nn.Linear(500, 10)
    self.ceriation = nn.CrossEntropyLoss()
    def forward(self, x, target):
    x = self.conv1(x)
    x = F.max_pool2d(x, 2, 2)
    x = F.relu(x)
    x = self.conv2(x)
    x = F.max_pool2d(x, 2, 2)
    x = F.relu(x)
    x = x.view(-1, 4*4*50)
    x = self.fc1(x)
    x = self.fc2(x)
    loss = self.ceriation(x, target)
    return x, loss
    def name(self):
    return 'lenet'
    ## training
    model = MLPNet().cuda()

    optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.9)
    for epoch in xrange(10):
    # trainning
    for batch_idx, (x, target) in enumerate(train_loader):
    optimizer.zero_grad()
    x, target = Variable(x.cuda()), Variable(target.cuda())
    _, loss = model(x, target)
    loss.backward()
    optimizer.step()
    if batch_idx % 100 == 0:
    print '==>>> epoch: {}, batch index: {}, train loss: {:.6f}'.format(epoch, batch_idx, loss.data[0])
    # testing
    correct_cnt, ave_loss = 0, 0
    for batch_idx, (x, target) in enumerate(test_loader):
    x, target = Variable(x.cuda(), volatile=True), Variable(target.cuda(), volatile=True)
    score, loss = model(x, target)
    _, pred_label = torch.max(score.data, 1)
    correct_cnt += (pred_label == target.data).sum()
    ave_loss += loss.data[0]
    accuracy = correct_cnt*1.0/len(test_loader)/batch_size
    ave_loss /= len(test_loader)
    print '==>>> epoch: {}, test loss: {:.6f}, accuracy: {:.4f}'.format(epoch, ave_loss, accuracy)

    torch.save(model.state_dict(), model.name())