How does Federated Learning train without raw data?

Authors
How does Federated Learning train without raw data?

Federated learning trains one shared model by sending the model to the devices that hold the data, and only the learned updates come back, never the raw data.

In this blog, we will learn about how federated learning trains a model without ever collecting our raw data. We will also see why training on private data is a problem, how one training round works step by step, how the averaging happens in code, how the updates are kept private in real systems, and where it is used.

We will cover the following:

  • The problem of training on private data
  • The naive fix and why it fails
  • What is federated learning?
  • How the round works step by step
  • A step-by-step numeric walkthrough
  • The averaging in code
  • Keeping the updates private
  • Where federated learning is used

I am Amit Shekhar, Founder @ Outcome School, I have taught and mentored many developers, and their efforts landed them high-paying tech jobs, helped many tech companies in solving their unique problems, and created many open-source libraries being used by top companies. I am passionate about sharing knowledge through open-source, blogs, and videos.

I teach AI and Machine Learning at Outcome School.

Let's get started.

The problem of training on private data

Suppose we are building a keyboard app that guesses the next word. To train a good model, we need real sentences that people actually type.

The sentences people type are deeply private. They contain addresses, secret messages, and personal chats. People do not want all of that leaving their phone.

The normal way to train a model is to gather all the data in one big place, like below:

Phone A typing data  --->  |              |
Phone B typing data  --->  |  BIG SERVER  |  ---> train model here
Phone C typing data  --->  |              |

Here, we can see that every phone sends its raw typing data to one big server. The server collects everything and trains the model on it.

This works for learning, but it creates a big risk. All our private text now sits on someone else's computer. If that server is hacked or misused, every private message is exposed.

So, the question is: can we train a good model without ever collecting people's raw data in one place?

The naive fix and why it fails

The most obvious idea is to hide the names before sending the data. Remove the person's name and send the rest of the text to the server. This is called anonymizing the data.

The raw text itself still gives the person away. A message like "My new home is at 5th street near the blue temple" points straight to a real person, even with no name attached. The private content is still there.

So, hiding the name does not help. The raw data is still leaving the phone, and the raw data is exactly the thing we wanted to protect.

The issue with this approach is that we are still moving the private data off the device. As long as the raw data travels, it can leak. Let's see how the next idea removes the travel completely.

What is federated learning?

So, here comes federated learning to the rescue.

Federated learning is a way to train one shared model by sending the learning to the data, instead of sending the data to one place.

A federation is a group of members that stay separate but work toward one shared goal, like many states forming one country. Federated learning is many devices that stay separate but train one shared model together.

Means, the raw data never leaves the phone. A model stores what it learns as numbers called weights. Each phone trains the model on its own data, and only the changes in the weights travel back, never the raw data itself.

Let's say each phone is a student doing homework at home. The teacher never collects the students' private notebooks. Instead, each student does the practice at home and sends back only the final answers. The teacher combines the answers to improve the lesson for everyone.

We can picture the flow as below:

+-----------------------------+
|       CENTRAL SERVER        |
|       (shared model)        |
+-----------------------------+
               |
     sends the model down
               ↓
+-------------+ +-------------+
|   Phone A   | |   Phone B   |
|  trains on  | |  trains on  |
|  own data   | |  own data   |
+-------------+ +-------------+
               |
   sends only the update back
         (no raw data)
               ↓
+-----------------------------+
|       CENTRAL SERVER        |
|    averages the updates     |
+-----------------------------+

Here, we can see that the server sends the shared model down to each phone. Each phone trains it on its own private data. Then each phone sends back only the small update, which means the change in the weights, never the raw text. No private text travels to the server.

Planning your move into AI?

No matter which tech domain you work in today, with your effort, you can transition to these roles:

  • AI Engineer
  • Forward Deployed Engineer
  • AI Agent Engineer
  • Harness Engineer
  • Voice AI Engineer
  • AI Architect
  • LLM Engineer
  • LLM Inference Engineer
  • Gen AI Engineer
  • MLOps Engineer
  • ML Engineer
  • Deep Learning Engineer

Our AI and Machine Learning Program at Outcome School takes you there.

Now, let's understand how one round of this actually works.

How the round works step by step

Federated learning happens in repeated rounds. Each round has the same shape.

First, the server sends the current shared model down to a group of devices. Every chosen device now holds the same starting model.

Then, each device trains that model on its own private data, right on the device. The phone improves the weights a little based only on what it sees locally.

After that, each device sends back only the update, which means how much the weights changed. The raw data never moves.

Finally, the server combines all the updates into one improved model. We will look at this averaging step closely soon. Then the next round begins with the better model.

So, the loop is: send model down, train on device, send update up, average. Repeat many times until the model is good.

We can picture this loop as below:

   +-------------------------------+
   |                               |
   ↓                               |
[1] Server sends model down        |
        |                          |
        ↓                          |
[2] Device trains on own data      |
        |                          |
        ↓                          |
[3] Device sends update up         |
        |                          |
        ↓                          |
[4] Server averages the updates    |
        |                          |
        +---- repeat next round ---+

Here, we can see that the four steps run in order and then loop back, so each new round starts from a better model than the last.

A quick note for you

No matter which tech domain you work in, get familiar with these topics:

  • LLM
  • RAG
  • MCP
  • Agent
  • Fine-tuning
  • Quantization

We put it all together in one video:

AI Engineering Explained: LLM, RAG, MCP, Agent, Fine-Tuning, and Quantization

No need to stop reading - bookmark it and watch later when you get time. Future you will thank you.

Now, let's get back to the topic.

This is how one round works. Now, let's see this with real numbers.

A step-by-step numeric walkthrough

