N2CMU Arduino
N2Coprocessor Class Reference

Class representing the N2CMU device. More...

#include <n2cmu.h>

Public Member Functions

 N2Coprocessor (uint8_t rx=N2CMU_RX_PIN, uint8_t tx=N2CMU_TX_PIN)
 Constructor for N2Coprocessor class. More...
 
bool begin ()
 Initialize the N2CMU device. More...
 
bool handshake ()
 Perform handshake with the N2CMU device. More...
 
bool cpuReset ()
 Reset the CPU of the N2CMU device. More...
 
void createNetwork (uint8_t inputCount, uint8_t hiddenCount, uint8_t outputCount)
 Create a neural network with specified input, hidden, and output neuron counts. More...
 
bool train (float *data, float *output, uint16_t len, float learningRate)
 Train the neural network with provided data and output. More...
 
bool infer (float *input, float *output)
 Make inference with the neural network using provided input data. More...
 
void resetNetwork ()
 Reset the neural network parameters. More...
 
void setInputCount (uint8_t inputCount)
 Set the number of input neurons. More...
 
void setHiddenCount (uint8_t hiddenCount)
 Set the number of hidden neurons. More...
 
void setOutputCount (uint8_t outputCount)
 Set the number of output neurons. More...
 
void setEpochCount (uint16_t epoch)
 Set the epoch count for training. More...
 
uint8_t getInputCount ()
 Get the number of input neurons. More...
 
uint8_t getHiddenCount ()
 Get the number of hidden neurons. More...
 
uint8_t getOutputCount ()
 Get the number of output neurons. More...
 
uint16_t getEpochCount ()
 Get the epoch count for training. More...
 
bool setHiddenNeuron (float *hiddenNeuron)
 Set hidden neuron values. More...
 
bool setOutputNeuron (float *outputNeuron)
 Set output neuron values. More...
 
bool setHiddenWeights (float *hiddenWeights)
 Set hidden neuron weights. More...
 
bool setOutputWeights (float *outputWeights)
 Set output neuron weights. More...
 
bool setHiddenBias (float *hiddenBias)
 Set hidden neuron biases. More...
 
bool setOutputBias (float *outputBias)
 Set output neuron biases. More...
 
bool setHiddenGradient (float *hiddenGrad)
 Set hidden neuron gradients. More...
 
bool setOutputGradient (float *outputGrad)
 Set output neuron gradients. More...
 
void getHiddenNeuron (float *hiddenNeuron)
 Get hidden neuron values. More...
 
void getOutputNeuron (float *outputNeuron)
 Get output neuron values. More...
 
void getHiddenWeights (float *hiddenWeights)
 Get hidden neuron weights. More...
 
void getOutputWeights (float *outputWeights)
 Get output neuron weights. More...
 
void getHiddenBias (float *hiddenBias)
 Get hidden neuron biases. More...
 
void getOutputBias (float *outputBias)
 Get output neuron biases. More...
 
void getHiddenGradient (float *hiddenGrad)
 Get hidden neuron gradients. More...
 
void getOutputGradient (float *outputGrad)
 Get output neuron gradients. More...
 
void loadFromFile (const char *modelFilename)
 
void saveToFile (const char *modelFilename)
 

Detailed Description

Class representing the N2CMU device.

The N2Coprocessor class offers an interface for controlling and interacting with the N2CMU shield device. It encapsulates functions for initialization, configuration, training, inference, and parameter manipulation of neural networks.

Constructor & Destructor Documentation

◆ N2Coprocessor()

N2Coprocessor::N2Coprocessor ( uint8_t  rx = N2CMU_RX_PIN,
uint8_t  tx = N2CMU_TX_PIN 
)
inline

Constructor for N2Coprocessor class.

Constructs a new N2Coprocessor object with the specified RX and TX pins for serial communication.

Parameters
rxPin number for receiving data from N2CMU.
txPin number for transmitting data to N2CMU.

Member Function Documentation

◆ begin()

bool N2Coprocessor::begin ( )

Initialize the N2CMU device.

This function initializes the N2CMU device by establishing communication and performing necessary setup operations. It must be called before any other operations are performed on the N2CMU device.

Returns
True if initialization was successful, false otherwise.

◆ cpuReset()

bool N2Coprocessor::cpuReset ( )

Reset the CPU of the N2CMU device.

This function resets the CPU of the N2CMU device, restoring it to a known state. It can be useful for recovering from unexpected errors or initializing the device before starting a new operation.

Returns
True if CPU reset was successful, false otherwise.

◆ createNetwork()

void N2Coprocessor::createNetwork ( uint8_t  inputCount,
uint8_t  hiddenCount,
uint8_t  outputCount 
)

