Skip to content

Commit 7736ac8

Browse files
author
gramcracker
committed
Added python wrapper with pybind11
1 parent a992423 commit 7736ac8

File tree

9 files changed

+163
-16
lines changed

9 files changed

+163
-16
lines changed

.vscode/settings.json

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
{
2+
"files.associations": {
3+
"link.h": "c",
4+
"cctype": "c",
5+
"clocale": "c",
6+
"cmath": "c",
7+
"cstdarg": "c",
8+
"cstddef": "c",
9+
"cstdio": "c",
10+
"cstdlib": "c",
11+
"cstring": "c",
12+
"ctime": "c",
13+
"cwchar": "c",
14+
"cwctype": "c",
15+
"array": "c",
16+
"atomic": "c",
17+
"strstream": "c",
18+
"bit": "c",
19+
"*.tcc": "c",
20+
"bitset": "c",
21+
"chrono": "c",
22+
"complex": "c",
23+
"condition_variable": "c",
24+
"cstdint": "c",
25+
"deque": "c",
26+
"list": "c",
27+
"map": "c",
28+
"set": "c",
29+
"unordered_map": "c",
30+
"unordered_set": "c",
31+
"vector": "c",
32+
"exception": "c",
33+
"algorithm": "c",
34+
"functional": "c",
35+
"iterator": "c",
36+
"memory": "c",
37+
"memory_resource": "c",
38+
"netfwd": "c",
39+
"numeric": "c",
40+
"optional": "c",
41+
"random": "c",
42+
"ratio": "c",
43+
"string": "c",
44+
"string_view": "c",
45+
"system_error": "c",
46+
"tuple": "c",
47+
"type_traits": "c",
48+
"utility": "c",
49+
"fstream": "c",
50+
"initializer_list": "c",
51+
"iomanip": "c",
52+
"iosfwd": "c",
53+
"iostream": "c",
54+
"istream": "c",
55+
"limits": "c",
56+
"mutex": "c",
57+
"new": "c",
58+
"ostream": "c",
59+
"sstream": "c",
60+
"stdexcept": "c",
61+
"streambuf": "c",
62+
"thread": "c",
63+
"cfenv": "c",
64+
"cinttypes": "c",
65+
"typeindex": "c",
66+
"typeinfo": "c",
67+
"variant": "c"
68+
}
69+
}

