def calculate_accuracy(X: np.ndarray, targets: np.ndarray, model: SoftmaxModel) -> float: """ Args: X: images of shape [batch size, 785] targets: labels/targets of each image of shape: [batch size, 10] model: model of class SoftmaxModel Returns: Accuracy (float) """ output = model.forward(X) predictions = one_hot_encode(np.array([np.argmax(output, axis=1)]).T, 10) correct_pred = np.count_nonzero(targets * predictions) total_pred = output.shape[0] accuracy = correct_pred / total_pred return accuracy
return loss, accuracy_train, accuracy_val if __name__ == "__main__": # hyperparameters DO NOT CHANGE IF NOT SPECIFIED IN ASSIGNMENT TEXT num_epochs = 50 learning_rate = 0.01 batch_size = 128 l2_reg_lambda = 0 shuffle_dataset = True # Load dataset X_train, Y_train, X_val, Y_val = utils.load_full_mnist() X_train = pre_process_images(X_train) X_val = pre_process_images(X_val) Y_train = one_hot_encode(Y_train, 10) Y_val = one_hot_encode(Y_val, 10) # ANY PARTS OF THE CODE BELOW THIS CAN BE CHANGED. # Intialize model model = SoftmaxModel(l2_reg_lambda) # Train model trainer = SoftmaxTrainer( model, learning_rate, batch_size, shuffle_dataset, X_train, Y_train, X_val,