LoginSignup
1
3

More than 3 years have passed since last update.

keras model表示で活性化関数名を表示する

Posted at

pythonのkerasで、model.summary()によりモデルを表示できるが、
活性化関数名(Activation)が、
 'activation_1 (Activation)'
のように表示された。
'ReLU'など、どの活性化関数を用いたのか表示したい。

修正前

以下が、修正前のコード(モデル定義部分)。
活性化関数の定義を
 'model.add(Activation('relu'))' とした。.......(A)

model = Sequential()
model.add(Dense(4, input_shape=(784, 1)))
model.add(Activation('relu'))               # A:活性化関数定義
model.add(Dense(10, activation='softmax'))

model.summary()

モデル表示結果。

Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 784, 4)            8         
_________________________________________________________________
activation_1 (Activation)    (None, 784, 4)            0         
_________________________________________________________________
dense_2 (Dense)              (None, 784, 10)           50        
=================================================================
Total params: 58
Trainable params: 58
Non-trainable params: 0
_________________________________________________________________

活性化関数名が、
 'activation_1 (Activation)'
と表示され、どの活性化関数を用いたのかわからない状態。

修正後

kerasのAdvanced activationsを用いる。
Keras Documentation : Advanced Activationsレイヤー

Advanced activationsのReLU関数をインポート。

from keras.layers import ReLU

活性化関数の定義を
 'model.add(ReLU())' とする。......(B)

model = Sequential()
model.add(Dense(4, input_shape=(784, 1)))
model.add(ReLU())                           # B:活性化関数定義
model.add(Dense(10, activation='softmax'))

model.summary()

モデル表示結果。

Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 784, 4)            8         
_________________________________________________________________
re_lu_1 (ReLU)               (None, 784, 4)            0         
_________________________________________________________________
dense_2 (Dense)              (None, 784, 10)           50        
=================================================================
Total params: 58
Trainable params: 58
Non-trainable params: 0
_________________________________________________________________

活性化関数名が、
 're_lu_1 (ReLU)'
と表示され、ReLU関数を用いたことがわかる。

他の活性化関数についても、同様に可能。

補足

Advanced activationsを用いることにしたとき、
最初は、活性化関数の定義を以下のようにしていた。

model.add(Activation(ReLU()))

これだと、うまくいかない上に、以下の警告も表示されるので注意。
どうやら、'ReLU()'をActivationの引数として与えるのではなく、直接model.add()に渡すべきらしい。

UserWarning: Do not pass a layer instance (such as ReLU) as the activation argument of another layer. Instead, advanced activation layers should be used just like any other layer in a model.

1
3
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
1
3