Diwa
Lightweight implementation of Artificial Neural Network for resource-constrained environments
Loading...
Searching...
No Matches
diwa.h
Go to the documentation of this file.
1/*
2 * This file is part of the Diwa library.
3 * Copyright (c) 2024 Nathanne Isip
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
41#ifndef DIWA_H
42#define DIWA_H
43
44#ifdef ARDUINO
45# include <Arduino.h>
46# if defined(ARDUINO_ARCH_ESP32) || \
47 defined(ARDUINO_ARCH_ESP8266) || \
48 defined(ARDUINO_ARCH_RP2040)
49# include <FS.h>
50# else
51# include <SD.h>
52# endif
53#elif defined(__GNUC__) || \
54 defined(__GNUG__) || \
55 defined(__clang__) || \
56 defined(_MSC_VER)
57# include <fstream>
58# include <math.h>
59#endif
60
61#include <diwa_activations.h>
62#include <stdint.h>
63
79
98class Diwa final {
99private:
100 int inputNeurons;
101 int hiddenNeurons;
102 int hiddenLayers;
103 int outputNeurons;
105 int weightCount;
106 int neuronCount;
108 double *weights;
109 double *outputs;
110 double *deltas;
112 diwa_activation activation;
122 void randomizeWeights();
123
136 DiwaError initializeWeights();
137
149 bool testInference(double *testInput, double *testExpectedOutput);
150
151public:
158 Diwa();
159
166 ~Diwa();
167
185 int inputNeurons,
186 int hiddenLayers,
187 int hiddenNeurons,
188 int outputNeurons,
189 bool randomizeWeights = true
190 );
191
204 double* inference(double *inputs);
205
219 void train(
220 double learningRate,
221 double *inputNeurons,
222 double *outputNeurons
223 );
224
225 #ifdef ARDUINO
226
237 DiwaError loadFromFile(File annFile);
238
249 DiwaError saveToFile(File annFile);
250
251 #elif DOXYGEN
252
264
276
277 #elif defined(__GNUC__) || \
278 defined(__GNUG__) || \
279 defined(__clang__) || \
280 defined(_MSC_VER)
281
292 DiwaError loadFromFile(std::ifstream& annFile);
293
304 DiwaError saveToFile(std::ofstream& annFile);
305
306 #endif
307
321 double calculateAccuracy(double *testInput, double *testExpectedOutput, int epoch);
322
336 double calculateLoss(double *testInput, double *testExpectedOutput, int epoch);
337
349 void setActivationFunction(diwa_activation activation);
350
364
377
393 int recommendedHiddenLayerCount(int numSamples, int alpha);
394
403 int getInputNeurons() const;
404
414 int getHiddenNeurons() const;
415
424 int getHiddenLayers() const;
425
435 int getOutputNeurons() const;
436
446 int getWeightCount() const;
447
457 int getNeuronCount() const;
458
469 void getWeights(double* weights);
470
481 void getOutputs(double* outputs);
482};
483
484#endif // DIWA_H
Lightweight Feedforward Artificial Neural Network (ANN) library tailored for microcontrollers.
Definition diwa.h:98
int getHiddenNeurons() const
Get the number of neurons in the hidden layer.
Definition diwa.cpp:523
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
DiwaError loadFromFile(T annFile)
Load neural network model from file in Arduino environment.
int getNeuronCount() const
Get the total number of neurons in the neural network.
Definition diwa.cpp:539
void getOutputs(double *outputs)
Retrieve the outputs of the neural network.
Definition diwa.cpp:547
diwa_activation getActivationFunction() const
Retrieves the current activation function used by the neural network.
Definition diwa.cpp:495
int getHiddenLayers() const
Get the number of hidden layers in the neural network.
Definition diwa.cpp:527
DiwaError saveToFile(T annFile)
Save neural network model to file in Arduino environment.
void train(double learningRate, double *inputNeurons, double *outputNeurons)
Train the neural network using backpropagation.
Definition diwa.cpp:192
int getOutputNeurons() const
Get the number of output neurons in the neural network.
Definition diwa.cpp:531
int recommendedHiddenLayerCount(int numSamples, int alpha)
Calculates the recommended number of hidden layers based on the dataset size and complexity.
Definition diwa.cpp:506
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
~Diwa()
Destructor for the Diwa class.
Definition diwa.cpp:62
Diwa()
Default constructor for the Diwa class.
Definition diwa.cpp:57
void setActivationFunction(diwa_activation activation)
Sets the activation function for the neural network.
Definition diwa.cpp:491
void getWeights(double *weights)
Retrieve the weights of the neural network.
Definition diwa.cpp:543
int getWeightCount() const
Get the total number of weights in the neural network.
Definition diwa.cpp:535
int getInputNeurons() const
Get the number of input neurons in the neural network.
Definition diwa.cpp:519
int recommendedHiddenNeuronCount()
Calculates the recommended number of hidden neurons based on the input and output neurons.
Definition diwa.cpp:499
DiwaError
Enumeration representing various error codes that may occur during the operation of the Diwa library.
Definition diwa.h:70
@ MALLOC_FAILED
Definition diwa.h:77
@ MODEL_SAVE_ERROR
Definition diwa.h:74
@ STREAM_NOT_OPEN
Definition diwa.h:76
@ MODEL_READ_ERROR
Definition diwa.h:73
@ NO_ERROR
Definition diwa.h:71
@ INVALID_PARAM_VALUES
Definition diwa.h:72
@ INVALID_MAGIC_NUMBER
Definition diwa.h:75
Defines activation functions for use in the Diwa neural network.
double(* diwa_activation)(double)
Typedef for activation function pointer.
Definition diwa_activations.h:59