> For the complete documentation index, see [llms.txt](https://medomicslab.gitbook.io/medfl-app-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://medomicslab.gitbook.io/medfl-app-docs/tutorials/simulation/hyperparameters-optimization.md).

# Hyperparameters optimization

{% hint style="info" %}
In this tutorial, we will learn how to use MEDfl to optimize the hyperparameters of the model and apply them effectively.
{% endhint %}

## Introduction

We will explore how to use **MEDfl** to optimize the hyperparameters of your machine-learning models. [Hyperparameter optimization](https://en.wikipedia.org/wiki/Hyperparameter_optimization) is a crucial step in enhancing the performance of your models. MEDfl provides robust tools to streamline this process, allowing you to fine-tune your models efficiently. By the end of this tutorial, you will understand how to leverage **MEDfl** for hyperparameter optimization and utilize the optimized parameters to improve your model's accuracy and effectiveness.&#x20;

**Let's get started!**

## Video tutorial

{% embed url="<https://youtu.be/q3__0SdatiA>" %}

## Optimization methods&#x20;

MEDfl gives the possibility to optimize hyperparameters with 3 different methods&#x20;

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2FFakpNpWY3eKMjgCiSrIa%2Fimage.png?alt=media&amp;token=25fa204e-4553-4a6a-88a4-eb7d7d497208" alt=""><figcaption></figcaption></figure>

* **Grid Search Optimization**: A straightforward method where the user specifies a list of metrics to optimize, such as the number of layers, hidden size, and others.

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2FVe8jVOcD69Lrj3qSILq7%2Fimage.png?alt=media&amp;token=d066b11a-c7a2-4f46-bf63-b75b58f60a2a" alt="" width="257"><figcaption></figcaption></figure>

* **Optuna Central Optimization**: Utilizes Optuna to optimize parameters on the central server. Users can specify Optuna parameters like metric, direction, optimization algorithm, and intervals for each hyperparameter.

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2FcKmi0B4VnMgclZhbGDbH%2Fimage.png?alt=media&amp;token=2db93ee4-3fe0-438d-9ef4-2afbb3db4244" alt="" width="247"><figcaption></figcaption></figure>

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2F1P2Vstr6JoLQpjMLsFo8%2Fimage.png?alt=media&amp;token=755bf85b-70fc-42fd-af36-b60fa2a09ffa" alt=""><figcaption></figcaption></figure>

* **Optuna Federated Optimization**: Uses Optuna for hyperparameter optimization in a federated manner. Optimization occurs during the execution of the federated pipeline, adapting parameters based on distributed data.

## Run optimization

To run the optimization click on the run button and a new popup will show to verify the optimization configuration then click the button <mark style="color:yellow;">"Optimize hyperparameters"</mark>

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2FautyYQHDF6S3dRHRP4dJ%2Fimage.png?alt=media&amp;token=6e2a6377-9778-436d-8a0e-93a1c2413f6e" alt=""><figcaption></figcaption></figure>

After running the optimization you will see a progress bar showing the progress of the execution&#x20;

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2F2Vgrc05NFd6ca3hDPn7h%2Fimage.png?alt=media&amp;token=fe29d12f-7382-477f-b0da-0bb7f09e2752" alt=""><figcaption></figcaption></figure>

## Optimization results&#x20;

Once the execution is completed a popup will show the results&#x20;

* GRID SEARCH results : The results contains the best score and best hyperparameters&#x20;

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2FXAMd8dOJuoDXSHxQwrot%2Fimage.png?alt=media&amp;token=eb776ba7-45fc-45ac-ab24-33f04a09712e" alt=""><figcaption></figcaption></figure>

* Optuna Results: The results contain the best score and best hyperparameters and some statistical graphs&#x20;

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2FTzHwj4k1dIwxAzs7CknS%2Fimage.png?alt=media&amp;token=dac54407-e5b2-445c-9af1-83d894eacfdf" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2Fpe1K74nUcxXxienNvwYh%2Fimage.png?alt=media&amp;token=06ad8072-193f-4b08-86a9-19da9aa2ad4b" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2FWDlijkaoBRpDTu146hRC%2Fimage.png?alt=media&amp;token=efe36331-ea9b-4b31-a2ff-d483eb5921b3" alt=""><figcaption></figcaption></figure>

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2FkS5epl4fvyrGJJqVhTaS%2Fimage.png?alt=media&amp;token=b9299500-b39c-4cc1-ad1a-0d7b31b52afc" alt=""><figcaption></figcaption></figure>

## Save Optimization results&#x20;

MEDfl allows users to save the optimization results into files and visualize them on separate screens. This feature helps in easily tracking and analyzing the performance of different hyperparameter configurations.

to save the results click on the button "Save results" and an input will show to give the file name&#x20;

<figure><img src="https://2289920470-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOpYh7tOQkAu0q6hPpqe9%2Fuploads%2F2BMCmcUMvhxSNKNaiiSF%2FList%20of%20nodes%20(9).png?alt=media&amp;token=6e0a0ff2-36ce-4f94-b519-97b0fba46c15" alt=""><figcaption></figcaption></figure>

The application will save two files under the specified name (JSON and medflopt) in the "optimization" folder.

<pre><code><strong>EXPERIMENTS                          
</strong>    ├───FL                 
        └─── Optimization
             └─── optimization_results.json       -> json file 
             └─── optimization_results.medflopt   -> medflres file
</code></pre>

{% hint style="info" %}
You can visualize the file by double-clicking on them directly
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://medomicslab.gitbook.io/medfl-app-docs/tutorials/simulation/hyperparameters-optimization.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
