Diwa: Tiny AI/ML Library

Diwa is a lightweight library providing a simple implementation of Feedforward Artificial Neural Networks (ANNs) for microcontrollers such as ESP8266, ESP32, RP2040, and similar development boards (specially boards with PSRAM); it is also compatible for desktop environments (tested only on Windows OS) and even PSP gaming console. 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.
Diwa stands out as a straightforward and effective solution for implementing artificial neural networks on microcontrollers. Key features include:
- Lightweight: Designed for resource-constrained microcontroller environments yet can still be used within non-Arduino environments.
- Simple Implementation: Provides a basic yet effective implementation of a Feedforward ANN.
- Easy Integration: Suitable for microcontrollers like ESP8266, ESP32, and RP2040. Also compatible with desktop environments and even PSP gaming console.
- Training Support: Includes methods for training the neural network using backpropagation.
Diwa is primarily intended for lightweight applications. For more intricate tasks, consider using advanced machine learning libraries on more powerful platforms.
See live demo on Wokwi.
Architecture/Platform Support
Diwa are tested on the following architecture/platform:
Arch/Platform | Remarks |
✅ ESP32-WROOM
✅ ESP32-WROVER | NodeMCU DevKit |
✅ ESP8266 | Wokwi Emulation |
✅ RP2040 | Raspberry Pi Zero |
✅ PSP | PPSSPP Emulator |
🔼 AMD64 | Works on Windows, segmentation fault on Linux systems |
Getting Started
On Arduino
To start using Diwa library in your Arduino projects, follow these simple steps:
- Download the Diwa library from the GitHub repository.
- Extract the downloaded archive and rename the folder to "diwa".
- Move the "diwa" folder to the Arduino libraries directory on your computer.
- Windows:
Documents\Arduino\libraries\
- MacOS:
~/Documents/Arduino/libraries/
- Linux:
~/Arduino/libraries/
- Launch the Arduino IDE.
Examples
To access the examples:
- Open the Arduino IDE.
- Click on
File > Examples > diwa
to see the list of available examples.
- Upload the example sketch to your Arduino board and see the results in action.
Here's a full example usage for an Arduino environment.
void setup() {
Serial.begin(115200);
double trainingInput[4][2] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
double trainingOutput[4][1] = {{1}, {0}, {0}, {1}};
Serial.println(F("Done initializing neural network."));
else {
Serial.println(F("Something went wrong initializing neural network."));
while(true);
}
Serial.println(F("Starting training..."));
for(uint32_t epoch = 0; epoch < 10000; epoch++) {
network.
train(6, trainingInput[0], trainingOutput[0]);
network.
train(6, trainingInput[1], trainingOutput[1]);
network.
train(6, trainingInput[2], trainingOutput[2]);
network.
train(6, trainingInput[3], trainingOutput[3]);
if(epoch % 1000 == 0) {
double accuracy = 0.0, loss = 0.0;
for(uint8_t i = 0; i < 4; i++) {
loss += network.
calculateLoss(trainingInput[i], trainingOutput[i], 3);
}
accuracy /= 4, loss /= 4;
Serial.print(F("Epoch: "));
Serial.print(epoch);
Serial.print(F(", accuracy: "));
Serial.print(accuracy * 100);
Serial.print(F("%, loss: "));
Serial.print(loss * 100);
Serial.println(F("%"));
}
}
Serial.println(F("Training done!"));
Serial.println(F("Testing inferences..."));
for(uint8_t i = 0; i < 4; i++) {
double* row = trainingInput[i];
char str[100];
sprintf(str, "Output for [%1.f, %1.f]: %1.f (%g)\n", row[0], row[1], inferred[0], inferred[0]);
Serial.print(str);
}
}
void loop() {
delay(10);
}
Lightweight Feedforward Artificial Neural Network (ANN) library tailored for microcontrollers.
Definition diwa.h:98
double * inference(double *inputs)
Perform inference on the neural network.
Definition diwa.cpp:140
double calculateAccuracy(double *testInput, double *testExpectedOutput, int epoch)
Calculates the accuracy of the neural network on test data.
Definition diwa.cpp:478
void train(double learningRate, double *inputNeurons, double *outputNeurons)
Train the neural network using backpropagation.
Definition diwa.cpp:192
double calculateLoss(double *testInput, double *testExpectedOutput, int epoch)
Calculates the loss of the neural network on test data.
Definition diwa.cpp:487
DiwaError initialize(int inputNeurons, int hiddenLayers, int hiddenNeurons, int outputNeurons, bool randomizeWeights=true)
Initializes the Diwa neural network with specified parameters.
Definition diwa.cpp:75
This file contains the declaration of the Diwa class, a lightweight Feedforward Artificial Neural Net...
@ NO_ERROR
Definition diwa.h:71
Contribution and Feedback
Contributions 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.
License
Copyright 2023 - Nathanne Isip
Permission 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:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE 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.