keras 自定义激活函数(失败经验)
按照网上的经验,使用keras自定义了激活函数,却出现了意想不到的错误,AttributeError: ‘Activation’ object has no attribute ‘name’,旺大佬指教。
问题一
1 首先展示下自己源代码
### 自定义激活函数my_f
from keras.layers import Activation
from keras import backend as K
from keras.utils.generic_utils import get_custom_objects
def my_f(x, axis=-1):
ndim = K.ndim(x)
if ndim==2:
print('hahhah')
if ndim>2:
print('HHH')
e = math_ops.exp(x - math_ops.reduce_max(x, axis=axis, keepdims=True))
s = math_ops.reduce_sum(e, axis=axis, keepdims=True)
return e / s
get_custom_objects().update({'my_f': Activation(my_f)})
### 网络框架
input1= keras.layers.Input(shape=train_data_block5_3.shape[1:])
x1 = keras.layers.GlobalAveragePooling2D()(input1)
x1= keras.layers.Dense(1024, activation='relu')(x1)
x1 = keras.layers.Dropout(0.5)(x1)
out = keras.layers.Dense(17,activation='my_f')(x1)
### 训练
from keras.callbacks import ModelCheckpoint
filepath = 'model.best.h5'
checkpoint = ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True,
mode='max')
callbacks_list = [checkpoint]
adam = keras.optimizers.Adam(lr=0.001, decay=1e-8)
model = keras.models.Model(inputs=input1, outputs=out)
model.compile(loss='categorical_crossentropy',
optimizer= adam,
metrics=['accuracy'])
model.fit([train_data_block5_3, [train_labels_block5_3],
epochs=50, batch_size=20,
validation_data=(validation_data_block5_3, validation_labels_block5_3),
callbacks=callbacks_list)
2 一共50个批次,往往在训练完第一个批次的时候会报错,AttributeError: ‘Activation’ object has no attribute ‘name’
Epoch 00001: val_accuracy improved from -inf to 0.67882, saving model to E:CatV/feature/model.best.h5
Traceback (most recent call last):
File "C:/Users/anaconda3/envs/tensorflow2/Lib/site-packages/tensorflow_core/python/keras/softmax_tiao.py", line 100, in <module>
callbacks=callbacks_list)
File "C:\Users\anaconda3\envs\tensorflow2\lib\site-packages\keras\engine\training.py", line 1239, in fit
validation_freq=validation_freq)
File "C:\Users\anaconda3\envs\tensorflow2\lib\site-packages\keras\engine\training_arrays.py", line 216, in fit_loop
callbacks.on_epoch_end(epoch, epoch_logs)
File "C:\Users\anaconda3\envs\tensorflow2\lib\site-packages\keras\callbacks\callbacks.py", line 152, in on_epoch_end
callback.on_epoch_end(epoch, logs)
File "C:\Users\anaconda3\envs\tensorflow2\lib\site-packages\keras\callbacks\callbacks.py", line 719, in on_epoch_end
self.model.save(filepath, overwrite=True)
File "C:\Users\anaconda3\envs\tensorflow2\lib\site-packages\keras\engine\network.py", line 1152, in save
save_model(self, filepath, overwrite, include_optimizer)
File "C:\Users\anaconda3\envs\tensorflow2\lib\site-packages\keras\engine\saving.py", line 449, in save_wrapper
save_function(obj, filepath, overwrite, *args, **kwargs)
File "C:\Users\anaconda3\envs\tensorflow2\lib\site-packages\keras\engine\saving.py", line 541, in save_model
_serialize_model(model, h5dict, include_optimizer)
File "C:\Users\anaconda3\envs\tensorflow2\lib\site-packages\keras\engine\saving.py", line 129, in _serialize_model
model_config['config'] = model.get_config()
File "C:\Users\anaconda3\envs\tensorflow2\lib\site-packages\keras\engine\network.py", line 879, in get_config
layer_config = layer.get_config()
File "C:\Users\anaconda3\envs\tensorflow2\lib\site-packages\keras\layers\core.py", line 925, in get_config
'activation': activations.serialize(self.activation),
File "C:\Users\anaconda3\envs\tensorflow2\lib\site-packages\keras\activations.py", line 200, in serialize
return activation.__name__
AttributeError: 'Activation' object has no attribute '__name__'
Process finished with exit code 1
如果有用过自定义激活函数的朋友,望指教!!!
问题二
- 如果把激活函数改成默认的softmax,能够正常的运行,可是奇怪的是我通过如下命令找到keras源文件的位置,把激活函数的py文件中定义的“softmax”部分注释掉,在jupyter notebook中仍然能够正常运行,在pycharm中则不能。
使用如下命令找到keras源文件位置
keras.__path__
['C:\\Users\\anaconda3\\envs\\tensorflow2\\lib\\site-packages\\keras']
在jupyter notebook中依然能正常运行,玄乎至极,望知道的大佬不吝赐教。