-
Notifications
You must be signed in to change notification settings - Fork 0
/
pybind11test.cpp
152 lines (121 loc) · 4.15 KB
/
pybind11test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
#include "ANG.h"
#include <iostream>
using namespace std;
ANG Train(vector<vector<float>> inputs, vector<vector<float>> targets, vector<int> hidden_layers) {
ANG ang(hidden_layers, targets[0].size(), inputs[0].size());
ang.ANG_grow(inputs, targets);
return ang;
}
vector<int> test_ang(vector<int> hidden_layers, vector<vector<float>> inputs, vector<vector<float>> targets) {
ANG ang;
ang = Train(inputs, targets, hidden_layers);
vector<int> pred;
for (int i = 0; i < inputs.size(); i++) {
vector<float> output = ang.forward_propogate(inputs[i]);
//cout << output[0] << endl;
if (output[0] >= 0.5) pred.push_back(1);
else pred.push_back(0);
}
//ang.Print3D(ang.weights);
//Test saving
ang.save_model("pybindsave");
ANG ang2;
vector<int> pred2;
ang2.load_model("pybindsave");
for (int i = 0; i < inputs.size(); i++) {
vector<float> output = ang2.forward_propogate(inputs[i]);
//cout << output[0] << endl;
if (output[0] >= 0.5) pred2.push_back(1);
else pred2.push_back(0);
}
for (int j = 0; j< pred2.size(); j++){
cout << pred2[j] << "\t";
}
return pred;
//for (int j = 0; j < pred.size(); j++) {
// cout << pred[j] << '\t';
//}
}
//int main() {
//ANG ang({3,3}, 1, 10);
//ang.PrintVector({ 1,2,3 });
//vector<vector<float>> i = { {1, 1, 1}, {0, 0, 0}, {1, 1, 1} , {0,0,0} ,{1,1,1}, {1,1,1} , {0,0,0}, {0,0,0} ,{1,1,1} , {0,0,0}, {1, 1, 1}, {0, 0, 0}, {1, 1, 1} , { 0,0,0} ,{1,1,1}, {1,1,1} , {0,0,0}, {0,0,0} ,{1,1,1} , {0,0,0}, {1, 1, 1}, {0, 0, 0}, {1, 1, 1} , { 0,0,0} ,{1,1,1}, {1,1,1} , {0,0,0}, {0,0,0} ,{1,1,1} , {0,0,0}, {1, 1, 1}, {0, 0, 0}, {1, 1, 1} , { 0,0,0} ,{1,1,1}, {1,1,1} , {0,0,0}, {0,0,0} ,{1,1,1} , {0,0,0}, {1, 1, 1}, {0, 0, 0}, {1, 1, 1} , { 0,0,0} ,{1,1,1}, {1,1,1} , {0,0,0}, {0,0,0} ,{1,1,1} , {0,0,0} };
//vector<vector<float>> t = { {1} , {0}, {0} , {0}, {1}, {1}, {0} , {0}, {1}, {0}, {1} , {0}, {1} , {0}, {1}, {1}, {0} , {0}, {1}, {0}, {1} , {0}, {1} , {0}, {1}, {1}, {0} , {0}, {1}, {0}, {1} , {0}, {1} , {0}, {1}, {1}, {0} , {0}, {1}, {0}, {1} , {0}, {1} , {0}, {1}, {1}, {0} , {0}, {1}, {0} };
//test_ang({ 3,3 }, i, t);
//}
class Ang {
ANG ang;
public:
Ang(vector<int> hidden_layers, int t_size, int i_size) {
ANG ang2(hidden_layers, t_size, i_size);
ang = ang2;
}
ANG train(vector<vector<float>> inputs, vector<vector<float>> targets) {
ang.ANG_grow(inputs, targets);
return ang;
}
void save(string s) {
ang.save_model(s);
}
ANG load(string s) {
ANG loaded_ang;
loaded_ang.load_model(s);
return loaded_ang;
}
vector<int> test(vector<vector<float>> inp) {
vector<int> pred;
for (int i = 0; i < inp.size(); i++) {
vector<float> output = ang.forward_propogate(inp[i]);
//cout << output[0] << endl;
if (output[0] >= 0.5) pred.push_back(1);
else pred.push_back(0);
}
return pred;
}
};
namespace py = pybind11;
/*
class TestClass {
float multiplier1;
float multiplier2;
public:
TestClass(float m, float n) { multiplier1 = m;
multiplier2 = n;}
float multiply(float input) {
return multiplier1 * input * multiplier2;
}
std::vector<float> multiply_lists(std::vector<float> a, std::vector<float> b){
for(int i = 0; i<a.size(); i++) a[i] = a[i] * b [i];
return a;
}
};
*/
/*
float add(float a, float b) {
return a + b;
}
*/
PYBIND11_MODULE(example1, m) {
m.doc() = "first test of pybind11 on ANG";
m.def("test_ang", &test_ang, "Function to test Artificial NeuroGenesis");
py::class_<Ang>(m, "ClassTest")
.def(py::init<vector<int>, int, int>())
.def("Train", &Ang::train)
.def("save", &Ang::save)
.def("load", &Ang::load)
.def("test", &Ang::test)
;
}
/*
m.doc() = "pybind11 example plugin"; // optional module docstring
m.def("add", &add, "A function which adds two numbers");
py::class_<TestClass> (m,"PyTest")
.def(py::init<float, float>())
.def("multiply", &TestClass::multiply)
.def("multiply_list", &TestClass::multiply_lists)
;
}
*/