Ejemplo n.º 1
0
def train_overfit_net(save_net = False):


    # get CIFAR10 data
    data = get_CIFAR10_data()
        
    # subsample the data. 50 draws
    indices_to_select = np.random.randint(0, len(data["X_train"]), 50)

    # extract the samples
    data["X_train"] = data["X_train"][indices_to_select]
    data["y_train"] = data["y_train"][indices_to_select]
    
    # intialize net
    model = FullyConnectedNet([50],
                              input_dim      = 32*32*3,
                              num_classes    = 10,
                              dropout        = 0,
                              reg            = 0.0,
                              weight_scale   = 1e-2)
        
        
    # initialize solver
    solver = Solver(model,data,
                    update_rule  = 'sgd',
                    optim_config = {'learning_rate': 5e-4},
                    lr_decay     = 0.85,
                    num_epochs   = 20,
                    batch_size   = 5,
                    print_every  = 100)
    
    # train the net 
    solver.train()
    
    if save_net:
        
        # test the net and save its training, validation and testing accuracies.

        # get training accuracy
        train_acc = str.format("{0:.2f}", solver.check_accuracy(data["X_train"], data["y_train"]) * 100) + "\%"
        
        # get validation accuracy
        val_acc = str.format("{0:.2f}", solver.best_val_acc * 100) + "\%"
        
        # get testing accuracy
        test_acc = str.format("{0:.2f}", solver.check_accuracy(data["X_test"], data["y_test"]) * 100) + "\%"

        text = "Accuracies: " + train_acc + " training, " + val_acc + " validation  \& " + test_acc + " testing."

        # write to file
        append_to_file("nets/overfit_net/info.tex", text, mode =  "w")
        
        # save net info
        save_net_info("nets/overfit_net", solver)
def train_cifar10_net(save_net=False):
    """
    Uses a Solver instance to train a TwoLayerNet that achieves at least 50% 
    accuracy on the validation set.
    """

    # get CIFAR10 data
    data = get_CIFAR10_data()

    # intialize net
    model = FullyConnectedNet([100],
                              input_dim=32 * 32 * 3,
                              num_classes=10,
                              dropout=0,
                              reg=0.0)

    # initialize solver
    solver = Solver(model,
                    data,
                    update_rule='sgd',
                    optim_config={'learning_rate': 1e-3},
                    lr_decay=0.95,
                    num_epochs=20,
                    batch_size=100,
                    print_every=100)

    # train the net
    solver.train()

    if save_net:

        # test the net and save its training, validation and testing accuracies.
        # get training accuracy
        train_acc = str.format(
            "{0:.2f}",
            solver.check_accuracy(data["X_train"], data["y_train"]) *
            100) + "\%"

        # get validation accuracy
        val_acc = str.format("{0:.2f}", solver.best_val_acc * 100) + "\%"

        # get testing accuracy
        test_acc = str.format(
            "{0:.2f}",
            solver.check_accuracy(data["X_test"], data["y_test"]) * 100) + "\%"

        text = "Accuracies: " + train_acc + " training, " + val_acc + " validation  \& " + test_acc + " testing."

        # write to file
        append_to_file("nets/train_net/info.tex", text, mode="w")

        # save net info
        save_net_info("nets/train_net", solver)
model.mean_image = data['mean_image']
lr = 0.013614446824659357
solver = Solver(model,
                data,
                update_rule='sgd_momentum',
                optim_config={
                    'learning_rate': lr,
                },
                lr_decay=0.8,
                num_epochs=100,
                batch_size=70,
                print_every=100,
                checkpoint_name="intermediate")

solver.train()
acc1 = solver.check_accuracy(data['X_val'], data['y_val'])
acc2 = solver.check_accuracy(data['X_train'], data['y_train'])

#val_acc = solver.best_val_acc
#acc = max(solver.train_acc_history)
#json_log = open("output_test.json", mode='wt', buffering=1)
#json_log.write(json.dumps({'Learning Rate': lr,'accuracy': acc, 'val_acc': val_acc,}) + '\n')

##############################################################################
#                             END OF YOUR CODE                               #
##############################################################################
import matplotlib.pyplot as plt

plt.subplot(2, 1, 1)
plt.title("Training loss")
plt.plot(solver.loss_history, "o")
Ejemplo n.º 4
0
                          num_classes=10,
                          dtype=np.float64)

solver = Solver(model,
                data,
                update_rule='sgd',
                optim_config={
                    'learning_rate': 1e-3,
                },
                lr_decay=0.85,
                num_epochs=30,
                batch_size=65,
                print_every=1000)
solver.train()

acc = solver.check_accuracy(data['X_train'], data['y_train'])
print("Training accuracy {} on {} examples".format(acc, len(data['X_train'])))
acc = solver.check_accuracy(data['X_val'], data['y_val'])
print("validation accuracy {} on {} examples".format(acc, len(data['X_val'])))
acc = solver.check_accuracy(data['X_test'], data['y_test'])
print("Test accuracy {} on {} examples".format(acc, len(data['X_test'])))

import matplotlib.pyplot as plt
plt.subplot(2, 1, 1)
plt.title("Training loss")
plt.plot(solver.loss_history, "o")
plt.xlabel('Iteration')
plt.subplot(2, 1, 2)
plt.title('Accuracy')
plt.plot(solver.train_acc_history, '-o', label='train')
plt.plot(solver.val_acc_history, '-o', label='val')