示例#1
0
    def getQoS(self):
        X_test, y_test = self._get_test_data()

        input_size = 32 * 32 * 3
        hidden_size = 110
        num_classes = 10

        net = TwoLayerNet(input_size, hidden_size, num_classes)
        try:
            net.params['W1'] = pickle.load(open(self.run_dir + "model_nn_w1.p",
                                                "rb"),
                                           encoding='latin1')
            net.params['b1'] = pickle.load(open(self.run_dir + "model_nn_b1.p",
                                                "rb"),
                                           encoding='latin1')
            net.params['W2'] = pickle.load(open(self.run_dir + "model_nn_w2.p",
                                                "rb"),
                                           encoding='latin1')
            net.params['b2'] = pickle.load(open(self.run_dir + "model_nn_b2.p",
                                                "rb"),
                                           encoding='latin1')

            test_accuracy = (net.predict(X_test) == y_test).mean()
        except:
            test_accuracy = 0.0
        print("qos", str(test_accuracy))
        return test_accuracy * 100.0
示例#2
0
def init_toy_model():
    np.random.seed(0)
    input_size = 4
    hidden_size = 10
    num_classes = 3
    num_inputs = 5

    return TwoLayerNet(input_size, hidden_size, num_classes, std=1e-1)
def NeuralNet(train_data, train_label, validation_data, validation_label,
              test_data, test_label):
    input_size = 32 * 32 * 3
    hidden_size = 50
    num_classes = 10
    net = TwoLayerNet(input_size, hidden_size, num_classes)

    print train_label.shape
    # Train the network
    stats = net.train(train_data,
                      train_label,
                      validation_data,
                      validation_label,
                      num_iters=4000,
                      batch_size=1500,
                      learning_rate=5e-3,
                      learning_rate_decay=0.96,
                      reg=2,
                      verbose=True,
                      method='adam')

    # Predict on the validation set
    val_acc = (net.predict(validation_data) == validation_label).mean()
    print('Validation accuracy: ', val_acc)

    # Plot the loss function and train / validation accuracies
    plt.subplot(2, 1, 1)
    plt.plot(stats['loss_history'])
    plt.title('Loss history')
    plt.xlabel('Iteration')
    plt.ylabel('Loss')

    plt.subplot(2, 1, 2)
    plt.plot(stats['train_acc_history'], label='train')
    plt.plot(stats['val_acc_history'], label='val')
    plt.title('Classification accuracy history')
    plt.xlabel('Epoch')
    plt.ylabel('Clasification accuracy')
    plt.legend()
    plt.show()

    show_net_weights(net)
示例#4
0
def main(job_id, params):
    print('Training new neural net and calculating validation value job #%d' % job_id)
    np.random.seed(0)  # For repeatability.
    X_train, y_train, X_val, y_val, _, _ = get_CIFAR10_data()
    
    input_size = 32 * 32 * 3
    num_classes = 10
    hidden_size = params['hidden_size']
    lr = params['learning_rate']
    lrdc = params['learning_rate_decay']
    r = params['regularization']
    net = TwoLayerNet(input_size, hidden_size, num_classes) 
    stats = net.train(X_train, y_train, X_val, y_val,
                      num_iters=600, batch_size=200,
                      learning_rate=lr, learning_rate_decay=lrdc,
                      reg=r)

    val_acc = (net.predict(X_val) == y_val).mean()
    print('Validation accuracy: ', val_acc)
    
    # We wish to maximize the validation accuracy, so minimize the negative.
    return -val_acc
示例#5
0
print(X_train_feats.shape)

from cs231n.classifiers.neural_net import TwoLayerNet

input_dim = X_train_feats.shape[1]
hidden_dim = 500
num_classes = 10

best_net = None
best_val = -1
learning_rates = [9e-1,7e-1,5e-1,3e-1,1e-1,9e-2,7e-2,5e-2,3e-2]
regularization_strengths = [1e-3,3e-3,5e-3,7e-3,9e-3]

for lr in learning_rates:
    for reg in regularization_strengths:
        net = TwoLayerNet(input_dim, hidden_dim, num_classes)
        net.train(X_train_feats, y_train, X_val_feats, y_val,
                learning_rate=lr, learning_rate_decay=0.95,
                reg=reg, num_iters=6000, batch_size=200, verbose=False)
       
        train_accuracy = (net.predict(X_train_feats) == y_train).mean()
        val_accuracy = (net.predict(X_val_feats) == y_val).mean()
        print('lr %e reg %e train accuracy: %f val accuracy: %f' % (lr,reg,train_accuracy,val_accuracy))
        if val_accuracy > best_val:
            best_val = val_accuracy
            best_net = net

print('best validation accuracy achieved during cross-validation: %f' % best_val)

test_accuracy = (best_net.predict(X_test_feats) == y_test).mean()
print(test_accuracy)
示例#6
0
def init_toy_model():
    np.random.seed(0)
    return TwoLayerNet(input_size, hidden_size, num_classes, std=1e-1)
示例#7
0
文件: myNN.py 项目: wycjl/CS231n
# 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()
print('Validation accuracy: ', val_acc)



