无法更改现有Keras模型中的激活

时间:2017-03-26 15:33:39

标签: python keras keras-layer

我有一个正常的VGG16模型,relu激活,即

def VGG_16(weights_path=None):
    model = Sequential()
    model.add(ZeroPadding2D((1, 1),input_shape=(3, 224, 224)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(ZeroPadding2D((1, 1)))
    model.add(Convolution2D(64, 3, 3, activation='relu'))
    model.add(MaxPooling2D((2, 2), strides=(2, 2)))
[...]
    model.add(Flatten())
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1000, activation='softmax'))

    if weights_path:
        model.load_weights(weights_path)

    return model

我正在使用现有权重对其进行实例化,现在想要将所有relu次激活更改为softmax(我知道这没用)

model = VGG_16('vgg16_weights.h5')
sgd = SGD(lr=0.1, decay=1e-6, momentum=0.9, nesterov=True)

softmax_act = keras.activations.softmax
for (n, layer) in enumerate(model.layers):
    if 'activation' in layer.get_config() and layer.get_config()['activation'] == 'relu':
        print('replacing #{}: {}, {}'.format(n, layer, layer.activation))
        layer.activation = softmax_act
        print('-> {}'.format(layer.activation))

model.compile(optimizer=sgd, loss='categorical_crossentropy')

注意:{/ 1>}在更改之后被称为,因此模型应该仍然可以修改。

但是,即使调试打印正确地说

model.compile

实际结果与replacing #1: <keras.layers.convolutional.Convolution2D object at 0x7f7d7c497f50>, <function relu at 0x7f7dbe699a28> -> <function softmax at 0x7f7d7c4972d0> [...] 激活的模型相同 为什么Keras不使用改变的激活功能?

3 个答案:

答案 0 :(得分:3)

您可能想要使用apply_modifications

idx_of_layer_to_change = -1
model.layers[idx_of_layer_to_change].activation = activations.softmax
model = utils.apply_modifications(model)

答案 1 :(得分:2)

因为仅在keras层中设置激活实际上并不会更改图形,所以我们需要保存修改后的模型并将其加载回去:

from keras import activations
from keras.models import load_model

model.layers[-1].activation = activations.example
model.save(some_path)
model = load_model(some_path)

答案 2 :(得分:1)

函数utils.apply_modifications()对我不起作用。它给了我一个警告

警告:tensorflow:在保存文件中找不到训练配置: 模型是 not 编译的。手动编译。

然后我重新编译该模型,然后它开始工作。为了说明起见,我将所有激活都更改为S型。参见下面的示例

from tensorflow.keras.activations import relu,sigmoid,elu
from tensorflow.keras.applications.vgg16 import VGG16
base_model = VGG16(weights='imagenet', include_top=False,pooling='avg',input_shape= 
    (100, 100, 3))
# before if you check 
base_model.get_config() # you will see all activation are relu 
for layer in base_model.layers:
    if (hasattr(layer,'activation'))==True:
         layer.activation = sigmoid
# without compiling you should not see any changes
# when calling base_model.get_config()
# when compiling
base_model.compile(loss="categorical_crossentropy") #it forced me to put the loss
# now you will see the changes when calling
base_model.get_config()