Steps involved in the Federated Learning Approach

  • The mobile devices download the global ML model

  • Data is being generated while the user is using application linked with the ML model

  • As the user starts to interact with the application more, the user gets much better predictions according to his usage

  • Once the model is ready for the scheduled sync with the server. The personalised model that was getting trained with the on device capability is sent to the server.

  • Models from all the devices are collected and a Federated average function is used to generate a much imporved version of the model than the previous one

  • Once trained the improved version is sent to all the devices where the user gets the experience based on the usage by all the devices arround the globe.

Installing PySyft

In order to install PySyft, it is recommended that you set up a conda environment first

conda create -n pysyft python=3
conda activate pysyft
conda install jupyter notebook

You then need to install the package

pip install syft

Step by Step guide to develop the neural network using federated learning approach

Importing the libraries:

  • Numpy

  • PyTorch

  • PySyft

  • Pickle

import pickle
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import TensorDataset, DataLoader
import time
import copy
import numpy as np
import syft as sy
from syft.frameworks.torch.federated import utils
from syft.workers.websocket_client import WebsocketClientWorker

Initializing the training parameters

Learning rate 0.001

Neural network 100 epoches

total batches 8

class Parser:
def __init__(self):
self.epoches = 100
self.lr = 0.001
self.test_batch_size = 8
self.batch_size =8
self.log_interval = 10
self.seed = 1 args = Parser()
torch.manual_seed(args.seed)

Dataset Preprocessing

with open('boston_housing.pickle','rb') as f:
((x,y),(x_test,y_test)) = pickle.load(f) x = torch.from_numpy(x).float()
y = torch.from_numpy(y).float() x_test = torch.from_numpy(x_test).float()
y_test = torch.from_numpy(y_test).float()
mean = x.mean(0,keepdim=True)

dev = x.std(0,keepdim=True)

mean[:,3] = 0.

dev[:,3] = 1.

x = (x-mean)/dev
x_test = (x_test - mean)/dev train = TensorDataset(x,y)
test = TensorDataset(x_test,y_test) train_loader = DataLoader(train,batch_size = args.batch_size,shuffle=True)
train_loader = DataLoader(test,batch_size=args.test_batch_size,shuffle=True)

Creating Neural Network with PyTorch

Creating the architecture of the neural network model

class Net(nn.Module):
def __init__(self):
super(Net,self).__init__()
self.fc1 = nn.Linear(13,32)
self.fc2 = nn.Linear(32,24)
self.fc3 = nn.Linear(24,16)
self.fc4 = nn.Linear(16,1) def __init__(self):
x = x.view(-1,13)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc4(x))
x = self.fc(x)
return x

Connecting the data with the remote mobile devices

Though data will be available offline for federated learning with the workers but here we are sending the data over to the workers for training with ondevice capability

remote_dataset = (list(), list())
train_distributed_dataset = [] for batch_idx, (data,target) in enumerate(train_loader):
data = data.send(compute_nodes[batch_idx % len(compute_nodes)])
target = target.send(compute_nodes[batch_idx % len(compute_nodes)])
remote_dataset[batch_idx % len(compute_nodes)].append((data, target)) bobs_model = Net()
alices_model = Net()
bobs_optimizer = optim.SGD(bobs_model.parameters(), lr=args.lr)
alices_optimizer = optim.SGD(alices_model.parameters(), lr=args.lr) models = [bobs_model,alices_model]
optimizers = [bobs_optimizer,alices_optimizer] model = Net()

Connect to the workers or the devices for training

hook = sy.TorchHook(torch)
bob_worker = sy.VirtualWorker(hook, id="bob")
alice_worker = sy.VirtualWorker(hook, id="alice")
compute_nodes = [bob_worker, alice_worker]

Training the Neural Network

def update(data, target, model, optimizer):
model.send(data.location)
optimizer.zero_grad()
prediction = model(data)
loss = F.mse_loss(prediction.view(-1), target)
loss.backward()
optimizer.step()
return model def train():
for data_index in range(len(remote_dataset[0])-1):
for remote_index in range(len(compute_nodes)):
data, target = remote_dataset[remote_index][data_index]
models[remote_index] = update(data, target, models[remote_index], optimizers[remote_index])
for model in models:
model.get()
return utils.federated_avg({
"bob": models[0],
"alice": models[1]
})
def test(federated_model):
federated_model.eval()
test_loss = 0
for data, target in test_loader:
output = federated_model(data)
test_loss += F.mse_loss(output.view(-1), target, reduction='sum').item()
predection = output.data.max(1, keepdim=True)[1] test_loss /= len(test_loader.dataset)
print('Test set: Average loss: {:.4f}'.format(test_loss))
for epoch in range(args.epochs):
start_time = time.time()
print(f"Epoch Number {epoch + 1}")
federated_model = train()
model = federated_model
test(federated_model)
total_time = time.time() - start_time
print('Communication time over the network', round(total_time, 2), 's\n')

References:

Federated Learning with PySyft

