CheckPoint、h5、pkl格式保存模型和参数

CheckPoint、h5、pkl格式保存模型和参数

请注意,本文编写于 429 天前,最后修改于 429 天前,其中某些信息可能已经过时。

CheckPoint、h5、pkl格式保存模型和参数

1. TensorFlow

1、CheckPoint(.ckpt)

在训练 TensorFlow 模型时,每迭代若干轮需要保存一次权值到磁盘,称为“checkpoint”。这种格式文件是由 tf.train.Saver() 对象调用 saver.save() 生成的,只包含若干 Variables 对象序列化后的数据,不包含图结构,所以只给 checkpoint 模型不提供代码是无法重新构建计算图的。

2. Keras

2.1 保存整个模型(.h5)

model.save(filepath)将Keras模型和权重保存在一个HDF5文件中,该文件将包含:

模型的结构、模型的权重、训练配置(损失函数,优化器,准确率等)、优化器的状态,以便于从上次训练中断的地方:

model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'
2.2 保存模型结构

model.to_jason()将模型序列化保存为json文件,里面记录了网络的整体结构, 各个层的参数设置等信息. 将json字符串保存到文件.

除了json格式,还可以保存为yaml格式的字符串,形式与JSON一样:

json_string = model.to_json() # save as JSON
yaml_string = model.to_yaml() # save as YAML
2.3 保存模型权重(.h5)

经过调参后网络的输出精度比较满意后,可以将训练好的网络权重参数保存下来,可通过下面的代码利用HDF5进行保存:

model.save_weights(‘model_weights.h5’)
2.4 加载整个模型
model.save('my_model.h5')
model = load_model('my_model.h5')
2.5 加载模型结构
# model reconstruction from JSON:
from keras.models import model_from_json
model = model_from_json(json_string)
 
# model reconstruction from YAML
model = model_from_yaml(yaml_string)
2.6 加载模型权重
model.save_weights('my_model_weights.h5')
model.load_weights('my_model_weights.h5') #初始化一个完全相同的模型
model.load_weights('my_model_weights.h5', by_name=True) #加载权重到不同的网络结构,例如fine-tuning;可以通过层名字来加载

3. PyTorch

3.1 保存和加载整个模型(.pkl)
torch.save(model_object, ‘model.pkl’)
model = torch.load(‘model.pkl’)
3.2 仅保存和加载模型参数(推荐使用)
torch.save(model_object.state_dict(), ‘params.pkl’)
model_object.load_state_dict(torch.load(‘params.pkl’))
3.3 pkl与pth格式文件的区别

pytorch保存数据的格式为.pkl文件或者.pth文件

假设我有一个训练好的模型名叫net1,则

torch.save(net1, ‘7-net.pth’) # 保存整个神经网络的结构和模型参数
torch.save(net1, ‘7-net.pkl’) 
torch.save(net1.state_dict(), ‘7-net_params.pth’) # 只保存神经网络的模型参数
torch.save(net1.state_dict(), ‘7-net_params.pkl’) 

如果你是使用torch.save方法来进行模型参数的保存,那保存文件的后缀其实没有任何影响,结果都是一样的,很多.pkl的文件也是用torch.save保存下来的,和.pth文件一模一样的。。。

4. 参考资料:

  1. PyTorch中pkl和pth的区别?
  2. Keras中的模型的保存与重用
  3. tensorflow保存和恢复模型的两种方法介绍

此处评论已关闭

disulfiram tablets 200mg can i buy valtrex over the counter prescription drug wellbutrin generic viagra discount can you buy cymbalta over the counter tadalafil capsules for sale trazodone purchase zoloft 50 mg pill online pharmacy us tadalafil wellbutrin average price buspar price uk benicar 20 mg prices buy sildenafil citrate online 20 mg levitra tablets paxil medicine fluoxetine hcl 20 mg inderal 160 mg cialis 5mg daily price keflex cheap strattera 50 mg

buy nexium online uk fluoxetine tablets uk finasteride purchase erythromycin generic pharmacy generic valtrex

buy generic antabuse online bactrim 200mg cheapest price for sildenafil 100 mg abilify 10 advair 250 price hydroxychloroquine 2 seroquel 200 mg pill order robaxin without prescription canada zoloft sale online cost of arimidex in australia

dapoxetine uk prescription

nexium 40 mg cost purchase kamagra 100 online canada

setraline purchase cost of diclofenac sildalis tablets buy singulair tablets levitra online australia can you buy strattera over the counter

genuine viagra tablets

voltaren pills 50 mg

generic for valtrex buy without a prescription kamagra jelly bulk price of over the counter nexium levitra 20mg price australia wellbutrin 400mg augmentin 375mg cytotec 200mg pills generic ampicillin 100 best price for tadalafil erythromycin 250 mg coupon antabuse buy canada voltaren gel cheapest price can you buy plaquenil in mexico levitra brand in india kamagra without prescription order strattera online order buspar online can you buy sildenafil over the counter tadalafil online in india paxil price

albendazole brand name canada

order effexor augmentin capsules 875 mg 25 mg elavil tizanidine online pharmacy amoxicillin pill 500mg cheapest citalopram online viagra generic viagra

cost of effexor in australia flagyl 250 sildenafil otc india amoxicillin canadian pharmacy viagra canada order

order ciprofloxacin 500mg propranolol where to get buy bactrim online purplepill.com advair over the counter viagra paypal australia prinzide zestoretic where to buy kamagra oral jelly in pattaya plaquenil 200 mg canada buy chloroquine phosphate tablets

chloroquine 600 mg buy erythromycin 500mg generic amoxil online cheapest brand plaquenil wellbutrin 75 mg price zoloft discount coupon acyclovir price uk anafranil price in south africa

hydroxychloroquine sulfate cost generic sildalis buy sildenafil paypal order wellbutrin sr motrin 200 where to buy aralen buy zanaflex 4mg buy effexor online usa brand viagra no prescription levitra canada no rx

cheapest advair online cipro 500mg cost generic strattera 2017 cost of viagra 100mg in canada generic effexor xr zofran medicine price

average cost of viagra 100mg periactin generic brand

brand name amoxicillin capsule

tadalafil online order chloroquine for sale buy seroquel xr generic zofran price sildenafil us pharmacy

quineprox 750mg antabuse online can i buy viagra online in australia buy effexor online valtrex prescription price

  1. 1
  2. ...
  3. 991
  4. 992
  5. 993