def run_optimized_training(input, output, num_classes, source): X, y = load_data(input, source) X, y = shuffle_data(X, y) ## === Divide training data. === X, y, X_test, y_test = partition_data(X, y, split=.9) classifier, opts, val_accuracy = optimize_hyperparams( X, y, num_classes, HIDDEN_LAYER_OPTS, REGULARIZATION_OPTS, MAX_ITERATION_OPTS) test_accuracy = classifier_accuracy(classifier, X_test, y_test) print() print('===================') print('OPTIMAL PARAMETERS:') print('Hidden layer size: ', opts[0]) print('Regularization value: ', opts[1]) print('Max training iterations: ', opts[2]) print('Accuracy on validation set:', val_accuracy) print('Accuracy on test set:', test_accuracy) print('===================') print() print('Saving out Neural Network weights.') save_classifier(classifier, output)
def visualize_data(input, source, label): # Load Training Data print('> Loading and Visualizing Data ...\n') X, y = load_data(input, source) if label is not None: X = X[y == label, :] y = y[y == label] m = X.shape[0] # Randomly select 100 data points to display sel = random.sample(range(m), 100) print(y[sel].reshape((10, 10), order='F')) display_data(X[sel, :], order='F');
def visualize_data(input, source, label): # Load Training Data print('> Loading and Visualizing Data ...\n') X, y = load_data(input, source) if label is not None: X = X[y == label, :] y = y[y == label] m = X.shape[0] # Randomly select 100 data points to display sel = random.sample(range(m), 100) print(y[sel].reshape((10, 10), order='F')) display_data(X[sel, :], order='F')
def run_training(input, output, image_pixels, regularization, hidden_layer_size, num_classes, max_iterations, source): ## === Load training data. === X, y = load_data(input, source) input_layer_size = image_pixels * image_pixels # NxN input images assert(input_layer_size == X.shape[1]) # Train a classifier seeded with metaparams. trainer = make_trainer(X, y, num_classes) classifier = trainer(hidden_layer_size, regularization, max_iterations) ## === Predict labels for training data === accuracy = classifier_accuracy(classifier, X, y) print('Training Set Accuracy: {}\n'.format(accuracy)); ## == Save weights! == print('Saving out Neural Network weights.\n') save_classifier(classifier, output)
def run_predicton(test_data, weights, hidden_layer_size, num_classes, source): ## === Load and visualize the test data. === X, y = load_data(test_data, source) input_layer_size = X.shape[1] ## === Load NN weights. === print('Loading saved Neural Network parameters ...') classifier = load_classifier(weights) ## === Predict labels for test data === accuracy = classifier_accuracy(classifier, X, y) print('Test Set Accuracy:', accuracy) # Predict value for a random instance. instance = random.randint(0, X.shape[0]) example = (X[instance, :].reshape(20, 20, order='F').round().astype(np.uint8)) print(example) pred = predict(classifier, example.reshape(1, 400, order='F'), 1) print(pred, y[instance])
def convert_data(input, output, source, target): X, y = load_data(input, source) m = X.shape[0] # Load Training Data print('Loading data!') # Randomly select 100 data points to display sel = random.sample(range(m), 100) display_data(X[sel, :], order=FORMAT[target]) # Transpose all images. for i in range(m): pixels = int(math.sqrt(X.shape[1])) image = X[i, :].reshape(pixels, pixels, order=FORMAT[source]) X[i, :] = image.reshape(1, X.shape[1], order=FORMAT[target]) display_data(X[sel, :], order=FORMAT[target]) sio.savemat(output, {'X': X, 'y': y})
def make_one_hot(labels): return (np.arange(num_labels) == labels[:,None]).astype(np.float32) def merge_data(X, y, A, b): print(X.shape, A.shape) print(y.shape, b.shape) return np.vstack((X, A)), np.vstack((y, b)) def accuracy(predictions, labels): return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0]) font_dataset, font_labels = load_data(data_file, 'numpy') font_dataset = font_dataset.astype(np.float32) font_labels = make_one_hot(font_labels) font_dataset, font_labels = shuffle_data(font_dataset, font_labels) train_dataset, train_labels, X, y = partition_data(font_dataset, font_labels, split=.9) valid_dataset, valid_labels, test_dataset, test_labels = partition_data(X, y, split=.5) print('Original set', font_dataset.shape, font_labels.shape) print('Training set', train_dataset.shape, train_labels.shape) print('Validation set', valid_dataset.shape, valid_labels.shape) print('Test set', test_dataset.shape, test_labels.shape) graph = tf.Graph()
def accuracy(predictions, labels): return (100.0 * np.sum(np.argmax(predictions, 1) == np.argmax(labels, 1)) / predictions.shape[0]) def gen_feedforwarder(weights_1, biases_1, weights_2, biases_2): def feedforwarder(data): logits_1 = tf.matmul(data, weights_1) + biases_1 output_1 = tf.nn.relu(logits_1) logits_2 = tf.matmul(output_1, weights_2) + biases_2 return logits_2 return feedforwarder font_dataset, font_labels = load_data(data_file, 'numpy') font_dataset = font_dataset.astype(np.float32) font_labels = make_one_hot(font_labels) font_dataset, font_labels = shuffle_data(font_dataset, font_labels) train_dataset, train_labels, X, y = partition_data(font_dataset, font_labels, split=.9) valid_dataset, valid_labels, test_dataset, test_labels = partition_data(X, y, split=.5) print('Original set', font_dataset.shape, font_labels.shape) print('Training set', train_dataset.shape, train_labels.shape) print('Validation set', valid_dataset.shape, valid_labels.shape) print('Test set', test_dataset.shape, test_labels.shape) graph = tf.Graph()