tensorflow - keras 模型中的平均权重

标签 tensorflow neural-network keras deep-learning keras-layer

当我用不同的初始化训练几个具有相同架构的模型时,如何在 Keras 模型中平均权重?

现在我的代码看起来像这样?

datagen = ImageDataGenerator(rotation_range=15,
                             width_shift_range=2.0/28,
                             height_shift_range=2.0/28
                            )

epochs = 40 
lr = (1.234e-3)
optimizer = Adam(lr=lr)

main_input = Input(shape= (28,28,1), name='main_input')

sub_models = []

for i in range(5):

    x = Conv2D(32, kernel_size=(3,3), strides=1)(main_input)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = MaxPool2D(pool_size=2)(x)

    x = Conv2D(64, kernel_size=(3,3), strides=1)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = MaxPool2D(pool_size=2)(x)

    x = Conv2D(64, kernel_size=(3,3), strides=1)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)

    x = Flatten()(x)

    x = Dense(1024)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dropout(0.1)(x)

    x = Dense(256)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Dropout(0.4)(x)

    x = Dense(10, activation='softmax')(x)

    sub_models.append(x)

x = keras.layers.average(sub_models)

main_output = keras.layers.average(sub_models)

model = Model(inputs=[main_input], outputs=[main_output])

model.compile(loss='categorical_crossentropy', metrics=['accuracy'],
              optimizer=optimizer)

print(model.summary())

plot_model(model, to_file='model.png')

filepath="weights.best.hdf5"
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
tensorboard = TensorBoard(log_dir='./Graph', histogram_freq=0, write_graph=True, write_images=True)
callbacks = [checkpoint, tensorboard]

model.fit_generator(datagen.flow(X_train, y_train, batch_size=128),
                    steps_per_epoch=len(X_train) / 128,
                    epochs=epochs,
                    callbacks=callbacks,
                    verbose=1,
                    validation_data=(X_test, y_test))

所以现在我只平均最后一层,但我想在单独训练每一层后平均所有层的权重。

谢谢!

最佳答案

所以让我们假设 models是您的模型的集合。首先 - 收集所有权重:

weights = [model.get_weights() for model in models]

现在 - 创建一个新的平均权重:
new_weights = list()

for weights_list_tuple in zip(*weights):
    new_weights.append(
        [numpy.array(weights_).mean(axis=0)\
            for weights_ in zip(*weights_list_tuple)])

剩下的就是在新模型中设置这些权重:
new_model.set_weights(new_weights)

当然 - 平均权重可能是一个坏主意,但如果你尝试 - 你应该遵循这种方法。

关于tensorflow - keras 模型中的平均权重,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48212110/

相关文章:

python - 尝试使用 .get_layer 方法在 Keras 中创建模型时图形断开连接

python - 从 keras 中的预训练模型加载权重进行微调时出现层错误

python - 将tensorflow/keras模型转换为tensorflow lite模型时出现问题

ubuntu - TensorBoard 无法绑定(bind)到端口 6006,它已经在使用中

python - 更新 Tensorflow 中的变量切片

keras - keras lstm层中的多个内核是什么意思?

python - 如何在 Keras 的每个训练时期之后进行预测?

python-3.x - 如何从 tfrecord 解码 vggish 音频集嵌入?

python - 在 TensorFlow 中实现简单的 PPO 代理

algorithm - Adaboost在神经网络上的实现