我想使用 skorch 在 Pytorch 中应用交叉验证,所以我准备了我的模型和我的 tensorDataset,它返回(图像、标题和标题长度),所以它有 X 和 Y,所以我无法在方法中设置 Y
net.fit(dataset)
Run Code Online (Sandbox Code Playgroud)
但是当我尝试时出现错误:
ValueError: Stratified CV 需要明确传递一个合适的 y
这是我的代码的一部分:
start = time.time()
net = NeuralNetClassifier(
decoder, criterion= nn.CrossEntropyLoss,
max_epochs=args.epochs,
lr=args.lr,
optimizer=optim.SGD,
device='cuda', # uncomment this to train with CUDA
)
net.fit(dataset, y=None)
end = time.time()
Run Code Online (Sandbox Code Playgroud) machine-learning computer-vision deep-learning pytorch skorch
我有特征和标签x的维度数据。(n_samples, time_steps, n_features)(n_samples, 1, n_labels)y
由此我创建了一个训练、开发和测试 pytorch 数据集。
我想使用 GridSearchCV 对超参数进行网格搜索。那是我写的:
'Define the network'
sampling_interval = 0.1
net = ConvNet(time_window, ny)
net.float()
'Split test training set'
# trainin test. In this case we take some experiements as test and some as trainint
train_set_split = 0.9
dev_set_split = 0.05
test_set_split = 0.05
# Creating data indices for training and validation splits:
dataset_size = x.shape[0]
indices = list(range(dataset_size))
np.random.shuffle(indices)
split1 = int(np.floor(train_set_split * dataset_size))
split2 = int(np.floor(dev_set_split …Run Code Online (Sandbox Code Playgroud) 我是 pytorch 的新手。我正在尝试进行交叉验证,我找到了 skorch 库,它允许用户将 sklearn 函数与火炬模型一起使用。所以,我定义了一个神经网络类:
torch.manual_seed(42)
class Netcross(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(5,30)
self.sig1 = nn.Tanh()
#self.dout = nn.Dropout(0.2)
self.fc2 = nn.Linear(30,30)
self.sig2 = nn.Sigmoid()
self.out = nn.Linear(30, 1)
self.out_act = nn.Sigmoid()
#self.fc1.weight = torch.nn.Parameter(torch.rand(50,5))
def forward(self, x):
x = self.fc1(x)
x = self.sig1(x)
#x = self.dout(x)
x = self.fc2(x)
x = self.sig2(x)
x = self.out(x)
y = self.out_act(x)
return y
crossnet1 = NeuralNet(
Netcross,
max_epochs = 5,
criterion=torch.nn.BCELoss,
#user defined coeff.
callbacks = [epoch_acc, epoch_f1, epoch_phi], …Run Code Online (Sandbox Code Playgroud) 我正在使用神经网络进行回归。对于 NN 来说,这应该是一项简单的任务,我有 10 个特征和 1 个要预测的输出。我正在为我的项目使用 pytorch,但我的模型学习不好。损失从一个非常高的值(40000)开始,然后在前 5-10 个时期之后损失迅速减少到 6000-7000,然后它卡在那里,无论我做什么。我什至尝试更改为 skorch 而不是 pytorch,以便我可以使用交叉验证功能,但这也无济于事。我尝试了不同的优化器并向网络添加了层和神经元,但这没有帮助,它停留在 6000,这是一个非常高的损失值。我在这里做回归,我有 10 个特征,我试图预测一个连续值。这应该很容易做到,这就是为什么它让我更加困惑。
这是我的网络:我在这里尝试了从制作更复杂的架构(例如添加层和单元到批量标准化、更改激活等)的所有可能性。没有任何效果
class BearingNetwork(nn.Module):
def __init__(self, n_features=X.shape[1], n_out=1):
super().__init__()
self.model = nn.Sequential(
nn.Linear(n_features, 512),
nn.BatchNorm1d(512),
nn.LeakyReLU(),
nn.Linear(512, 64),
nn.BatchNorm1d(64),
nn.LeakyReLU(),
nn.Linear(64, n_out),
# nn.LeakyReLU(),
# nn.Linear(256, 128),
# nn.LeakyReLU(),
# nn.Linear(128, 64),
# nn.LeakyReLU(),
# nn.Linear(64, n_out)
)
def forward(self, x):
out = self.model(x)
return out
Run Code Online (Sandbox Code Playgroud)
这是我的设置:使用 skorch 比使用 pytorch 更容易。在这里,我还监控 R2 指标,并将 RMSE 作为自定义指标来监控我的模型的性能。我也为 Adam 尝试了 amsgrad,但这没有帮助。
R2 = EpochScoring(r2_score, lower_is_better=False, name='R2')
explained_var_score …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 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 …Run Code Online (Sandbox Code Playgroud) 有没有办法让 Skorch 的训练/验证损失适合例如列表(如果你想做一些绘图、统计)?