Code
Hub
Workspaces
Following
Trending
Connect
MCP
copy
Create free account
hub
/
github.com/chalakpaneer/feddefender
/ functions
Functions
83 in github.com/chalakpaneer/feddefender
⨍
Functions
83
◇
Types & classes
12
↓ 6 callers
Function
_style
()
utils/plotter.py:31
↓ 6 callers
Method
train
Run all `num_rounds` FL rounds. Parameters ---------- callback : optional function called after each round
models/fl_server.py:274
↓ 4 callers
Method
__init__
(self, in_dim: int, out_dim: int, dropout: float = 0.2)
models/transformer_gnn.py:57
↓ 4 callers
Function
fedavg
w_global = (1/N) Σ w_i (Eq. 7) If client_sizes provided, uses weighted average (proportional to local dataset siz
aggregation/aggregator.py:41
↓ 4 callers
Function
flatten_weights
Stack all parameter tensors into a single 1-D numpy array.
aggregation/aggregator.py:22
↓ 3 callers
Method
_evaluate_global
Evaluate global model accuracy on (X, y). If adversarial=True, first perturb with PGD.
models/fl_server.py:122
↓ 3 callers
Method
get_all_reputations
(self)
blockchain/reputation.py:192
↓ 3 callers
Method
get_trusted_clients
Returns list of client IDs whose reputation ≥ τ. Only these clients' updates are aggregated.
blockchain/reputation.py:176
↓ 2 callers
Method
_build_node_features
Approximate per-node features by repeating the batch mean. In production, each node would have its own traffic summary.
models/fl_client.py:106
↓ 2 callers
Method
_compute_hash
(self)
blockchain/reputation.py:42
↓ 2 callers
Function
_generate_demo_history
Generate realistic synthetic history for demo mode.
dashboard/app.py:87
↓ 2 callers
Method
get_excluded_clients
(self)
blockchain/reputation.py:186
↓ 2 callers
Method
get_weights
Return local model weights. If malicious: inject poisoned updates instead.
models/fl_client.py:92
↓ 2 callers
Method
is_valid
Verify chain integrity by recomputing all hashes.
blockchain/reputation.py:95
↓ 2 callers
Method
set_weights
Load global model weights into the local model.
models/fl_client.py:88
↓ 2 callers
Method
to_dict
(self)
blockchain/reputation.py:54
↓ 2 callers
Method
update
Apply Eq. 9: R_i ← λ·R_i + (1-λ)·S_i Record on ledger.
blockchain/reputation.py:150
↓ 1 callers
Method
_adversarial_robustness
Robustness = Acc(adversarial) / Acc(clean) (Eq. 12) Uses val set with PGD perturbation.
models/fl_server.py:161
↓ 1 callers
Method
_compute_benign_mean
Compute mean weights over currently trusted clients.
models/fl_server.py:314
↓ 1 callers
Method
_compute_score
S_i = weighted combination of: • accuracy – how well the client's update improved validation acc • consistency – cosin
blockchain/reputation.py:141
↓ 1 callers
Method
_cosine_filter
Returns indices of clients that pass the cosine filter. If no global model yet → accept all.
aggregation/aggregator.py:190
↓ 1 callers
Method
_genesis_block
(self)
blockchain/reputation.py:76
↓ 1 callers
Function
_serialise
(obj)
main.py:75
↓ 1 callers
Method
add_record
(self, round_num: int, client_id: int, score: float, reputation: float)
blockchain/reputation.py:82
↓ 1 callers
Method
aggregate
Full anomaly-aware aggregation pipeline. Returns ------- aggregated global weights
aggregation/aggregator.py:215
↓ 1 callers
Function
apply_backdoor_trigger
Injects a fixed-pattern trigger into the last `trigger_features` dimensions of every sample. In a backdoor attack the adversary trains w
attacks/adversarial.py:104
↓ 1 callers
Function
build_graph_edges
Builds a random Erdős–Rényi edge index for the IoT device graph G = (V, E) as described in Section III-A. Returns ------- edge_i
data/dataset.py:96
↓ 1 callers
Function
compute_final_metrics
Aggregate summary metrics from the full training history. Returns ------- dict with final accuracy, robustness, malicious detection
utils/metrics.py:45
↓ 1 callers
Function
compute_update_consistency
Measures how similar a client's update is to the benign cluster mean using normalised cosine similarity. High similarity → trustworthy u
blockchain/reputation.py:211
↓ 1 callers
Function
fgsm_attack
Fast Gradient Sign Method (Eq. 5): δ = ε · sign( ∇_x J(θ, x, y) ) x_adv = x + δ Parameters ---------- model : Th
attacks/adversarial.py:24
↓ 1 callers
Function
generate_adversarial_samples
Wrapper that generates adversarial samples for adversarial training (Section III-B). Randomly mixes clean and adversarial samples based
attacks/adversarial.py:141
↓ 1 callers
Function
generate_synthetic_data
Creates a labelled synthetic traffic dataset. Each class has its own mean vector so that the five traffic categories are distinguishable
data/dataset.py:31
↓ 1 callers
Method
get_log
(self)
aggregation/aggregator.py:260
↓ 1 callers
Method
get_recent_records
(self, n: int = 50)
blockchain/reputation.py:111
↓ 1 callers
Function
krum
Byzantine-resilient Krum aggregation (Eq. 8). Algorithm: 1. Flatten each client's weights → vectors v_1 … v_N 2. For each v_i co
aggregation/aggregator.py:64
↓ 1 callers
Function
load_or_run
Load pre-computed results from results/metrics.json or generate synthetic demonstration data.
dashboard/app.py:74
↓ 1 callers
Method
local_train
Run `local_epochs` of training on the local partition. For benign clients → adversarial training (FGSM/PGD) For malicious c
models/fl_client.py:117
↓ 1 callers
Function
main
()
main.py:41
↓ 1 callers
Function
multi_krum
Multi-Krum: selects the top-m clients by Krum score and averages their updates. More robust than single-Krum for larger federations.
aggregation/aggregator.py:115
↓ 1 callers
Function
partition_data_noniid
Non-IID partition via Dirichlet distribution (Dir(α)). Lower α → more heterogeneous label distributions. Returns ------- List of
data/dataset.py:141
↓ 1 callers
Function
pgd_attack
Projected Gradient Descent (Eq. 6): x_t+1 = Proj_ε( x_t + α · sign( ∇_x J(θ, x_t, y) ) ) Stronger iterative version of FGSM.
attacks/adversarial.py:64
↓ 1 callers
Function
plot_accuracy_curve
(history: dict, save_path: str = "accuracy_curve.png")
utils/plotter.py:41
↓ 1 callers
Function
plot_client_reputations
(rep_mgr, malicious_ids: Set[int], save_path: str =
utils/plotter.py:189
↓ 1 callers
Function
plot_malicious_detection
(history: dict, malicious_ids: Set[int], save_path
utils/plotter.py:74
↓ 1 callers
Function
plot_reputation_heatmap
(history: dict, save_path: str = "reputation_heatmap.png")
utils/plotter.py:112
↓ 1 callers
Function
plot_robustness_comparison
(history: dict, save_path: str = "robustness.png")
utils/plotter.py:143
↓ 1 callers
Function
poison_model_weights
Simulates a Byzantine client that submits deliberately corrupted weight updates. Strategy: scale weights by a large random factor + Gaus
attacks/adversarial.py:121
↓ 1 callers
Function
prepare_data
End-to-end data preparation: 1. Generate (or load) raw data 2. Standardise features 3. Train / val / test split 4. Partit
data/dataset.py:177
↓ 1 callers
Function
print_summary
Pretty-print the final metrics summary.
utils/metrics.py:95
↓ 1 callers
Method
run_round
Execute one complete federated learning round.
models/fl_server.py:177
↓ 1 callers
Function
trimmed_mean
Coordinate-wise trimmed mean. Removes the top and bottom `trim_ratio` fraction of values at each coordinate before averaging.
aggregation/aggregator.py:149
Method
__getitem__
(self, idx)
data/dataset.py:89
Method
__init__
(self, X: np.ndarray, y: np.ndarray, seq_len: int = 10)
data/dataset.py:80
Method
__init__
(self, index: int, round_num: int, client_id: int, score: float, reputation:
blockchain/reputation.py:30
Method
__init__
(self)
blockchain/reputation.py:73
Method
__init__
(self, num_clients: int, cfg: dict)
blockchain/reputation.py:128
Method
__init__
(self, client_id: int, X: np.ndarray, y: np.ndarray,
models/fl_client.py:47
Method
__init__
(self, d_model: int, max_len: int = 512, dropout: float = 0.1)
models/transformer_gnn.py:25
Method
__init__
(self, in_dim: int, hidden_dim: int, num_layers: int, dropout: float = 0.2)
models/transformer_gnn.py:91
Method
__init__
(self, input_dim: int, d_model: int, nhead: int, num_layers: int, dropout: f
models/transformer_gnn.py:110
Method
__init__
(self, cfg: dict)
models/transformer_gnn.py:154
Method
__init__
(self, fl_cfg: dict, model_cfg: dict, adv_cfg: dict,
models/fl_server.py:44
Method
__init__
(self, method: str = "krum", f: int = 4, cosine_threshold: float = 0.0)
aggregation/aggregator.py:183
Method
__len__
(self)
data/dataset.py:86
Function
compare_with_baselines
Baseline numbers from Table II and Table III of the paper. Used for the comparison chart.
utils/metrics.py:112
Function
compute_detection_accuracy
Eq. 10: Accuracy = (TP + TN) / (TP + TN + FP + FN)
utils/metrics.py:15
Function
compute_f1
(precision: float, recall: float)
utils/metrics.py:39
Function
compute_fpr
Eq. 11: FPR = FP / (FP + TN)
utils/metrics.py:24
Function
compute_robustness
Eq. 12: Robustness = Accuracy(adversarial) / Accuracy(clean)
utils/metrics.py:32
Method
evaluate
Evaluate local model on validation / test data. Returns accuracy (float in [0, 1]).
models/fl_client.py:198
Method
forward
(self, x: torch.Tensor)
models/transformer_gnn.py:41
Method
forward
h : (num_nodes, in_dim) edge_index : (2, num_edges) — [source_nodes, target_nodes]
models/transformer_gnn.py:64
Method
forward
(self, h: torch.Tensor, edge_index: torch.Tensor)
models/transformer_gnn.py:100
Method
forward
x : (batch, seq_len, input_dim) Returns H : (batch, d_model) — Eq. 2
models/transformer_gnn.py:129
Method
forward
x_seq : (batch, seq_len, input_dim) — sequential traffic node_feats : (num_nodes, input_dim) — per-device features
models/transformer_gnn.py:192
Method
get_history
(self, client_id: int)
blockchain/reputation.py:105
Method
get_weights
Return model parameters as a flat list for FL aggregation.
models/transformer_gnn.py:219
Function
partition_data_iid
IID partition: each client receives an equal, random slice. Returns ------- List of (X_i, y_i) tuples, one per client.
data/dataset.py:125
Function
plot_convergence_comparison
Static chart replicating Table IV.
utils/plotter.py:229
Method
reset_client
Restore a client to initial reputation (manual override).
blockchain/reputation.py:195
Method
set_weights
Load aggregated global weights back into the model.
models/transformer_gnn.py:223
Method
snapshot
Dict snapshot for logging / dashboard.
blockchain/reputation.py:199
Function
unflatten_weights
Reconstruct the weight list from a flat numpy array.
aggregation/aggregator.py:27