0% found this document useful (0 votes)
8 views2 pages

AutoEncoders for IoT Dataset Training

The document outlines the structure and components of a project folder named 'bfl_project' for implementing an AutoEncoders model using PyTorch. It includes instructions for setting up the project, installing necessary Python packages, and provides code snippets for training the model and simulating federated learning. Additionally, it references an IoT dataset available on Kaggle for use in the project.

Uploaded by

2453005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

AutoEncoders for IoT Dataset Training

The document outlines the structure and components of a project folder named 'bfl_project' for implementing an AutoEncoders model using PyTorch. It includes instructions for setting up the project, installing necessary Python packages, and provides code snippets for training the model and simulating federated learning. Additionally, it references an IoT dataset available on Kaggle for use in the project.

Uploaded by

2453005
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

project folder: folder called bfl_project

bfl_project/
├─ data/ ← where dataset will go
├─ models/ ← where your trained models will be saved
├─ results/ ← where results/graphs will go
├─ src/ ← your Python files will go here

Python packages:
pip install torch scikit-learn pandas numpy matplotlib tqdm

IoT Dataset (N-BaIoT)


👉 [Link]

AutoEncoders model (paper’s minimal AE):python program

import torch
import [Link] as nn

class SmallAE([Link]):
def __init__(self, input_dim=115):
super().__init__()
[Link] = [Link](
[Link](input_dim, 2),
[Link](),
[Link](2, 1),
[Link]()
)
[Link] = [Link](
[Link](1, 2),
[Link](),
[Link](2, input_dim),
[Link]()
)
def forward(self, x):
z = [Link](x)
out = [Link](z)
return out

Train AE parameters:

def train_local(model, train_loader, lr=1e-3, momentum=0.9, epochs=30,


device='cpu'):
opt = [Link]([Link](), lr=lr, momentum=momentum)
loss_fn = [Link]()
[Link](device)
for epoch in range(epochs):
[Link]()
for X in train_loader:
X = [Link](device)
opt.zero_grad()
recon = model(X)
loss = loss_fn(recon, X)
[Link]()
[Link]()
return model

Federated learning simulation


For distance:-

def flatten_weights(model):
return [Link]([[Link]().view(-1).cpu() for p in [Link]()])

def euclidean(a, b):


return [Link](a - b).item()

Aggregation:-
# Li: list of received client models (as state_dicts)
# Wi_i: centre local model state_dict
# alpha: alpha_i (0..1)
# nj: number of samples on client j (use equal weights or true sample counts)
def aggregate_models(Wi_i, client_models, nj_list, alpha):
# compute mi
mi = sum(nj_list)
# weighted sum of client params
agg = {}
for k in Wi_i.keys():
agg[k] = alpha * Wi_i[k].float()
# sum peer contributions
peer_sum = sum((nj/mi) * client_models[idx][k].float() for idx, nj in enumerate(nj_list))
agg[k] += (1 - alpha) * peer_sum
return agg

You might also like