将keras模型权重直接保存到字节/内存中吗? [英] Save keras model weights directly to bytes/memory?

查看:144
本文介绍了将keras模型权重直接保存到字节/内存中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Keras允许保存整个模型或仅保存模型权重(请参见线程).保存权重时,必须将其保存到文件中,例如:

Keras allows for saving entire models or just model weights (see thread). When saving the weights, they must be saved to a file, eg:

model = keras_model()
model.save_weights('/tmp/model.h5')

我不想只写文件,而是想将字节保存到内存中.像

Instead of writing to file, I'd like to just save the bytes into memory. Something like

model.dump_weights()

Tensorflow似乎没有此功能,因此,作为一种解决方法,我先写入磁盘,然后再读取至内存:

Tensorflow doesn't seem to have this, so as a workaround I'm writing to disk and then reading into memory:

temp = '/tmp/weights.h5'
model.save_weights(temp)
with open(temp, 'rb') as f:
    weightbytes = f.read()

有什么办法可以避免这个环形交叉路口?

Any way to avoid this roundabout?

推荐答案

weights = model.get_weights()将获得模型权重. model.set_weights(weights)将设置模型权重.问题之一是何时保存模型权重.通常,您要保存验证损失最小的时期的模型权重. Keras回调ModelCheckpoint将具有最小验证损失的权重保存到文件中.我发现保存到文件很不方便,所以我写了一个小的自定义回调,只是将具有最小验证损失的权重保存到类变量中,然后在训练完成后将这些权重加载到模型中以进行预测.代码如下所示.编译模型时,只需将save_best_weights添加到回调列表中即可.

weights=model.get_weights() will get the model weights. model.set_weights(weights) will set the model weights.One of the issues though is WHEN do you save the model weights. Generally you want to save the model weights for the epoch in which you had the lowest validation loss. The Keras callback ModelCheckpoint will save the weights with the lowest validation loss to a file. I found that saving to a file is inconvenient so I wrote a small custom callback to just save the weight with the lowest validation loss into a class variable then after training is complete load those weights into the model to make predictions. Code is shown below. Just add save_best_weights to the list of callbacks when you compile the model.

class save_best_weights(tf.keras.callbacks.Callback):
best_weights=model.get_weights()    
def __init__(self):
    super(save_best_weights, self).__init__()
    self.best = np.Inf
def on_epoch_end(self, epoch, logs=None):
    current_loss = logs.get('val_loss')
    accuracy=logs.get('val_accuracy')* 100
    if np.less(current_loss, self.best):
        self.best = current_loss            
        save_best_weights.best_weights=model.get_weights()
        print('\nSaving weights validation loss= {0:6.4f}  validation accuracy= {1:6.3f} %\n'.format(current_loss, accuracy))   

这篇关于将keras模型权重直接保存到字节/内存中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