1. 程式人生 > >keras、pytorch模型和引數的儲存

keras、pytorch模型和引數的儲存

這篇博文主要簡單介紹深度學習框架keras和pytorch的模型儲存方法和模型引數儲存方法。

keras:

import keras
form keras.models import load_model,Sequential,Model,Input
from keras.layers import Dense,........######
model = Sequential()
model.add(Dense(units = 10,input_shape = (),name = 'dense'))
model.compile(loss = 'mse',optimizer = 'adam'
) model.fit() #########這是整個模型的儲存方法 model.save('/home/路徑/model.h5') ###### 匯出模型 model = load_model('model.h5')# 具體路徑先不寫了,因為我懶

如果你只想儲存模型引數的話:

#先按照上面的方法搭建一個model
model.sequential()
model.add(Dense(12),name )
model.save_weights('weights.h5')
#####想要匯出weights的話
model.load_weights('weights.h5')
###其實在model.save_weights中還有一個引數,by_name 這個引數可以指定匯入一些模型的引數
model.load_weights('weights.h5',by_name = True)

pytorch:

剛入手pytorch 沒多久 ,
直接上程式碼吧
先寫個簡單的主要框架。。

import torch
import torch.optim as optim
import torch.nn as nn2d
class NeuralModel(nn.Module):#### 這裡的nn.Module是pytorch 神經網路的結構
    def __init__(self):
        super(NeuralModel,self).__init__()
        self.conv = nn.Sequential(nn.Conv2d()
        nn.BatchNorm2d(通道數)
        nn.ReLU(inplace = True
)) self.fc1 = nn.Linear(input_dim,units) def forward(self,x): out = self.conv1(x) out = self.fc(x) return out model = NeuralModel().cuda() ########## 土豪如果有GPU就用cuda吧 print(model)#######直接將model 打印出來 lr = 0.1 criterion = nn.CrossEntropyLoss() optimizer = optim.SGD(model.parameters(), lr=lr, momentum=0.9, nesterov=True, weight_decay=0.0001) is_train = True if is_train = True: for epoch in epochs: model.train() for i,(features,labels) in enumerate(train_loader): #train_loader 用pytorch自用的生成器程式碼 features = Variable(features).cuda() labels = Variable(labels).cuda()#cuda 是土豪專用,請謹慎使用,一旦這裡所有的變數使用了cuda,那麼模型的加速也就需要使用cuda,就是上面提到的model = NeuralModel().cuda(),這裡的cuda不能落下,在測試階段也一樣,輸入必須為cuda tensor optimizer.zero_grad() outputs = model(images) loss = criterion(outputs, labels) loss.backward() optimizer.step() if (i+1) % 100 == 0: print("Epoch [%d/%d], Iter [%d/%d] Loss: %.4f" %(epoch+1, total_epoch, i+1, len(train_loader), loss.data[0])) print('the epoch takes time:',time.time()-tims) print('evaluate test set:') acc = test(model, test_loader, btrain=True) if acc > acc_best: acc_best = acc print('current best acc,', acc_best) torch.save(model.state_dict(), model_file) # Decaying Learning Rate if (epoch+1) / float(total_epoch) == 0.3 or (epoch+1) / float(total_epoch) == 0.6 or (epoch+1) / float(total_epoch) == 0.9: lr /= 10 print('reset learning rate to:', lr) for param_group in optimizer.param_groups: param_group['lr'] = lr print(param_group['lr']) # optimizer = torch.optim.Adam(model.parameters(), lr=lr) # optim.SGD(model.parameters(), lr=lr, momentum=0.9, nesterov=True, weight_decay=0.0001) # Save the Model torch.save(model.state_dict(), 'last_model_92_sgd.pkl') else: model.load_state_dict(torch.load(model_file))####### 這個就是匯出模型的引數 model.eval() pred = model(test_features)

torch 這部分的程式碼其實已經搭了一個簡單的torch框架,具體有關torch的一些細節我們之後討論,torch中用來儲存模型引數的程式碼主要是torch.save(model.state_dict(),model_file)
將引數load到model中的程式碼是model.load_state_dict(torch.load(model_file))