jacobian

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 0d7741eae359ef5287ac7aa6efabb61feb9c9218
parent 9493a2b16d0c16b70af2f5f5038bc8f0933db345
Author: David Freifeld <freifeld.david@gmail.com>
Date:   Mon, 27 Jul 2020 14:20:50 -0700

Working on more benchmarks

Diffstat:
Abench/mlpack.cpp | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Abench/pytorch.py | 48++++++++++++++++++++++++++++++++++++++++++++++++
Abench/scikit.py | 18++++++++++++++++++
3 files changed, 122 insertions(+), 0 deletions(-)

diff --git a/bench/mlpack.cpp b/bench/mlpack.cpp @@ -0,0 +1,56 @@ +#include <mlpack/core.hpp> +#include <mlpack/methods/ann/layer/layer.hpp> +#include <mlpack/methods/ann/ffn.hpp> +using namespace mlpack; +using namespace mlpack::ann; +int main() +{ + // Load the training set and testing set. + arma::mat trainData; + data::Load("data_banknote_authentication.csv", trainData, true); + arma::mat testData; + data::Load("test.csv", testData, true); + // Split the labels from the training set and testing set respectively. + arma::mat trainLabels = trainData.row(trainData.n_rows - 1); + arma::mat testLabels = testData.row(testData.n_rows - 1); + trainData.shed_row(trainData.n_rows - 1); + testData.shed_row(testData.n_rows - 1); + // Initialize the network. + FFN<> model; + model.Add<Linear<> >(trainData.n_rows, 8); + model.Add<SigmoidLayer<> >(); + model.Add<Linear<> >(8, 3); + model.Add<LogSoftMax<> >(); + // Train the model. + model.Train(trainData, trainLabels); + // Use the Predict method to get the predictions. + arma::mat predictionTemp; + model.Predict(testData, predictionTemp); + /* + Since the predictionsTemp is of dimensions (3 x number_of_data_points) + with continuous values, we first need to reduce it to a dimension of + (1 x number_of_data_points) with scalar values, to be able to compare with + testLabels. + The first step towards doing this is to create a matrix of zeros with the + desired dimensions (1 x number_of_data_points). + In predictionsTemp, the 3 dimensions for each data point correspond to the + probabilities of belonging to the three possible classes. + */ + arma::mat prediction = arma::zeros<arma::mat>(1, predictionTemp.n_cols); + // Find index of max prediction for each data point and store in "prediction" + for (size_t i = 0; i < predictionTemp.n_cols; ++i) + { + // we add 1 to the max index, so that it matches the actual test labels. + prediction(i) = arma::as_scalar(arma::find( + arma::max(predictionTemp.col(i)) == predictionTemp.col(i), 1)) + 1; + } + /* + Compute the error between predictions and testLabels, + now that we have the desired predictions. + */ + size_t correct = arma::accu(prediction == testLabels); + double classificationError = 1 - double(correct) / testData.n_cols; + // Print out the classification error for the testing dataset. + std::cout << "Classification Error for the Test set: " << classificationError << std::endl; + return 0; +} diff --git a/bench/pytorch.py b/bench/pytorch.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +import torch + +# N is batch size; D_in is input dimension; +# H is hidden dimension; D_out is output dimension. +N, D_in, H, D_out = 16, 1000, 6, 10 + +# Create random Tensors to hold inputs and outputs +x = torch.randn(N, D_in) +y = torch.randn(N, D_out) + +# Use the nn package to define our model and loss function. +model = torch.nn.Sequential( + torch.nn.Linear(D_in, H), + torch.nn.ReLU(), + torch.nn.Linear(H, D_out), +) +loss_fn = torch.nn.MSELoss(reduction='sum') + +# Use the optim package to define an Optimizer that will update the weights of +# the model for us. Here we will use Adam; the optim package contains many other +# optimization algorithms. The first argument to the Adam constructor tells the +# optimizer which Tensors it should update. +learning_rate = 1e-4 +optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) +for t in range(500): + # Forward pass: compute predicted y by passing x to the model. + y_pred = model(x) + + # Compute and print loss. + loss = loss_fn(y_pred, y) + if t % 100 == 99: + print(t, loss.item()) + + # Before the backward pass, use the optimizer object to zero all of the + # gradients for the variables it will update (which are the learnable + # weights of the model). This is because by default, gradients are + # accumulated in buffers( i.e, not overwritten) whenever .backward() + # is called. Checkout docs of torch.autograd.backward for more details. + optimizer.zero_grad() + + # Backward pass: compute gradient of the loss with respect to model + # parameters + loss.backward() + + # Calling the step function on an Optimizer makes an update to its + # parameters + optimizer.step() diff --git a/bench/scikit.py b/bench/scikit.py @@ -0,0 +1,18 @@ +from sklearn.neural_network import MLPClassifier +import csv + +with open("./data_banknote_authentication.txt", 'rt') as f: + reader = csv.reader(f) + data = list(reader) + for a in data: + for b, c in enumerate(a): + a[b] = float(a[b]) + +X_train = [] +y_train = [] +for i in data: + X_train.append(data[:-1]) + y_train.append(data[-1]) +print(X_train[-1]) + +clf = MLPClassifier(random_state=1, max_iter=300).fit(X_train, y_train)