gen*_*ted 5 python deep-learning pytorch skorch
我正在尝试将 PyTorch 功能合并到一个scikit-learn
环境中(特别是 Pipelines 和 GridSearchCV),因此一直在研究skorch
. 神经网络的标准文档示例如下所示
import torch.nn.functional as F
from torch import nn
from skorch import NeuralNetClassifier
class MyModule(nn.Module):
def __init__(self, num_units=10, nonlin=F.relu):
super(MyModule, self).__init__()
self.dense0 = nn.Linear(20, num_units)
self.nonlin = nonlin
self.dropout = nn.Dropout(0.5)
...
...
self.output = nn.Linear(10, 2)
...
...
Run Code Online (Sandbox Code Playgroud)
您可以通过将输入和输出维度硬编码到构造函数中来显式传递它们。然而,这并不是scikit-learn
接口真正的工作方式,其中输入和输出维度是由fit
方法派生的,而不是显式传递给构造函数。作为一个实际的例子考虑
# copied from the documentation
net = NeuralNetClassifier(
MyModule,
max_epochs=10,
lr=0.1,
# Shuffle training data on each epoch
iterator_train__shuffle=True,
)
# any general Pipeline interface
pipeline = Pipeline([
('transformation', AnyTransformer()),
('net', net)
])
gs = GridSearchCV(net, params, refit=False, cv=3, scoring='accuracy')
gs.fit(X, y)
Run Code Online (Sandbox Code Playgroud)
除了变压器中的任何地方都不需要指定输入和输出维度这一事实之外,在模型之前应用的变压器可能会改变训练集的维度(考虑降维等),因此在神经网络中对输入和输出进行硬编码网络构造函数是不行的。
我是否误解了这是如何工作的,或者建议的解决方案是什么(我正在考虑将构造函数指定到forward
您已经可以使用的方法中X
,但我不确定这是一个好的做法)?
这是一个非常好的问题,恐怕对此有最佳实践答案,因为 PyTorch 通常是以初始化和执行是单独步骤的方式编写的,这正是您在这种情况下不想要的。
有几种前进的方法,它们都朝着相同的方向发展,即内省输入数据并在拟合之前重新初始化网络。我能想到的最简单的方法是编写一个回调,在训练开始期间设置相应的参数:
class InputShapeSetter(skorch.callbacks.Callback):
def on_train_begin(self, net, X, y):
net.set_params(module__input_dim=X.shape[-1])
Run Code Online (Sandbox Code Playgroud)
这会在训练开始期间设置一个模块参数,该参数将使用所述参数重新初始化 PyTorch 模块。此特定回调期望调用第一层的参数input_dim
,但您可以根据需要更改此设置。
一个完整的例子:
import torch
import skorch
from sklearn.datasets import make_classification
from sklearn.pipeline import Pipeline
from sklearn.decomposition import PCA
X, y = make_classification()
X = X.astype('float32')
class ClassifierModule(torch.nn.Module):
def __init__(self, input_dim=80):
super().__init__()
self.l0 = torch.nn.Linear(input_dim, 10)
self.l1 = torch.nn.Linear(10, 2)
def forward(self, X):
y = self.l0(X)
y = self.l1(y)
return torch.softmax(y, dim=-1)
class InputShapeSetter(skorch.callbacks.Callback):
def on_train_begin(self, net, X, y):
net.set_params(module__input_dim=X.shape[-1])
net = skorch.NeuralNetClassifier(
ClassifierModule,
callbacks=[InputShapeSetter()],
)
pipe = Pipeline([
('pca', PCA(n_components=10)),
('net', net),
])
pipe.fit(X, y)
print(pipe.predict(X))
Run Code Online (Sandbox Code Playgroud)