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# include <FS.h>
47#else
48# include <fstream>
49# include <stdint.h>
50#endif
51
52#include <math.h>
53
60typedef enum {
69 #ifdef ARDUINO
70 NO_ESP_PSRAM
71 #endif
72} DiwaError;
73
92class Diwa {
93private:
94 int inputNeurons;
95 int hiddenNeurons;
96 int hiddenLayers;
97 int outputNeurons;
99 int weightCount;
100 int neuronCount;
102 double *weights;
103 double *outputs;
104 double *deltas;
111 void randomizeWeights();
112
119 DiwaError initializeWeights();
120
121public:
129
137
155 int inputNeurons,
156 int hiddenLayers,
157 int hiddenNeurons,
158 int outputNeurons,
159 bool randomizeWeights = true
160 );
161
174 double* inference(double *inputs);
175
189 void train(
190 double learningRate,
191 double *inputNeurons,
192 double *outputNeurons
193 );
194
195 #ifdef ARDUINO
196
207 DiwaError loadFromFile(File annFile);
208
219 DiwaError saveToFile(File annFile);
220
221 #else
222
233 DiwaError loadFromFile(std::ifstream& annFile);
234
245 DiwaError saveToFile(std::ofstream& annFile);
246
247 #endif
248};
249
250#endif // DIWA_H
Lightweight Feedforward Artificial Neural Network (ANN) library tailored for microcontrollers.
Definition diwa.h:92
double * inference(double *inputs)
Perform inference on the neural network.
DiwaError loadFromFile(std::ifstream &annFile)
Load neural network model from file in non-Arduino environment.
void train(double learningRate, double *inputNeurons, double *outputNeurons)
Train the neural network using backpropagation.
DiwaError initialize(int inputNeurons, int hiddenLayers, int hiddenNeurons, int outputNeurons, bool randomizeWeights=true)
Initializes the Diwa neural network with specified parameters.
~Diwa()
Destructor for the Diwa class.
Diwa()
Default constructor for the Diwa class.
DiwaError saveToFile(std::ofstream &annFile)
Save neural network model to file in non-Arduino environment.
DiwaError
Enumeration representing various error codes that may occur during the operation of the Diwa library.
Definition diwa.h:60
@ MALLOC_FAILED
Definition diwa.h:67
@ MODEL_SAVE_ERROR
Definition diwa.h:64
@ STREAM_NOT_OPEN
Definition diwa.h:66
@ MODEL_READ_ERROR
Definition diwa.h:63
@ NO_ERROR
Definition diwa.h:61
@ INVALID_PARAM_VALUES
Definition diwa.h:62
@ INVALID_MAGIC_NUMBER
Definition diwa.h:65