Create a neural network with specified input, hidden, and output neuron counts.

This function creates a neural network model with the specified number of input, hidden, and output neurons. It initializes the network architecture for subsequent training and inference tasks.

Parameters
inputCountNumber of input neurons.
hiddenCountNumber of hidden neurons.
outputCountNumber of output neurons.

◆ getEpochCount()

uint16_t N2Coprocessor::getEpochCount ( )

Get the epoch count for training.

This function retrieves the number of training epochs currently set for the neural network.

Returns
Epoch count for training.

◆ getHiddenBias()

void N2Coprocessor::getHiddenBias ( float *  hiddenBias)

Get hidden neuron biases.

This function retrieves the biases of the hidden neurons in the neural network. It allows obtaining the current biases of the hidden neurons during or after training or inference.

Parameters
hiddenBiasArray to store hidden neuron biases.

◆ getHiddenCount()

uint8_t N2Coprocessor::getHiddenCount ( )

Get the number of hidden neurons.

This function retrieves the number of hidden neurons currently set for the neural network.

Returns
Number of hidden neurons.

◆ getHiddenGradient()

void N2Coprocessor::getHiddenGradient ( float *  hiddenGrad)

Get hidden neuron gradients.

This function retrieves the gradients of the hidden neurons in the neural network. It allows obtaining the current gradients of the hidden neurons during or after training.

Parameters
hiddenGradArray to store hidden neuron gradients.

◆ getHiddenNeuron()

void N2Coprocessor::getHiddenNeuron ( float *  hiddenNeuron)

Get hidden neuron values.

This function retrieves the values of the hidden neurons in the neural network. It allows obtaining the current state of the hidden neurons during or after training or inference.

Parameters
hiddenNeuronArray to store hidden neuron values.

◆ getHiddenWeights()

void N2Coprocessor::getHiddenWeights ( float *  hiddenWeights)

Get hidden neuron weights.

This function retrieves the weights of the connections between input and hidden neurons in the neural network. It allows obtaining the current weights of these connections during or after training or inference.

Parameters
hiddenWeightsArray to store hidden neuron weights.

◆ getInputCount()

uint8_t N2Coprocessor::getInputCount ( )

Get the number of input neurons.

This function retrieves the number of input neurons currently set for the neural network.

Returns
Number of input neurons.

◆ getOutputBias()

void N2Coprocessor::getOutputBias ( float *  outputBias)

Get output neuron biases.

This function retrieves the biases of the output neurons in the neural network. It allows obtaining the current biases of the output neurons during or after training or inference.

Parameters
outputBiasArray to store output neuron biases.

◆ getOutputCount()

uint8_t N2Coprocessor::getOutputCount ( )

Get the number of output neurons.

This function retrieves the number of output neurons currently set for the neural network.

Returns
Number of output neurons.

◆ getOutputGradient()

void N2Coprocessor::getOutputGradient ( float *  outputGrad)

Get output neuron gradients.

This function retrieves the gradients of the output neurons in the neural network. It allows obtaining the current gradients of the output neurons during or after training.

Parameters
outputGradArray to store output neuron gradients.

◆ getOutputNeuron()

void N2Coprocessor::getOutputNeuron ( float *  outputNeuron)

Get output neuron values.

This function retrieves the values of the output neurons in the neural network. It allows obtaining the current state of the output neurons during or after training or inference.

Parameters
outputNeuronArray to store output neuron values.

◆ getOutputWeights()

void N2Coprocessor::getOutputWeights ( float *  outputWeights)

Get output neuron weights.

This function retrieves the weights of the connections between hidden and output neurons in the neural network. It allows obtaining the current weights of these connections during or after training or inference.

Parameters
outputWeightsArray to store output neuron weights.

◆ handshake()

bool N2Coprocessor::handshake ( )

Perform handshake with the N2CMU device.

This function performs a handshake with the N2CMU device to ensure proper communication and synchronization. It verifies the connection and readiness of the N2CMU device for subsequent operations.

Returns
True if handshake was successful, false otherwise.

◆ infer()

bool N2Coprocessor::infer ( float *  input,
float *  output 
)

Make inference with the neural network using provided input data.

This function performs inference with the trained neural network using the provided input data. It computes the output of the network based on the input and returns the result.

Parameters
inputPointer to the input data array.
outputPointer to store the output data array.
Returns
True if inference was successful, false otherwise.

◆ resetNetwork()

void N2Coprocessor::resetNetwork ( )

Reset the neural network parameters.

This function resets the parameters of the neural network to their initial values. It clears any previously learned weights, biases, or gradients, effectively resetting the network to its initial state.

◆ setEpochCount()

void N2Coprocessor::setEpochCount ( uint16_t  epoch)

Set the epoch count for training.

