PyTorchでウェイトを初期化する方法
A short tutorial on how you can initialize weights in PyTorch with code and interactive visualizations.
Created on August 15|Last edited on August 17
Comment
目次
紹介する内容
この記事では、PyTorchモデルの異なるレイヤーのウェイトを初期化する方法について説明します。
Tensorflowとは違って、PyTorchは異なるレイヤーでウェイトを初期化するための簡単なインターフェースを提供していません( torch.nn.init はポイントですが)。そのため、XavierやHe Initializationなどのよく知られている方法でウェイトを初期化しようとすると、難しくなります。
そこで、ウェイトを初期化する方法について見ていきたいと思います。Colabを見ながら行いたい場合は、こちらからご確認ください:
💡
クラス関数を持つPyTorchでウェイトをゼロに初期化する
ウェイトを初期化する最も一般的な方法の1つは、カスタムPyTorchモデルの__init__関数の最後に呼び出せるクラス関数を使用することになります。
import torch.nn as nnclass Model(nn.Module):# . . .def __init__(self):# .self.apply(self._init_weights)def _init_weights(self, module):if isinstance(module, nn.Linear):module.weight.data.normal_(mean=0.0, std=1.0)if module.bias is not None:module.bias.data.zero_()
このコードスニペットは、平均0と標準偏差1の正規分布からすべてのウェイトを初期化し、すべてのバイアスをゼロに初期化します。これをnn.LayerNormやnn.Embeddingなどの他のレイヤーに拡張するのは非常に簡単です。
def _init_weights(self, module):if isinstance(module, nn.Embedding):module.weight.data.normal_(mean=0.0, std=1.0)if module.padding_idx is not None:module.weight.data[module.padding_idx].zero_()elif isinstance(module, nn.LayerNorm):module.bias.data.zero_()module.weight.data.fill_(1.0)
Pytorch ウェイトの初期化実験
下記の Weights & Biasesのグラフは、ウェイトの初期化をより分かりやすく説明するために、Colabから抽出されたものとなります。繰り返しになりますが、こちらからご覧いただけます:
ここでは、正規分布の様々な標準偏差が、どのようにパフォーマンスの点で互いに異なっている��を見ることができます。
標準偏差の大きな値は明らかに良い結果をもたらさず、局所的な最小値につながる可能性が高くなります。一方、値が小さいほどパフォーマンスが向上します。
要約
この記事では、PyTorchディープラーニングモデルのウェイトを初期化する方法と、Weights & Biasesを使用して指標を測定する方法について説明しました。
W&B機能の完全版を確認するには、こちらの5分間のショートガイドをご覧ください。数学と「ゼロから」コードの実装をカバーするレポートをさらにご希望の場合は、下のコメントまたはフォーラム ✨でお知らせください!
Weights & Biasesを試す
Weights & Biasesは、機械学習実験を追跡するのに役立ちます。 実行からのハイパーパラメータと出力メトリックを記録後、結果を視覚化・比較し、調査結果をチームとすばやく共有できるツールをお試しください。
手順:
- 下の緑色の[実行]ボタンをクリックします(最初に[実行]をクリックすると、Replitでのマシンの割り当てに、約30~45秒かかります)。
- ターミナルウィンドウのプロンプトに従います(下図の右下のペイン)
- ターミナルウィンドウ(右下)のサイズを変更して、ビューを拡大表示することができます
おすすめ記事
How To Use GPU with PyTorch
A short tutorial on using GPUs for your deep learning models with PyTorch, from checking availability to visualizing usable.
A Gentle Introduction To Weight Initialization for Neural Networks
An explainer and comprehensive overview of various strategies for neural network weight initialization
PyTorch Dropout for regularization - tutorial
Learn how to regularize your PyTorch model with Dropout, complete with a code tutorial and interactive visualizations
How to save and load models in PyTorch
This article is a machine learning tutorial on how to save and load your models in PyTorch using Weights & Biases for version control.
Image Classification Using PyTorch Lightning and Weights & Biases
This article provides a practical introduction on how to use PyTorch Lightning to improve the readability and reproducibility of your PyTorch code.
How to Compare Keras Optimizers in Tensorflow for Deep Learning
A short tutorial outlining how to compare Keras optimizers for your deep learning pipelines in Tensorflow, with a Colab to help you follow along.
Add a comment
Iterate on AI agents and models faster. Try Weights & Biases today.