{"id":13419080,"url":"https://github.com/stagadish/NNplusplus","last_synced_at":"2025-03-15T04:32:01.387Z","repository":{"id":45113450,"uuid":"66572811","full_name":"stagadish/NNplusplus","owner":"stagadish","description":"A small and easy to use neural net implementation for C++. Just download and #include!","archived":false,"fork":false,"pushed_at":"2017-06-25T17:23:15.000Z","size":3669,"stargazers_count":228,"open_issues_count":0,"forks_count":35,"subscribers_count":19,"default_branch":"master","last_synced_at":"2024-07-31T22:45:39.882Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stagadish.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"License.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-25T15:59:42.000Z","updated_at":"2024-01-04T16:07:08.000Z","dependencies_parsed_at":"2022-09-02T22:40:32.696Z","dependency_job_id":null,"html_url":"https://github.com/stagadish/NNplusplus","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stagadish%2FNNplusplus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stagadish%2FNNplusplus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stagadish%2FNNplusplus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stagadish%2FNNplusplus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stagadish","download_url":"https://codeload.github.com/stagadish/NNplusplus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243685506,"owners_count":20330980,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T22:01:11.052Z","updated_at":"2025-03-15T04:32:00.374Z","avatar_url":"https://github.com/stagadish.png","language":"C++","funding_links":[],"categories":["TODO scan for Android support in followings","C++","[Libraries](#contents)"],"sub_categories":["Machine Learning"],"readme":"\u003ca href=\"http://i.imgur.com/dPoSllF.png\"\u003e\n    \u003cimg src=\"http://i.imgur.com/dPoSllF.png\" alt=\"N++\"\n         title=\"stagadish/NN++\" align=\"right\" /\u003e\n\u003c/a\u003e\n\n# NN++\n\nA short, self-contained, and easy-to-use neural net implementation for C++. It includes the neural net implementation and a Matrix class for *basic* linear algebra operations. This project is mostly for **learning purposes**, but preliminary testing results over the MNIST dataset show some promise.\n\n## Getting Started\n\nThese instructions will get you a copy of the net up and running on your local machine for development and testing purposes.\n\n### Prerequisites\n\nAny compiler that can handle C++11.\n\n### Installing\n\n1. Download `Matrix.hpp`, `Matrix.cpp`, `NeuralNet.hpp`, and `NeuralNet.cpp` and place them in your project's working directory.\n2. Include the headers in your main driver program (e.g. `main.cpp`).\n\n```\n#include \"Matrix.hpp\"\n#include \"NeuralNet.hpp\"\n```\n_**NOTE:** It is not required to `#include \"Matrix.hpp\"` since it is included within `NeuralNet.hpp`. However it is probably better to do so for clarity and safety in case you plan on using Matrix objects in your code (and you probably will if you use NeuralNet)._\n## Example Code\n### The Matrix Class\nFirst you need to know how to use the Matrix class.\nMatrix objects are basically 2D-vectors with built-in linear algebra operations.\n\n#### Matrix Initialization\n\n```\nMatrix A;       // Initializes a 0x0 matrix.\nMatrix B(2,2)   // Initializes a 2x2 matrix with all zeros. Values are doubles.\nMatrix C(2,1)   // Initializes a 2x1 matrix.\n```\n#### Element Access\nTo access/modify a value in a matrix, use `operator()`, NOT `operator[]`:\n\n```\nB(0,0) = 1; B(0,1) = 2; B(1,0) = 3; B(1,1) = 4;   // [1    2]\n                                                  // [3    4]\n\nC(0,0) = 1; C(1,0) = 2;                           // [1]\n                                                  // [2]\n```\n\n#### Matrix Term-by-Term Addition/Subtraction/Multiplication\n```\n// Commutative property is supported for addition\nMatrix D = B+B;       // D = [2   4]\n                             [6   8]\n                             \nMatrix E = B-B;       // E = [0   0]\n                             [0   0]\n                             \n// Commutative property is supported for multiplication                             \nMatrix F = B*B        // F = [1   4]\n                             [9  16]\n                             \n// Mismatching matrix dimensions in term-by-term operations\n// is illegal and a MatrixDimensionsMismatch exception will be thrown.\nMatrix G = B+C;       // Throws MatrixDimensionsMismatch()\nMatrix G = B-C;       // Throws MatrixDimensionsMismatch()\nMatrix G = B*C;       // Throws MatrixDimensionsMismatch()\n```\n\n#### Matrix and Scalars\n```\n// Commutative property is supported for addition\nMatrix BplusTwo = B+2;  // (== 2+B)   BplusTwo = [3   4]\n                                                 [5   6]\n\nMatrix CminusTwo = C-2; //           CminusTwo = [-1]\n                                                 [ 0]\n                                                 \nMatrix TwominusB = 2-C; //           TwominusB = [ 1]\n                                                 [ 0]\n                                                 \n// Commutative property is supported for multiplication\nMatrix BtimesThree = B*3; // (== 3*B) BtimesThree = [3    6]\n                                                    [9   12]\n```\n\n#### Matrix Multiplication (Dot Product)\n```\nMatrix BB = B.dot(B);     // BB = [ 7  10]\n                                  [15  22]\n                                  \nMatrix BC = B.dot(C);     // BC = [ 5]\n                                  [11]\n\n// Mismatching the number of columns in the left-hand-side matrix\n// with the number of rows in the right-hand-side matrix is illegal\n// A MatrixInnderDimensionsMismatch exception will be thrown.\nMatrix CB = C.dot(B);     // Throws MatrixInnderDimensionsMismatch()\n```\n\n#### Matrix Transpose\n```\nMatrix B_T = B.T();   // B_T = [1   3]\n                               [2   4]\n                                 \nMatrix C_T = C.T();   // C_T = [1   2]\n```\n\n#### An Example of Populating a 4x3 Matrix\n```\nint m = 4;\nint n = 3;\n\nMatrix mtrx(m,n);\n\nint count = 1;\nfor (int i = 0; i \u003c mtrx.getNumOfRows(); ++i) {\n    for (int j = 0; j \u003c mtrx.getNumOfCols(); ++j) {\n        mtrx(i,j) = count;\n        ++count;\n    }\n}\n```\nThis will result with `mtrx` ==\n```\n[ 1     2     3]\n[ 4     5     6]\n[ 7     8     9]\n[10    11    12]\n```\n\n### The NeuralNet Class\n#### Neural Net Initialization (The Parameters)\nWhen initialized, a net takes in five parameters:  \n1. Number of input nodes.  \n2. Number of nodes per hidden layer.  \n3. Number of output nodes.  \n4. Number of hidden layers.  \n5. The learning rate.  \n\n```\nNeuralNet NN(4, 3, 1, 10, 0.1);\n```\n_This_ particular neural net has 4 input nodes, 1 hidden layer with 3 nodes, 10 output node, and has a learning rate of 0.1.  \nNew neural nets' weights are initialized with values drawn from a normal distribution centered at 0, with standard deviation that is equal to `1/sqrt(number_of_inputs_to_nodes_in_next_layer)`. In other words, small negative and positive values that are proportional to the size of their previous layer.\n\n#### A Training Cycle\nOnce the net is initialized, it is ready to do work.  \n__ONE__ training cycle == one feed forward and one back propagation with weight adjustments.  \n  \nTo train one cycle, the input data must be parsed into a Matrix object with dimensions: `1xnumber_of_input_nodes` (1x4 in our case), and the target output must be parsed into a Matrix object with dimensions: `1xnumber_of_output_nodes` (1x10 in our case).  \n```\nMatrix input(1,4);\ninput(0,0) =  0.3;\ninput(0,1) = -0.1;\ninput(0,2) =  0.2;\ninput(0,3) =  0.8;\n\nMatrix targetOutput(1,1);\ntarget(0,0) =  0.5;\ntarget(0,1) = -0.3;\n        .\n        .\n        .\ntarget(0,9) =  0.23;        // Obviously, matrices should be populated using\n                            // some parser and not manualy like this.\n\n```\nThen, simply execute the training cycle on the data as follows:\n```\nNN.trainingCycle(input, targetOutput);\n```\nRepeate the process over all training instances.\n\n#### Querying the Net\nOnce the training phase is complete, you can query it as follows:  \n(Technically speaking, you can query it right after initialization).\n\nParse the query into a Matrix like parsed the training instance:\n```\nMatrix query(1,2);\ninput(0,0) =  0.5;\ninput(0,1) = -0.2;\ninput(0,2) = -0.3;\ninput(0,3) =  0.4;\n```\n\nQuery the net and catch the result:\n```\nMatrix prediction = NN.queryNet(query);   // Will return a 1x10 Matrix object with net's prediction\n```\nAND THAT'S IT!\n\n## TODO\n1. Add `array`, `std::vector`, and `std::initializer_list` constructors to the Matrix class\n2. Either improve on or replace my Matrix class for better/faster performance\n3. Add multiple epoch learning with early stopping.\n\n## Authors\n\n* **Gil Dekel** - *Initial implementation* - [stagadish](https://github.com/stagadish)\n\nSee also the list of [contributors](https://github.com/stagadish/NNplusplus/contributors) who participated in this project.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE.md](https://github.com/stagadish/NNplusplus/blob/master/License.md) file for details\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstagadish%2FNNplusplus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstagadish%2FNNplusplus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstagadish%2FNNplusplus/lists"}