模型的Keras重量大于预期 [英] Keras weight of a model bigger than expected
问题描述
我很好地调整了cifar10数据集上的Inception v3模型.保存后,模型的大小为175 Mo,在训练之前为90 Mo,所以我想知道为什么微调模型更大?当我查看两个模型的参数数量时,几乎是相同的.
I am fine tunig the inception v3 model on cifar10 dataset. After saving it, the size of the model is 175 Mo, before the training it is about 90 Mo so I am wondering why the fine tuned model is bigger ? When I look the number of parameters of the two models it is almost the same.
原始模型: 参数总计:23,851,784 可训练的参数:23,817,352 不可训练参数:34,432
Original model : Total params: 23,851,784 Trainable params: 23,817,352 Non-trainable params: 34,432
微调模型: 参数总计:21,823,274 可训练的参数:21,788,842 不可训练参数:34,432
Fine tuned model : Total params: 21,823,274 Trainable params: 21,788,842 Non-trainable params: 34,432
有人对此有想法吗? 为了保存模型,我在fit generator函数内部的回调中使用了ModelCheckpoint函数.
Does anyone have an idea about this ? To save the model, I use the function ModelCheckpoint in a callback inside the fit generator function.
checkpoints = ModelCheckpoint(output+'-{epoch:02d}.hdf5', verbose=1, save_best_only=False, period=checkpoint)
callbacks_list = [callback_tensorboard, checkpoints]
# train
model.fit_generator(train_generator,
steps_per_epoch=step_train,
epochs=epochs,
verbose=1,
validation_data=test_generator,
validation_steps=step_test,
callbacks=callbacks_list)
推荐答案
默认情况下,ModelCheckpoint
保存有关模型的所有信息,包括以下内容:
By default ModelCheckpoint
saves everything about a model, including the following:
- 模型架构
- 重量
- 优化器和损失函数之类的训练配置
- 优化器的状态,因此您可以直接从检查点恢复训练.
如果使用保持状态的优化器(如ADAM或RMSProp),其中每个参数均保持运行平均值,则该优化器状态也将保存到HDF5文件中,从而将最终文件大小增加一个由优化器.
If you use an optimizer that keeps state, like ADAM or RMSProp, where a running average is kept for each parameter, then this optimizer state is also saved into the HDF5 file, increasing the final file size by a factor defined by the optimizer.
一个简单的解决方案是手动使用model.save_weights
仅保存权重,或者在您的ModelCheckpoint
实例中将save_weights_only
参数设置为True.
An easy solution is to either use model.save_weights
manually to only save the weights, or to set the save_weights_only
parameter to True in your instance of ModelCheckpoint
.
这篇关于模型的Keras重量大于预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!