Skip to content

Commit 0c39fe8

Browse files
Merge pull request #7 from Krymancer/master
Adding a simple feed foward neural network
2 parents 0eb1234 + d2577b3 commit 0c39fe8

File tree

11 files changed

+1056
-0
lines changed

11 files changed

+1056
-0
lines changed

NeuralNetwork/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.vscode
2+
*
3+
!/**/
4+
!*.*
5+
*.in
6+
*.out
7+
*.o
8+
!Makefile

NeuralNetwork/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
CC=g++
2+
IPATH=-Iinc/
3+
SRC=src
4+
OBJ=obj
5+
BIN=bin
6+
FLAGS=-std=c++2a
7+
8+
SRC_FILES=$(wildcard $(SRC)/*.cpp)
9+
OBJ_FILES=$(patsubst $(SRC)/%.cpp,$(OBJ)/%.o,$(SRC_FILES))
10+
11+
all: app
12+
@echo "Sucess"
13+
14+
clean:
15+
@rm -rf $(OBJ)*.o $(BIN)*
16+
17+
run:
18+
@$(BIN)/app
19+
20+
dev: app
21+
@$(BIN)/app
22+
23+
app: $(OBJ_FILES)
24+
@$(CC) -o $(BIN)/$@ $^ $(FLAGS)
25+
26+
$(OBJ)/%.o: $(SRC)/%.cpp
27+
@$(CC) $(IPATH) $(FLAGS) -c -o $@ $<

NeuralNetwork/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Simple Cpp Neural Network
2+
3+
Simple Cpp Neural Network
4+
5+
## Commands:
6+
7+
Build the project and put the binary in ./bin
8+
9+
make
10+
11+
Build and run the project
12+
13+
make dev
14+
15+
Only run the project
16+
17+
make run

NeuralNetwork/bin/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

NeuralNetwork/inc/Matrix.hpp

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#pragma once
2+
#include <stdlib.h>
3+
4+
#include <cmath>
5+
#include <fstream>
6+
#include <iostream>
7+
#include <random>
8+
#include <sstream>
9+
#include <string>
10+
#include <tuple>
11+
#include <vector>
12+
13+
typedef double (*functioncall)(double args);
14+
15+
using std::tuple;
16+
using std::vector;
17+
18+
class Matrix {
19+
private:
20+
unsigned rows;
21+
unsigned cols;
22+
vector<vector<double>> data;
23+
24+
public:
25+
/**
26+
* Create a Matrix and set an initial value to
27+
* every element in the matrix
28+
*
29+
* @param rows number of Rows
30+
* @param cols number of Columns
31+
* @param value An initial value to every element
32+
*
33+
* @returns Returns a matrix
34+
*/
35+
Matrix(unsigned, unsigned, double);
36+
37+
/**
38+
* Create a Matrix
39+
*
40+
* @param rows number of Rows
41+
* @param cols number of Columns
42+
*
43+
* @returns Returns a matrix
44+
*/
45+
Matrix(unsigned, unsigned);
46+
47+
/**
48+
* Create a Matrix from a file
49+
*
50+
* @param filepath read a file and transform into a matrix
51+
*
52+
* @returns Returns a matrix
53+
*/
54+
Matrix(const char *);
55+
56+
/**
57+
* Create a matrix from another matrix
58+
*
59+
* @param matrix an matrix to copy
60+
*
61+
* @returns Returns the copied matrix
62+
*/
63+
Matrix(const Matrix &);
64+
65+
/**
66+
* Create a matrix from another matrix
67+
*
68+
* @param matrix an matrix to copy
69+
*
70+
* @returns Returns the copied matrix
71+
*/
72+
Matrix(vector<double>);
73+
74+
/**
75+
* Create an empty matrix with no rows and no columns
76+
*
77+
* @returns Returns an empty matrix
78+
*/
79+
Matrix();
80+
81+
// Matrix Operations
82+
Matrix operator+(Matrix &);
83+
Matrix operator-(Matrix &);
84+
Matrix operator*(Matrix &);
85+
Matrix operator+=(Matrix &);
86+
Matrix operator-=(Matrix &);
87+
Matrix operator*=(Matrix &);
88+
Matrix transpose();
89+
Matrix hadamard(Matrix &);
90+
Matrix kronecker(Matrix &);
91+
Matrix horizontalConcatenation(Matrix &);
92+
93+
// Scalar Operations
94+
Matrix operator+(double);
95+
Matrix operator-(double);
96+
Matrix operator*(double);
97+
Matrix operator/(double);
98+
Matrix operator+=(double);
99+
Matrix operator-=(double);
100+
Matrix operator*=(double);
101+
Matrix operator/=(double);
102+
103+
// Aesthetic Methods
104+
double &operator()(const unsigned &, const unsigned &);
105+
void print() const;
106+
friend std::ostream &operator<<(std::ostream &os, const Matrix &matrix);
107+
unsigned getRows() const;
108+
unsigned getCols() const;
109+
vector<double> toArray();
110+
111+
// Power Iteration
112+
tuple<Matrix, double, int> powerIter(unsigned, double);
113+
114+
// Deflation
115+
Matrix deflation(Matrix &, double &);
116+
117+
//Randomize
118+
void randomize();
119+
120+
//Activation Function
121+
Matrix sigmod();
122+
Matrix dSigmod();
123+
124+
Matrix hyperbolicTangent();
125+
Matrix dHyperbolicTangent();
126+
127+
// Map
128+
Matrix map(functioncall);
129+
};

NeuralNetwork/inc/NeuralNetwork.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#pragma once
2+
3+
#include <cmath>
4+
#include <vector>
5+
6+
#include "Matrix.hpp"
7+
8+
class NeuralNetwork {
9+
private:
10+
int input_nodes;
11+
int hidden_nodes;
12+
int output_nodes;
13+
Matrix weights_ih;
14+
Matrix weights_ho;
15+
Matrix bias_h;
16+
Matrix bias_o;
17+
18+
double learningRate = 0.1;
19+
20+
public:
21+
NeuralNetwork(const NeuralNetwork &);
22+
NeuralNetwork(unsigned, unsigned, unsigned);
23+
NeuralNetwork();
24+
25+
unsigned getInputNodes();
26+
unsigned getHiddenNodes();
27+
unsigned getOutputNodes();
28+
29+
Matrix getWeightsIh();
30+
Matrix getWeightsHo();
31+
32+
Matrix getHiddenBias();
33+
Matrix getOutputBias();
34+
35+
void setLearningRate(double);
36+
37+
void train(vector<double>, vector<double>);
38+
vector<double> predict(vector<double>);
39+
void mutate(functioncall);
40+
41+
NeuralNetwork& copy();
42+
43+
//Tests
44+
void print();
45+
};

NeuralNetwork/inc/main.hpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <random>
5+
#include <istream>
6+
#include <string>
7+
#include <vector>
8+
9+
#include "NeuralNetwork.hpp"
10+
11+
#define nl std::cout << std::endl;
12+
13+
double relu(double x) {
14+
return x > 0 ? x : 0;
15+
}
16+
17+
double dtanh(double value) {
18+
return -0.5 * ((tanh(value) - 1) * (tanh(value) + 1));
19+
}
20+
21+
const char* message = "CPP-PROJECT TEMPLATE";

NeuralNetwork/obj/main.obj

2.68 KB
Binary file not shown.

0 commit comments

Comments
 (0)