联邦学习PySyft的更多相关文章

  1. 联邦学习开源框架FATE助力腾讯神盾沙箱,携手打造数据安全合作生态

    近日,微众银行联邦学习FATE开源社区迎来了两位新贡献者——来自腾讯的刘洋及秦姝琦,作为云计算安全领域的专家,两位为FATE构造了新的功能点,并在Github上提交修复了相关漏洞.(Github项目地 ...

  2. 联邦学习(Federated Learning)

    联邦学习简介        联邦学习(Federated Learning)是一种新兴的人工智能基础技术,在 2016 年由谷歌最先提出,原本用于解决安卓手机终端用户在本地更新模型的问题,其设计目标是 ...

  3. 联邦学习 Federated Learning 相关资料整理

    本文链接:https://blog.csdn.net/Sinsa110/article/details/90697728代码微众银行+杨强教授团队的联邦学习FATE框架代码:https://githu ...

  4. 腾讯数据安全专家谈联邦学习开源项目FATE:通往隐私保护理想未来的桥梁

    数据孤岛.数据隐私以及数据安全,是目前人工智能和云计算在大规模产业化应用过程中绕不开的“三座大山”. “联邦学习”作为新一代的人工智能算法,能在数据不出本地的情况下,实现共同建模,提升AI模型的效果, ...

  5. Federal Learning(联邦学习)认知

    本人是学生党,同时也是小菜鸡一枚,撞运气有机会能够给老师当项目助理,在这个过程中肯定会学到一些有趣的知识,就在此平台上记录一下,在知识点方面有不对的还请各位指正. What(什么是联邦学习?) 联邦学 ...

  6. Apache Pulsar 在腾讯 Angel PowerFL 联邦学习平台上的实践

    腾讯 Angel PowerFL 联邦学习平台 联邦学习作为新一代人工智能基础技术,通过解决数据隐私与数据孤岛问题,重塑金融.医疗.城市安防等领域. 腾讯 Angel PowerFL 联邦学习平台构建 ...

  7. MindSpore联邦学习框架解决行业级难题

    内容来源:华为开发者大会2021 HMS Core 6 AI技术论坛,主题演讲<MindSpore联邦学习框架解决隐私合规下的数据孤岛问题>. 演讲嘉宾:华为MindSpore联邦学习工程 ...

  8. 联邦学习:按Dirichlet分布划分Non-IID样本

    我们在<Python中的随机采样和概率分布(二)>介绍了如何用Python现有的库对一个概率分布进行采样,其中的dirichlet分布大家一定不会感到陌生.该分布的概率密度函数为 \[P( ...

  9. 【流行前沿】联邦学习 Federated Learning with Only Positive Labels

    核心问题:如果每个用户只有一类数据,如何进行联邦学习? Felix X. Yu, , Ankit Singh Rawat, Aditya Krishna Menon, and Sanjiv Kumar ...

随机推荐

  1. Saiku上线部署准备(三十)

    Saiku上线部署准备 零零散散琢磨了快5个月了,终于快要上线了哈哈哈哈哈.....  激动!!! 以下是本地打包编译saiku至部署到服务器上使用的完整步骤哦 saiku部署到服务器 源码编译需要注 ...

  2. IT兄弟连 Java语法教程 数组 什么是数组

    数组是编程语言中最常见的一种数据结构,可用于存储多个数据,每个数组元素存放一个数据,通常可通过数组元素的索引来访问数组元素,包括为数组元素赋值和取出数组元素的值.Java语言的数组则具有其特有的特征, ...

  3. SpringBoot系列之Spring容器添加组件方式

    SpringBoot系列之Spring容器添加组件方式 本博客介绍SpringBoot项目中将组件添加到Spring容器中的方法,SpringBoot项目有一个很明显的优点,就是不需要再编写xml配置 ...

  4. mosquitto配置文件

    #配置文件为mosquitto #参见mosquitto.conf(5)了解更多信息. #显示默认值,取消注释以更改. #使用#字符来表示注释,但只有当它是 #第一个字符就行了. #========= ...

  5. CentOS安装Docker-ce并配置中国国内加速(aliyun)镜像

    前提条件 1.系统.内核 CentOS7 要求64位系统.内核版本3.10以上 CentOS6 要求版本在6.5以上,系统64位.内核版本2.6.32-431以上 查看内核版本号 uname -r # ...

  6. Docker开启Remote API 访问 2375端口

    Docker常见端口 我看到的常见docker端口包括: 2375:未加密的docker socket,远程root无密码访问主机2376:tls加密套接字,很可能这是您的CI服务器4243端口作为h ...

  7. python中time、datetime模块的使用

    目录 python中time.datetime模块的使用 1.前言 2.time模块 1.时间格式转换图 2.常用方法 3.datetime模块 python中time.datetime模块的使用 1 ...

  8. QT信号槽连接语法总结

    信号槽是 Qt 框架引以为豪的机制之一. 所谓信号槽,实际就是观察者模式.当某个事件发生之后,比如,按钮检测到自己被点击了一下,它就会发出一个信号(signal).这种触发是没有目的的,类似广播.如果 ...

  9. Kali linux-信息收集-dmitry

    信息收集-dmitry DMitry(Deepmagic Information Gathering Tools 深度信息收集工具)是一个kali linux下用C语言写的工具.主要功能为端口扫描,w ...

  10. Macbook触控板使用技巧

    1. 在Storyboard鼠标右键可以直接拖线的,如果你用的是外接的第三方鼠标,没必要按着 control 键再用鼠标左键拖线 如果是触控板的话,双指按下去就可以直接拖线,带3Dtouch功能的触控 ...