Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/ko/federated/tutorials/simulations.ipynb
25118 views
Kernel: Python 3
#@title Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License.

TFF를 사용하는 고성능 시뮬레이션

이 튜토리얼에서는 다양한 일반적인 시나리오에서 TFF로 고성능 시뮬레이션을 설정하는 방법을 설명합니다.

TODO: b/134543154 - 여기에서 다룰 몇 가지 사항으로 내용을 채우세요.

  • 단일 시스템 설정에서 GPU 사용하기

  • TPU가 있거나 없는 GCP/GKE에서 다중 시스템 설정하기

  • interfacing MapReduce-like backends,

  • 현재 제한 사항과 이를 완화할 수 있는 시기/방법

시작하기 전에

먼저, 관련 구성 요소(다중 시스템 시나리오에 대한 gRPC 종속성 포함)가 컴파일된 백엔드에 노트북이 연결되어 있는지 확인합니다.

이제 TFF 웹 사이트에서 MNIST 예제를 로드하고 10개의 클라이언트 그룹에 대해 작은 실험 루프를 실행할 Python 함수를 선언하는 것으로 시작하겠습니다.

#@test {"skip": true} !pip install --quiet --upgrade tensorflow-federated
/bin/sh: pip: command not found
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.models.from_keras_model( model, input_spec=element_spec, loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=[tf.keras.metrics.SparseCategoricalAccuracy()]) trainer = tff.learning.algorithms.build_weighted_fed_avg( 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() result = trainer.next(state, train_data) state = result.state train_metrics = result.metrics['client_work']['train'] t2 = time.time() print('train metrics {m}, round time {t:.2f} seconds'.format( m=train_metrics, t=t2 - t1))

단일 머신 시뮬레이션

다음은 기본입니다.

evaluate()
train metrics OrderedDict([('sparse_categorical_accuracy', 0.15329218), ('loss', 2.918891), ('num_examples', 9720), ('num_batches', 490)]), round time 4.64 seconds train metrics OrderedDict([('sparse_categorical_accuracy', 0.18004115), ('loss', 2.7677088), ('num_examples', 9720), ('num_batches', 490)]), round time 2.37 seconds train metrics OrderedDict([('sparse_categorical_accuracy', 0.21841563), ('loss', 2.511075), ('num_examples', 9720), ('num_batches', 490)]), round time 2.30 seconds train metrics OrderedDict([('sparse_categorical_accuracy', 0.27160493), ('loss', 2.340346), ('num_examples', 9720), ('num_batches', 490)]), round time 2.25 seconds train metrics OrderedDict([('sparse_categorical_accuracy', 0.34115225), ('loss', 2.0537064), ('num_examples', 9720), ('num_batches', 490)]), round time 2.27 seconds train metrics OrderedDict([('sparse_categorical_accuracy', 0.3745885), ('loss', 1.9158486), ('num_examples', 9720), ('num_batches', 490)]), round time 2.21 seconds train metrics OrderedDict([('sparse_categorical_accuracy', 0.41502059), ('loss', 1.7523248), ('num_examples', 9720), ('num_batches', 490)]), round time 2.19 seconds train metrics OrderedDict([('sparse_categorical_accuracy', 0.47644034), ('loss', 1.6085855), ('num_examples', 9720), ('num_batches', 490)]), round time 2.20 seconds train metrics OrderedDict([('sparse_categorical_accuracy', 0.5126543), ('loss', 1.5272282), ('num_examples', 9720), ('num_batches', 490)]), round time 2.27 seconds train metrics OrderedDict([('sparse_categorical_accuracy', 0.5576132), ('loss', 1.393721), ('num_examples', 9720), ('num_batches', 490)]), round time 2.16 seconds

GCP/GKE, GPU, TPU 등의 다중 머신 시뮬레이션

곧 추가될 예정입니다.