層の初期化と転移学習

PyTorchで学ぶIntroduction to Deep Learning

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

層の初期化

import torch.nn as nn

layer = nn.Linear(64, 128)
print(layer.weight.min(), layer.weight.max())
(tensor(-0.1250, grad_fn=<MinBackward1>), tensor(0.1250, grad_fn=<MaxBackward1>))

$$

  • 層の重みは小さな値で初期化される
  • 入力と重みを小さく保つと出力が安定する
PyTorchで学ぶIntroduction to Deep Learning

層の初期化

import torch.nn as nn

layer = nn.Linear(64, 128)
nn.init.uniform_(layer.weight)

print(layer.weight.min(), layer.weight.max())
(tensor(0.0002, grad_fn=<MinBackward1>), tensor(1.0000, grad_fn=<MaxBackward1>))
PyTorchで学ぶIntroduction to Deep Learning

転移学習

  • 類似タスクに対し、最初のタスクで学習したモデルを再利用する
    • 米国のデータサイエンティスト給与で学習
    • その重みを欧州の給与での学習に流用

$$

import torch

layer = nn.Linear(64, 128)
torch.save(layer, 'layer.pth')

new_layer = torch.load('layer.pth')
PyTorchで学ぶIntroduction to Deep Learning

ファインチューニング

  • 転移学習の一種
    • 学習率を小さくする
    • ネットワークの一部のみ学習(いくつかは「凍結」)
    • 目安: 早い層を凍結し、出力層に近い層を微調整
import torch.nn as nn

model = nn.Sequential(nn.Linear(64, 128),
                      nn.Linear(128, 256))

for name, param in model.named_parameters():
    if name == '0.weight':
        param.requires_grad = False
PyTorchで学ぶIntroduction to Deep Learning

演習に進みましょう

PyTorchで学ぶIntroduction to Deep Learning

Preparing Video For Download...