TensorFlow Federated:基于分散式数据的机器学习
https://www.tensorflow.org/federated/
-
Federated Learning (FL) API
该层提供了一组高阶接口,使开发者能够将包含的联合训练和评估实现应用于现有的 TensorFlow 模型。 -
Federated Core (FC) API
该系统的核心是一组较低阶接口,可以通过在强类型函数式编程环境中结合使用 TensorFlow 与分布式通信运算符,简洁地表达新的联合算法。这一层也是我们构建联合学习的基础。 -
借助 TFF,开发者能够以声明方式表达联合计算,从而将它们部署到不同的运行时环境中。TFF 包含一个用于实验的单机模拟运行时。请访问相关教程,并亲自试用!
from six.moves import range
import tensorflow as tf
import tensorflow_federated as tff
from tensorflow_federated.python.examples import mnist
tf.compat.v1.enable_v2_behavior()
# Load simulation data.
source, _ = tff.simulation.datasets.emnist.load_data()
def client_data(n):
dataset = source.create_tf_dataset_for_client(source.client_ids[n])
return mnist.keras_dataset_from_emnist(dataset).repeat(10).batch(20)
# Pick a subset of client devices to participate in training.
train_data = [client_data(n) for n in range(3)]
# Grab a single batch of data so that TFF knows what data looks like.
sample_batch = tf.nest.map_structure(
lambda x: x.numpy(), iter(train_data[0]).next())
# Wrap a Keras model for use with TFF.
def model_fn():
return tff.learning.from_compiled_keras_model(
mnist.create_simple_keras_model(), sample_batch)
# Simulate a few rounds of training with the selected client devices.
trainer = tff.learning.build_federated_averaging_process(model_fn)
state = trainer.initialize()
for _ in range(5):
state, metrics = trainer.next(state, train_data)
print (metrics.loss)