def main(): from keras.datasets import mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() # data preprocessing for neural network with fully-connected layers data = { 'X_train': np.array(x_train[:55000], np.float32).reshape( (55000, -1)), # training data 'y_train': np.array(y_train[:55000], np.int32), # training labels 'X_val': np.array(x_train[55000:], np.float32).reshape( (5000, -1)), # validation data 'y_val': np.array(y_train[55000:], np.int32), # validation labels } model = softmax.SoftmaxClassifier(hidden_dim=100) #breakpoint() # data preprocessing for neural network with convolutional layers #data = { # 'X_train': np.array(x_train[:55000], np.float32).reshape((55000, 1, 28, 28)), # training data # 'y_train': np.array(y_train[:55000], np.int32), # training labels # 'X_val': np.array(x_train[55000:], np.float32).reshape((5000, 1, 28, 28)), # validation data # 'y_val': np.array(y_train[55000:], np.int32), # validation labels #} #model = cnn.ConvNet(hidden_dim=100) # the update rule of 'adam' can be used to replace 'sgd' if it is helpful. solver = Solver(model, data, update_rule='sgd', optim_config={ 'learning_rate': 1e-3, }, lr_decay=0.95, num_epochs=10, batch_size=100, print_every=10) solver.train() # Plot the training losses plt.plot(solver.loss_history) plt.xlabel('Iteration') plt.ylabel('Loss') plt.title('Training loss history') plt.show() plt.savefig('loss.png') plt.close() test_acc = solver.check_accuracy(X=np.array(x_test, np.float32).reshape( (10000, -1)), y=y_test) #test_acc = solver.check_accuracy(X=np.array(x_test, np.float32).reshape((10000, 1, 28, 28)), y=y_test) print('Test accuracy', test_acc)
def main(): N = 100 # number of points per class num_dims = 2 # dimensionality num_classes = 3 # number of classes X, y = data_generator.generate_spiral_data(N, num_dims, num_classes, seed=0) # Hyperparameters learning_rate = 1e-0 regularization_strength = 1e-3 num_epochs = 200 # Train a Linear Classifier softmax_cl = softmax.SoftmaxClassifier(num_dims, num_classes, num_epochs, learning_rate, regularization_strength) softmax_cl.train(X, y, print_loss=10) # Evaluate training set accuracy predicted_class = softmax_cl.predict(X) print('Training accuracy: {0}'.format(np.mean(predicted_class == y))) # Plot the resulting classifier softmax_cl.plot_classification_surfaces(X, y) softmax_cl.plot_loss()
images = np.asarray(images) labels = np.asarray(labels) N, D = images.shape training_per = 0.75 training_len = int(training_per * N) train_data = images[:training_len] train_tar = labels[:training_len] test_data = images[training_len:] test_tar = labels[training_len:] data = { 'X_train': train_data, # training data 'y_train': train_tar, # training labels 'X_val': train_data, # validation data 'y_val': train_tar # validation labels } model = soft.SoftmaxClassifier(hidden_dim=100, reg=0.0) solver = Solver(model, data, update_rule='sgd', optim_config={ 'learning_rate': 2e-5, }, lr_decay=0.95, num_epochs=10, batch_size=100, print_every=1) solver.train()
data = pickle.load(f, encoding='latin1') print(data[0][0].shape) x_test = data[2][0] y_test = data[2][1] data_input = { 'X_train': data[0][0], 'y_train': data[0][1], 'X_val': data[1][0], 'y_val': data[1][1] # validation labels } #model = softmax.SoftmaxClassifier(input_dim=784, num_classes=10,reg=0.02) model = softmax.SoftmaxClassifier(input_dim=784, hidden_dim=256, num_classes=10, reg=0.02) solver = solver.Solver(model, data_input, update_rule='sgd_momentum', optim_config={ 'learning_rate': 4e-1, "momentum": 0.8, 'velocity': 0 }, lr_decay=0.98, num_epochs=20, batch_size=100, print_every=1000) solver.train()