├── example ├── sample.jpg ├── README.md ├── example_svm.cpp ├── example_mlp.cpp ├── sample_svm.csv └── sample.csv ├── README.md ├── LICENSE ├── mlp.h └── svm.h /example/sample.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kivantium/libNN/HEAD/example/sample.jpg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # libnn 2 | Neural network library written in C++ 3 | 4 | #Usage 5 | just include header files in your project 6 | 7 | for more information, see example/ 8 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # Example 2 | 3 | ## sample data 4 | * sample.csv 5 | * two dimensional three class classification 6 | * first column shows label, second and third column show data 7 | * sample.jpg 8 | * visualization of sample.csv 9 | 10 | ## multi layer perceptron 11 | * example\_mlp.cpp 12 | * classify sample data by using multi layer perceptron 13 | * as code is heavily commented, you can understand how it works 14 | -------------------------------------------------------------------------------- /example/example_svm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "svm.h" 4 | using namespace std; 5 | 6 | int main(void){ 7 | // number of test data 8 | const int sample = 100; 9 | // size (dimension) of input vector 10 | const int size = 2; 11 | 12 | // create SVM (dimension is size) 13 | svm detector(size); 14 | 15 | // train data 16 | float x[size*sample]; 17 | // label data 18 | int t[sample]; 19 | 20 | // load CSV 21 | FILE *fp = fopen("sample_svm.csv", "r"); 22 | if(fp==NULL) return -1; 23 | for(int i=0; i0){ 37 | if(detector.test(i)==1) correct++; 38 | }else{ 39 | if(detector.test(i)==-1) correct++; 40 | } 41 | } 42 | cout << (correct*100.0/sample) << "%" << endl; 43 | 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /example/example_mlp.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "../mlp.h" 4 | using namespace std; 5 | 6 | int main(void){ 7 | // number of test data 8 | const int sample = 150; 9 | // size (dimension) of input vector 10 | const int size = 2; 11 | // number of labels 12 | const int label = 3; 13 | 14 | // create MLP(input 2, hidden 3, output 3) 15 | // number of hidden layer is your choice 16 | mlp net(size,3,label); 17 | 18 | // train data 19 | float x[size*sample]; 20 | // label data 21 | int t[sample]; 22 | 23 | // load CSV 24 | FILE *fp = fopen("sample.csv", "r"); 25 | if(fp==NULL) return -1; 26 | for(int i=0; i 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | class mlp { 11 | private: 12 | // number of each layer 13 | int in, hid, out; 14 | // input layer 15 | float *xi1, *xi2, *xi3; 16 | // output layer 17 | float *o1, *o2, *o3; 18 | // error value 19 | float *d2, *d3; 20 | // wait 21 | float *w1, *w2; 22 | 23 | //sigmoid function 24 | float sigmoid(float x){ 25 | return 1/(1+exp(-x)); 26 | } 27 | 28 | //derivative of sigmoid function 29 | float d_sigmoid(float x){ 30 | return (1-sigmoid(x))*sigmoid(x); 31 | } 32 | 33 | //caluculate forward propagation of input x 34 | void forward(float *x){ 35 | //calculation of input layer 36 | for(int j=0; j