LoginSignup
8
5

More than 5 years have passed since last update.

KerasのSequentialモデルでSwishを使う

Posted at

やりたいこと

[1710.05941] Swish: a Self-Gated Activation Function

の論文で提案されている活性化関数

f(x) = x\cdot\sigma(x)

をKerasでも使いたい

やりかた

モデルの定義

式が単純なのでやりかたも簡単

from keras.models import Sequential
from keras.layers.convolutional import Conv2D
from keras import backend as K

# Swishの定義
def swish(x):
    return x * K.sigmoid(x)

# モデルの定義
model = Sequential()
model.add(Conv2D(
    32,
    (3, 3),
    strides=(1, 1),
    padding='same',
    data_format=d_format,
    activation=swish,  # <- ここ
    input_shape=input_shape,
    name='conv1'
    ))
# 以下略

学習済みモデルのロード

学習済みモデルを推論用にロードするときは、load_model()custom_objects の情報を与える必要がある。

model = models.load_model(
    model_path,
    custom_objects=
    {
        'swish': swish
    })

そのまま学習済みモデルをロードしようとすると、下記のようなエラーが発生する。

ValueError: Unknown activation function:swish

ベンチマーク結果とかはそのうち気が向いたら書く

参考資料

ほぼ公式リファレンス通り
https://keras.io/ja/activations/

8
5
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
8
5