Skip to content

Commit

Permalink
Driver program shows example NN call interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandroarmas committed Apr 11, 2022
1 parent 966c00f commit f88489f
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,57 @@
#include "matrix.h"
#include "generator.h"
#include "matrix_printer.h"
// #include "network_layer.h"
#include "network_layer.h"
#include "activation_functions.h"
#include "m_algorithms.h"
#include "matrix_benchmark.h"
#include "context_object.h"


int main(void) {
/*
using matrix_t = Matrix::Representation;
model = nn.Sequential(
nn.Linear(28*28, 512),
nn.ReLU(),
nn.Linear(512, 512),
nn.ReLU(),
nn.Linear(512, 10),
)
std::unique_ptr<matrix_t> ma = std::make_unique<matrix_t>(4000, 4000);
std::unique_ptr<matrix_t> mb = std::make_unique<matrix_t>(4000, 4000);
X = torch.rand(1, 28, 28, device=device)
logits = model(X)
pred_probab = nn.Softmax(dim=1)(logits)
*/

Matrix::Generation::Normal<0, 1> normal_distribution_init;
int main(void) {

using matrix_t = Matrix::Representation;

std::unique_ptr<matrix_t> ma = std::make_unique<matrix_t>(Matrix::Rows(1), Matrix::Columns(2000));
Matrix::Generation::Normal<0, 1> normal_distribution_init;
ma = normal_distribution_init(std::move(ma));
mb = normal_distribution_init(std::move(mb));


std::unique_ptr<Matrix::Operations::Multiplication::RecursiveParallel> mul_ptr_r = std::make_unique<Matrix::Operations::Multiplication::RecursiveParallel>();
std::unique_ptr<Matrix::Operations::Multiplication::Square> mul_ptr_s = std::make_unique<Matrix::Operations::Multiplication::Square>();
std::unique_ptr<Matrix::Operations::Multiplication::Naive> mul_ptr_n = std::make_unique<Matrix::Operations::Multiplication::Naive>();
NeuralNetwork::Sequential model;

model.add(std::make_unique<NeuralNetwork::Layer>(
std::make_unique<NeuralNetwork::MatrixMultiplyStep>(Matrix::Rows(2000), Matrix::Columns(1000), normal_distribution_init),
std::make_unique<NeuralNetwork::AddStep>(Matrix::Columns(1000), normal_distribution_init)
));
model.add(std::make_unique<NeuralNetwork::ActivationFunctions::ReLU>());
model.add(std::make_unique<NeuralNetwork::Layer>(
std::make_unique<NeuralNetwork::MatrixMultiplyStep>(Matrix::Rows(1000), Matrix::Columns(10), normal_distribution_init),
std::make_unique<NeuralNetwork::AddStep>(Matrix::Columns(10), normal_distribution_init)
));
model.add(std::make_unique<NeuralNetwork::ActivationFunctions::ReLU>());


Matrix::Benchmark mul_bm_r(std::move(mul_ptr_r));
Matrix::Benchmark mul_bm_s(std::move(mul_ptr_s));
Matrix::Benchmark mul_bm_n(std::move(mul_ptr_n));
std::unique_ptr<matrix_t> mc = mul_bm_n(ma, mb);
std::unique_ptr<matrix_t> me = mul_bm_s(ma, mb);
std::unique_ptr<matrix_t> mf = mul_bm_r(ma, mb);

// Matrix::Printer p;

// p(*ma);
// p(*mb);
// p(*mc);
auto out = model.forward(std::move(ma));

// NeuralNetwork::Computation::Tree::ComputeOperation handler;
// handler.setNextHandler(std::make_unique<NeuralNetwork::Computation::Tree::TimerHandler>());
// handler.setNextHandler(std::make_unique<NeuralNetwork::Computation::Tree::CreateAutoDiffNode>());
// handler.handle("test");


return 0;
Expand Down

0 comments on commit f88489f

Please sign in to comment.