<jupyter_start><jupyter_text>Experiments on FedMNIST **Colab Support**Only run the following lines if you want to run the code on Google Colab<jupyter_code># Enable access to files stored in Google Drive
from google.colab import drive

drive.mount('/content/gdrive/')
% cd /content/gdrive/My Drive/OPT4ML/src<jupyter_output>/content/gdrive/My Drive/OPT4ML/src<jupyter_text>Main<jupyter_code># Install necessary requirements
!pip install -r ../requirements.txt
# Make sure cuda support is available
import torch

if torch.cuda.is_available():
    device_name = "cuda:0"
else:
    device_name = "cpu"
print("device_name: {}".format(device_name))
device = torch.device(device_name)
%load_ext autoreload
%autoreload 2
import sys
import warnings

warnings.filterwarnings("ignore")

from server import Server
from utils import plot_exp

import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [6, 6]
plt.rcParams['figure.dpi'] = 100<jupyter_output><empty_output><jupyter_text>First experiment : impact of federated learning<jupyter_code>LR = 0.01
EPOCHS = 1
NR_TRAINING_ROUNDS = 30
BATCH_SIZE = 128

RANGE_NR_CLIENTS = [1,5,10]
experiment_losses, experiment_accs = [], []
for nr_clients in RANGE_NR_CLIENTS:
    print(f"### Number of clients : {nr_clients} ###\n\n")
    server = Server(
        nr_clients=nr_clients,
        nr_training_rounds=NR_TRAINING_ROUNDS,
        data='MNIST',
        epochs=EPOCHS,
        lr=LR,
        batch_size=BATCH_SIZE,
        is_private=False,
        epsilon=None,
        max_grad_norm=None,
        noise_multiplier=None,
        is_parallel=True,
        device=device,
        verbose='server')

    test_losses, test_accs = server.train()
    
    experiment_losses.append(test_losses)
    experiment_accs.append(test_accs)
names = [f'{i} clients' for i in RANGE_NR_CLIENTS]
title = 'First experiment : MNIST database'
fig = plot_exp(experiment_losses, experiment_accs, names, title)
fig.savefig("MNIST_exp1.pdf")<jupyter_output><empty_output><jupyter_text>Second experiment : impact of differential privacy<jupyter_code>NR_CLIENTS = 10
NR_TRAINING_ROUNDS = 30
EPOCHS = 1
LR = 0.01
BATCH_SIZE = 128

MAX_GRAD_NORM = 1.2
NOISE_MULTIPLIER = None

RANGE_EPSILON = [10,50,100]
experiment_losses, experiment_accs = [], []
for epsilon in RANGE_EPSILON:
    print(f"### ε : {epsilon} ###\n\n")
    server = Server(
        nr_clients=NR_CLIENTS,
        nr_training_rounds=NR_TRAINING_ROUNDS,
        data='MNIST',
        epochs=EPOCHS,
        lr=LR,
        batch_size=BATCH_SIZE,
        is_private=True,
        epsilon=epsilon,
        max_grad_norm=MAX_GRAD_NORM,
        noise_multiplier=NOISE_MULTIPLIER,
        is_parallel=True,
        device=device,
        verbose='server')

    test_losses, test_accs = server.train()
    experiment_losses.append(test_losses)
    experiment_accs.append(test_accs)
names = [f'ε = {i}' for i in RANGE_EPSILON]
title = 'Second experiment : MNIST database'
fig = plot_exp(experiment_losses, experiment_accs, names, title)
plt.savefig('MNIST_exp2.pdf')<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup