def get_valid_score(train_loader, valid_loader, n_epoch, step_size, weight_decay):
    print("weight_decay: ", weight_decay)
    net = KLayerNeuralNetwork(n_hidden_nodes=[50, 50], p_dropout=0.0, batch_norm=True, batch_norm_momentum=0.7)
    scheduler = CyclicLR(eta_min=1e-5, eta_max=1e-1, step_size=step_size)
    # =========================================================================
    best_valid_acc = -np.inf
    for epoch in range(n_epoch):
        train(train_loader, net, weight_decay, scheduler)
        valid_acc = evaluate(valid_loader, net)
        if valid_acc > best_valid_acc:
            best_valid_acc = valid_acc

        print("[Evaluate] Valid. Acc.: %.3f%%" % (
            valid_acc*100))

    print("[Result] Valid. Acc.: %.3f%%" % (
        best_valid_acc*100))

    return best_valid_acc
Example #2
0
 train_loader = cifar10_DataLoader(train_data, batch_size=batch_size)
 valid_loader = cifar10_DataLoader(valid_data, batch_size=batch_size)
 test_loader = cifar10_DataLoader(test_data, batch_size=batch_size)
 # ==================================================================
 net = TwoLayerNeuralNetwork(n_hidden_nodes=[800], p_dropout=0.0)
 # net = TwoLayerNeuralNetwork(n_hidden_nodes=[768])
 ntrain = train_data['labels'].shape[0]
 n_step_per_cycle = 3
 ncycle = 3
 n_epoch = ncycle * n_step_per_cycle * 2
 iter_per_epoch = int(np.ceil(ntrain / batch_size))
 step_size = n_step_per_cycle * iter_per_epoch
 # weight_decay = 0.004454
 # weight_decay = 0.005
 weight_decay = 0.000
 scheduler = CyclicLR(eta_min=1e-5, eta_max=1e-1, step_size=step_size)
 # =========================================================================
 print("--------- Train Schedule ---------")
 print("ncycle: ", ncycle)
 print("n_epoch: ", n_epoch)
 print("step_size: ", step_size)
 print("iter_per_epoch: ", iter_per_epoch)
 print("n_step_per_cycle: ", n_step_per_cycle)
 print("weight_decay: ", weight_decay)
 print("--------- Train Schedule ---------")
 best_valid_acc = -np.inf
 for epoch in range(n_epoch):
     train(train_loader, net, weight_decay, scheduler)
     valid_acc = evaluate(valid_loader, net)
     test_acc = evaluate(test_loader, net)
     if valid_acc > best_valid_acc: