Inicializace vrstev a transfer learning

Introduction to Deep Learning with PyTorch

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

Inicializace vrstvy

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>))

$$

  • Váhy vrstvy jsou inicializovány na malé hodnoty
  • Malé hodnoty vstupních dat i vah zajišťují stabilní výstupy
Introduction to Deep Learning with PyTorch

Inicializace vrstvy

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>))
Introduction to Deep Learning with PyTorch

Transfer learning

  • Opětovné využití modelu trénovaného na první úloze pro podobnou druhou úlohu
    • Model trénovaný na platech datových vědců v USA
    • Váhy použity k trénování na evropských platech

$$

import torch

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

new_layer = torch.load('layer.pth')
Introduction to Deep Learning with PyTorch

Fine-tuning

  • Typ transfer learningu
    • Nižší rychlost učení
    • Trénování části sítě (některé vrstvy zmrazíme)
    • Doporučení: zmrazit první vrstvy sítě, doladit vrstvy blíže výstupní vrstvě
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
Introduction to Deep Learning with PyTorch

Pojďme procvičovat!

Introduction to Deep Learning with PyTorch

Preparing Video For Download...