网站排版类型,石家庄优化,我要外包网站,怎做网站手机目录
简介
导入
基本批量大小和学习率
计算按比例分配的批量大小和学习率 政安晨的个人主页#xff1a;政安晨 欢迎 #x1f44d;点赞✍评论⭐收藏 收录专栏: TensorFlow与Keras机器学习实战 希望政安晨的博客能够对您有所裨益#xff0c;如有不足之处#xff0c;欢迎在…目录
简介
导入
基本批量大小和学习率
计算按比例分配的批量大小和学习率 政安晨的个人主页政安晨 欢迎 点赞✍评论⭐收藏 收录专栏: TensorFlow与Keras机器学习实战 希望政安晨的博客能够对您有所裨益如有不足之处欢迎在评论区提出指正 本文目标使用 KerasNLP 和 tf.distribute 进行数据并行训练。
简介 分布式训练是一种在多台设备或机器上同时训练深度学习模型的技术。它有助于缩短训练时间并允许使用更多数据训练更大的模型。KerasNLP 是一个为自然语言处理任务包括分布式训练提供工具和实用程序的库。
在本文中我们将使用 KerasNLP 在 wikitext-2 数据集维基百科文章的 200 万字数据集上训练基于 BERT 的屏蔽语言模型 (MLM)。MLM 任务包括预测句子中的屏蔽词这有助于模型学习单词的上下文表征。 本指南侧重于数据并行性尤其是同步数据并行性即每个加速器GPU 或 TPU都拥有一个完整的模型副本并查看不同批次的部分输入数据。部分梯度在每个设备上计算、汇总并用于计算全局梯度更新。
具体来说本文将教您如何在以下两种设置中使用 tf.distribute API 在多个 GPU 上训练 Keras 模型只需对代码做最小的改动 —— 在一台机器上安装多个 GPU通常为 2 至 8 个单主机、多设备训练。这是研究人员和小规模行业工作流程最常见的设置。 —— 在由多台机器组成的集群上每台机器安装一个或多个 GPU多设备分布式训练。这是大规模行业工作流程的良好设置例如在 20-100 个 GPU 上对十亿字数据集进行高分辨率文本摘要模型训练。 !pip install -q --upgrade keras-nlp
!pip install -q --upgrade keras # Upgrade to Keras 3.
导入
import osos.environ[KERAS_BACKEND] tensorflowimport tensorflow as tf
import keras
import keras_nlp
在开始任何训练之前让我们配置一下我们的单 GPU使其显示为两个逻辑设备。 在使用两个或更多物理 GPU 进行训练时这完全没有必要。这只是在默认 colab GPU 运行时只有一个 GPU 可用上显示真实分布式训练的一个技巧。
!nvidia-smi --query-gpumemory.total --formatcsv,noheader
physical_devices tf.config.list_physical_devices(GPU)
tf.config.set_logical_device_configuration(physical_devices[0],[tf.config.LogicalDeviceConfiguration(memory_limit15360 // 2),tf.config.LogicalDeviceConfiguration(memory_limit15360 // 2),],
)logical_devices tf.config.list_logical_devices(GPU)
logical_devicesEPOCHS 3
24576 MiB 要使用 Keras 模型进行单主机、多设备同步训练您需要使用 tf.distribute.MirroredStrategy API。下面是其工作原理 —— 实例化 MirroredStrategy可选择配置要使用的特定设备默认情况下该策略将使用所有可用的 GPU。 —— 使用该策略对象打开一个作用域并在该作用域中创建所需的包含变量的所有 Keras 对象。通常情况下这意味着在分发作用域内创建和编译模型。 —— 像往常一样通过 fit() 训练模型。 strategy tf.distribute.MirroredStrategy()
print(fNumber of devices: {strategy.num_replicas_in_sync})
INFO:tensorflow:Using MirroredStrategy with devices (/job:localhost/replica:0/task:0/device:GPU:0, /job:localhost/replica:0/task:0/device:GPU:1)
Number of devices: 2
基本批量大小和学习率
base_batch_size 32
base_learning_rate 1e-4
计算按比例分配的批量大小和学习率
scaled_batch_size base_batch_size * strategy.num_replicas_in_sync
scaled_learning_rate base_learning_rate * strategy.num_replicas_in_sync
现在我们需要下载并预处理 wikitext-2 数据集。该数据集将用于预训练 BERT 模型。我们将过滤掉短行以确保数据有足够的语境用于训练。
keras.utils.get_file(originhttps://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-2-v1.zip,extractTrue,
)
wiki_dir os.path.expanduser(~/.keras/datasets/wikitext-2/)# Load wikitext-103 and filter out short lines.
wiki_train_ds (tf.data.TextLineDataset(wiki_dir wiki.train.tokens,).filter(lambda x: tf.strings.length(x) 100).shuffle(buffer_size500).batch(scaled_batch_size).cache().prefetch(tf.data.AUTOTUNE)
)
wiki_val_ds (tf.data.TextLineDataset(wiki_dir wiki.valid.tokens).filter(lambda x: tf.strings.length(x) 100).shuffle(buffer_size500).batch(scaled_batch_size).cache().prefetch(tf.data.AUTOTUNE)
)
wiki_test_ds (tf.data.TextLineDataset(wiki_dir wiki.test.tokens).filter(lambda x: tf.strings.length(x) 100).shuffle(buffer_size500).batch(scaled_batch_size).cache().prefetch(tf.data.AUTOTUNE)
)
在上述代码中我们下载并提取了 wikitext-2 数据集。然后我们定义了三个数据集wiki_train_ds、wiki_val_ds 和 wiki_test_ds。我们对这些数据集进行了过滤以去除短行并对其进行批处理以提高训练效率。
在 NLP 训练/调整中使用衰减学习率是一种常见的做法。在这里我们将使用多项式衰减时间表PolynomialDecay schedule。
total_training_steps sum(1 for _ in wiki_train_ds.as_numpy_iterator()) * EPOCHS
lr_schedule tf.keras.optimizers.schedules.PolynomialDecay(initial_learning_ratescaled_learning_rate,decay_stepstotal_training_steps,end_learning_rate0.0,
)class PrintLR(tf.keras.callbacks.Callback):def on_epoch_end(self, epoch, logsNone):print(f\nLearning rate for epoch {epoch 1} is {model_dist.optimizer.learning_rate.numpy()})
我们还要回调 TensorBoard这样就能在本教程后半部分训练模型时可视化不同的指标。我们将所有回调放在一起如下所示
callbacks [tf.keras.callbacks.TensorBoard(log_dir./logs),PrintLR(),
]print(tf.config.list_physical_devices(GPU))
[PhysicalDevice(name/physical_device:GPU:0, device_typeGPU)]
准备好数据集后我们现在要在 strategy.scope() 中初始化并编译模型和优化器
with strategy.scope():# Everything that creates variables should be under the strategy scope.# In general this is only model construction compile().model_dist keras_nlp.models.BertMaskedLM.from_preset(bert_tiny_en_uncased)# This line just sets pooled_dense layer as non-trainiable, we do this to avoid# warnings of this layer being unusedmodel_dist.get_layer(bert_backbone).get_layer(pooled_dense).trainable Falsemodel_dist.compile(losskeras.losses.SparseCategoricalCrossentropy(from_logitsTrue),optimizertf.keras.optimizers.AdamW(learning_ratescaled_learning_rate),weighted_metrics[keras.metrics.SparseCategoricalAccuracy()],jit_compileFalse,)model_dist.fit(wiki_train_ds, validation_datawiki_val_ds, epochsEPOCHS, callbackscallbacks)
Epoch 1/3
Learning rate for epoch 1 is 0.00019999999494757503239/239 ━━━━━━━━━━━━━━━━━━━━ 43s 136ms/step - loss: 3.7009 - sparse_categorical_accuracy: 0.1499 - val_loss: 1.1509 - val_sparse_categorical_accuracy: 0.3485
Epoch 2/3239/239 ━━━━━━━━━━━━━━━━━━━━ 0s 122ms/step - loss: 2.6094 - sparse_categorical_accuracy: 0.5284
Learning rate for epoch 2 is 0.00019999999494757503239/239 ━━━━━━━━━━━━━━━━━━━━ 32s 133ms/step - loss: 2.6038 - sparse_categorical_accuracy: 0.5274 - val_loss: 0.9812 - val_sparse_categorical_accuracy: 0.4006
Epoch 3/3239/239 ━━━━━━━━━━━━━━━━━━━━ 0s 123ms/step - loss: 2.3564 - sparse_categorical_accuracy: 0.6053
Learning rate for epoch 3 is 0.00019999999494757503239/239 ━━━━━━━━━━━━━━━━━━━━ 32s 134ms/step - loss: 2.3514 - sparse_categorical_accuracy: 0.6040 - val_loss: 0.9213 - val_sparse_categorical_accuracy: 0.4230
根据范围拟合模型后我们对其进行正常评估
model_dist.evaluate(wiki_test_ds) 29/29 ━━━━━━━━━━━━━━━━━━━━ 3s 60ms/step - loss: 1.9197 - sparse_categorical_accuracy: 0.8527[0.9470901489257812, 0.4373602867126465] 对于跨多台计算机的分布式训练而不是只利用单台计算机上的多个设备进行训练您可以使用两种分布式策略MultiWorkerMirroredStrategy 和 ParameterServerStrategy —— tf.distribution.MultiWorkerMirroredStrategy多工作站策略实现了一种 CPU/GPU 多工作站同步解决方案可与 Keras 风格的模型构建和训练循环配合使用并使用跨副本的梯度同步还原。 —— tf.distribution.experimental.ParameterServerStrategy参数服务器策略实现了一种异步 CPU/GPU 多工作站解决方案其中参数存储在参数服务器上工作站异步更新梯度到参数服务器。