out = self.fc1(x)
        out = F.relu(out)
        out = self.fc2(out)
        return out


#net = Net(input_size, hidden_size, num_classes)
from lenet3 import LeNet
net = LeNet()
net.cuda()
net.train()
# Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = PIDOptimizer(net.parameters(),
                         lr=learning_rate,
                         weight_decay=0.0001,
                         momentum=0.9,
                         I=I,
                         D=D)
# Train the Model
for epoch in range(num_epochs):

    train_loss_log = AverageMeter()
    train_acc_log = AverageMeter()
    val_loss_log = AverageMeter()
    val_acc_log = AverageMeter()
    for i, (images, labels) in enumerate(train_loader):
        # Convert torch tensor to Variable
        images = Variable(images.cuda())
        labels = Variable(labels.cuda())

        # Forward + Backward + Optimize
Exemple #2
0
def training(optimizer_sign=0):
    training_data = {
        'train_loss': [],
        'val_loss': [],
        'train_acc': [],
        'val_acc': []
    }
    net = Net(input_size, hidden_size, num_classes)
    # net = Net(input_size, hidden_size, num_classes)
    net.cuda()
    net.train()
    # Loss and Optimizer
    criterion = nn.CrossEntropyLoss()
    if optimizer_sign == 0:
        optimizer = torch.optim.RMSprop(net.parameters(), lr=learning_rate)
    elif optimizer_sign == 1:
        optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
    elif optimizer_sign == 2:
        optimizer = SGD(net.parameters(),
                        lr=learning_rate,
                        weight_decay=0.0001,
                        momentum=0.9)
    elif optimizer_sign == 3:
        optimizer = PIDOptimizer(net.parameters(),
                                 lr=learning_rate,
                                 weight_decay=0.0001,
                                 momentum=0.9,
                                 I=I,
                                 D=D)
    else:
        optimizer = PIDOptimizer(net.parameters(),
                                 lr=learning_rate,
                                 weight_decay=0.0001,
                                 momentum=0.9,
                                 I=I,
                                 D=0)
    # Train the Model
    for epoch in range(num_epochs):

        train_loss_log = AverageMeter()
        train_acc_log = AverageMeter()
        val_loss_log = AverageMeter()
        val_acc_log = AverageMeter()
        for i, (images, labels) in enumerate(train_loader):
            # Convert torch tensor to Variable
            images = images.view(-1, 28 * 28).cuda()
            labels = Variable(labels.cuda())

            # Forward + Backward + Optimize
            optimizer.zero_grad()  # zero the gradient buffer
            outputs = net(images)
            train_loss = criterion(outputs, labels)
            train_loss.backward()
            optimizer.step()
            prec1, prec5 = accuracy(outputs.data, labels.data, topk=(1, 5))
            train_loss_log.update(train_loss.data, images.size(0))
            train_acc_log.update(prec1, images.size(0))

            if (i + 1) % 100 == 0:
                print('Epoch [%d/%d], Step [%d/%d], Loss: %.4f, Acc: %.8f' %
                      (epoch + 1, num_epochs, i + 1, len(train_dataset) //
                       batch_size, train_loss_log.avg, train_acc_log.avg))
                training_data['train_loss'].append(
                    train_loss_log.avg.detach().cpu().numpy())
                training_data['train_acc'].append(
                    train_acc_log.avg.detach().cpu().numpy())

        # Test the Model
        net.eval()
        correct = 0
        loss = 0
        total = 0
        for images, labels in test_loader:
            images = images.view(-1, 28 * 28).cuda()
            labels = Variable(labels).cuda()
            outputs = net(images)
            test_loss = criterion(outputs, labels)
            val_loss_log.update(test_loss.data, images.size(0))
            prec1, prec5 = accuracy(outputs.data, labels.data, topk=(1, 5))
            val_acc_log.update(prec1, images.size(0))

        #logger.append([learning_rate, train_loss_log.avg, val_loss_log.avg, train_acc_log.avg, val_acc_log.avg])
        print('Accuracy of the network on the 10000 test images: %.8f %%' %
              (val_acc_log.avg))
        print('Loss of the network on the 10000 test images: %.8f' %
              (val_loss_log.avg))
        training_data['val_loss'].append(
            val_loss_log.avg.detach().cpu().numpy())
        training_data['val_acc'].append(val_acc_log.avg.detach().cpu().numpy())
    #logger.close()
    #logger.plot()
    return training_data