# Plot the loss function and train / validation accuracies
plt.subplot(2, 1, 1)
示例#8
0
#net = TwoLayerNet(input_size, hi_size, num_classes)
#%%
count = 0  # for tracking iterations, visualization, not required for model
#for hi_size in hidden_range:
for lr in learning_range:
    for reg in regularization_range:

        #n = 0
        # lr = learning_range[n]
        #reg = regularization_range[n]

        count += 1  # track how many times we have calculated the results
        print('Calculate %d out of %d' % (count, total_para_sets))

        net = TwoLayerNet(input_size, hi_size, num_classes)
        # Train the network
        stats = net.train(
            X_train_feats0,
            y_train,
            X_val_feats0,
            y_val,
            num_iters=1000,
            batch_size=
            400,  # increase the number of iteration, and batch_size will help to increase number of epoches
            # in the train(), iterations_per_epoch = max(num_train / batch_size, 1), increase batch_size
            # will decrease iterations_per_epoch, and increase number of epoches
            # iterations // iterations_per_epoch, increase the num_iter, will help increase number of epoches
            learning_rate=lr,
            learning_rate_decay=0.95,
            reg=reg,
示例#9
0
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)

# # Train a network
# To train our network we will use SGD with momentum. In addition, we will
# adjust the learning rate with an exponential learning rate schedule as
# optimization proceeds; after each epoch, we will reduce the learning
# rate by multiplying it by a decay rate.

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-3,
                  learning_rate_decay=0.95,
                  reg=0.25,
                  verbose=True)

# Predict on the validation set
val_acc = (net.predict(X_val) == y_val).mean()
示例#10
0
#easily be able to achieve over 55% classification accuracy on the test set;
#our best model achieves about 60% classification accuracy.

# In[ ]:

print(X_train_feats.shape)

# In[ ]:

from cs231n.classifiers.neural_net import TwoLayerNet

input_dim = X_train_feats.shape[1]
hidden_dim = 500
num_classes = 10

net = TwoLayerNet(input_dim, hidden_dim, num_classes)
best_net = None

################################################################################
# TODO: Train a two-layer neural network on image features. You may want to    #
# cross-validate various parameters as in previous sections. Store your best   #
# model in the best_net variable.                                              #
################################################################################
hidden_sizes = [500]
learning_rates = [1e-1, 5e-1, 1]
batch_sizes = [200]
regularization_strengths = [1e-3, 5e-3, 5e-2]
best_val = -1
input_size = X_train_feats.shape[1]
num_classes = 10
best_combination = [-1, -1, -1, -1]
示例#11
0
def init_toy_model():
    np.random.seed(0)
    # 以0为种子对网络参数初始化,每次都会得到同样的W和d
    return TwoLayerNet(input_size, hidden_size, num_classes, std=1e-1)
示例#12
0
num_classes = 10

best_net = None

################################################################################
# TODO: Train a two-layer neural network on image features. You may want to    #
# cross-validate various parameters as in previous sections. Store your best   #
# model in the best_net variable.                                              #
################################################################################
print '-------------_*****--------------'
print 'Tuning Parameters for Neural Nets: '
for lr in np.arange(0.5, 2, 0.1):
    for reg in np.arange(0.001, 0.01, 0.002):
        for hid in hidden_dim:
            print 'At hidden size: %d, LR: %f, and REG: %f' % (hid, lr, reg)
            net_iter = TwoLayerNet(input_dim, hid, num_classes)
            stats_iter = net_iter.train(X_train_feats,
                                        y_train,
                                        X_val_feats,
                                        y_val,
                                        num_iters=3000,
                                        batch_size=200,
                                        learning_rate=lr,
                                        learning_rate_decay=0.95,
                                        reg=reg,
                                        verbose=True)
            y_val_pred_iter = net_iter.predict(X_val_feats)
            y_train_pred_iter = net_iter.predict(X_train_feats)
            val_acc = np.mean(y_val == y_val_pred_iter)
            train_acc = np.mean(y_train == y_train_pred_iter)
            print 'Validation accuracy: %f' % (val_acc)
reg = [0.24, 0.26]

def loguniform(low=0, high=1, size=None): # https://stackoverflow.com/questions/43977717/how-do-i-generate-log-uniform-distribution-in-python
    return 10**(np.random.uniform(low, high, size))

hidden_units = np.random.randint(hidden_units[0], hidden_units[1], num_iters)
learning_rate = loguniform(learning_rate[0], learning_rate[1], num_iters)
reg = np.random.uniform(reg[0], reg[1], num_iters)

input_size = 32 * 32 * 3
num_classes = 10

val_acc_best = -float('inf')

for unit, lr, rg in zip(hidden_units, learning_rate, reg):
    net = TwoLayerNet(input_size, unit, 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=rg, verbose=True)
    

    # Predict on the validation set
    val_acc = (net.predict(X_val) == y_val).mean()
    print('Validation accuracy: ', val_acc)
    
    if val_acc > val_acc_best:
        best_net = net
        val_acc_best = val_acc
示例#14
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()
示例#15
0
def init_toy_model():
    """ generates test model """
    np.random.seed(0)
    return TwoLayerNet(INPUT_SIZE, HIDDEN_SIZE, NUM_CLASSES, std=1e-1)