{"id":20120816,"url":"https://github.com/nthnn/diwa","last_synced_at":"2025-07-27T22:34:47.837Z","repository":{"id":218394759,"uuid":"745834356","full_name":"nthnn/diwa","owner":"nthnn","description":"Lightweight simple implementation of Artificial Neural Network for Arduino ESP32, ESP8266, RP2040, PlayStation Portable SDK, WebAssembly, and even Windows, macOS, and Linux-based OSes.","archived":false,"fork":false,"pushed_at":"2025-01-24T14:07:52.000Z","size":1215,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-14T17:49:15.199Z","etag":null,"topics":["arduino","arduino-library","artificial-neural-network","esp32","esp32-arduino","esp32-wroom","neural-network","neural-networks","platformio-arduino","platformio-library","psp-sdk","wasm","wasm-library"],"latest_commit_sha":null,"homepage":"https://nthnn.github.io/diwa/","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/nthnn.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","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,"zenodo":null},"funding":{"github":"nthnn","custom":"https://www.buymeacoffee.com/nthnn"}},"created_at":"2024-01-20T09:34:05.000Z","updated_at":"2025-03-23T14:53:51.000Z","dependencies_parsed_at":"2024-01-21T18:26:22.461Z","dependency_job_id":"d145fa64-00aa-4d1d-84e0-bc2ec5d63bbb","html_url":"https://github.com/nthnn/diwa","commit_stats":null,"previous_names":["nthnn/diwa"],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/nthnn/diwa","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nthnn%2Fdiwa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nthnn%2Fdiwa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nthnn%2Fdiwa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nthnn%2Fdiwa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nthnn","download_url":"https://codeload.github.com/nthnn/diwa/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nthnn%2Fdiwa/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267436687,"owners_count":24086898,"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","status":"online","status_checked_at":"2025-07-27T02:00:11.917Z","response_time":82,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["arduino","arduino-library","artificial-neural-network","esp32","esp32-arduino","esp32-wroom","neural-network","neural-networks","platformio-arduino","platformio-library","psp-sdk","wasm","wasm-library"],"created_at":"2024-11-13T19:23:16.190Z","updated_at":"2025-07-27T22:34:47.808Z","avatar_url":"https://github.com/nthnn.png","language":"C++","funding_links":["https://github.com/sponsors/nthnn","https://www.buymeacoffee.com/nthnn"],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://github.com/nthnn/diwa/blob/main/logo.png\" width=\"200\" /\u003e\n    \u003cbr/\u003e\u003cbr/\u003e\n    \u003ch1\u003eDiwa: Tiny AI/ML Library\u003c/h1\u003e\n\u003c/p\u003e\n\n![GCC Build CI](https://github.com/nthnn/diwa/actions/workflows/gcc_build_ci.yml/badge.svg)\n![Emscripten Build CI](https://github.com/nthnn/diwa/actions/workflows/emcc_build_ci.yml/badge.svg)\n![Arduino CI](https://github.com/nthnn/diwa/actions/workflows/arduino_ci.yml/badge.svg)\n![Arduino Lint](https://github.com/nthnn/diwa/actions/workflows/arduino_lint.yml/badge.svg)\n![Arduino Release](https://img.shields.io/badge/Library%20Manager-v0.0.8-red?logo=Arduino)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/nthnn/diwa/blob/main/LICENSE)\n\nDiwa is a lightweight library providing a simple implementation of Feedforward Artificial Neural Networks (ANNs) for microcontrollers such as ESP32, ESP8266, RP2040, and similar development boards (specially boards with PSRAM); it is also compatible for desktop environments (Windows, MacOS, and Linux-based OSes), WebAssembly, and even PSP gaming consoles. It is designed for resource-constrained environments but can be used with non-Arduino platform projects, offering a streamlined solution for tasks that require neural network capabilities.\n\nDiwa stands out as a straightforward and effective solution for implementing artificial neural networks on microcontrollers. Key features include:\n\n- **Lightweight**: Designed for resource-constrained microcontroller environments yet can still be used within non-Arduino environments.\n- **Simple Implementation**: Provides a basic yet effective implementation of a Feedforward ANN.\n- **Easy Integration**: Suitable for microcontrollers like ESP8266, ESP32, and RP2040. Also compatible with desktop environments, WebAssembly, and even PSP gaming console.\n- **Training Support**: Includes methods for training the neural network using backpropagation.\n\n\u003e [!NOTE]\n\u003e Diwa is primarily intended for lightweight applications. For more intricate tasks, consider using advanced machine learning libraries on more powerful platforms.\n\nSee live demo on [Wokwi](https://wokwi.com/projects/387551593748039681).\n\n## Architecture/Platform Support\n\nDiwa are tested on the following architecture/platform:\n\n| Arch/Platform                              | Remarks                                              |\n|--------------------------------------------|------------------------------------------------------|\n| ✅ ESP32-WROOM\u003cbr/\u003e✅ ESP32-WROVER         | NodeMCU DevKit (Automatically using PSRAM available) |\n| ✅ ESP8266                                 | [Wokwi](https://wokwi.com/) Emulation                |\n| ✅ RP2040                                  | Raspberry Pi Pico (RP2040)                           |\n| 🔼 PSP                                     | PPSSPP Emulator (Diwa::loadFromFile and Diwa::saveToFile not yet supported)  |\n| ✅ Desktop Environments                    | Works perfectly on Windows, MacOS, and Linux.        |\n| ✅ WebAssembly (WASM)                      | Tested via Emscripten                                |\n\n## Getting Started\n\n## Docker\n\nYou can have a quickstart by running the Docker image.\n\n```shell\ndocker run nthnn/diwa:0.0.7\n```\n\n### On Arduino Platform\n\nTo start using Diwa library in your Arduino projects, follow these simple steps:\n\n1. Open your Library Manager on Arduino IDE.\n2. Type `diwa` and click \"Install.\"\n\nAlternatively, you can follow the steps below:\n\n1. Download the Diwa library from the GitHub repository.\n2. Extract the downloaded archive and rename the folder to \"diwa\".\n3. Move the \"diwa\" folder to the Arduino libraries directory on your computer.\n    - Windows: `Documents\\Arduino\\libraries\\`\n    - MacOS: `~/Documents/Arduino/libraries/`\n    - Linux: `~/Arduino/libraries/`\n4. Launch the Arduino IDE.\n\n### On C++ Projects (Including Emscripten)\n\nUsing Diwa on C++ projects might be quite different depending on what build tools you are using.\n\n1. Include the Diwa git repository as submodule on your project's `lib` folder.\n\n```bash\ngit submodule add https://github.com/nthnn/diwa.git\n```\n\nTo do this, your project must be a git repository.\n\n2. Add the `lib/diwa/src` folder on your project's `Makefile`.\n3. Add the `*.cpp` files on the build source files.\n\n\u003e [!NOTE]\n\u003e Alternatively, you can download the `*.deb` file from the [release](https://github.com/nthnn/diwa/releases) page to install Diwa as a shared library on Debian-based operating systems.\n\n### PlayStation Portable SDK\n\nSame on every C++ projects, depending on your build process, this might be quite different on some instances.\n\n1. Add `--std=c++17` on the compiler arguments.\n2. Include the `src` folder on the include arguments.\n3. Finally, include the `*.cpp` files from the `src` folder on compilation.\n\nOr you can check the [examples/psp_xor/build.bat](examples/psp_xor/build.bat) for reference.\n\n## Examples\n\nTo access the examples:\n\n1. Open the Arduino IDE.\n2. Click on `File \u003e Examples \u003e diwa` to see the list of available examples.\n3. Upload the example sketch to your Arduino board and see the results in action.\n\nHere's a full example usage for an Arduino environment:\n```cpp\n#include \u003cdiwa.h\u003e\n\nvoid setup() {\n    // Initialize serial communication with a baud rate of 115200\n    Serial.begin(115200);\n\n    // Define training input and output data for XOR operation\n    double trainingInput[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};\n    double trainingOutput[4][1] = {{1}, {0}, {0}, {1}};\n\n    // Create an instance of the Diwa neural network with 2 input neurons,\n    // 1 hidden layer with 3 neurons, and 1 output neuron\n    Diwa network;\n    if(network.initialize(2, 1, 3, 1) == NO_ERROR)\n        Serial.println(F(\"Done initializing neural network.\"));\n    else {\n        Serial.println(F(\"Something went wrong initializing neural network.\"));\n        while(true);\n    }\n\n    // Train the network for 3000 epochs using the XOR training data\n    Serial.println(F(\"Starting training...\"));\n    for(uint32_t epoch = 0; epoch \u003c 10000; epoch++) {\n        // Train the network for each set of input and target output values\n        network.train(6, trainingInput[0], trainingOutput[0]);\n        network.train(6, trainingInput[1], trainingOutput[1]);\n        network.train(6, trainingInput[2], trainingOutput[2]);\n        network.train(6, trainingInput[3], trainingOutput[3]);\n\n        // Show accuracy and loss on training for every 100th epoch\n        if(epoch % 1000 == 0) {\n            double accuracy = 0.0, loss = 0.0;\n\n            // Calculate accuracy and loss for each training sample\n            for(uint8_t i = 0; i \u003c 4; i++) {\n                accuracy += network.calculateAccuracy(trainingInput[i], trainingOutput[i], 3);\n                loss += network.calculateLoss(trainingInput[i], trainingOutput[i], 3);\n            }\n\n            // Average accuracy and loss over all samples\n            accuracy /= 4, loss /= 4;\n\n            // Print the accuracy and loss\n            Serial.print(F(\"Epoch: \"));\n            Serial.print(epoch);\n            Serial.print(F(\", accuracy: \"));\n            Serial.print(accuracy * 100);\n            Serial.print(F(\"%, loss: \"));\n            Serial.print(loss * 100);\n            Serial.println(F(\"%\"));\n        }\n    }\n    Serial.println(F(\"Training done!\"));\n\n    // Perform inference on the trained network and print the results\n    Serial.println(F(\"Testing inferences...\"));\n    for(uint8_t i = 0; i \u003c 4; i++) {\n        // Get the current input row\n        double* row = trainingInput[i];\n\n        // Perform inference using the trained network\n        double* inferred = network.inference(row);\n\n        // Print the result for the current input\n        char str[100];\n        sprintf(str, \"Output for [%1.f, %1.f]: %1.f (%g)\\n\", row[0], row[1], inferred[0], inferred[0]);\n        Serial.print(str);\n    }\n}\n\nvoid loop() {\n    delay(10);\n}\n```\n\n## Contribution and Feedback\n\nContributions and feedback are all welcome to enhance this library. If you encounter any issues, have suggestions for improvements, or would like to contribute code, please do so.\n\n## License\n\nCopyright 2023 - Nathanne Isip\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnthnn%2Fdiwa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnthnn%2Fdiwa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnthnn%2Fdiwa/lists"}