def best_nn(X_train, y_train, X_val, y_val, input_size, num_classes):
    best_net = None
    # Define discrete hyperparameters to sweep through
    hidden_size = [40, 60, 80, 100, 120]
    learning_rate = [1e-4, 5e-4, 1e-3, 5e-3]
    reg = [0.2, 0.4, 0.6]
    best_acc = -1

    log = {}

    for hs in hidden_size:
        for lr in learning_rate:
            for r in reg:

                # Set up the network
                net = TwoLayerNet(input_size, hs, num_classes)

                # Train the network
                stats = net.train(X_train,
                                  y_train,
                                  X_val,
                                  y_val,
                                  num_iters=1000,
                                  batch_size=200,
                                  learning_rate=lr,
                                  learning_rate_decay=0.95,
                                  reg=r,
                                  verbose=False)

                acc = stats['val_acc_history'][-1]
                log[(hs, lr, r)] = acc

                # Print Log
                print('for hs: %e, lr: %e and r: %e, valid accuracy is: %f' %
                      (hs, lr, r, acc))

                if acc > best_acc:
                    best_net = net
                    best_acc = acc

    print('Best Networks has an Accuracy of: %f' % best_acc)
 def __evaluate(self, param):
     print 'parameters: ', param
     net = TwoLayerNet(self.input_size, param['hidden_size'], self.num_classes)
     # Train the network
     stats = net.train(self.X_train, self.y_train, self.X_val, self.y_val,
                 num_iters=param['num_iters'], batch_size=param['batch_size'],
                 learning_rate=param['learning_rate'], learning_rate_decay=0.95,
                 reg=param['reg'], verbose=True)
     # Predict on the validation set
     trn_acc = (net.predict(self.X_train) == self.y_train).mean()
     val_acc = (net.predict(self.X_val) == self.y_val).mean()
     print 'Validation accuracy: ', val_acc
     result = {}
     result['params'] = param
     result['trn_acc'] = trn_acc
     result['val_acc'] = val_acc
     self.results.append(result)
     if val_acc > self.best_val_acc:
         self.best_param = param
         self.best_net = net
         self.best_val_acc = val_acc
def init_toy_model():
    np.random.seed(0)  #seed tofor repeatability
    return TwoLayerNet(input_size, hidden_size, num_classes, std=1e-1)
Example #4
0
def init_toy_model():
    np.random.seed(0)
    return TwoLayerNet(input_size, hidden_size, num_classes, std=1e-1)
Example #5
0
    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()
print('Train data shape: ', X_train.shape)
print('Train labels shape: ', y_train.shape)
print('Validation data shape: ', X_val.shape)
print('Validation labels shape: ', y_val.shape)
print('Test data shape: ', X_test.shape)
print('Test labels shape: ', y_test.shape)

input_size = 32 * 32 * 3
hidden_size = 50
num_classes = 10
net = TwoLayerNet(input_size, hidden_size, num_classes)

# Train the network
stats = net.train(X_train,
                  y_train,
                  X_val,
                  y_val,
                  num_iters=1000,
                  batch_size=200,
                  learning_rate=1e-4,
                  learning_rate_decay=0.95,
                  reg=0.25,
                  verbose=True)

# Predict on the validation set
val_acc = (net.predict(X_val) == y_val).mean()
Example #6
0
from util.read_mat import ReadMat
from classifiers.neural_net import TwoLayerNet
import matplotlib.pyplot as plt

myReader = ReadMat('data/homework.mat')
X, y, X_val, y_val = myReader.getData()

input_size = 2
hidden_size = 1
output_size = 2

net = TwoLayerNet(input_size, hidden_size, output_size)

stats = net.train(X, y, X, y, learning_rate=1e-1, num_iters=1000)
print('Final training loss: {}'.format(stats['loss_history'][-1]))

# plot the loss history
plt.plot(stats['loss_history'])
plt.xlabel('iteration')
plt.ylabel('training loss')
plt.title('Training Loss history')
plt.show()

plt.plot(stats['train_acc_history'])
plt.xlabel('iteration')
plt.ylabel('training accuracy')
plt.title('Training accuracy history')
plt.show()

plt.plot(stats['val_acc_history'])
plt.xlabel('iteration')