Learning by example is the best way to learn. Let's take a tiny example with just one weight, so we can follow every number.

Assume that the shared model starts with one weight set to 10. The server sends this 10 down to three phones.

Step 1: Phone A trains on its own private data. Its data pushes the weight up to 12. So Phone A's update is +2.

Step 2: Phone B trains on its own private data. Its data pushes the weight up to 16. So Phone B's update is +6.

Step 3: Phone C trains on its own private data. Its data pulls the weight down to 7. So Phone C's update is -3.

Each phone sends back only its update number. The server never sees what data caused it.

The updates look like below:

Starting weight (sent to all): 10

Phone A new weight: 12   ->  update sent: +2
Phone B new weight: 16   ->  update sent: +6
Phone C new weight:  7   ->  update sent: -3

Here, we can see that the server received three plain numbers, +2, +6, and -3. It received no sentences, no text, and no private content. Just three updates.

Now the server averages the updates. It adds them and divides by the number of phones.

average update = (2 + 6 + (-3)) / 3
               = 5 / 3
               = 1.67 (rounded)

new shared weight = 10 + 1.67 = 11.67

Here, we can notice that the new shared weight became 11.67. This single weight now carries a little learning from all three phones at once, even though the server never touched their raw data. The next round will start from 11.67 and improve again.

This is exactly how the model gets better without seeing private data. Now, let's see the averaging in code.

The averaging in code

So, here comes the averaging step in code. This averaging method has a name. It is called Federated Averaging, often shortened to FedAvg.

FedAvg takes the updates from many devices and averages them into one improved model.

We have a detailed blog on distributed training that explains how many machines in a data center average their gradients to train one shared model.

Let's see the code for one round of federated averaging as below:

def federated_round(global_weight, device_updates):
    # device_updates is a list of weight changes from each device
    total = sum(device_updates)
    average_update = total / len(device_updates)
    new_global_weight = global_weight + average_update
    return new_global_weight

global_weight = 10
device_updates = [2, 6, -3]   # +2 from A, +6 from B, -3 from C

print(federated_round(global_weight, device_updates))

Here, we can see what we have done:

  • global_weight is the shared model weight that the server holds.
  • device_updates is the list of small updates, one from each device. It holds numbers, not raw data.
  • We add the updates and divide by how many there are to get the average_update.
  • We add that average to the global_weight to get the improved model.

This will print the following:

11.666666666666666

Here, we can notice that the result matches our hand calculation of 11.67. The server combined the learning from all three devices using only their update numbers. The raw data stayed on the devices.

Note:

  • A real model has millions of weights, not one. The server averages every weight the same way, all at once.
  • In real FedAvg, a device that trained on more data gets a bigger say in the average. A phone with 1000 sentences counts more than a phone with 10. For the sake of understanding, we used a plain average here.

If we want to go deep into Model Development and Training, Neural Networks, and Optimization and Scaling Techniques, check out our AI and Machine Learning Program at Outcome School.

This is how the averaging works. Now, let's make the updates even more private.

Keeping the updates private

We have stopped sending raw data. The update itself can sometimes leak a hint about the data that produced it.

So, here come two more tricks to the rescue.

The first trick is secure aggregation. The devices scramble their updates so that the server can only read the final sum, never any single device's update.

Let's say three friends want to know their average salary without telling each other their own salary. They can each add a secret random number, share only the scrambled totals, and the random numbers cancel out in the sum. The average comes out correct, yet no single salary was ever revealed. Secure aggregation works in that spirit.

The second trick is differential privacy. In one common setup, each device adds a tiny bit of random noise to its update before sending it. The noise is small enough that the average stays close to correct, but large enough that it becomes very hard to trace any single person's data back.

We can picture both tricks as below:

Raw update  --> add random noise (differential privacy)
            --> scramble it (secure aggregation)
            --> only the final SUM is readable by the server

Here, we can see that the update is first hidden with noise, then scrambled, so the server only ever learns the combined result. No single device's contribution can be pulled back out.

So, with these tricks, the updates are protected too.

Stay updated: Subscribe to our newsletter to get our latest AI and Machine Learning blogs straight to your inbox.

Now, let's see where federated learning is used.

Where federated learning is used

  • It lets many hospitals or banks train one shared model without sharing their sensitive records with each other.
  • It powers everyday features like next-word prediction on phone keyboards.
  • It helps companies follow strict privacy laws, because the sensitive data is not collected in one place.

This way we can use federated learning to solve the interesting problem of training a model on private data without ever collecting that data in one place.

Frequently Asked Questions

Is anonymizing the data enough to protect privacy?

No. Removing the person's name is not enough. The raw text itself can still give the person away, like a message that mentions a home address. As long as the raw data leaves the device, it can leak. Federated learning avoids this by keeping the raw data on the device.

What is FedAvg in federated learning?

FedAvg, short for Federated Averaging, is the method that combines the updates from many devices into one improved model. The server averages the weight changes it receives and adds that average to the shared model. A real model has millions of weights, and the server averages every weight the same way, all at once.

Does every device count equally in FedAvg?

No. In real FedAvg, a device that trained on more data gets a bigger say in the average. For example, a phone with 1000 sentences counts more than a phone with 10. A plain average, where every device counts the same, is only a simpler version used for understanding.

What is the difference between secure aggregation and differential privacy?

Secure aggregation scrambles the updates so that the server can only read the final sum, never any single device's update. With differential privacy, in one common setup, each device adds a tiny bit of random noise to its update, small enough that the average stays close to correct but large enough that it becomes very hard to trace any single person's data back.

That's it for now.

Thanks

Amit Shekhar
Founder @ Outcome School

You can connect with me on:

Follow Outcome School on:

Read all of our high-quality blogs here.