## 5 Application of Multilayer Architecture
### Implementation  
The system was implemented and analyzed using Oracle Cloud Infrastructure (OCI) ([[https://www.oracle.com/it/cloud/](https://www.oracle.com/it/cloud/)]([https://www.oracle.com/it/cloud/](https://www.oracle.com/it/cloud/)), accessed on 14 September 2024). This infrastructure offers x86-based virtual machines and ARM64-based virtual machines. We initially implemented our cluster using three ARM-based virtual machines and connected them using a private network. That is, after that we installed the k3s controller and rancher in one of the machines, using the remaining two as k3s workers. To facilitate the usage of the cluster, we also instantiated rancher inside the cluster. Rancher is a Kubernetes management platform, which enables the administrator to graphically analyze and use the cluster without dealing with advanced Kubernetes configurations from the command line. This enabled us to automatically manage the infrastructure, adding and removing nodes easily. To achieve the federated learning and to run the models we installed Flower [28] inside the container images. This tool, which is a framework for creating federated learning models, removes the daunting task of adapting our model to the updates sent by the users. This is implemented by using two docker containers: flower-superlink, the main module that aggregates the workers and flower-supernode, the worker itself that runs the data-gathering and model update application. The implementation is pictured in Figure 1. In this picture, the containers, the infrastructure and the controller are visible. The controller possesses its initial dataset which can be distributed to the users (in production the dataset will be augmented with user personal data or private dataset). The nodes use the flower as the machine learning platform to analyze the data and aggregate the result to the flower superlink. Obviously, more pods than one per worker can be instantiated. The user can then access the superlink to obtain the result of the learning and the outcome of the learning process. The dataset is non-IID distributed over the machines using Flower. That is, every node obtains a subset of data that is distributed in a non-identical fashion. In Listing 1 the code extract that performs the distribution is present and the data are equally distributed over the nodes. ```
defsplit_data_for_federated_learning(data,num_clients): data_per_client=len(data[0])//num_clients datasets=[(data[0][i*data_per_client:(i+1)*data_per_client], data[1][i*data_per_client:(i+1)*data_per_client]) foriinrange(num_clients)] returndatasets
```  
The data are computed on the various nodes and sent to a super link using Flower. This approach enables the distribution to an arbitrary number of nodes, without associating directly the data with the nodes. Before this, the data are randomly shuffled to avoid overfitting due to sorted data. After this step, the training is performed, every client then independently analyzes part of the dataset, training its internal models with the provided data. That is, the client analyzes the data and sends out the weight to the main collector using flower Superlink. In Listing 2 we present this extract of code, conducting exactly this part of implementation:  
```
fori,(x_train,y_train)inenumerate(federated_datasets): client_models[i].fit(x_train,y_train,epochs=1,verbose=0) client_weights.append(client_models[i].get_weights())
```  
Listing 2: Federated Learning Algorithm. Line 1 runs over all the provided datasets for every client, analyzes them and retrieves the updated weight. The epoch is set to 1 due to the fact that these runs are replicated for the number of desired epochs and put in a for loop. The updated weights are then sent to the various federated clients, updating every one of them, and providing an updated model to every one of them. Using this technique, the nodes can have an updated and performant model without requiring specialized hardware or an enormous amount of resources such as GPUs, CPUs, RAM, secondary memory support, money, or time.