TensorFlow Federated:基于分散式数据的机器学习
import tensorflow as tf import tensorflow_federated as tff # Load simulation data. source, _ = tff.simulation.datasets.emnist.load_data() def client_data(n): return source.create_tf_dataset_for_client(source.client_ids[n]).map( lambda e: (tf.reshape(e['pixels'], [-1]), e['label']) ).repeat(10).batch(20) # Pick a subset of client devices to participate in training. train_data = [client_data(n) for n in range(3)] # Wrap a Keras model for use with TFF. def model_fn(): model = tf.keras.models.Sequential([ tf.keras.layers.Dense(10, tf.nn.softmax, input_shape=(784,), kernel_initializer='zeros') ]) return tff.learning.from_keras_model( model, input_spec=train_data[0].element_spec, loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=[tf.keras.metrics.SparseCategoricalAccuracy()]) # Simulate a few rounds of training with the selected client devices. trainer = tff.learning.build_federated_averaging_process( model_fn, client_optimizer_fn=lambda: tf.keras.optimizers.SGD(0.1)) state = trainer.initialize() for _ in range(5): state, metrics = trainer.next(state, train_data) print(metrics['train']['loss'])
-
Federated Learning (FL) API
该层提供了一组高阶接口,使开发者能够将包含的联合训练和评估实现应用于现有的 TensorFlow 模型。 -
Federated Core (FC) API
该系统的核心是一组较低阶接口,可以通过在强类型函数式编程环境中结合使用 TensorFlow 与分布式通信运算符,简洁地表达新的联合算法。这一层也是我们构建联合学习的基础。 -
借助 TFF,开发者能够以声明方式表达联合计算,从而将它们部署到不同的运行时环境中。TFF 包含一个用于实验的单机模拟运行时。请访问相关教程,并自行尝试看看!
如有疑问或想寻求帮助,请在 StackOverflow 上通过 tensorflow-federated 标签与我们联系。