![]() | ![]() | ![]() | ![]() |
บทช่วยสอนนี้สาธิตวิธีใช้ tf.distribute.Strategy
ด้วยลูปการฝึกอบรมที่กำหนดเอง เราจะฝึกโมเดล CNN อย่างง่ายบนชุดข้อมูลแฟชั่น MNIST ชุดข้อมูล MNIST ของแฟชั่นประกอบด้วยภาพรถไฟ 60000 ภาพขนาด 28 x 28 และภาพทดสอบ 10,000 ภาพขนาด 28 x 28
เราใช้ลูปการฝึกแบบกำหนดเองเพื่อฝึกโมเดลของเราเพราะมันทำให้เรามีความยืดหยุ่นและควบคุมการฝึกได้มากขึ้น ยิ่งไปกว่านั้นการดีบักโมเดลและลูปการฝึกนั้นง่ายกว่า
# Import TensorFlow
import tensorflow as tf
# Helper libraries
import numpy as np
import os
print(tf.__version__)
2.3.0
ดาวน์โหลดชุดข้อมูลแฟชั่น MNIST
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
# Adding a dimension to the array -> new shape == (28, 28, 1)
# We are doing this because the first layer in our model is a convolutional
# layer and it requires a 4D input (batch_size, height, width, channels).
# batch_size dimension will be added later on.
train_images = train_images[..., None]
test_images = test_images[..., None]
# Getting the images in [0, 1] range.
train_images = train_images / np.float32(255)
test_images = test_images / np.float32(255)
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz 32768/29515 [=================================] - 0s 0us/step Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz 26427392/26421880 [==============================] - 1s 0us/step Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz 8192/5148 [===============================================] - 0s 0us/step Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz 4423680/4422102 [==============================] - 1s 0us/step
สร้างกลยุทธ์ในการกระจายตัวแปรและกราฟ
กลยุทธ์ tf.distribute.MirroredStrategy
ทำงานอย่างไร
- ตัวแปรทั้งหมดและกราฟโมเดลถูกจำลองแบบบนแบบจำลอง
- ข้อมูลที่ป้อนจะกระจายอย่างเท่าเทียมกันทั่วทั้งแบบจำลอง
- แบบจำลองแต่ละตัวจะคำนวณการสูญเสียและการไล่ระดับสีสำหรับอินพุตที่ได้รับ
- การไล่ระดับสีจะซิงค์กับแบบจำลองทั้งหมดโดยการรวมเข้าด้วยกัน
- หลังจากการซิงค์จะมีการอัปเดตเดียวกันกับสำเนาของตัวแปรในแต่ละแบบจำลอง
# If the list of devices is not specified in the
# `tf.distribute.MirroredStrategy` constructor, it will be auto-detected.
strategy = tf.distribute.MirroredStrategy()
INFO:tensorflow:Using MirroredStrategy with devices ('/job:localhost/replica:0/task:0/device:GPU:0',)
print ('Number of devices: {}'.format(strategy.num_replicas_in_sync))
Number of devices: 1
ตั้งค่าท่อส่งข้อมูล
ส่งออกกราฟและตัวแปรไปยังรูปแบบ SavedModel แพลตฟอร์มที่ไม่เชื่อเรื่องพระเจ้า หลังจากบันทึกโมเดลของคุณแล้วคุณสามารถโหลดแบบจำลองโดยมีหรือไม่มีขอบเขตก็ได้
BUFFER_SIZE = len(train_images)
BATCH_SIZE_PER_REPLICA = 64
GLOBAL_BATCH_SIZE = BATCH_SIZE_PER_REPLICA * strategy.num_replicas_in_sync
EPOCHS = 10
สร้างชุดข้อมูลและแจกจ่าย:
train_dataset = tf.data.Dataset.from_tensor_slices((train_images, train_labels)).shuffle(BUFFER_SIZE).batch(GLOBAL_BATCH_SIZE)
test_dataset = tf.data.Dataset.from_tensor_slices((test_images, test_labels)).batch(GLOBAL_BATCH_SIZE)
train_dist_dataset = strategy.experimental_distribute_dataset(train_dataset)
test_dist_dataset = strategy.experimental_distribute_dataset(test_dataset)
สร้างแบบจำลอง
สร้างโมเดลโดยใช้ tf.keras.Sequential
คุณยังสามารถใช้ Model Subclassing API เพื่อทำสิ่งนี้ได้
def create_model():
model = tf.keras.Sequential([
tf.keras.layers.Conv2D(32, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Conv2D(64, 3, activation='relu'),
tf.keras.layers.MaxPooling2D(),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(10)
])
return model
# Create a checkpoint directory to store the checkpoints.
checkpoint_dir = './training_checkpoints'
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
กำหนดฟังก์ชันการสูญเสีย
โดยปกติในเครื่องเดียวที่มี GPU / CPU 1 ตัวการสูญเสียจะถูกหารด้วยจำนวนตัวอย่างในชุดอินพุต
ดังนั้นควรคำนวณการสูญเสียอย่างไรเมื่อใช้ tf.distribute.Strategy
ตัวอย่างเช่นสมมติว่าคุณมี GPU 4 ตัวและขนาดแบทช์ 64 ชุดอินพุตหนึ่งชุดจะกระจายไปตามแบบจำลอง (4 GPU) แต่ละตัวจำลองจะได้รับอินพุตขนาด 16
แบบจำลองบนแบบจำลองแต่ละแบบจะทำการส่งต่อพร้อมอินพุตตามลำดับและคำนวณการสูญเสีย ตอนนี้แทนที่จะหารการสูญเสียด้วยจำนวนตัวอย่างในอินพุตตามลำดับ (BATCH_SIZE_PER_REPLICA = 16) การสูญเสียควรหารด้วย GLOBAL_BATCH_SIZE (64)
ทำแบบนี้ทำไม?
- สิ่งนี้จำเป็นต้องทำเนื่องจากหลังจากคำนวณการไล่ระดับสีในแต่ละแบบจำลองแล้วจะมีการซิงค์กับแบบจำลองโดยการ สรุป
วิธีทำใน TensorFlow
หากคุณกำลังเขียนลูปการฝึกแบบกำหนดเองดังในบทช่วยสอนนี้คุณควรสรุปผลขาดทุนต่อตัวอย่างและหารผลรวมด้วย GLOBAL_BATCH_SIZE:
scale_loss = tf.reduce_sum(loss) * (1. / GLOBAL_BATCH_SIZE)
หรือคุณสามารถใช้tf.nn.compute_average_loss
ซึ่งรับการสูญเสียต่อตัวอย่างน้ำหนักตัวอย่างที่เป็นทางเลือกและ GLOBAL_BATCH_SIZE เป็นอาร์กิวเมนต์และส่งคืนการสูญเสียที่ปรับขนาดหากคุณใช้การสูญเสียการทำให้เป็นมาตรฐานในแบบจำลองของคุณคุณจะต้องปรับขนาดมูลค่าการสูญเสียตามจำนวนแบบจำลอง คุณสามารถทำได้โดยใช้ฟังก์ชัน
tf.nn.scale_regularization_loss
ไม่แนะนำให้ใช้
tf.reduce_mean
การทำเช่นนี้จะหารการสูญเสียตามจริงต่อขนาดแบทช์จำลองซึ่งอาจแตกต่างกันไปในแต่ละขั้นตอนการลดขนาดและการปรับขนาดนี้ทำได้โดยอัตโนมัติใน keras
model.compile
และmodel.fit
หากใช้คลาส
tf.keras.losses
(ดังตัวอย่างด้านล่าง) การลดการสูญเสียจะต้องระบุอย่างชัดเจนให้เป็นหนึ่งในNONE
หรือSUM
AUTO
และSUM_OVER_BATCH_SIZE
ไม่ได้รับอนุญาตเมื่อใช้กับtf.distribute.Strategy
ไม่อนุญาตให้ใช้AUTO
เนื่องจากผู้ใช้ควรคิดอย่างชัดเจนเกี่ยวกับสิ่งที่ต้องการลดลงเพื่อให้แน่ใจว่าถูกต้องในกรณีแบบกระจายSUM_OVER_BATCH_SIZE
ไม่ได้รับอนุญาตเนื่องจากในปัจจุบันจะหารด้วยขนาดแบตช์ของการจำลองเท่านั้นและปล่อยให้ผู้ใช้หารด้วยจำนวนแบบจำลองซึ่งอาจทำให้พลาดได้ง่าย ดังนั้นเราจึงขอให้ผู้ใช้ทำการลดเองโดยชัดแจ้งแทนหาก
labels
เป็นแบบหลายมิติให้เฉลี่ยper_example_loss
กับจำนวนองค์ประกอบในแต่ละตัวอย่าง ตัวอย่างเช่นหากรูปร่างของpredictions
เป็น(batch_size, H, W, n_classes)
และlabels
คือ(batch_size, H, W)
คุณจะต้องอัปเดตper_example_loss
เช่นper_example_loss /= tf.cast(tf.reduce_prod(tf.shape(labels)[1:]), tf.float32)
with strategy.scope():
# Set reduction to `none` so we can do the reduction afterwards and divide by
# global batch size.
loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
from_logits=True,
reduction=tf.keras.losses.Reduction.NONE)
def compute_loss(labels, predictions):
per_example_loss = loss_object(labels, predictions)
return tf.nn.compute_average_loss(per_example_loss, global_batch_size=GLOBAL_BATCH_SIZE)
กำหนดเมตริกเพื่อติดตามการสูญเสียและความแม่นยำ
เมตริกเหล่านี้ติดตามการสูญเสียการทดสอบและการฝึกอบรมและความแม่นยำในการทดสอบ คุณสามารถใช้. .result()
เพื่อรับสถิติสะสมได้ตลอดเวลา
with strategy.scope():
test_loss = tf.keras.metrics.Mean(name='test_loss')
train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
name='train_accuracy')
test_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
name='test_accuracy')
ห่วงการฝึก
# model, optimizer, and checkpoint must be created under `strategy.scope`.
with strategy.scope():
model = create_model()
optimizer = tf.keras.optimizers.Adam()
checkpoint = tf.train.Checkpoint(optimizer=optimizer, model=model)
def train_step(inputs):
images, labels = inputs
with tf.GradientTape() as tape:
predictions = model(images, training=True)
loss = compute_loss(labels, predictions)
gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients(zip(gradients, model.trainable_variables))
train_accuracy.update_state(labels, predictions)
return loss
def test_step(inputs):
images, labels = inputs
predictions = model(images, training=False)
t_loss = loss_object(labels, predictions)
test_loss.update_state(t_loss)
test_accuracy.update_state(labels, predictions)
# `run` replicates the provided computation and runs it
# with the distributed input.
@tf.function
def distributed_train_step(dataset_inputs):
per_replica_losses = strategy.run(train_step, args=(dataset_inputs,))
return strategy.reduce(tf.distribute.ReduceOp.SUM, per_replica_losses,
axis=None)
@tf.function
def distributed_test_step(dataset_inputs):
return strategy.run(test_step, args=(dataset_inputs,))
for epoch in range(EPOCHS):
# TRAIN LOOP
total_loss = 0.0
num_batches = 0
for x in train_dist_dataset:
total_loss += distributed_train_step(x)
num_batches += 1
train_loss = total_loss / num_batches
# TEST LOOP
for x in test_dist_dataset:
distributed_test_step(x)
if epoch % 2 == 0:
checkpoint.save(checkpoint_prefix)
template = ("Epoch {}, Loss: {}, Accuracy: {}, Test Loss: {}, "
"Test Accuracy: {}")
print (template.format(epoch+1, train_loss,
train_accuracy.result()*100, test_loss.result(),
test_accuracy.result()*100))
test_loss.reset_states()
train_accuracy.reset_states()
test_accuracy.reset_states()
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.6/site-packages/tensorflow/python/data/ops/multi_device_iterator_ops.py:601: get_next_as_optional (from tensorflow.python.data.ops.iterator_ops) is deprecated and will be removed in a future version. Instructions for updating: Use `tf.data.Iterator.get_next_as_optional()` instead. INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',). INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',). INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',). INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',). INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',). INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',). Epoch 1, Loss: 0.50295090675354, Accuracy: 82.1116714477539, Test Loss: 0.3852590322494507, Test Accuracy: 86.5999984741211 INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',). INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',). INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',). INFO:tensorflow:Reduce to /job:localhost/replica:0/task:0/device:CPU:0 then broadcast to ('/job:localhost/replica:0/task:0/device:CPU:0',). Epoch 2, Loss: 0.32958829402923584, Accuracy: 88.20333862304688, Test Loss: 0.3391425311565399, Test Accuracy: 87.6500015258789 Epoch 3, Loss: 0.2872008979320526, Accuracy: 89.57167053222656, Test Loss: 0.2974696457386017, Test Accuracy: 89.31000518798828 Epoch 4, Loss: 0.255713552236557, Accuracy: 90.58499908447266, Test Loss: 0.2988712787628174, Test Accuracy: 89.31999969482422 Epoch 5, Loss: 0.23122134804725647, Accuracy: 91.41667175292969, Test Loss: 0.27742496132850647, Test Accuracy: 89.99000549316406 Epoch 6, Loss: 0.212575763463974, Accuracy: 92.17333221435547, Test Loss: 0.2573488652706146, Test Accuracy: 90.75 Epoch 7, Loss: 0.1963273137807846, Accuracy: 92.77166748046875, Test Loss: 0.2587501108646393, Test Accuracy: 90.66000366210938 Epoch 8, Loss: 0.1779220998287201, Accuracy: 93.46666717529297, Test Loss: 0.267805814743042, Test Accuracy: 90.55999755859375 Epoch 9, Loss: 0.16410504281520844, Accuracy: 93.91333770751953, Test Loss: 0.25632956624031067, Test Accuracy: 91.00999450683594 Epoch 10, Loss: 0.14829590916633606, Accuracy: 94.47833251953125, Test Loss: 0.25820475816726685, Test Accuracy: 91.00999450683594
สิ่งที่ควรทราบในตัวอย่างด้านบน:
- เรากำลังทำซ้ำ
train_dist_dataset
และtest_dist_dataset
โดยใช้ afor x in ...
- การสูญเสียที่ปรับขนาดคือค่าส่งคืนของ
distributed_train_step
ค่านี้จะรวมข้ามแบบจำลองโดยใช้การเรียกtf.distribute.Strategy.reduce
จากนั้นข้ามแบตช์โดยการรวมค่าส่งคืนของการเรียกtf.distribute.Strategy.reduce
-
tf.keras.Metrics
ควรได้รับการอัปเดตภายในtrain_step
และtest_step
ที่ดำเนินการโดยtf.distribute.Strategy.run
*tf.distribute.Strategy.run
ผลลัพธ์จากแบบจำลองท้องถิ่นแต่ละรายการในกลยุทธ์และมีหลายวิธีในการใช้ผลลัพธ์นี้ คุณสามารถทำtf.distribute.Strategy.reduce
เพื่อรับค่ารวม คุณยังสามารถทำtf.distribute.Strategy.experimental_local_results
เพื่อรับรายการค่าที่มีอยู่ในผลลัพธ์หนึ่งรายการต่อแบบจำลองท้องถิ่น
กู้คืนด่านล่าสุดและทดสอบ
โมเดลที่ถูกตรวจสอบด้วย tf.distribute.Strategy
สามารถเรียกคืนได้โดยมีหรือไม่มีกลยุทธ์
eval_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
name='eval_accuracy')
new_model = create_model()
new_optimizer = tf.keras.optimizers.Adam()
test_dataset = tf.data.Dataset.from_tensor_slices((test_images, test_labels)).batch(GLOBAL_BATCH_SIZE)
@tf.function
def eval_step(images, labels):
predictions = new_model(images, training=False)
eval_accuracy(labels, predictions)
checkpoint = tf.train.Checkpoint(optimizer=new_optimizer, model=new_model)
checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
for images, labels in test_dataset:
eval_step(images, labels)
print ('Accuracy after restoring the saved model without strategy: {}'.format(
eval_accuracy.result()*100))
Accuracy after restoring the saved model without strategy: 91.00999450683594
วิธีอื่นในการวนซ้ำชุดข้อมูล
การใช้ตัวทำซ้ำ
หากคุณต้องการวนซ้ำตามจำนวนขั้นตอนที่กำหนดและไม่ได้ทำผ่านชุดข้อมูลทั้งหมดคุณสามารถสร้างตัววนซ้ำโดยใช้การเรียกใช้ iter
และการเรียกที่ชัดเจน next
จากตัววนซ้ำ คุณสามารถเลือกที่จะวนซ้ำชุดข้อมูลได้ทั้งภายในและภายนอกฟังก์ชัน tf นี่คือตัวอย่างข้อมูลขนาดเล็กที่สาธิตการวนซ้ำของชุดข้อมูลภายนอกฟังก์ชัน tf โดยใช้ตัววนซ้ำ
for _ in range(EPOCHS):
total_loss = 0.0
num_batches = 0
train_iter = iter(train_dist_dataset)
for _ in range(10):
total_loss += distributed_train_step(next(train_iter))
num_batches += 1
average_train_loss = total_loss / num_batches
template = ("Epoch {}, Loss: {}, Accuracy: {}")
print (template.format(epoch+1, average_train_loss, train_accuracy.result()*100))
train_accuracy.reset_states()
Epoch 10, Loss: 0.12157603353261948, Accuracy: 95.0 Epoch 10, Loss: 0.1367541253566742, Accuracy: 94.6875 Epoch 10, Loss: 0.14902949333190918, Accuracy: 93.90625 Epoch 10, Loss: 0.12149540334939957, Accuracy: 95.625 Epoch 10, Loss: 0.13160167634487152, Accuracy: 94.6875 Epoch 10, Loss: 0.13297739624977112, Accuracy: 95.3125 Epoch 10, Loss: 0.16038034856319427, Accuracy: 94.53125 Epoch 10, Loss: 0.1035340279340744, Accuracy: 96.40625 Epoch 10, Loss: 0.11846740543842316, Accuracy: 95.625 Epoch 10, Loss: 0.09006750583648682, Accuracy: 96.71875
ทำซ้ำภายในฟังก์ชัน tf
คุณยังสามารถวนซ้ำในชุดข้อมูล train_dist_dataset
ทั้งหมดภายในฟังก์ชัน tf โดยใช้โครงสร้าง for x in ...
หรือโดยการสร้างตัววนซ้ำเหมือนที่เราทำข้างต้น ตัวอย่างด้านล่างแสดงให้เห็นถึงการสรุปหนึ่งช่วงของการฝึกอบรมในฟังก์ชัน tf และการทำซ้ำ train_dist_dataset
ภายในฟังก์ชัน
@tf.function
def distributed_train_epoch(dataset):
total_loss = 0.0
num_batches = 0
for x in dataset:
per_replica_losses = strategy.run(train_step, args=(x,))
total_loss += strategy.reduce(
tf.distribute.ReduceOp.SUM, per_replica_losses, axis=None)
num_batches += 1
return total_loss / tf.cast(num_batches, dtype=tf.float32)
for epoch in range(EPOCHS):
train_loss = distributed_train_epoch(train_dist_dataset)
template = ("Epoch {}, Loss: {}, Accuracy: {}")
print (template.format(epoch+1, train_loss, train_accuracy.result()*100))
train_accuracy.reset_states()
Epoch 1, Loss: 0.13680464029312134, Accuracy: 94.90499877929688 Epoch 2, Loss: 0.12503673136234283, Accuracy: 95.33499908447266 Epoch 3, Loss: 0.11472766101360321, Accuracy: 95.71333312988281 Epoch 4, Loss: 0.10419528931379318, Accuracy: 96.13500213623047 Epoch 5, Loss: 0.09566374123096466, Accuracy: 96.44833374023438 Epoch 6, Loss: 0.08704081922769547, Accuracy: 96.82499694824219 Epoch 7, Loss: 0.08157625794410706, Accuracy: 96.96333312988281 Epoch 8, Loss: 0.07562965154647827, Accuracy: 97.11000061035156 Epoch 9, Loss: 0.0676642507314682, Accuracy: 97.47999572753906 Epoch 10, Loss: 0.06430575996637344, Accuracy: 97.58333587646484
ติดตามการสูญเสียการฝึกอบรมข้ามแบบจำลอง
เรา ไม่ แนะนำให้ใช้ tf.metrics.Mean
ถึงการติดตามการสูญเสียการฝึกอบรมในแบบจำลองต่างๆเนื่องจากการคำนวณการคำนวณการสูญเสียมาตราส่วนที่ดำเนินการ
ตัวอย่างเช่นหากคุณดำเนินงานฝึกอบรมที่มีคุณสมบัติดังต่อไปนี้:
- สองแบบจำลอง
- มีการประมวลผลสองตัวอย่างในแต่ละแบบจำลอง
- ค่าการสูญเสียที่เกิดขึ้น: [2, 3] และ [4, 5] ในแต่ละแบบจำลอง
- ขนาดแบทช์ส่วนกลาง = 4
ด้วยการวัดความสูญเสียคุณจะคำนวณมูลค่าต่อตัวอย่างของการสูญเสียในแต่ละแบบจำลองโดยการเพิ่มค่าการสูญเสียจากนั้นหารด้วยขนาดแบทช์ส่วนกลาง ในกรณีนี้: (2 + 3) / 4 = 1.25
และ (4 + 5) / 4 = 2.25
หากคุณใช้ tf.metrics.Mean
เพื่อติดตามการสูญเสียในสองแบบจำลองผลลัพธ์จะแตกต่างกัน ในตัวอย่างนี้คุณจะได้ผล total
เป็น 3.50 และ count
2 ซึ่งผลลัพธ์ total
/ count
= 1.75 เมื่อ result()
ถูกเรียกใช้ในเมตริก การสูญเสียคำนวณด้วย tf.keras.Metrics
จะถูกปรับขนาดโดยปัจจัยเพิ่มเติมที่เท่ากับจำนวนแบบจำลองที่ซิงค์
คำแนะนำและตัวอย่าง
นี่คือตัวอย่างบางส่วนสำหรับการใช้กลยุทธ์การกระจายกับลูปการฝึกอบรมที่กำหนดเอง:
- คู่มือการฝึกอบรมแบบกระจาย
- ตัวอย่าง DenseNet โดยใช้
MirroredStrategy
- ตัวอย่าง BERT ได้รับการฝึกฝนโดยใช้
MirroredStrategy
และTPUStrategy
ตัวอย่างนี้เป็นประโยชน์อย่างยิ่งสำหรับการทำความเข้าใจวิธีโหลดจากจุดตรวจและสร้างจุดตรวจเป็นระยะระหว่างการฝึกอบรมแบบกระจายเป็นต้น - ตัวอย่าง NCF ได้รับการฝึกฝนโดยใช้
MirroredStrategy
ที่สามารถเปิดใช้งานโดยใช้แฟkeras_use_ctl
- ตัวอย่าง NMT ที่ ฝึกโดยใช้
MirroredStrategy
ตัวอย่างเพิ่มเติมที่ระบุไว้ใน คู่มือกลยุทธ์การจัดจำหน่าย
ขั้นตอนถัดไป
- ลองใช้
tf.distribute.Strategy
API ใหม่ในแบบจำลองของคุณ - ไปที่ ส่วนประสิทธิภาพ ในคำแนะนำเพื่อเรียนรู้เพิ่มเติมเกี่ยวกับกลยุทธ์และ เครื่องมือ อื่น ๆ ที่คุณสามารถใช้เพื่อเพิ่มประสิทธิภาพของโมเดล TensorFlow ของคุณ