隠れ層とパラメータ

PyTorchで学ぶIntroduction to Deep Learning

Jasmin Ludolf

Senior Data Science Content Developer, DataCamp

nn.Sequential() で層を積む

# Create network with three linear layers
model = nn.Sequential(

nn.Linear(n_features, 8),
nn.Linear(8, 4), nn.Linear(4, n_classes)
)
  • 入力は線形層を順に通過します
  • nn.Sequential() 内の層は隠れ層です
PyTorchで学ぶIntroduction to Deep Learning

nn.Sequential() で層を積む

# Create network with three linear layers
model = nn.Sequential(
    nn.Linear(n_features, 8), # n_features represents number of input features
    nn.Linear(8, 4),
    nn.Linear(4, n_classes) # n_classes represents the number of output classes  
)
  • 入力は線形層を順に通過します
  • nn.Sequential() 内の層は隠れ層です
  • n_featuresn_classes はデータセットで決まります
PyTorchで学ぶIntroduction to Deep Learning

層を追加する

2つの隠れ層を持つニューラルネットワークの図

PyTorchで学ぶIntroduction to Deep Learning

層を追加する

4つの隠れ層を持つニューラルネットワークの図

PyTorchで学ぶIntroduction to Deep Learning

層を追加する

4つの隠れ層を持つニューラルネットワークの図

PyTorchで学ぶIntroduction to Deep Learning

層を追加する

4つの隠れ層を持つニューラルネットワークの図

PyTorchで学ぶIntroduction to Deep Learning

層を追加する

# Create network with three linear layers
model = nn.Sequential(
    nn.Linear(10, 18),
    nn.Linear(18, 20),
    nn.Linear(20, 5)
)
PyTorchで学ぶIntroduction to Deep Learning

層を追加する

# Create network with three linear layers
model = nn.Sequential(
    nn.Linear(10, 18), # Takes 10 features and outputs 18
    nn.Linear(18, 20),
    nn.Linear(20, 5)
)
  • 入力10 ➡ 出力18 ➡ 出力20 ➡ 出力5
PyTorchで学ぶIntroduction to Deep Learning

層を追加する

# Create network with three linear layers
model = nn.Sequential(
    nn.Linear(10, 18),
    nn.Linear(18, 20), # Takes 18 and outputs 20
    nn.Linear(20, 5)
)
  • 入力10 ➡ 出力18 ➡ 出力20 ➡ 出力5
PyTorchで学ぶIntroduction to Deep Learning

層を追加する

# Create network with three linear layers
model = nn.Sequential(
    nn.Linear(10, 18),
    nn.Linear(18, 20),
    nn.Linear(20, 5) # Takes 20 and outputs 5
)
  • 入力10 ➡ 出力18 ➡ 出力20 ➡ 出力5
PyTorchで学ぶIntroduction to Deep Learning

層はニューロンで構成される

$$

  • 各ニューロンが直前の層の全ニューロンに接続されていれば全結合

入力層と出力層を矢印で結んだ図

PyTorchで学ぶIntroduction to Deep Learning

層はニューロンで構成される

$$

  • 各ニューロンが直前の層の全ニューロンに接続されていれば全結合

$$

  • 線形層のニューロン:

入力層と出力層を矢印で結び、1つの出力ニューロンを円で囲んだ図

PyTorchで学ぶIntroduction to Deep Learning

層はニューロンで構成される

$$

  • 各ニューロンが直前の層の全ニューロンに接続されていれば全結合

$$

  • 線形層のニューロン:
    • 直前の層の全ニューロンを使って線形演算を行う

入力層と出力層を矢印で結び、1つの出力ニューロンと該当矢印を強調した図

PyTorchで学ぶIntroduction to Deep Learning

層はニューロンで構成される

$$

  • 各ニューロンが直前の層の全ニューロンに接続されていれば全結合

$$

  • 線形層のニューロン:
    • 直前の層の全ニューロンを使って線形演算を行う
    • N+1個のパラメータを持つ:入力からN、バイアスで1

N+1

PyTorchで学ぶIntroduction to Deep Learning

パラメータとモデル容量

  • 隠れ層が多いほど = パラメータが多いほど = モデル容量が高い

$$

2層ネットワークのコードブロック

PyTorchで学ぶIntroduction to Deep Learning

パラメータとモデル容量

  • 隠れ層が多いほど = パラメータが多いほど = モデル容量が高い

$$

2層ネットワークのコードブロック

$$

手計算のパラメータ数:

  • 第1層は4ニューロン、各ニューロンは8+1パラメータ。9×4 = 36
PyTorchで学ぶIntroduction to Deep Learning

パラメータとモデル容量

  • 隠れ層が多いほど = パラメータが多いほど = モデル容量が高い

$$

2層ネットワークのコードブロック

$$

手計算のパラメータ数:

  • 第1層は4ニューロン、各ニューロンは8+1パラメータ。9×4 = 36
PyTorchで学ぶIntroduction to Deep Learning

パラメータとモデル容量

  • 隠れ層が多いほど = パラメータが多いほど = モデル容量が高い

$$

2層ネットワークのコードブロック

$$

手計算のパラメータ数:

  • 第1層は4ニューロン、各ニューロンは8+1パラメータ。9×4 = 36
  • 第2層は2ニューロン、各ニューロンは4+1パラメータ。5×2 = 10
PyTorchで学ぶIntroduction to Deep Learning

パラメータとモデル容量

  • 隠れ層が多いほど = パラメータが多いほど = モデル容量が高い

$$

2層ネットワークのコードブロック

$$

手計算のパラメータ数:

  • 第1層は4ニューロン、各ニューロンは8+1パラメータ。9×4 = 36
  • 第2層は2ニューロン、各ニューロンは4+1パラメータ。5×2 = 10
  • 36 + 10 = 学習可能パラメータ46
PyTorchで学ぶIntroduction to Deep Learning

パラメータとモデル容量

  • 隠れ層が多いほど = パラメータが多いほど = モデル容量が高い

$$

2層ネットワークのコードブロック

$$

PyTorchで数える:

  • .numel(): テンソル内の要素数を返す
total = 0
for parameter in model.parameters():
    total += parameter.numel()
print(total)
46
PyTorchで学ぶIntroduction to Deep Learning

複雑さと効率のバランス

複雑さと効率のバランス

PyTorchで学ぶIntroduction to Deep Learning

¡Vamos a practicar!

PyTorchで学ぶIntroduction to Deep Learning

Preparing Video For Download...