我试图通过翻译一个简单的pytorch模型来学习Skorch,该模型预测一组MNIST多位数图片中包含的2位数字。这些图片包含2个重叠的数字,它们是输出标签(y)。我收到以下错误:
ValueError: Stratified CV requires explicitely passing a suitable y
我遵循了“带有SciKit-Learn和skorch的MNIST”笔记本,并通过创建自定义的get_loss函数应用了“正向的多个返回值”中概述的多个输出修复程序。 数据维度为:
X - (40000, 1, 4, 28)
y - (40000, 2)
class Flatten(nn.Module):
"""A custom layer that views an input as 1D."""
def forward(self, input):
return input.view(input.size(0), -1)
class CNN(nn.Module):
def __init__(self):
super(CNN, self).__init__()
self.conv1 = nn.Conv2d(1, 32, 3)
self.pool1 = nn.MaxPool2d((2, 2))
self.conv2 = nn.Conv2d(32, 64, 3)
self.pool2 = nn.MaxPool2d((2, 2))
self.flatten = Flatten()
self.fc1 = nn.Linear(2880, 64)
self.drop1 = nn.Dropout(p=0.5)
self.fc2 = nn.Linear(64, 10)
self.fc3 = nn.Linear(64, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = self.pool1(x)
x = F.relu(self.conv2(x))
x = self.pool2(x)
x = self.flatten(x)
x = self.fc1(x)
x = self.drop1(x)
out_first_digit = self.fc2(x)
out_second_digit = self.fc3(x)
return out_first_digit, out_second_digit
torch.manual_seed(0)
class CNN_net(NeuralNetClassifier):
def get_loss(self, y_pred, y_true, *args, **kwargs):
loss1 = F.cross_entropy(y_pred[0], y_true[:,0])
loss2 = F.cross_entropy(y_pred[1], y_true[:,1])
return 0.5 * (loss1 + loss2)
net = CNN_net(
CNN,
max_epochs=5,
lr=0.1,
device=device,
)
net.fit(X_train, y_train);
答案 0 :(得分:0)
skorch的{{1}}默认情况下应用分层的交叉验证拆分,以便为您提供诸如训练期间验证准确性之类的指标。当然,这有必要以这种方式拆分数据 。由于每个图像都有两个标签,因此没有简单的方法可以进行分层拆分(尽管有are ways)。
想到两个解决方案:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12 ;
),并在训练过程中丢失验证NeuralNetClassifier
,将火车拆分为非分层火车由于我猜想您在训练最终代码时需要验证指标,因此应该像这样:
train_split=None