<reponame>luoshao23/ML_algorithm<filename>Deep_Learning/CNN/opt_NN.py
import numpy as np
import matplotlib.pyplot as plt

from cs231n.classifiers.neural_net import TwoLayerNet

from __future__ import print_function

from cs231n.data_utils import load_CIFAR10

def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=1000):
    """
    Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
    it for the two-layer neural net classifier. These are the same steps as
    we used for the SVM, but condensed to a single function.
    """
    # Load the raw CIFAR-10 data
    cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'
    X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)

    # Subsample the data
    mask = list(range(num_training, num_training + num_validation))
    X_val = X_train[mask]
    y_val = y_train[mask]
    mask = list(range(num_training))
    X_train = X_train[mask]
    y_train = y_train[mask]
    mask = list(range(num_test))
    X_test = X_test[mask]
    y_test = y_test[mask]

    # Normalize the data: subtract the mean image
    mean_image = np.mean(X_train, axis=0)
    X_train -= mean_image
    X_val -= mean_image
    X_test -= mean_image

    # Reshape data to rows
    X_train = X_train.reshape(num_training, -1)
    X_val = X_val.reshape(num_validation, -1)
    X_test = X_test.reshape(num_test, -1)

    return X_train, y_train, X_val, y_val, X_test, y_test


# Invoke the above function to get our data.
X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR10_data()

best_net = None # store the best model into this

#################################################################################
# TODO: Tune hyperparameters using the validation set. Store your best trained  #
# model in best_net.                                                            #
#                                                                               #
# To help debug your network, it may help to use visualizations similar to the  #
# ones we used above; these visualizations will have significant qualitative    #
# differences from the ones we saw above for the poorly tuned network.          #
#                                                                               #
# Tweaking hyperparameters by hand can be fun, but you might find it useful to  #
# write code to sweep through possible combinations of hyperparameters          #
# automatically like we did on the previous exercises.                          #
#################################################################################
# Look for the best net
best_net = None      # store the best model into this
best_val = -100
best_count = None
input_size = 32 * 32 * 3
hidden_size = 256
num_classes = 10
net = TwoLayerNet(input_size, hidden_size, num_classes)


max_count = 100
for count in xrange(1, max_count + 1):
    reg = 10 ** np.random.uniform(-4, 1)
    lr = 10 ** np.random.uniform(-5, -3)
    stats = net.train(X_train, y_train, X_val, y_val, num_iters=200,
                  batch_size=200, mu=0.9, mu_increase=1.0, learning_rate=lr,
                  learning_rate_decay=0.95, reg=reg, verbose=False)
    if best_val < stats['val_acc_history'][-1]:
        best_val = stats['val_acc_history'][-1]
        best_net = net
        best_count = count

#     print('val_acc: %f'%stats['val_acc_history'][-1])

    print('val_acc: %f, lr: %s, reg: %s, %d/%d'%(stats['val_acc_history'][-1], format(lr, 'e'), format(reg, 'e'), count, max_count))


print('The best number is %d' % best_count)
import numpy as np
import matplotlib.pyplot as plt

from cs231n.classifiers.neural_net import TwoLayerNet

from __future__ import print_function

from cs231n.data_utils import load_CIFAR10

def get_CIFAR10_data(num_training=49000, num_validation=1000, num_test=1000):
    """
    Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
    it for the two-layer neural net classifier. These are the same steps as
    we used for the SVM, but condensed to a single function.
    """
    # Load the raw CIFAR-10 data
    cifar10_dir = 'cs231n/datasets/cifar-10-batches-py'
    X_train, y_train, X_test, y_test = load_CIFAR10(cifar10_dir)

    # Subsample the data
    mask = list(range(num_training, num_training + num_validation))
    X_val = X_train[mask]
    y_val = y_train[mask]
    mask = list(range(num_training))
    X_train = X_train[mask]
    y_train = y_train[mask]
    mask = list(range(num_test))
    X_test = X_test[mask]
    y_test = y_test[mask]

    # Normalize the data: subtract the mean image
    mean_image = np.mean(X_train, axis=0)
    X_train -= mean_image
    X_val -= mean_image
    X_test -= mean_image

    # Reshape data to rows
    X_train = X_train.reshape(num_training, -1)
    X_val = X_val.reshape(num_validation, -1)
    X_test = X_test.reshape(num_test, -1)

    return X_train, y_train, X_val, y_val, X_test, y_test


# Invoke the above function to get our data.
X_train, y_train, X_val, y_val, X_test, y_test = get_CIFAR10_data()

best_net = None # store the best model into this

#################################################################################
# TODO: Tune hyperparameters using the validation set. Store your best trained  #
# model in best_net.                                                            #
#                                                                               #
# To help debug your network, it may help to use visualizations similar to the  #
# ones we used above; these visualizations will have significant qualitative    #
# differences from the ones we saw above for the poorly tuned network.          #
#                                                                               #
# Tweaking hyperparameters by hand can be fun, but you might find it useful to  #
# write code to sweep through possible combinations of hyperparameters          #
# automatically like we did on the previous exercises.                          #
#################################################################################
# Look for the best net
best_net = None      # store the best model into this
best_val = -100
best_count = None
input_size = 32 * 32 * 3
hidden_size = 256
num_classes = 10
net = TwoLayerNet(input_size, hidden_size, num_classes)


max_count = 100
for count in xrange(1, max_count + 1):
    reg = 10 ** np.random.uniform(-4, 1)
    lr = 10 ** np.random.uniform(-5, -3)
    stats = net.train(X_train, y_train, X_val, y_val, num_iters=200,
                  batch_size=200, mu=0.9, mu_increase=1.0, learning_rate=lr,
                  learning_rate_decay=0.95, reg=reg, verbose=False)
    if best_val < stats['val_acc_history'][-1]:
        best_val = stats['val_acc_history'][-1]
        best_net = net
        best_count = count

#     print('val_acc: %f'%stats['val_acc_history'][-1])

    print('val_acc: %f, lr: %s, reg: %s, %d/%d'%(stats['val_acc_history'][-1], format(lr, 'e'), format(reg, 'e'), count, max_count))


print('The best number is %d' % best_count)
Load the CIFAR-10 dataset from disk and perform preprocessing to prepare     it for the two-layer neural net classifier. These are the same steps as     we used for the SVM, but condensed to a single function. # Load the raw CIFAR-10 data # Subsample the data # Normalize the data: subtract the mean image # Reshape data to rows # Invoke the above function to get our data. # store the best model into this ################################################################################# # TODO: Tune hyperparameters using the validation set. Store your best trained  # # model in best_net.                                                            # #                                                                               # # To help debug your network, it may help to use visualizations similar to the  # # ones we used above; these visualizations will have significant qualitative    # # differences from the ones we saw above for the poorly tuned network.          # #                                                                               # # Tweaking hyperparameters by hand can be fun, but you might find it useful to  # # write code to sweep through possible combinations of hyperparameters          # # automatically like we did on the previous exercises.                          # ################################################################################# # Look for the best net # store the best model into this #     print('val_acc: %f'%stats['val_acc_history'][-1])