The Future of AI: Federated Learning
A closer step towards Federated Learning
Federated Learning is a technique for training Deep Learning models on data to which you do not have access, so basically, instead of bringing all the data into one machine in the cloud and training a model. we’re going to bring that model to the data, train it locally wherever the data lives and merely upload model updates to the central server.
The idea is that whoever is creating the data will own the data and we can learn from them without learning about them. Pretty cool, eh?
The challenge of federated Learning is to learn a model with minimal information over-read between client and the curator
Is the model gonna keep learning as I use it?
Working on Federated Learning:
Let’s train a basic model in a conventional manner. All our data will be aggregated into our local machine and we can use it to make updates to our model.
Step 1: Importing all Dependencies
Step 2: Creating a dataset and model
Step 3: Training the model
# 1) erase previous gradients (if they exist)
# 2) make a prediction
# 3) calculate how much we missed
# 4) figure out which weights caused us to miss
# 5) change those weights
# 6) print our progress
Federated Learning, however, doesn’t work this way. So, let’s modify this example to do it the Federated Learning way!
Step 1. create a couple of workers
Step 2. get pointers to training data on each worker
Step 3. Updated training logic to do federated learning
# 1) send model to correct worker
# 2) train on the data located there
# 3) get the model back and repeat with next worker
Modifying the Conventional Example with Toy Dataset:
Step 1: Importing all Dependencies
import syft as sy
hook = sy.TorchHook(torch)# Create a couple of workers
bob = sy.VirtualWorker(hook, id="bob")
alice = sy.VirtualWorker(hook, id="alice")
Step 2: Creating dataset and model
# Toy Dataset
data = torch.tensor([[0,0],[0,1],[1,0],[1,1.]],requires_grad=True)
target = torch.tensor([[0],[0],[1],[1.]], requires_grad=True)# get pointers to training data on each worker
data_bob = data[0:2]
target_bob = target[0:2]
data_alice = data[2:]
target_alice = target[2:]
# Creating Toy Model
model = nn.Linear(2,1)# sending some training data to bob and alice
data_bob = data_bob.send(bob)
data_alice = data_alice.send(alice)
target_bob = target_bob.send(bob)
target_alice = target_alice.send(alice)
# organize pointers into a list
datasets = [(data_bob,target_bob),(data_alice,target_alice)]
Step 3: Training the Model
optimizer = optim.SGD(params=model.parameters(),lr=0.1)
for i in range(20):
# iterate through each worker's dataset for data,target in datasets:
# send model to correct worker
model.send(data.location)
# 1) erase previous gradients (if they exist)
optimizer.zero_grad()
# 2) make a prediction
pred = model(data)
# 3) calculate how much we missed
loss = ((pred - target)**2).sum()
# 4) figure out which weights caused us to miss
loss.backward()
# 5) change those weights
optimizer.step()
# get the model back (with gradients)
model.get()
# 6) print our progress
print(loss.get()) #use .get() to get loss
Conclusion
We now are training a conventional Deep Learning model using Federated Learning! Here, we send the model to each worker, generate a new gradient, and then bring the gradient back to our local server where we update our global model. In this process, we never request access to training data. Hence, We preserve the privacy of Bob and Alice!
References
[1] Comic Pictures taken from https://federated.withgoogle.com/
Thank You, If Found Useful Clap it! and Share it! to others in need!!