Pytorch和Keras如何保存模型

一、Pytorch保存模型

1.只保存参数及其加载:

torch.save(model.state_dict(),path)
再次加载:
torch.load_state_dict(torch.load(path))
model.eval()

2.保存整个model及加载:

torch.save(model,path)
再次加载:
model = torch.load(path)
model.ecal()

3.只加载某一层的参数:

conv1_weight_state = torch.load(’./model_state_dict.pt’)[‘conv1.weight’]

实例:

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim


class TheModelClass(nn.Module):        
def __init__(self):              
	super(TheModelClass,self).__init__()
	self.conv1 = nn.Conv2d(3,6,5          		
	self.pool = nn.MaxPool2d(2,2)                
	self.conv2 = nn.Conv2d(6,16,5)                
	self.fc1 = nn.Linear(16*5*5,120)                
	self.fc2 = nn.Linear(120,84)                
	self.fc3 = nn.Linear(84,10)         
def forward(self,x):                
	x = self.pool(F.relu(self.conv1(x)))                
	x = self.pool(F.relu(self.conv2(x)))                
	x = x.view(-1,16*5*5)                
	x = F.relu(self.fc1(x))               
	x = F.relu(self.fc2(x))              
	x = self.fc3(x)                
	return x

model = TheModelClass()
optimizer = optim.SGD(model.parameters(),lr=0.001,momentum=0.9
print("model's state_dict:")for param_tensor in model.state_dict():    print(param_tensor,'\t',model.state_dict()[param_tensor].size())


OUTOUT:
model's state_dict:
conv1.weight 	 torch.Size([6, 3, 5, 5])
conv1.bias 	 torch.Size([6])
conv2.weight 	 torch.Size([16, 6, 5, 5])
conv2.bias 	 torch.Size([16])
fc1.weight 	 torch.Size([120, 400])
fc1.bias 	 torch.Size([120])
fc2.weight 	 torch.Size([84, 120])
fc2.bias 	 torch.Size([84])
fc3.weight 	 torch.Size([10, 84])
fc3.bias 	 torch.Size([10])

print(model.state_dict())


OUPUT:
OrderedDict([('conv1.weight', tensor([[[[ 0.0081,  0.0695, -0.0572, -0.0857, -0.0297],
          [ 0.1128, -0.0293,  0.0652, -0.0494,  0.0893],
          [ 0.0837,  0.0063,  0.0255,  0.0576, -0.1125],
          [ 0.0774, -0.0902, -0.0800,  0.1093, -0.0501],
          [ 0.0266,  0.0295,  0.0587, -0.0067,  0.0775]],

         [[-0.0500,  0.0022,  0.0709, -0.0174, -0.0659],........
 

print("\noptimizer's state_dict")
for var_name in optimizer.state_dict():
	print(var_name,'\t',optimizer.state_dict()[var_name])

OUPUT:
optimizer's state_dict
state 	 {}
param_groups 	 [{'lr': 0.001, 'momentum': 0.9, 'dampening': 0, 'weight_decay': 0, 'nesterov': False, 'params': [139952857947448, 139952857947592, 139952990248320, 139952858045824, 139952858046328, 139952858045608, 139952858042944, 139952858043736, 139952858045680, 139952858046040]}]


## 仅仅加载某一层的参数
conv1_weight_state = torch.load('./model_state_dict.pt')['conv1.weight']
print(conv1_weight_state==model.conv1.weight) model_2 = TheModelClass()
model_2.load_state_dict(torch.load('./model_state_dict.pt'))
model_2.conv1.requires_grad=False
print(model_2.conv1.requires_grad)
print(model_2.conv1.bias.requires_grad)

二、Keras保存和加载模型

保存模型:keras提供了简单的方法:

checkpoint = ModelCheckpoint(filepath=filepath[fold],monitor='val_loss',mode='auto' ,save_best_only=True,save_weights_only=False)
callback_lists=[checkpoint]    
model.fit(x_train,y_train,batch_size=batch_size,epochs=epochs,verbose=2,validation_data=(x_val, y_val),callbacks=callback_lists)

monitor:检测指标,如果没有其他特殊参数,则根据指标选择最好的模型保存
save_weights_only =True时,只保存参数,那么对应加载模型的load_weights;若为False,则保存网络对应
load_model()

我常用的是以下2种:

1.通过加载模型:load_model

文件必须已经保存了模型,load_model除了包含load_weights的代码,还有自身加载网络结构的代码。load_model加载模型,直接调用,不需要先有模型的限制:
load_model(path)
对应pytorch:
model = torch.load(path)
model.eval()#pytorch中,这个必须要有,转变训练模式至测试模式

2.通过加载权重load_weights

仅加载权重,同pytorch一样,它在使用时必须先有网络:
model = MODEL()
model.load_weights(path)
model.evaluate()

你可能感兴趣的:(机器学习)