def train(): """ Performs training and evaluation of MLP model. TODO: Implement training and evaluation of MLP model. Evaluate your model on the whole test set each eval_freq iterations. """ ### DO NOT CHANGE SEEDS! # Set the random seeds for reproducibility np.random.seed(42) ## Prepare all functions # Get number of units in each hidden layer specified in the string such as 100,100 if FLAGS.dnn_hidden_units: dnn_hidden_units = FLAGS.dnn_hidden_units.split(",") dnn_hidden_units = [ int(dnn_hidden_unit_) for dnn_hidden_unit_ in dnn_hidden_units ] else: dnn_hidden_units = [] ######################## # PUT YOUR CODE HERE # ####################### data = cifar10_utils.get_cifar10(FLAGS.data_dir) n_inputs = 3 * 32 * 32 n_classes = 10 model = MLP(n_inputs, dnn_hidden_units, n_classes) loss_fn = CrossEntropyModule() max_accuracy = 0.0 start_time = time.perf_counter() for step in range(1, FLAGS.max_steps + 1): x, targets = data['train'].next_batch(FLAGS.batch_size) input = x.reshape((FLAGS.batch_size, -1)) predictions = model.forward(input) gradient = loss_fn.backward(predictions, targets) model.backward(gradient) model.step(FLAGS.learning_rate) if step == 1 or step % FLAGS.eval_freq == 0: training_loss = loss_fn.forward(predictions, targets) test_predictions = model.forward(data['test'].images.reshape( data['test'].num_examples, -1)) test_loss = loss_fn.forward(test_predictions, data['test'].labels) test_acc = accuracy(test_predictions, data['test'].labels) if test_acc > max_accuracy: max_accuracy = test_acc print( "step %d/%d: training loss: %.3f test loss: %.3f accuracy: %.1f%%" % (step, FLAGS.max_steps, training_loss, test_loss, test_acc * 100)) time_taken = time.perf_counter() - start_time print("Done. Scored %.1f%% in %.1f seconds." % (max_accuracy * 100, time_taken))
def train(): """ Performs training and evaluation of MLP model. TODO: Implement training and evaluation of MLP model. Evaluate your model on the whole test set each eval_freq iterations. """ ### DO NOT CHANGE SEEDS! # Set the random seeds for reproducibility np.random.seed(42) ## Prepare all functions # Get number of units in each hidden layer specified in the string such as 100,100 if FLAGS.dnn_hidden_units: dnn_hidden_units = FLAGS.dnn_hidden_units.split(",") dnn_hidden_units = [ int(dnn_hidden_unit_) for dnn_hidden_unit_ in dnn_hidden_units ] else: dnn_hidden_units = [] # Preparation for training print('- Init parameters') data = cifar10_utils.get_cifar10(FLAGS.data_dir) train_data = data['train'] test_data = data['test'] w, h, d = train_data.images[0].shape n_classes = train_data.labels[0].shape[0] criterion = CrossEntropyModule() model = MLP(w * h * d, dnn_hidden_units, n_classes) train_losses = [] test_losses = [] accuracies = [] print('- Start training') for step in range(FLAGS.max_steps): x_batch, x_labels = train_data.next_batch(FLAGS.batch_size) x = x_batch.reshape((FLAGS.batch_size, -1)) predictions = model.forward(x) gradient = criterion.backward(predictions, x_labels) model.backward(gradient) model.step(FLAGS.learning_rate) if step % FLAGS.eval_freq == 0 or step == FLAGS.max_steps - 1: print(' - Step: {}'.format(step)) loss = criterion.forward(predictions, x_labels) out_test = model.forward( test_data.images.reshape(test_data.num_examples, -1)) test_loss = criterion.forward(out_test, test_data.labels) acc = accuracy(out_test, test_data.labels) train_losses.append(loss) test_losses.append(test_loss) accuracies.append(acc) # Save stuff print(accuracies[-1])