本文主要是介绍【Python】利用skorch进行深度学习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
【Python】利用skorch进行深度学习
利用pytorch能够很好地进行私人定制的深度学习,然而torch中的张量总是感觉充满神秘色彩,导致很多时候要进行很久的debug。具有numpy和sklearn特色的skorch应运而生。本文浅尝辄止,仅给出一个实际案例和代码。**
import skorch
from skorch import NeuralNetRegressor
from sklearn.model_selection import RandomizedSearchCV
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
class MyModule(nn.Module):
def __init__(self,num_units=10,nonlin=F.relu,drop=.5):
super(MyModule,self).__init__()
self.module = nn.Sequential(
nn.Linear(7,num_units),
nn.LeakyReLU(),
nn.Dropout(p=drop),
nn.Linear(num_units,1),
)
def forward(self,X):
X = self.module(X)
return X
sknet = NeuralNetRegressor(
MyModule,
criterion=nn.MSELoss,
max_epochs=10,
optimizer=optim.Adam,
optimizer__lr = .005
)
lr = (10**np.random.uniform(-5,-2.5,1000)).tolist()
params = {
'optimizer__lr': lr,
'max_epochs':[300,400,500],
'module__num_units': [14,20,28,36,42],
'module__drop' : [0,.1,.2,.3,.4]
}
gs = RandomizedSearchCV(net,params,refit=True,cv=3,scoring='neg_mean_squared_error',n_iter=100)
这篇关于【Python】利用skorch进行深度学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!