# Training Models

> Train machine learning models using the prepared dataset.

Now that we have a dataset, we can proceed to train machine learning models on Nordic Edge AI Lab.

<tip>

If you haven't already, [Register](https://app.lab.nordicsemi.com/) for an Nordic Edge AI Lab account.<br />


See the [Nordic Edge AI Lab documentation](https://docs.nordicsemi.com/r/bundle/edge-ai-lab/page/registration_and_login.html)
for more details.

</tip>

The next steps are:

1. Uploading the dataset to Nordic Edge AI Lab.
2. Configuring the model training parameters.
3. Initiating the training process.
4. Evaluating the trained model.
5. Repeat and Refine.

For this demo, we'll use a very basic sample here, to only use accelerometer data (no magnitudes).
Each dataset and usecase is different.

<steps level="2">

## Creating a "Solution"

Nordic Edge AI Lab treats "Solutions" as the project to train a model.
Additionally, datasets can only be uploaded from a solution, so that's where we'll start.
See the [Nordic Edge AI Lab documentation](https://docs.nordicsemi.com/r/bundle/edge-ai-lab/page/get_started.html/signing-in-and-creating-a-solution)
for more details on Solutions.

<steps level="3">

### Create a new solution

Log in and from the home page, select "Add New Solution", select "Classification" and set a name.

### Dataset

Follow the prompts to upload your dataset (or select an existing one).
Once a dataset is uploaded, it can be reused in other solutions.

<tip>

Compressing large datasets into a .zip file can drastically speed the upload process.

</tip>

### Solution Dataset Configuration

For this solution, there are some additional dataset configurations.

After selecting the model, you can either include a Holdout validation subset
(or allow Nordic Edge AI Lab to auto-generate it from the original dataset).
Using the auto-holdout is easiest.

<tip>

Smaller datasets benefit from using a manual Holdout validation subset to ensure reliable evaluation while training the model.
This manifests as the validation set missing certain labels with fewer instances.
Otherwise, you can manually create a validation set to ensure all labels are adequately represented.

</tip>

Finally, we can indicate which columns are labels and which are metadata.
The options are:

- **Target Column**: The column containing the labels (`label_id`).
- **Session ID**: The column containing the session/window identifiers. This is what allows
the training process to performing sliding window operations correctly.
- **Remove**: Select columns to be excluded from the model training.
This is useful for metadata or for intentionally removing features inputs from training, such as only training on magnitude.

</steps>

For this demo, use the automatic holdout validation subset. Then set the following:

- Target Column to `label_id`,
- Session ID to `window_id`,
- Tick the box for the `acc_magnitude` column to remove it from training.

## Feature Extraction

Nordic's Edge AI Lab provides (optional) built-in feature extraction capabilities to transform raw data into meaningful features for model training.
This immensely simplifies the process, but still have a lot to configure and is not a silver bullet.

Feature Extraction takes place across each column, however does not consider cross-column interactions.
This is why something like the *Magnitude* feature needs to be manually calculated.

Nordic Edge AI Lab provides the following high level options:

### Windowing

The size of the input window is a critical parameter in feature extraction. It controls the amount of temporal context captured from the raw data.
It's key to ensure that the behaviours of interest are adequately captured within the chosen window size, especially for longer duration activities.
However it's a balancing act.
A larger window captures more context but increases computational latency. Additionally, multiple events may occur back to back but could be merged into a single window.
But in turn, a smaller window may miss important context, leading to incomplete feature representation.

The following options can be tuned:

- Auto-detecting optimal window size.
- Sliding window with a specified step size for both training and inference. This refers to overlap between consecutive windows.

  - When using for training, maximises data from the dataset, and randomises the starting point of the intput buffer.
  - When enabled for inference, utilises more ram for the larger input buffer, but can improve robustness and reduce sensitivity to shorter events.

For the demo, leave auto-detection on, enter the sample rate of the dataset and leave the sliding shift empty for automatic detection.

### Sub-windowing

Subwindowing can be used to further divide the main input window into smaller segments, allowing for more granular feature extraction.
For example a single tap, double tap and triple tap are highly temporal and plainly looking at statistical features have limited discriminative power.
This could be simplified using sub-windows and a simpler tap detection approach.
By detecting a tap, some idle window and subsequent taps, the model can focus on the relevant segments, improving both accuracy and efficiency.

For the demo, enable sub-windowing and set it to 3 sub-windows & enable "Include full-window features". This number can be increased if using higher sampling rates.

### Raw Data Training

Instead of extracting features, raw data training uses the raw sensor data instead. This can sometimes be useful and trains more on the temporal signals,
rather than relying on precomputed features.

For the demo leave this disabled.

### Feature Extraction

There are a list of features available split into "Time" and "Frequency" domains.
Time series data impose fewer limitations.

Comparatively Frequency domain features utilise FFTs and restrict other settings (like Sliding Shift and Subwindows).
Simliarly, the input window size is limited to powers of two ranging from 128-2048.

<tip>

FFTs can be quite computationally intensive, so it's important to consider the trade-offs when using frequency domain features.
When using FFTs, an easy way to minimise computations is to perform the FFT on the magnitude (and not the individual axes).
This has to be manually configured by selecting "edit" in the Feature Extraction list for all Frequency Domain Features.

</tip>

The "Feature Selection" can be used to determine which selected features are relevant by calculating correlation to the labelled data.
As a result it can be useful to select all available features and enable this option.

For the demo, enable all time-domain features and enable "Feature Selection".

### Input Data

The model itself can utilise raw integer or floating point data.
Integer values are smaller than using floating point data and reduce the model's RAM footprint, since
less memeory is required for the input buffer.

However, using smaller values, limits the input range. For example 3-axis accelerometer data is native 16 bit
(ranging from -32768 to 32767), however without preprocessing, magnitudes can overflow beyond this, requiring 32 bit integers (which aren't supported).

For the demo select the Floating point option.

<warning>

You may receive a warning that your values are outside the valid range.
This will be due to the values in the magnitude column.
This error still shows even if the magnitude column is excluded.

</warning>

### Considerations

There are a number of options to dial in, and these will all depend on the quality of dataset and the signals being classified themselves.
However, set a configuration and test how it goes, you can always retry, change settings and refine the process.

<warning>

Depending on the settings, certain dataset windows may be too short.<br />


Similarly, the 25Hz sample rate may be too low. If so, try increasing the sample rate.

</warning>

## Training

The final step is to train the model. There are a few remaining configurations:

### Weights and coefficients

This is used to configure the model's internal types (int8, int16, or Floating Point).
Benefits of using Quantised Integer:

- Reduces model size
- Can increase processing speed by using [SIMD](https://en.wikipedia.org/wiki/Single_instruction,_multiple_data).

However Floating Point options:

- Supports a wider range of input values.
- Can improve model accuracy for certain tasks.
- Simpler and easier to use
- These microcontrollers have an FPU, so there's no massive computation penalty.

Similarly, the output value (probabilities) can be configured as:

- Single-Precision 32-bit floating point value (from 0-1) or,
- An unsigned 8 bit integer (with 0-1 represented as 0-255) or,
- an unsigned 16 bit integer (with 0-1 represented as 0-65535).

For this model, select the Quantised 16 bit integer for both input and output.

### Early Stopping

Training can be stopped early if the model's performance on a validation set stops improving. This helps prevent overfitting and can save training time.
This can be based on exceeding a maximum training time, model accuracy or model size.

For this model, these can be left as their default values.

### Target Hardware

Once the model is trained, it is prepared for the target hardware.
The nRF54L15 hardware uses the Arm Cortex-M M33 core.
Enable the "target hardware" your hardware uses (or others you intend to use the model on later).

### Start Training

Once all configurations are set, you can start the training process by selecting "Start New Training".
Depending on the success, and selections, the training process can take from 15 minutes to several hours.
Even training failure can take an hour or so.

## Repeat and Refine

Tweaking the training process is a little tricker. Nordic Edge AI Lab does not allow a solution to be changed or tweaked after training has commenced.

Instead, head to the home page or the "My Solutions" page in the menu, and for the given Solution, select the Settings icon and Select "Copy".
This will not copy all configurations across, but will create a solution with most of the settings.

Repeat, tweak and refine the process till you have a suitable model.
Alternatively, refine and expand your dataset.

With a suitable model trained, you can proceed to deploy it on your target hardware.

</steps>
