---
title: "Federated Learning using Hugging Face and Flower"
description: "Federated fine-tuning of a pre-trained distilBERT model for IMDB sentiment classification uses Flower to coordinate multiple clients and a server, so raw data never leaves the clients."
url: "https://swe.quest/articles/federated-learning-using-hugging-face-and-flower-51b18eb47f6bd9fe"
canonical: "https://huggingface.co/blog/fl-with-flower"
source: "Hugging Face"
published: "2023-03-27"
difficulty: "medium"
kind: "How-to"
reading_minutes: 5
tags: ["model-training","machine-learning","python","privacy"]
---

# Federated Learning using Hugging Face and Flower

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2023-03-27, 5 min read, How-to. Original: [https://huggingface.co/blog/fl-with-flower](https://huggingface.co/blog/fl-with-flower)

## Summary

Federated fine-tuning of a pre-trained distilBERT model for IMDB sentiment classification uses Flower to coordinate multiple clients and a server, so raw data never leaves the clients. The walkthrough covers loading and tokenizing IMDB with Hugging Face datasets, standard PyTorch train/test loops, a Flower NumPyClient that exchanges model state\_dict parameters, and a server running the FedAvg strategy with weighted metric aggregation over three rounds. A Colab notebook variant uses Flower's simulation mode (start\_simulation) instead of separate client processes.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Model training](https://swe.quest/tags/model-training.md) (AI & ML)
- [Machine learning](https://swe.quest/tags/machine-learning.md) (AI & ML)
- [Python](https://swe.quest/tags/python.md) (Languages)
- [Privacy](https://swe.quest/tags/privacy.md) (Security)

## Prerequisites

- PyTorch training loops ([easy posts on Model training](https://swe.quest/tags/model-training.md?difficulty=easy))
- Hugging Face Transformers basics
- federated learning concepts

## Keywords

Flower, Hugging Face Transformers, distilBERT, FedAvg, IMDB dataset, PyTorch

## Related posts

- [The Partnership: Amazon SageMaker and Hugging Face](https://swe.quest/articles/the-partnership-amazon-sagemaker-and-hugging-face-bdc6f1a55662e5fc.md): Hugging Face · 2021-03-23 · Medium · 14 min read · Model training, ML platforms, Cloud platforms, Python
  Hugging Face and AWS partner, making AWS Hugging Face's preferred cloud and introducing Hugging Face Deep Learning Containers plus a HuggingFace Estimator in the SageMaker Python SDK for training Transformers models.
- [Applying federated learning to protect data on mobile devices](https://swe.quest/articles/applying-federated-learning-to-protect-data-on-mobile-devices-b070f62868b3f5e6.md): Meta Engineering · 2022-06-14 · Easy · 3 min read · Privacy, Model training, ML platforms, Mobile
  Meta is evaluating federated learning with differential privacy (FL-DP), which trains models on mobile devices and adds noise to updates before they reach a global model so user data stays local and is harder to memorize or reconstruct.
- [Data-Centric Machine Learning: Building Shopify Inbox’s Message Classification Model](https://swe.quest/articles/data-centric-machine-learning-building-shopify-inbox-s-message-classification-89547c88c3590e73.md): Shopify Engineering · 2022-07-21 · Medium · 12 min read · Machine learning, Model training, Privacy, Communications
  Shopify Inbox's message classifier improved from ~70% to ~90% accuracy, and from ~35% to ~80% high-confidence coverage, by investing in better training data rather than model tweaks.
- [My Journey to a serverless transformers pipeline on Google Cloud](https://swe.quest/articles/my-journey-to-a-serverless-transformers-pipeline-on-google-cloud-202ccbc112026927.md): Hugging Face · 2021-03-18 · Medium · 8 min read · Serverless, Inference & serving, Cloud platforms, Python, Containers
  A Hugging Face transformers sentiment-analysis pipeline (DistilBERT fine-tuned on SST-2) was deployed as a low-traffic microservice on Google Cloud Run after AI Platform Prediction (beta instability, checkpoint not a pure TensorFlow model) and App Engine (TensorFlow install failure, PyTorch handling only 2 requests per instance) fell short.
- [Opinion Classification with Kili and HuggingFace AutoTrain](https://swe.quest/articles/opinion-classification-with-kili-and-huggingface-autotrain-97ebbffd8c146463.md): Hugging Face · 2022-04-28 · Medium · 18 min read · Machine learning, Model training, Python
  Kili's annotation platform and Hugging Face AutoTrain combine into an active-learning pipeline that classifies about 40,000 Google Play reviews of the Medium app into Subscription, Content, Interface and User Experience, then applies sentiment analysis to the classified reviews.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
