无法让skorch在GPU上运行

时间:2018-07-05 22:12:24

标签: python gpu skorch

我的skorch Neural Net GridsearchCV代码正在运行,它似乎不在GPU上,所以运行缓慢。

任何帮助表示赞赏。

每个纪元需要13秒。我知道pytorch在GPU上的运行速度要快得多。

代码:

import skorch
import torch
import torch.nn as nn

from skorch.classifier import NeuralNetBinaryClassifier
from sklearn import model_selection
from torch.nn import functional as F

cuda = torch.device('cuda')
parameters = {'lr':[1e-4,1e-5]}

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.lin1 = nn.Linear(50, 30)
        self.lin2 = nn.Linear(30, 1)

    def forward(self, x):
        x = F.tanh(self.lin1(x))
        return self.lin2(x).squeeze()

class NLL(torch.nn.modules.loss._Loss):
    def forward(self, input, target):
        loss = F.nll_loss(input.unsqueeze(1),target)
        return loss   


model = Model()
model.to(cuda)

cl1 = NeuralNetBinaryClassifier(
    module=model,
    criterion=NLL,
    optimizer=torch.optim.Adam,
    max_epochs=100,
    train_split=None,
    device='cuda',
    )  

clf = model_selection.GridSearchCV(cl1, parameters, 
    iid=False, 
    scoring='neg_log_loss',
    cv=ps,
    verbose=1,
    n_jobs=1,
    return_train_score=True,
    )

clf.fit(X, Y)

0 个答案:

没有答案