High-performance simulations with TFF
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
This tutorial will describe how to setup high-performance simulations with TFF
in a variety of common scenarios.
TODO(b/134543154): Populate the content, some of the things to cover here:
- using GPUs in a single-machine setup,
- multi-machine setup on GCP/GKE, with and without TPUs,
- interfacing MapReduce-like backends,
- current limitations and when/how they will be relaxed.
准备工作
首先,请确保您的笔记本电脑连接到具有已编译相关组件(包括用于多机场景的 gRPC 依赖关系)的后端。
现在,我们从加载 TFF 网站上的 MNIST 示例开始,然后声明 Python 函数,该函数将在一组 10 个客户端上运行一个小型实验循环。
!pip install --quiet --upgrade tensorflow-federated-nightly
!pip install --quiet --upgrade nest-asyncio
import nest_asyncio
nest_asyncio.apply()
import collections
import time
import tensorflow as tf
import tensorflow_federated as tff
source, _ = tff.simulation.datasets.emnist.load_data()
def map_fn(example):
return collections.OrderedDict(
x=tf.reshape(example['pixels'], [-1, 784]), y=example['label'])
def client_data(n):
ds = source.create_tf_dataset_for_client(source.client_ids[n])
return ds.repeat(10).shuffle(500).batch(20).map(map_fn)
train_data = [client_data(n) for n in range(10)]
element_spec = train_data[0].element_spec
def model_fn():
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=(784,)),
tf.keras.layers.Dense(units=10, kernel_initializer='zeros'),
tf.keras.layers.Softmax(),
])
return tff.learning.from_keras_model(
model,
input_spec=element_spec,
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()])
trainer = tff.learning.build_federated_averaging_process(
model_fn, client_optimizer_fn=lambda: tf.keras.optimizers.SGD(0.02))
def evaluate(num_rounds=10):
state = trainer.initialize()
for _ in range(num_rounds):
t1 = time.time()
state, metrics = trainer.next(state, train_data)
t2 = time.time()
print('metrics {m}, round time {t:.2f} seconds'.format(
m=metrics, t=t2 - t1))
单机模拟
现在默认开启。
evaluate()
metrics <sparse_categorical_accuracy=0.13858024775981903,loss=3.0073554515838623>, round time 3.59 seconds
metrics <sparse_categorical_accuracy=0.1796296238899231,loss=2.749046802520752>, round time 2.29 seconds
metrics <sparse_categorical_accuracy=0.21656379103660583,loss=2.514779567718506>, round time 2.33 seconds
metrics <sparse_categorical_accuracy=0.2637860178947449,loss=2.312587261199951>, round time 2.06 seconds
metrics <sparse_categorical_accuracy=0.3334362208843231,loss=2.068122386932373>, round time 2.00 seconds
metrics <sparse_categorical_accuracy=0.3737654387950897,loss=1.9268712997436523>, round time 2.42 seconds
metrics <sparse_categorical_accuracy=0.4296296238899231,loss=1.7216310501098633>, round time 2.20 seconds
metrics <sparse_categorical_accuracy=0.4655349850654602,loss=1.6489890813827515>, round time 2.18 seconds
metrics <sparse_categorical_accuracy=0.5048353672027588,loss=1.5485210418701172>, round time 2.16 seconds
metrics <sparse_categorical_accuracy=0.5564814805984497,loss=1.4140453338623047>, round time 2.41 seconds
在 GCP/GKE、GPU、TPU 等上进行多机模拟…
敬请期待。
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2022-01-24。
[null,null,["最后更新时间 (UTC):2022-01-24。"],[],[],null,["# High-performance simulations with TFF\n\n\u003cbr /\u003e\n\nThis tutorial will describe how to setup high-performance simulations with TFF\nin a variety of common scenarios.\n\nTODO: b/134543154 - Populate the content, some of the things to cover here:\n\n- using GPUs in a single-machine setup,\n- multi-machine setup on GCP/GKE, with and without TPUs,\n- interfacing MapReduce-like backends,\n- current limitations and when/how they will be relaxed.\n\n|--------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------|\n| [View on TensorFlow.org](https://www.tensorflow.org/federated/tutorials/simulations) | [Run in Google Colab](https://colab.research.google.com/github/tensorflow/federated/blob/v0.88.0/docs/tutorials/simulations.ipynb) | [View source on GitHub](https://github.com/tensorflow/federated/blob/v0.88.0/docs/tutorials/simulations.ipynb) | [Download notebook](https://storage.googleapis.com/tensorflow_docs/federated/docs/tutorials/simulations.ipynb) |\n\nBefore we begin\n---------------\n\nFirst, make sure your notebook is connected to a backend that has the relevant\ncomponents (including gRPC dependencies for multi-machine scenarios) compiled.\n\nNow, let's start by loading the MNIST example from the TFF website, and\ndeclaring the Python function that will run a small experiment loop over\na group of 10 clients. \n\n pip install --quiet --upgrade tensorflow-federated\n\n import collections\n import time\n\n import tensorflow as tf\n import tensorflow_federated as tff\n\n source, _ = tff.simulation.datasets.emnist.load_data()\n\n\n def map_fn(example):\n return collections.OrderedDict(\n x=tf.reshape(example['pixels'], [-1, 784]), y=example['label']\n )\n\n\n def client_data(n):\n ds = source.create_tf_dataset_for_client(source.client_ids[n])\n return ds.repeat(10).shuffle(500).batch(20).map(map_fn)\n\n\n train_data = [client_data(n) for n in range(10)]\n element_spec = train_data[0].element_spec\n\n\n keras_model = tf.keras.models.Sequential([\n tf.keras.layers.InputLayer(input_shape=(784,)),\n tf.keras.layers.Dense(units=10, kernel_initializer='zeros'),\n tf.keras.layers.Softmax(),\n ])\n tff_model = tff.learning.models.functional_model_from_keras(\n keras_model,\n loss_fn=tf.keras.losses.SparseCategoricalCrossentropy(),\n input_spec=element_spec,\n metrics_constructor=collections.OrderedDict(\n accuracy=tf.keras.metrics.SparseCategoricalAccuracy\n ),\n )\n\n\n trainer = tff.learning.algorithms.build_weighted_fed_avg(\n tff_model,\n client_optimizer_fn=tff.learning.optimizers.build_sgdm(learning_rate=0.02),\n )\n\n\n def evaluate(num_rounds=10):\n state = trainer.initialize()\n for _ in range(num_rounds):\n t1 = time.time()\n result = trainer.next(state, train_data)\n state = result.state\n train_metrics = result.metrics['client_work']['train']\n t2 = time.time()\n print(\n 'train metrics {m}, round time {t:.2f} seconds'.format(\n m=train_metrics, t=t2 - t1\n )\n )\n\nSingle-machine simulations\n--------------------------\n\nNow on by default. \n\n evaluate()\n\n```\ntrain metrics OrderedDict([('accuracy', 0.14557613)]), round time 3.11 seconds\ntrain metrics OrderedDict([('accuracy', 0.181893)]), round time 1.25 seconds\ntrain metrics OrderedDict([('accuracy', 0.23374486)]), round time 1.23 seconds\ntrain metrics OrderedDict([('accuracy', 0.26759258)]), round time 1.18 seconds\ntrain metrics OrderedDict([('accuracy', 0.31944445)]), round time 1.14 seconds\ntrain metrics OrderedDict([('accuracy', 0.37222221)]), round time 1.14 seconds\ntrain metrics OrderedDict([('accuracy', 0.42685184)]), round time 1.25 seconds\ntrain metrics OrderedDict([('accuracy', 0.4712963)]), round time 1.35 seconds\ntrain metrics OrderedDict([('accuracy', 0.5269547)]), round time 1.31 seconds\ntrain metrics OrderedDict([('accuracy', 0.55833334)]), round time 1.31 seconds\n```\n\nMulti-machine simulations on GCP/GKE, GPUs, TPUs, and beyond...\n---------------------------------------------------------------\n\nComing very soon."]]