Esempio n. 1
0
def main(controller, cosine_annealing_scheduler):
    running_reward = 0
    child = Child(dropout_rate=float(args.dropout),
                  use_auxiliary=args.use_auxiliary).to(device)

    child_optimizer = optim.SGD(child.parameters(),
                                lr=0.05,
                                momentum=0.9,
                                weight_decay=1e-4,
                                nesterov=True)
    cosine_lr_scheduler = cosine_annealing_scheduler(child_optimizer, lr=0.05)

    for epoch in range(150):
        cosine_lr_scheduler.step()
        child = train_child(epoch, controller, child, child_optimizer)
        torch.save(child.state_dict(), './save_model/child.pth')
        torch.save(controller.state_dict(), './save_model/controller.pth')

        if epoch < 150 - 1:
            controller_model, running_reward = train_controller(
                controller, child, running_reward)

    final_model = model
    final_acc = test_final_model(final_model)
    print('training is done. Final accuarcy is ', final_acc)
        if batch_idx % 10 == 0:
            print('train child epoch : {} [{}/{}]| loss: {:.3f} | acc: {:.3f}'.
                  format(epoch, batch_idx, len(train_loader),
                         train_loss / (batch_idx + 1), 100. * correct / total))

    child.eval()

    test_loss = 0
    correct = 0
    total = 0

    with torch.no_grad():
        for batch_idx, (inputs, targets) in enumerate(test_loader):
            inputs = inputs.to(device)
            targets = targets.to(device)
            outputs, aux_outs = child(inputs, normal_arc, reduction_arc)
            loss = criterion(outputs, targets)

            test_loss += loss.item()
            _, predicted = outputs.max(1)
            total += targets.size(0)
            correct += predicted.eq(targets).sum().item()

    acc = 100 * correct / total
    print('epoch : [{}/310]| acc: {:.3f}'.format(epoch, acc))

    if acc >= best_acc:
        print('best accuracy is ', best_acc)
        best_acc = acc
        torch.save(child.state_dict(), './save_model/best_child.pt')