if train_labels[i,j] < 0.5: train_labels[i,j] = .05 else: train_labels[i,j] = .95 NN = NeuralNet(train_images, train_labels) _v_learning_rate = 0.1 _w_learning_rate = 0.01 for i in range(12): if (i > 0): data = np.concatenate((train_images, train_labels), axis=1) train_images = data[:, :len(images[0])] train_labels = data[:, len(images[0]):] NN.updateData(train_images, train_labels) if ((i % 6) == 0): _v_learning_rate = 0.9*_v_learning_rate _w_learning_rate = 0.6*_w_learning_rate NN.trainMini(batch_size=25,v_learning_rate=_v_learning_rate, w_learning_rate=_w_learning_rate) f = open('kaggle_submission.csv', 'w') header = 'Id,Category\n' f.write(header) predictions = NN.classifyAll(test_data) for i in range(len(test_data)): predicted_class = predictions[i] S = str(i + 1) + ',' + str(predicted_class) + '\n' f.write(S) f.close()
train_labels[i,j] = 0.1 else: train_labels[i,j] = 0.9 # CREATE NeuralNet Object NN = NeuralNet(train_images, train_labels) # TRAIN NeuralNet Object for i in range(5): if (i > 0): data = np.concatenate((train_images, train_labels), axis=1) np.random.shuffle(data) train_images = data[:, :len(images[0])] train_labels = data[:, len(images[0]):] NN.updateData(train_images, train_labels) NN.trainMini(batch_size=25, v_learning_rate=0.1, w_learning_rate=0.01) correct_list = list() incorrect_list = list() # TRAINING ACCURACY y_hat = NN.classifyAll(train_images) test_correct = 0 test_size = len(NN.images) for i in range(test_size): if (y_hat[i] == np.argmax(NN.labels[i]) + 1): test_correct += 1 else: continue print('\nTest Classfication Complete:') print('Test Set Error: ', 1 - (test_correct / test_size))
def main(batch_size = 25, target_values=[0.05,0.95], epochs=18, v_learning_rate=0.1, w_learning_rate=0.01, v_decay_rate=0.9, w_decay_rate=0.6, decay_frequency=6): _v_learning_rate = v_learning_rate _w_learning_rate = w_learning_rate images = np.load('data/images.npy') labels = np.load('data/vec_labels.npy') data = np.concatenate((images, labels), axis=1) np.random.shuffle(data) train_images = data[:round(.8*len(data)), :len(images[0])] train_labels = data[:round(.8*len(data)), len(images[0]):] validation_images = data[round(.8*len(data)):, :len(images[0])] validation_labels = data[round(.8*len(data)):, len(images[0]):] scaler = skp.StandardScaler() train_images = scaler.fit_transform(train_images) validation_images = scaler.transform(validation_images) # Set target values in our labels matrix(e.g. to 0.15 and 0.85). if target_values is not None: for i in range(len(train_labels[:,0])): for j in range(len(train_labels[0,:])): if train_labels[i,j] < 0.5: train_labels[i,j] = target_values[0] else: train_labels[i,j] = target_values[1] print('\n============\n SETTINGS\n============') print('Batch Size: ', batch_size) print('Target Values: ', target_values) print('Number of Epochs: ', epochs) print('V Learning Rate: ', v_learning_rate) print('W Learning Rate: ', w_learning_rate) print('V Decay Rate: ', v_decay_rate) print('W Decay Rate: ', w_decay_rate) print('Decay Frequency: ', decay_frequency) NN = NeuralNet(train_images, train_labels) for i in range(epochs): if (i > 0): data = np.concatenate((train_images, train_labels), axis=1) np.random.shuffle(data) train_images = data[:, :len(images[0])] train_labels = data[:, len(images[0]):] NN.updateData(train_images, train_labels) if ((i % decay_frequency) == 0): _v_learning_rate = v_decay_rate*_v_learning_rate _w_learning_rate = w_decay_rate*_w_learning_rate NN.trainMini(batch_size=batch_size,v_learning_rate=_v_learning_rate, w_learning_rate=_w_learning_rate) print('\n============\n EPOCH:', i, '\n============') # TRAINING ACCURACY y_hat = NN.classifyAll(train_images) test_correct = 0 test_size = len(NN.images) for j in range(test_size): if (y_hat[j] == np.argmax(NN.labels[j]) + 1): test_correct += 1 else: continue print('\nTest Classfication Complete:') print('Test Set Error: ', 1 - (test_correct / test_size)) # VALIDATION ACCURACY total_correct = 0 validation_size = len(validation_images) z = NN.classifyAll(validation_images) for j in range(len(z)): if (z[j] == np.argmax(validation_labels[j]) + 1): total_correct += 1 else: continue print('\nValidation Classification Complete:') print('Validation Error Rate: ', 1 - (total_correct/validation_size), '\n')
# Set target values in our labels matrix(e.g. to 0.15 and 0.85). for i in range(len(train_labels[:,0])): for j in range(len(train_labels[0,:])): if train_labels[i,j] < 0.5: train_labels[i,j] = .05 else: train_labels[i,j] = .95 NN = NeuralNet(train_images, train_labels) for i in range(8): if (i > 0): data = np.concatenate((train_images, train_labels), axis=1) train_images = data[:, :len(images[0])] train_labels = data[:, len(images[0]):] NN.updateData(train_images, train_labels) NN.trainMini(batch_size=50,v_learning_rate=0.1, w_learning_rate=0.01, v_decay_rate=1, w_decay_rate=1) f = open('kaggle_submission.csv', 'w') header = 'Id,Category\n' f.write(header) predictions = NN.classifyAll(test_data) for i in range(len(test_data)): predicted_class = predictions[i] S = str(i + 1) + ',' + str(predicted_class) + '\n' f.write(S) f.close()