CMakeLists.txt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
cmake_minimum_required(VERSION 3.10)
2+
3+
project(neuralnetwork)
4+
5+
6+
27
file(GLOB_RECURSE libsrc src/*.cpp)
38
add_library(neuralnetwork SHARED ${libsrc})
49
target_link_libraries(neuralnetwork pthread)
5-
target_include_directories(neuralnetwork PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
10+
target_include_directories(neuralnetwork PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
11+
12+
#for using python:
13+
#(requires installing python3-pybind11)
14+
find_package(pybind11 REQUIRED)
15+
16+
#use this instead for local pybind11 lib package:
17+
#(copy pybind11 into python folder)
18+
# add_subdirectory(python/pybind11)
19+
# target_include_directories(pyneuralnetwork PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/python/pybind11/include )
20+
#also comment out this line:
21+
pybind11_add_module(pyneuralnetwork python/pyInterface.cpp)
22+
23+
target_link_libraries(pyneuralnetwork PUBLIC neuralnetwork)
24+
set_target_properties(neuralnetwork PROPERTIES
25+
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)
26+
set_target_properties(pyneuralnetwork PROPERTIES
27+
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/python)
28+

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# neural_network
2-
This is my personal project to make a realatively simple neural network framework in C++.
3-
I created it specifically for experimentation with real time natural language processing
4-
and sementic analysis, although it is still a work in progress.
2+
This is a realatively simple neural network framework in C++.
53

6-
I created this because I needed a framework that could be configured in complex and irregular ways for easy experimentation. In order to suit my needs, it has have the following features:
4+
I created this because I needed a framework that could be configured in complex and irregular ways for easy experimentation. It has have the following features:
75

6+
* -NEW- now outputs a python wrapper module.
87
* Layer by layer syntax. ✓
98
* Copy and paste of any portion of the net. ✓
109
* Ability for network to build or erase connections. x
@@ -19,15 +18,23 @@ As stated above, this is still a work in progress, and I have yet to implement a
1918

2019
## Getting Started
2120
### Prerequisites
22-
You will need support for c++ >= 11. This works best with g++ >= 11, and the pthread library.
21+
-cmake
22+
-You will need support for c++ >= 11. This works best with g++ >= 11, and the pthread library.
23+
-To generate the python wrapper you will also need python3-pybind11 or copy the git repo into the local python folder. If using pybind11 locally, make sure to follow directions in the CMakeLists.txt file.
2324

24-
### Installing
25+
26+
### building
2527
```
2628
cd build
2729
cmake ..
2830
make
2931
```
30-
then copy the generated neuralnetwork.so file to your project or library path. E.g. /usr/lib/
32+
### Installing
33+
```
34+
chmod +x install.sh
35+
sudo sh install.sh
36+
```
37+
this just copies the generated neuralnetwork.so file to your project to /usr/local/lib/ and the header files to /usr/local/include/neuralnetwork/
3138

3239
### Usage
3340
Examples on how to use this library can be found in neural_network/examples/.

include/net.h

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <string>
1111
#include <tuple>
1212
#include <functional>
13+
#include <iostream>
14+
1315

1416

1517
using namespace std;
@@ -20,6 +22,7 @@ class net //represents a the vector of vectors of neurons
2022

2123
data_operations::random_number_generator rand_generator;
2224

25+
2326
struct Operation
2427
{
2528
public:
@@ -106,24 +109,30 @@ class net //represents a the vector of vectors of neurons
106109
Sequence.at(iteration).push_back(new Operation(this, func, t... ));
107110
}
108111

112+
113+
109114
void AddToSequence(int iteration, int layer, int first, int last, void(neuron::*func)());
110115
// void AddToSequence(int iteration, int layer, void(neuron::*func)());
111116
//TODO: make main way to run any training and prediction.
112117
//(higher level functions like restricted boltzman set up sequence, then call run sequence)
113118
void RunSequence();
114119
ofstream& Save(ofstream& file, bool save_neuron_values =false);
115120
ifstream& Open(ifstream& file);
121+
116122
void SetDesired(vector<double>& desired); //sets the desired Output of the network for Training
117123
void SetInput(vector <double>& input); //sets the input of the network
118124
void UserInput();
119125
void SetNeuronTo(int layer, int column, double val);
126+
120127
void SetBias(double val, int _layer, int _neuron);
121128
void SetBias(double val, int _layer);
122129
void SetBias(double val);
130+
123131
void SetWeights(double val, int _layer, int _neuron, int _link);
124132
void SetWeights(double val, int _layer, int _neuron);
125133
void SetWeights(double val, int _layer);
126134
void SetWeights(double val);
135+
127136
void RandomizeWeights(int _layer, int _neuron, int _link);
128137
void RandomizeWeights(int _layer, int _neuron);
129138
void RandomizeWeights(int _layer);
@@ -135,24 +144,31 @@ class net //represents a the vector of vectors of neurons
135144
void FeedForeward( int starting_layer, int ending_layer, int first_neuron, int last_neuron, neuron::ActivationType activation_type = neuron::fast_sigmoid);
136145
void FeedForeward(vector<double> &input);
137146
void FeedForeward(neuron::ActivationType activation_type = neuron::fast_sigmoid);
147+
138148
void Backpropagate(); // backpropagates the error through the network one layer at a time
139149
void TrainBackpropagation(vector<double>& _desired, vector <double>& _input);
140150
void TrainBackpropagation(vector<double>& _desired, vector <double>& _input, int _iterations);
141151
void ContrastDiverge( int starting_layer); //performs the contrast divergent algorithm between the layer selected and the next layer
152+
142153
void Output(); //displays the Output values of the neurons in every layer
143154
void Output(string cmd);
144155
double Output(int row, int column); //returns the output value of the neuron selected
156+
145157
void TrainRestrictedBoltzman(int starting_layer); //performs the restricted boltzmann machine algorithm between the layer selected and the next layer
146158
void TrainRestrictedBoltzman(int starting_layer, int iterations);
159+
147160
void OutputToVector(vector<vector<double>>& dataout); //outputs the values of the last layer to a specified vector of vectors
148161
void OutputToVector(vector<double>& dataout); //outputs the values of the last layer to a specified vector
162+
149163
void AddNeurons(int amount, int layer, string identifying_tag = "");
150164
void AddNeurons(vector<int> &dimensions, string identifying_tag = "");
165+
151166
void AddFullyConnectedNeurons(int amount, int layer = 0); //adds neurons to specified layer and fully conects them to the network
152167
net* AppendNet(net* net_from, bool pasteOver);
168+
153169
void Connect(int layer_from, int column_from, int layer_to, int column_to, float weight);
154170
void Connect(int layer_from, int column_from, int layer_to, int column_to);
155-
void ConnectToNet();
171+
156172
void SingleConnect();
157173
void SingleConnect(int layer_from, int layer_to);
158174
void SingleConnect( string identifying_tag_1, string Identifying_tag_2);
@@ -162,18 +178,17 @@ class net //represents a the vector of vectors of neurons
162178
void SingleConnectToColumns(string identifying_tag_1, string identifying_tag_2);
163179
void SingleConnectFromRows(int layer_from, int layer_to);
164180
void SingleConnectFromColumns(int layer_from, int layer_to);
181+
165182
void FullyConnect(); //fully connects all layers in the net(does not clear out previous links)
166183
void FullyConnect(int layer_from, int layer_to); //fully connects a specific portion of the network
167184
void FullyConnect(int layer_from, int lfstart, int lfend, int layer_to, int ltstart, int ltend);
168185
void FullyConnect( net* net_to, int layer_from, int layer_to); //fully connects a specific layer of a net to a specific layer in a seperate net
186+
169187
void CleanAfterBuild();//cleans up excess memory used for building network
188+
170189
int get_size();// returns size of the net
171190
int get_size(int i); //returns the size of the layer
172191

173-
174-
175-
176-
177192
protected:
178193
// if layers are filled manually, destructor
179194
//will cause destructor to delete objects twice
@@ -183,9 +198,9 @@ class net //represents a the vector of vectors of neurons
183198
private:
184199

185200

186-
187201
};
188202

189203

190204

205+
191206
#endif

install.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
lib=lib/libneuralnetwork.so
4+
include=include
5+
6+
if test -f "$lib"
7+
8+
then
9+
cp "$lib" /usr/local/lib/
10+
cp -a "$include"/. /usr/local/include/neuralnetwork/
11+
else
12+
echo "library not generated, must run cmake then make first."
13+
fi

lib/libneuralnetwork.so

639 KB
Binary file not shown.

python/.gitignore

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

python/pyInterface.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <net.h>
2+
#include <pybind11/pybind11.h>
3+
#include <pybind11/stl.h>
4+
#include <iostream>
5+
#include <vector>
6+
#include <string>
7+
8+
9+
using namespace::std;
10+
namespace py = pybind11;
11+
PYBIND11_MODULE(pyneuralnetwork, m) {
12+
py::class_<net>(m, "net")
13+
.def(py::init<>())
14+
.def("addNeurons", py::overload_cast<int , int , std::string >(&net::AddNeurons))
15+
.def("size", py::overload_cast<>(&net::get_size));
16+
}

src/net.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <tuple>
99
#include <string>
1010
#include <unordered_set>
11+
12+
13+
1114
using namespace std;
1215

1316
/**
@@ -16,7 +19,6 @@ using namespace std;
1619
net::net(){cout<<"new net"<<endl;};
1720

1821

19-
2022
/**
2123
* @brief Constructs the net object.
2224
*
@@ -754,7 +756,6 @@ void net::AddNeurons(vector<int> &dimensions, string identifying_tag){
754756

755757

756758

757-
758759
net* net::AppendNet(net* net_from, bool pasteOver)
759760
{
760761
if(pasteOver){

0 commit comments

Comments
 (0)