Models

This page documents the model components of ICOS-FL, particularly the classes and functions defined in icos_fl.models.lstm.

LSTM Model Components

class icos_fl.models.lstm.LSTMModel(hidden_layer_size, time_step, num_layers, output_size=1)

LSTM model for time series prediction.

Parameters:
  • hidden_layer_size (int) – Size of the LSTM hidden layer

  • time_step (int) – Number of time steps (sequence length) for LSTM input

  • num_layers (int) – Number of LSTM layers

  • output_size (int, optional) – Size of the output layer (default is 1 for single value prediction)

forward(input_seq)

Forward pass through the network.

Parameters:

input_seq (torch.Tensor) – Input tensor

Returns:

Output tensor of predictions

Return type:

torch.Tensor

Model Utility Functions

icos_fl.models.lstm.get_weights(model)

Extract model weights as a list of NumPy arrays.

Parameters:

model (nn.Module) – PyTorch model

Returns:

List of NumPy arrays containing model weights

Return type:

List[np.ndarray]

icos_fl.models.lstm.set_weights(model, weights)

Set model weights from a list of NumPy arrays.

Parameters:
  • model (nn.Module) – PyTorch model

  • weights (List[np.ndarray]) – List of NumPy arrays containing weights

icos_fl.models.lstm.train(model, train_dataloader, epochs, lr=0.001, criterion=None, optimizer=None)

Train the LSTM model.

Parameters:
  • model (LSTMModel) – The LSTM model to train

  • train_dataloader (DataLoader) – DataLoader containing training data

  • epochs (int) – Number of training epochs

  • lr (float, optional) – Learning rate, defaults to 0.001

  • criterion (nn.Module, optional) – Loss function (defaults to MSE if None)

  • optimizer (optim.Optimizer, optional) – Optimizer (defaults to Adam if None)

Returns:

Average training loss

Return type:

float

icos_fl.models.lstm.test(model, test_dataloader, device)

Evaluate model on the test dataset.

Parameters:
  • model (LSTMModel) – The LSTM model to evaluate

  • test_dataloader (DataLoader) – DataLoader containing test data

  • device (torch.device) – PyTorch device for computation

Returns:

Average test loss

Return type:

float

Model Architecture

The default LSTM model architecture in ICOS-FL consists of:

  1. LSTM Layer: Processes input sequences - Input size: time_step (sequence length) - Hidden size: hidden_layer_size (configurable) - Number of layers: num_layers (configurable)

  2. Linear Layer: Produces final predictions - Input size: hidden_layer_size - Output size: output_size (defaults to 1)

The model configuration can be adjusted through the pyproject.toml file:

[tool.flwr.app.config]
# LSTM model configuration
hidden-layer-size = 10
time-step = 10
num-layers = 1

Example Usage

from icos_fl.models.lstm import LSTMModel, train, test, get_weights, set_weights
import torch

# Create model
model = LSTMModel(
    hidden_layer_size=10,
    time_step=10,
    num_layers=1
)

# Train model
train_loss = train(
    model=model,
    train_dataloader=train_dataloader,
    epochs=100,
    lr=0.001
)

# Evaluate model
test_loss = test(
    model=model,
    test_dataloader=test_dataloader,
    device=torch.device("cpu")
)

# Get model weights (for federated learning)
weights = get_weights(model)

# Set model weights (from federated learning)
set_weights(model, weights)