0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

究極に分かるNoise_shapeの動作

Last updated at Posted at 2019-12-13

0. 概要

Dropoutの適用範囲を決めるNoise_shapeの動作であるが、以下のKerasの公式から詳細な処理を読み解くのは不可能に近い。

rate: 0と1の間の浮動小数点数.入力ユニットをドロップする割合.
noise_shape: 入力と乗算されるバイナリドロップアウトマスクのshapeは1階の整数テンソルで表す.例えば入力のshapeを(batch_size, timesteps, features)とし,ドロップアウトマスクをすべてのタイムステップで同じにしたい場合,noise_shape=(batch_size, 1, features)を使うことができる.
seed: random seedとして使うPythonの整数.

そこで、実験的に試して分かった動作をまとめる。

1. Noise_shapeの動作

まず、CNNから出力されたShapeが以下だとする。
image.png

そこでNoise_shapeを指定し、Dropoutを掛ける。
image.png

この時、1x1を指定すると、2x2を1x1のように扱ってくれる。
もう少し詳しく説明するとこんな感じである。
image.png

因みに2x2を指定すると、以下のように各画素に対してDropoutを設定できる。
image.png

これまではバッチサイズを入力数分確保して、独立的にDropoutを行っていたが、これを1つに見立てると以下のような感じの処理になる。

image.png

中々、慣れない表記である。

動作テストのコード

import numpy as np
from tensorflow import keras
from tensorflow.keras import backend as K
from tensorflow.keras.layers import Input, Dropout, Dense, Lambda, BatchNormalization

x_test = np.ones((5, 2, 5))
model = keras.models.Sequential()
#model.add(Lambda(lambda x: K.dropout(x, 0, seed=None)))
model.add(Lambda(lambda x: K.dropout(x, 0.5, seed=None, noise_shape=(5, 1, 5))))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
predictions = model.predict(x_test)
for i in predictions:
    print(i)
    print("----------")
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?