PyTorchでの重みの初期化方法
PyTorchで重みを初期化する方法に関する短いチュートリアル。コードとインタラクティブな可視化付き。
Created on August 1|Last edited on August 1
Comment
目次
私たちが取り上げる内容
この記事では、PyTorchモデルのさまざまなレイヤーの重みを初期化する方法を見ていきます。
テンソルフローと異なり、PyTorchはさまざまな層での重みの初期化に対する簡単なインターフェースを提供していませんが、トーチ.nn.init ですしたがって、XavierやHeの初期化のようなよく知られた手法に従って重みを初期化したいときには、ややこしくなります。
以下では、どのように重みを初期化できるかを見ていきますが、実行可能なColabで一緒に進めたい場合は、次の場所でできます。
💡
PyTorchのクラス関数で重みをゼロに初期化する方法
重みを初期化する最も一般的な方法の一つは、クラス関数を使用し、モデルの最後に呼び出すことです。初期化カスタムPyTorchモデルの関数。
import torch.nn as nn class 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からのものです。再度、以下の場所で見つけることができます。
ここでは、標準正規分布の様々な標準偏差が性能面でどのように異なるかを見ることができます。
Run set
42
明らかに標準偏差の大きな値は良い結果につながらず、ほとんどの場合、局所的な最小値につながります。これに対し、小さい値ははるかに良好な性能につながります。
要約
この記事では、PyTorchディープラーニングモデルの重みを初期化する方法と、Weights & Biasesを使用してメトリクスを監視することで貴重な洞察が得られる方法を学びました。
W&Bの全機能を見るには、こちらをご確認ください。short 5 minutes guide数学やゼロからのコード実装をカバーするレポートがもっと欲しい場合は、以下のコメント欄または私たちのサービスでお知らせください。forum ✨!
Weights & Biasesを試してください。
Weights & Biasesは、機械学習実験を追跡するのに役立ちます。私たちのツールを使用して、runからハイパーパラメーターと出力メトリクスをログし、結果を視覚化して比較し、同僚と学びを迅速に共有してください。
指示:
- Click the green "Run" button below (the first time you click Run, Replit will take approx 30-45 seconds to allocate a machine)
- Follow the prompts in the terminal window (the bottom right pane below)
- You can resize the terminal window (bottom right) for a larger view
おすすめ読書
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