This function sets the number of training epochs for the neural network. An epoch represents one complete pass through the entire training dataset.

Parameters
epochEpoch count for training.

◆ setHiddenBias()

bool N2Coprocessor::setHiddenBias ( float *  hiddenBias)

Set hidden neuron biases.

This function sets the biases of the hidden neurons in the neural network. It allows specifying the initial biases of the hidden neurons before training or inference.

Parameters
hiddenBiasArray of hidden neuron biases.
Returns
True if setting was successful, false otherwise.

◆ setHiddenCount()

void N2Coprocessor::setHiddenCount ( uint8_t  hiddenCount)

Set the number of hidden neurons.

This function sets the number of hidden neurons for the neural network. It allows dynamically configuring the hidden layer of the network to adjust its capacity and complexity.

Parameters
hiddenCountNumber of hidden neurons.
Note
After calling this function, all the network data such as weights, biases, and gradients will return to default 0 values.

◆ setHiddenGradient()

bool N2Coprocessor::setHiddenGradient ( float *  hiddenGrad)

Set hidden neuron gradients.

This function sets the gradients of the hidden neurons in the neural network. It allows specifying the initial gradients of the hidden neurons before training or inference.

Parameters
hiddenGradArray of hidden neuron gradients.
Returns
True if setting was successful, false otherwise.

◆ setHiddenNeuron()

bool N2Coprocessor::setHiddenNeuron ( float *  hiddenNeuron)

Set hidden neuron values.

This function sets the values of the hidden neurons in the neural network. It allows specifying the initial values of the hidden neurons before training or inference.

Parameters
hiddenNeuronArray of hidden neuron values.
Returns
True if setting was successful, false otherwise.

◆ setHiddenWeights()

bool N2Coprocessor::setHiddenWeights ( float *  hiddenWeights)

Set hidden neuron weights.

This function sets the weights of the connections between input and hidden neurons in the neural network. It allows specifying the initial weights of these connections before training or inference.

Parameters
hiddenWeightsArray of hidden neuron weights.
Returns
True if setting was successful, false otherwise.

◆ setInputCount()

void N2Coprocessor::setInputCount ( uint8_t  inputCount)

Set the number of input neurons.

This function sets the number of input neurons for the neural network. It allows dynamically configuring the input layer of the network to match the requirements of the input data.

Parameters
inputCountNumber of input neurons.
Note
After calling this function, all the network data such as weights, biases, and gradients will return to default 0 values.

◆ setOutputBias()

bool N2Coprocessor::setOutputBias ( float *  outputBias)

Set output neuron biases.

This function sets the biases of the output neurons in the neural network. It allows specifying the initial biases of the output neurons before training or inference.

Parameters
outputBiasArray of output neuron biases.
Returns
True if setting was successful, false otherwise.

◆ setOutputCount()

void N2Coprocessor::setOutputCount ( uint8_t  outputCount)

Set the number of output neurons.

This function sets the number of output neurons for the neural network. It allows dynamically configuring the output layer of the network to match the requirements of the output data.

Parameters
outputCountNumber of output neurons.
Note
After calling this function, all the network data such as weights, biases, and gradients will return to default 0 values.

◆ setOutputGradient()

bool N2Coprocessor::setOutputGradient ( float *  outputGrad)

Set output neuron gradients.

This function sets the gradients of the output neurons in the neural network. It allows specifying the initial gradients of the output neurons before training or inference.

Parameters
outputGradArray of output neuron gradients.
Returns
True if setting was successful, false otherwise.

◆ setOutputNeuron()

bool N2Coprocessor::setOutputNeuron ( float *  outputNeuron)

Set output neuron values.

This function sets the values of the output neurons in the neural network. It allows specifying the initial values of the output neurons before training or inference.

Parameters
outputNeuronArray of output neuron values.
Returns
True if setting was successful, false otherwise.

◆ setOutputWeights()

bool N2Coprocessor::setOutputWeights ( float *  outputWeights)

Set output neuron weights.

This function sets the weights of the connections between hidden and output neurons in the neural network. It allows specifying the initial weights of these connections before training or inference.

Parameters
outputWeightsArray of output neuron weights.
Returns
True if setting was successful, false otherwise.

◆ train()

bool N2Coprocessor::train ( float *  data,
float *  output,
uint16_t  len,
float  learningRate 
)

Train the neural network with provided data and output.

This function trains the neural network using the provided input data and corresponding output. It adjusts the network parameters based on the training data to improve its performance and accuracy.

Parameters
dataPointer to the input data array.
outputPointer to the output data array.
lenLength of the data arrays.
learningRateLearning rate for training.
Returns
True if training was successful, false otherwise.

The documentation for this class was generated from the following files: