Skip to content

Commit

Permalink
Used Tensor Constructor in layer initialization + removed exception t…
Browse files Browse the repository at this point in the history
…hrowing.
  • Loading branch information
alejandroarmas committed May 15, 2022
1 parent f29fa39 commit 153059d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 49 deletions.
52 changes: 24 additions & 28 deletions include/network_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@
#define NETWORK_LAYER_H

#include "tensor.h"
#include "tensor_factory.h"
#include "m_algorithms_utilities.h"

#include <cstdint>
#include <memory>
#include <functional>
#include <map>
#include <vector>

#include <iostream>

#define FLAT 1


template<typename T>
const char* getClassName(T) {
return typeid(T).name();
}


namespace NeuralNetwork {

constexpr u_int8_t FLAT = 1;


using namespace NeuralNetwork::Computation::Graph;
Expand Down Expand Up @@ -56,10 +55,9 @@ namespace NeuralNetwork {
class ComputationalStep: public StepInterface {

public:
std::shared_ptr<Tensor> forward(std::shared_ptr<Tensor> input) override {
std::shared_ptr<Tensor> forward(std::shared_ptr<Tensor> input) noexcept override{


// TODO: print, or error checking.
assert(input != nullptr && "Tensor has no data (pointing to null).");

auto out = Impl().doForward(input);

Expand Down Expand Up @@ -92,10 +90,9 @@ namespace NeuralNetwork {
class BinaryOperationStep: public ComputationalStep<BinaryOperationStep<Operation>> {

public:
BinaryOperationStep(Matrix::Rows _l, Matrix::Columns _w) :
matrix(std::make_shared<Tensor>(_l, _w, Computation::Graph::IsTrackable(true), Computation::Graph::IsLeaf(true))) {}
std::shared_ptr<Tensor> doForward(std::shared_ptr<Tensor> input) { return Impl()._doForward(input);}
std::shared_ptr<Tensor> releaseOperand() { return matrix; }
BinaryOperationStep(Matrix::Rows _l, Matrix::Columns _w) noexcept :
matrix(NeuralNetwork::Computation::Graph::TensorConstructor::create(_l, _w, Computation::Graph::IsTrackable(true), Computation::Graph::IsLeaf(true))) {}
std::shared_ptr<Tensor> doForward(std::shared_ptr<Tensor> input) noexcept { return Impl()._doForward(input);}
protected:
std::shared_ptr<Tensor> matrix;
private:
Expand All @@ -108,9 +105,9 @@ namespace NeuralNetwork {
class MatrixMultiplyStep: public BinaryOperationStep<MatrixMultiplyStep> {

public:
MatrixMultiplyStep(Matrix::Rows _l, Matrix::Columns _w) :
MatrixMultiplyStep(Matrix::Rows _l, Matrix::Columns _w) noexcept :
BinaryOperationStep<MatrixMultiplyStep>(_l, _w) {}
std::shared_ptr<Tensor> _doForward(std::shared_ptr<Tensor> input);
std::shared_ptr<Tensor> _doForward(std::shared_ptr<Tensor> input) noexcept;
};


Expand All @@ -124,9 +121,9 @@ namespace NeuralNetwork {
class AddStep: public BinaryOperationStep<AddStep> {

public:
AddStep(Matrix::Columns _w) :
AddStep(Matrix::Columns _w) noexcept :
BinaryOperationStep<AddStep>(Matrix::Rows(FLAT), _w) {}
std::shared_ptr<Tensor> _doForward(std::shared_ptr<Tensor> input);
std::shared_ptr<Tensor> _doForward(std::shared_ptr<Tensor> input) noexcept;
};

/*
Expand All @@ -138,25 +135,27 @@ namespace NeuralNetwork {
class ComposedStep {

public:
void add(std::unique_ptr<StepInterface> layer) {
void add(std::unique_ptr<StepInterface> layer) noexcept {

assert(layer != nullptr && "Layer has no data (pointing to null).");

Impl()._add(std::move(layer));
}
private:
friend Implementation;
Implementation& Impl() { return *static_cast<Implementation*>(this); }
ComposedStep() = default;
Implementation& Impl() noexcept { return *static_cast<Implementation*>(this); }
};


class Layer: public ComputationalStep<Layer>, public ComposedStep<Layer> {

public:
Layer(std::unique_ptr<StepInterface> _w,
std::unique_ptr<StepInterface> _b) :
std::unique_ptr<StepInterface> _b) noexcept :
weights(std::move(_w)), bias(std::move(_b)) {}

std::shared_ptr<Tensor> doForward(std::shared_ptr<Tensor> input);
void _add(std::unique_ptr<StepInterface> layer);
std::shared_ptr<Tensor> doForward(std::shared_ptr<Tensor> input) noexcept;
void _add(std::unique_ptr<StepInterface> layer) noexcept;

private:
std::unique_ptr<StepInterface> weights;
Expand Down Expand Up @@ -197,13 +196,10 @@ namespace NeuralNetwork {
*/
class Sequential: public ComputationalStep<Sequential>, public ComposedStep<Sequential> {
public:
Sequential() : last_key(0) {}
~Sequential() = default;
std::shared_ptr<Tensor> doForward(std::shared_ptr<Tensor> input);
void _add(std::unique_ptr<StepInterface> layer);
std::shared_ptr<Tensor> doForward(std::shared_ptr<Tensor> input) noexcept;
void _add(std::unique_ptr<StepInterface> layer) noexcept;
private:
std::map<const unsigned int, std::unique_ptr<StepInterface>> _modules;
unsigned int last_key;
std::vector<std::unique_ptr<StepInterface>> _modules;

};

Expand Down
30 changes: 9 additions & 21 deletions network_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@ namespace NeuralNetwork {



std::shared_ptr<Tensor> MatrixMultiplyStep::_doForward(std::shared_ptr<Tensor> input) {
std::shared_ptr<Tensor> MatrixMultiplyStep::_doForward(std::shared_ptr<Tensor> input) noexcept {

TensorOp mm(Matrix::Operations::Binary::Multiplication::ParallelDNC{});



auto out = mm(input, this->matrix);

// #if DEBUG
Expand All @@ -33,14 +31,11 @@ namespace NeuralNetwork {



std::shared_ptr<Tensor> AddStep::_doForward(std::shared_ptr<Tensor> input) {

std::shared_ptr<Tensor> AddStep::_doForward(std::shared_ptr<Tensor> input) noexcept {


TensorOp add(Matrix::Operations::Binary::Addition::Std{});



auto z = add(this->matrix, input);


Expand All @@ -53,11 +48,7 @@ namespace NeuralNetwork {
}


std::shared_ptr<Tensor> Layer::doForward(std::shared_ptr<Tensor> input) {

if (input == nullptr) {
throw std::invalid_argument("Matrix has no data (pointing to null).");
}
std::shared_ptr<Tensor> Layer::doForward(std::shared_ptr<Tensor> input) noexcept {


auto out = this->weights->forward(input);
Expand All @@ -69,11 +60,8 @@ namespace NeuralNetwork {
}


void Layer::_add(std::unique_ptr<StepInterface> matrix) {
void Layer::_add(std::unique_ptr<StepInterface> matrix) noexcept {

if (matrix == nullptr) {
throw std::invalid_argument("Matrix has no data (pointing to null).");
}

if (this->weights == nullptr) {
this->weights = std::move(matrix);
Expand All @@ -85,23 +73,23 @@ namespace NeuralNetwork {
}


std::shared_ptr<Tensor> Sequential::doForward(std::shared_ptr<Tensor> input) {
std::shared_ptr<Tensor> Sequential::doForward(std::shared_ptr<Tensor> input) noexcept {

std::shared_ptr<Tensor> current_value = input;

std::for_each(this->_modules.begin(), this->_modules.end(),
[&current_value](std::pair<const unsigned int, std::unique_ptr<StepInterface>>& _layer){
[&current_value](std::unique_ptr<StepInterface>& _layer){

current_value = _layer.second->forward(current_value);
current_value = _layer->forward(current_value);
});


return current_value;
}


void Sequential::_add(std::unique_ptr<StepInterface> layer) {
this->_modules.emplace(this->last_key++, std::move(layer));
void Sequential::_add(std::unique_ptr<StepInterface> layer) noexcept {
this->_modules.emplace_back(std::move(layer));
}

}

0 comments on commit 153059d

Please sign in to comment.