{"id":21195565,"url":"https://github.com/eantcal/nunn","last_synced_at":"2025-06-23T04:35:08.370Z","repository":{"id":35314069,"uuid":"39575594","full_name":"eantcal/nunn","owner":"eantcal","description":"Collection of Machine Learning Algorithms","archived":false,"fork":false,"pushed_at":"2024-05-11T06:26:35.000Z","size":13896,"stargazers_count":11,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-04-05T10:41:40.862Z","etag":null,"topics":["cplusplus-17","deep-neural-networks","handwritten-digits","linux","machine-learning","machine-learning-algorithms","macos","mnist","modern-cpp","multilayer-perceptron","multilayer-perceptron-network","neural-network","ocr-test","qlearning","qlearning-algorithm","reinforcement-learning-algorithms","sarsa","tictactoe","windows","xor-problem"],"latest_commit_sha":null,"homepage":"http://www.eantcal.eu","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/eantcal.png","metadata":{"files":{"readme":"README.md","changelog":"NEWS","contributing":null,"funding":null,"license":"COPYING","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-07-23T15:32:45.000Z","updated_at":"2025-02-12T08:20:37.000Z","dependencies_parsed_at":"2023-12-31T01:42:37.290Z","dependency_job_id":"f452babc-99c8-4bac-9dcf-e7c928ece200","html_url":"https://github.com/eantcal/nunn","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"purl":"pkg:github/eantcal/nunn","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eantcal%2Fnunn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eantcal%2Fnunn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eantcal%2Fnunn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eantcal%2Fnunn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eantcal","download_url":"https://codeload.github.com/eantcal/nunn/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eantcal%2Fnunn/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261416021,"owners_count":23155030,"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":["cplusplus-17","deep-neural-networks","handwritten-digits","linux","machine-learning","machine-learning-algorithms","macos","mnist","modern-cpp","multilayer-perceptron","multilayer-perceptron-network","neural-network","ocr-test","qlearning","qlearning-algorithm","reinforcement-learning-algorithms","sarsa","tictactoe","windows","xor-problem"],"created_at":"2024-11-20T19:28:50.665Z","updated_at":"2025-06-23T04:35:03.356Z","avatar_url":"https://github.com/eantcal.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nunn 2.0\n| Linux  | [![Linux Build](https://travis-ci.org/eantcal/nunn.svg?branch=master)](https://travis-ci.org/eantcal/nunn)  |\n\nNunn Library is a Free Open Source Machine Learning Library distributed under MIT License and written in C++17\n\n## Features\n- Supports fully connected multi-layers neural networks and other ML algorithms\n- Easy to use and understand\n- Easy to save and load entire states\n- Multi-platform\n\nThe library package includes the following samples and tools.\n\n## Nunn Topology -\u003e Graphviz format converter (nunn_topo)\nUsing this tool you can export neural network topologies and draw them using Graphviz dot.\ndot draws directed graphs. It reads attributed graph text files and writes drawings,\neither as graph files or in a graphics format such as GIF, PNG, SVG or PostScript\n(which can be converted to PDF).\n\n## MNIST Test Demo (mnist_test)\nThis demonstration involves training and testing an MLP neural network using the MNIST dataset.\nThe MNIST dataset consists of 60,000 training images plus 10,000 test images. These images are scans of handwritten digits, each with its correct classification.\nThe images are grayscale and have dimensions of 28 by 28 pixels.\nThe first part of the dataset, comprising 60,000 images, is used for training the neural network.\nThe second part, containing 10,000 images, is used for testing the network's performance.\nIt's important to note that the test data comes from a different set of individuals than the original training data.\nDuring training, the input is treated as a 784-dimensional vector, representing a 28x28 grid of pixel values.\nEach entry in the vector corresponds to the grayscale value of a single pixel in the image.\nThe desired output is a 10-dimensional vector, representing the classification of the digit.\nFor more information about the MNIST dataset, you can visit the following link: http://yann.lecun.com/exdb/mnist/\n\n### Handwritten Digit OCR Demo (ocr_test)\nThis is an interactive demo that utilizes a neural network trained on the MNIST dataset. The neural network was created using the nunn library.\nThe demo makes use of nunn status files (.net), which were generated by the mnist_test application.\n\n### [![Click here for watching the video](https://youtu.be/ereeEG_1lmY)](https://youtu.be/ereeEG_1lmY)\n![ocr_test](examples/images/ocr.jpg)\n\n## TicTacToe Demo (tictactoe)\nBasic Tic Tac Toe game which uses neural networks. \n\n### TicTacToe Demo for Windows (winttt)\nWinttt is an interactive version of Tic Tac Toe designed for Windows. It offers the flexibility to be dynamically trained or to utilize pre-trained neural networks, including those created using the tictactoe program.\n\n![tictactoe](examples/images/tictactoe.jpg)\n\n\n## XOR Problem sample (xor_test)\nThe XOR function serves as a typical example of a non-linearly separable function. It has been a classic problem in the field of neural networks, often used as a benchmark for testing their capabilities.\n \n### Solving the XOR Problem with Nunn Library\nXOR function takes two input arguments with values in [0,1] and returns one output in [0,1], as specified in the following table:\n\n```\n x1|x2 |  y\n --+---+----\n 0 | 0 |  0\n 0 | 1 |  1\n 1 | 0 |  1\n 1 | 1 |  0\n```\n\nThe XOR function, also known as the exclusive-or, computes a result of 1 only when the two inputs have different values. This particular classification cannot be solved with linear separation methods, as it requires a non-linear solution. However, Multi-Layer Perceptron (MLP) neural networks excel at generating such non-linear solutions, making them well-suited for solving the XOR problem.\n\n### Xor function implementation step by step\nDuring the testing phase, an MLP network was trained to perform a specific task. Through training, the algorithm receives examples of desired network behavior and adjusts the weights accordingly. Once the training is complete, the network is capable of providing the desired output for a given input. This iterative process of training allows the MLP network to learn and adapt, ultimately enabling it to produce the desired results.\n\n- Step 1: include MLP NN header\n```\n#include \"nu_mlpnn.h\"\n#include \u003ciostream\u003e\n#include \u003cmap\u003e\n```\n- Step 2: Define net topology\n\n```\nint main(int argc, char* argv[])\n{\n  using vect_t = nu::MlpNN::FpVector;\n\n  nu::MlpNN::Topology topology = {\n      2, // input layer takes a two dimensional vector\n      2, // hidden layer size\n      1  // output\n  };\n```\n\n- Step 3: Construct the network object specifying topology, learning rate and momentum\n```\n  try \n  {\n      nu::MlpNN nn {\n         topology,\n         0.4, // learning rate\n         0.9, // momentum\n      };\n```\n\n- Step 4: Create a training set needed to train the net.\nTraining set must be a collection of \u003cinput-vector, output-vector\u003e pairs. \n\n```\n// Create a training set\n      using training_set_t = std::map\u003c std::vector\u003cdouble\u003e, std::vector\u003cdouble\u003e \u003e;\n      training_set_t traing_set = {\n         { { 0, 0 },{ 0 } },\n         { { 0, 1 },{ 1 } },\n         { { 1, 0 },{ 1 } },\n         { { 1, 1 },{ 0 } }\n      };\n```\n- Step 5: Train the net using a trainer object. \nTrainer object iterates for each element of training set until the max number of epochs (20000) is reached or error computed by function passed as second parameter to train() method is less than min error (0.01).\n\n```\n        nu::MlpNNTrainer trainer(\n         nn,\n         20000, // Max number of epochs\n         0.01   // Min error\n      );\n      std::cout\n         \u003c\u003c \"XOR training start ( Max epochs count=\" \u003c\u003c trainer.get_epochs()\n         \u003c\u003c \" Minimum error=\" \u003c\u003c trainer.get_min_err() \u003c\u003c \" )\"\n         \u003c\u003c std::endl;\n      trainer.train\u003ctraining_set_t\u003e(\n         traing_set,\n         [](\n            nu::MlpNN\u0026 net,\n            const nu::MlpNN::FpVector_t \u0026 target) -\u003e double\n         {\n            static size_t i = 0;\n            if (i++ % 200 == 0)\n               std::cout \u003c\u003c \"\u003e\";\n             return net.calcMSE(target);\n         }\n      );\n```\n\n- Step 6: Test if net learnt XOR-function\n\n```\nauto step_f = [](double x) { return x \u003c 0.5 ? 0 : 1; };\n     std::cout \u003c\u003c  std::endl \u003c\u003c \" XOR Test \" \u003c\u003c std::endl;\n     for (int a = 0; a \u003c 2; ++a)\n     {\n        for (int b = 0; b \u003c 2; ++b)\n        {  \n            vect_t output_vec{ 0.0 };\n            vect_t input_vec{ double(a), double(b) }; \n\n            nn.setInputVector(input_vec);\n            nn.feedForward();\n            nn.getOutputVector(output_vec);\n\n            // Dump the network status\n            std::cout \u003c\u003c nn;\n            std::cout \u003c\u003c \"-------------------------------\" \u003c\u003c std::endl;\n            auto net_res = step_f(output_vec[0]);\n            std::cout \u003c\u003c a \u003c\u003c \" xor \" \u003c\u003c b \u003c\u003c \" = \" \u003c\u003c net_res \u003c\u003c std::endl;\n            auto xor_res = a ^ b;\n            \n            if (xor_res != net_res) {\n               std::cerr\n                  \u003c\u003c \"ERROR!: xor(\" \u003c\u003c a \u003c\u003c \",\" \u003c\u003c b \u003c\u003c \") !=\"\n                  \u003c\u003c xor_res\n                  \u003c\u003c std::endl; \n               return 1;\n            }\n            std::cout \u003c\u003c \"-------------------------------\" \u003c\u003c std::endl;\n        }\n     } \n      std::cout \u003c\u003c \"Test completed successfully\" \u003c\u003c std::endl;\n   }\n   catch (...)\n   {\n      std::cerr\n         \u003c\u003c \"Fatal error. Check for configuration parameters and retry\" \u003c\u003c std::endl;\n      return 1;\n   } \n   return 0;\n}\n\n```\n\n### Program output\n\n```\nXOR training start ( Max epochs count=20000 Minimum error=0.01) \n\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e\u003e \n\nXOR Test\nNet Inputs\n        [0] = 0\n        [1] = 0\n\nNeuron layer 0 Hidden\n        Neuron 0\n                Input  [0] = 0\n                Weight [0] = 0.941384\n                Input  [1] = 0\n                Weight [1] = 0.94404\n                Bias =       0.0307751\n                Ouput = 0.507693\n                Error = 0.0707432\n        Neuron 1\n                Input  [0] = 0\n                Weight [0] = 6.19317\n                Input  [1] = 0\n                Weight [1] = 6.49756\n                Bias =       -0.0227467\n                Ouput = 0.494314\n                Error = -0.0568667\n\nNeuron layer 1 Output\n        Neuron 0\n                Input  [0] = 0.507693\n                Weight [0] = -16.4831\n                Input  [1] = 0.494314\n                Weight [1] = 13.2566\n                Bias =       -0.00652012\n                Ouput = 0.139202\n                Error = -0.0171672\n-------------------------------\n0 xor 0 = 0\n-------------------------------\nNet Inputs\n        [0] = 0\n        [1] = 1\n\nNeuron layer 0 Hidden\n        Neuron 0\n                Input  [0] = 0\n                Weight [0] = 0.941384\n                Input  [1] = 1\n                Weight [1] = 0.94404\n                Bias =       0.0307751\n                Ouput = 0.726078\n                Error = 0.0707432\n        Neuron 1\n                Input  [0] = 0\n                Weight [0] = 6.19317\n                Input  [1] = 1\n                Weight [1] = 6.49756\n                Bias =       -0.0227467\n                Ouput = 0.998461\n                Error = -0.0568667\n\nNeuron layer 1 Output\n        Neuron 0\n                Input  [0] = 0.726078\n                Weight [0] = -16.4831\n                Input  [1] = 0.998461\n                Weight [1] = 13.2566\n                Bias =       -0.00652012\n                Ouput = 0.779318\n                Error = -0.0171672\n-------------------------------\n0 xor 1 = 1\n-------------------------------\nNet Inputs\n        [0] = 1\n        [1] = 0\n\nNeuron layer 0 Hidden\n        Neuron 0\n                Input  [0] = 1\n                Weight [0] = 0.941384\n                Input  [1] = 0\n                Weight [1] = 0.94404\n                Bias =       0.0307751\n                Ouput = 0.72555\n                Error = 0.0707432\n        Neuron 1\n                Input  [0] = 1\n                Weight [0] = 6.19317\n                Input  [1] = 0\n                Weight [1] = 6.49756\n                Bias =       -0.0227467\n                Ouput = 0.997914\n                Error = -0.0568667\n\nNeuron layer 1 Output\n        Neuron 0\n                Input  [0] = 0.72555\n                Weight [0] = -16.4831\n                Input  [1] = 0.997914\n                Weight [1] = 13.2566\n                Bias =       -0.00652012\n                Ouput = 0.77957\n                Error = -0.0171672\n-------------------------------\n1 xor 0 = 1\n-------------------------------\nNet Inputs\n        [0] = 1\n        [1] = 1\n\nNeuron layer 0 Hidden\n        Neuron 0\n                Input  [0] = 1\n                Weight [0] = 0.941384\n                Input  [1] = 1\n                Weight [1] = 0.94404\n                Bias =       0.0307751\n                Ouput = 0.871714\n                Error = 0.0707432\n        Neuron 1\n                Input  [0] = 1\n                Weight [0] = 6.19317\n                Input  [1] = 1\n                Weight [1] = 6.49756\n                Bias =       -0.0227467\n                Ouput = 0.999997\n                Error = -0.0568667\n\nNeuron layer 1 Output\n        Neuron 0\n                Input  [0] = 0.871714\n                Weight [0] = -16.4831\n                Input  [1] = 0.999997\n                Weight [1] = 13.2566\n                Bias =       -0.00652012\n                Ouput = 0.246297\n                Error = -0.0171672\n-------------------------------\n1 xor 1 = 0\n-------------------------------\nTest completed successfully\n```\n\nTopology is defined using a vector of positive integers, where the first element represents the size of the input layer, and the last element represents the size of the output layer. The remaining values represent the sizes of the hidden layers, arranged from input to output. The topology vector should contain at least three items, and all items must be non-zero positive integers.\n\n## Perceptron AND Sample (and_test)\nThe perceptron implementation of the AND function is an example of a linearly separable function. A single perceptron neural network can learn this type of function. The AND function computes the logical AND operation, resulting in an output of 1 only when both input values are 1.\n\n## Hopfield Test (hopfield_test)\nHopfield networks can be utilized to solve the recall problem by matching cues from an input pattern to a pre-learned pattern. These networks are a form of recurrent artificial neural networks that function as content-addressable memory systems, using binary threshold nodes. This test demonstrates the use of a Hopfield network as an auto-associative memory. In this example, a 100-pixel picture is recognized using a 100-neuron neural network.\n\n![hopfield test](examples/images/hopfield.jpg)\n\n# Reinforcement learning\nThe reinforcement learning library provides algorithms for implementing reinforcement learning, a machine learning approach focused on decision-making and maximizing rewards. Specifically, it includes implementations of Q-learning and SARSA (State–action–reward–state–action) algorithms.\n\nReinforcement learning is a branch of machine learning that involves an agent interacting with an environment, learning from feedback in the form of rewards or punishments. Q-learning is a popular algorithm used in reinforcement learning for learning optimal action-selection policies based on an action-value function. SARSA is another reinforcement learning algorithm that focuses on estimating the Q-values of state-action pairs.\n\nThese algorithms are fundamental tools in reinforcement learning and are useful for solving various problems that involve sequential decision-making in dynamic environments.\n\nSee the maze (https://github.com/eantcal/nunn/blob/master/examples/maze/maze.cc) and path finder (https://github.com/eantcal/nunn/blob/master/examples/path_finder/path_finder.cc) examples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feantcal%2Fnunn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feantcal%2Fnunn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feantcal%2Fnunn/lists"}