Switch to unified view

a b/federated_learning_healthcare.py
1
# Generate synthetic data (pretending it's medical data)
2
import torch
3
import numpy as np
4
5
# Generate synthetic data for two hospitals
6
np.random.seed(0)
7
hospital1_data = np.random.randn(100, 5)  # 100 samples, 5 features
8
hospital1_labels = (np.random.randn(100) > 0).astype(int)  # Binary labels for readmission risk
9
10
np.random.seed(1)
11
hospital2_data = np.random.randn(200, 5)  # 200 samples, 5 features
12
hospital2_labels = (np.random.randn(200) > 0).astype(int)  # Binary labels for readmission risk
13
14
# Implement the Federated Learning process
15
import syft as sy
16
from torch import nn, optim
17
18
# Hook PyTorch to PySyft
19
hook = sy.TorchHook(torch)
20
21
# Create virtual workers representing two hospitals
22
hospital1 = sy.VirtualWorker(hook, id="hospital1")
23
hospital2 = sy.VirtualWorker(hook, id="hospital2")
24
25
# Share synthetic data with respective hospitals
26
data_ptr_hospital1 = torch.tensor(hospital1_data).send(hospital1)
27
labels_ptr_hospital1 = torch.tensor(hospital1_labels).send(hospital1)
28
29
data_ptr_hospital2 = torch.tensor(hospital2_data).send(hospital2)
30
labels_ptr_hospital2 = torch.tensor(hospital2_labels).send(hospital2)
31
32
# Define the Federated Learning model (neural network)
33
class FederatedModel(nn.Module):
34
    def __init__(self):
35
        super(FederatedModel, self).__init__()
36
        self.fc = nn.Linear(5, 1)
37
38
    def forward(self, x):
39
        return torch.sigmoid(self.fc(x))
40
41
# Define the loss function and optimizer
42
model = FederatedModel()
43
criterion = nn.BCELoss()
44
optimizer = optim.SGD(model.parameters(), lr=0.1)
45
46
# Federated Learning training loop
47
epochs = 10
48
49
for epoch in range(epochs):
50
    # Train on hospital1 data
51
    model.send(hospital1)
52
    optimizer.zero_grad()
53
    pred_hospital1 = model(data_ptr_hospital1.float())
54
    loss_hospital1 = criterion(pred_hospital1.view(-1), labels_ptr_hospital1.float())
55
    loss_hospital1.backward()
56
    optimizer.step()
57
    model.get()
58
59
    # Train on hospital2 data
60
    model.send(hospital2)
61
    optimizer.zero_grad()
62
    pred_hospital2 = model(data_ptr_hospital2.float())
63
    loss_hospital2 = criterion(pred_hospital2.view(-1), labels_ptr_hospital2.float())
64
    loss_hospital2.backward()
65
    optimizer.step()
66
    model.get()
67
68
# Aggregate the models (average)
69
model_avg_params = (model.fc.weight.data + model.copy().send(hospital1).get().fc.weight.data) / 2
70
model_avg_bias = (model.fc.bias.data + model.copy().send(hospital1).get().fc.bias.data) / 2
71
72
# Update the model with averaged parameters
73
model_avg = FederatedModel()
74
model_avg.fc.weight.data = model_avg_params
75
model_avg.fc.bias.data = model_avg_bias
76
77
# Get the final model from any hospital (e.g., hospital1)
78
final_model = model_avg.get()
79
80
# For simplicity, let's use the same synthetic data from hospital1 as the test data
81
test_data = torch.tensor(hospital1_data)
82
test_labels = torch.tensor(hospital1_labels)
83
84
# Evaluate the federated model on the test data
85
with torch.no_grad():
86
    model_avg.eval()  # Set the model to evaluation mode
87
    predictions = model_avg(test_data.float()).round().squeeze().detach().numpy()
88
89
# Calculate accuracy
90
accuracy = (predictions == test_labels.numpy()).mean()
91
print("Accuracy:", accuracy)
92