Skip to content

Commit

Permalink
Commit Zero
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco086 committed Feb 13, 2016
0 parents commit fe4ecbf
Show file tree
Hide file tree
Showing 35 changed files with 2,821 additions and 0 deletions.
78 changes: 78 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# -- Mac OS files (from github's Global/OSX.gitignore) --
.DS_Store
.AppleDouble
.LSOverride
# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear on external disk
.Spotlight-V100
.Trashes
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# -- Eclipse files (from github's Global/Eclipse.gitignore) --
*.pydevproject
.metadata
.gradle
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
# External tool builders
.externalToolBuilders/
# Locally stored "Eclipse launch configurations"
*.launch
# CDT-specific
.cproject
# PDT-specific
.buildpath
# sbteclipse plugin
.target
# TeXlipse plugin
.texlipse

# -- Xcode files (from github's Global/Xcode.gitignore) --
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.xcuserstate

# -- project specific ignores --
lib*

src/*.o

debug/*.o
debug/exe

examples/*/a.out
examples/*/*.o

doc/*.aux
doc/*.dvi
doc/*.idx
doc/*.log
doc/*.out
doc/*.ps
doc/*.gz
6 changes: 6 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FeedForwardNeuralNetwork is free software, released under the GNU General Public License.
See http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.

If any work done using FeedForwardNeuralNetwork is released/published, I would greatly appreciate if:
1) I am informed about it by email;
2) an explicit acknowledgment with a link to the GitHub repository is included.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#FeedForwardNeuralNetwork

C++ Library for building and using a Feed Forward Neural Network.
It includes first and second derivatives in respect to the input values, and first derivatives in respect to the variational parameters.

In `doc/` there is a user manual in pdf.

In `examples/` there are several examples (STILL TO BE DONE).



#Build the library

Insert the system parameters in the `config.sh` and then simply execute the command

`./build.sh`

21 changes: 21 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

source config.sh

\rm -f *.so
cd src/
\rm -f *.o *.so
echo "$CC $FLAGS $OPTFLAGS -fpic -c *.cpp"
$CC $FLAGS $OPTFLAGS -fpic -c *.cpp
echo "$CC $FLAGS $OPTFLAGS -shared -o lib${LIBNAME}.so *.o"
$CC $FLAGS $OPTFLAGS -shared -o lib${LIBNAME}.so *.o
mv lib${LIBNAME}.so ../
cd ..

echo
echo "Library ready!"
echo
echo "Help, how can I use it?"
echo "1) $CC -I$(pwd)/src/ -c example.cpp"
echo " $CC -L$(pwd) example.o -l${LIBNAME}"
echo "2) $CC -I$(pwd)/src/ -L$(pwd) example.cpp -l${LIBNAME}"
15 changes: 15 additions & 0 deletions config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

# Library name
LIBNAME="ffnn"

# C++ compiler
CC="g++"
#CC="g++p-5"

# C++ flags (std=c++11 is necessary)
FLAGS="-std=c++11 -Wall -Werror"

# Optimization flags
OPTFLAGS="-O3"

4 changes: 4 additions & 0 deletions debug/FFNN.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
4
id_actf 2 log_actf 5 log_actf 3 id_actf 2
1.27868 1.12922 -1.52124 -1.68274 -2.0485 -2.99628 0.58088 -1.28258 -0.443995 2.96736 -1.42395 1.09417 -2.77795 2.95594 -2.83344 0.566994 1.71308 0.454561 -2.68739 1.52762 0.74647
0 0 1
194 changes: 194 additions & 0 deletions debug/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
#include <iostream>
#include <cmath>
#include <fstream>

#include "FeedForwardNeuralNetwork.hpp"


void printNNStructure(FeedForwardNeuralNetwork &nn)
{
using namespace std;

for (int i=0; i<nn.getNLayers(); ++i)
{
cout << "Layer " << i << " has " << nn.getLayerSize(i) << " units" << endl;
}
cout << endl;
}

void printNNValues(FeedForwardNeuralNetwork &nn)
{
using namespace std;

for (int i=0; i<nn.getNLayers(); ++i)
{
cout << "--- Layer " << i << endl;
cout << "Value: ";
for (int j=0; j<nn.getLayer(i)->getNUnits(); ++j)
{
cout << nn.getLayer(i)->getUnit(j)->getValue() << " ";
}
cout << endl;
cout << "First Derivative: ";
for (int j=0; j<nn.getLayer(i)->getNUnits(); ++j)
{
cout << nn.getLayer(i)->getUnit(j)->getFirstDerivativeValue(0) << " ";
}
cout << endl << endl;
}

cout << "Variational parameters: ";
for (int j=0; j<nn.getNVariationalParameters(); ++j)
{
cout << nn.getVariationalParameter(j) << " ";
}
cout << endl << endl;
}


int main(){
using namespace std;

cout << "Declare the FFANN" << endl;
FeedForwardNeuralNetwork * ffnn = new FeedForwardNeuralNetwork(4,6,2);
printNNStructure(*ffnn);

cout << "Change size of Layer 0 to 2" << endl;
ffnn->setLayerSize(0,2);
printNNStructure(*ffnn);

cout << "Pop a hidden layer" << endl;
ffnn->popHiddenLayer();
printNNStructure(*ffnn);

cout << "Add a hidden layer with size 5" << endl;
ffnn->pushHiddenLayer(5);
printNNStructure(*ffnn);

cout << "Add a hidden layer with size 3" << endl;
ffnn->pushHiddenLayer(3);
printNNStructure(*ffnn);

cout << "Connect the FFNN" << endl;
ffnn->connectFFNN();

ffnn->addFirstDerivativeSubstrate();
cout << "Add the first derivative to the computations" << endl;
ffnn->addSecondDerivativeSubstrate();
cout << "Add the second derivative to the computations" << endl;
ffnn->addVariationalFirstDerivativeSubstrate();
cout << "Add the variational first derivative to the computations" << endl;
cout << " -> Number of variational parameters: " << ffnn->getNVariationalParameters() << endl;
cout << endl;

double x=3.;
ffnn->setInput(1, &x);
ffnn->FFPropagate();
printNNValues(*ffnn);

// Plot of the 1-dimensional function
const double L=10.;
double xh;
const int N=150;
double dL=L/N;
const double h=0.00001;
double z, zh, zmh;
double z1d;
double z2d;
const int ivar=2;
double vp, vph;
// nn value
ofstream file;
file.open("randomNN.txt");
// first derivative
ofstream file1d;
file1d.open("randomNN-1d.txt");
// second derivative
ofstream file2d;
file2d.open("randomNN-2d.txt");
// variational first derivative
ofstream filev1d;
filev1d.open("randomNN-v1d.txt");
// numerical first derivative
ofstream filen1d;
filen1d.open("randomNN-n1d.txt");
// numerical second derivative
ofstream filen2d;
filen2d.open("randomNN-n2d.txt");
// numerical variational first derivative
ofstream filenv1d;
filenv1d.open("randomNN-nv1d.txt");
x=-L*0.5;
for (int i=0; i<N; ++i)
{
x+=dL;
ffnn->setInput(1, &x);
ffnn->FFPropagate();
z=ffnn->getOutput(1);
file << x << " " << z << endl;

// first derivative
z1d=ffnn->getFirstDerivative(1,0);
file1d << x << " " << z1d << endl;

// second derivative
z2d=ffnn->getSecondDerivative(1,0);
file2d << x << " " << z2d << endl;

// variational first derivative
z1d=ffnn->getVariationalFirstDerivative(1,ivar);
filev1d << x << " " << z1d << endl;

// numerical first derivative
xh=x+h;
ffnn->setInput(1, &xh);
ffnn->FFPropagate();
zh=ffnn->getOutput(1);
filen1d << x << " " << (zh-z)/h << endl;
ffnn->setInput(1, &x);

// numerical second derivative
xh=x-h;
ffnn->setInput(1, &xh);
ffnn->FFPropagate();
zmh=ffnn->getOutput(1);
filen2d << x << " " << (zh-2.*z+zmh)/(h*h) << endl;
ffnn->setInput(1, &x);

// numerical variational first derivative
vp=ffnn->getVariationalParameter(ivar);
vph=vp+h;
ffnn->setVariationalParameter(ivar,vph);
ffnn->FFPropagate();
zh=ffnn->getOutput(1);
filenv1d << x << " " << (zh-z)/h << endl;
ffnn->setVariationalParameter(ivar,vp);

}
file.close();
file1d.close();
file2d.close();
filev1d.close();
filen1d.close();
filen2d.close();
filenv1d.close();

// store NN in a file
ffnn->storeOnFile("FFNN.txt");

// create a new NN reading it from the file
cout << endl << endl << "CREATE A COPY OF THE PREVIOUS FFNN USING THE FILE IN WHICH IT WAS STORED ..." << endl;
FeedForwardNeuralNetwork * ffnn_copy =new FeedForwardNeuralNetwork("FFNN.txt");
x=3.;
ffnn->setInput(1, &x);
ffnn->FFPropagate();
printNNValues(*ffnn);

// free memory
delete ffnn_copy;
delete ffnn;

return 0;
}


Empty file added debug/randomNN-1d.txt
Empty file.
Empty file added debug/randomNN-2d.txt
Empty file.
Loading

0 comments on commit fe4ecbf

Please sign in to comment.