我刚开始学习pytorch,尝试使用前馈神经网络对mnist数据集进行分类,但在使用交叉验证时遇到了一些问题。我的数据形状如下:
x_train
:torch.Size([45000, 784])
,
y_train
:torch.Size([45000])
我尝试使用来自sklearn的KFold。
kfold = KFold(n_splits=10)
以下是我的训练方法的第一部分,其中我将数据划分为几个折叠:
for train_index, test_index in kfold.split(x_train, y_train):
x_train_fold = x_train[train_index]
x_test_fold = x_test[test_index]
y_train_fold = y_train[train_index]
y_test_fold = y_test[test_index]
print(x_train_fold.shape)
for epoch in range(epochs):
...
y_train_fold
变量的索引是正确的,它只是:[ 0 1 2 ... 4497 4498 4499]
,但对于x_train_fold
变量来说不是这样,它是[ 4500 4501 4502 ... 44997 44998 44999]
。测试折叠也是一样的。对于第一次迭代,我希望
x_train_fold
变量是前4500张图片,换句话说,它应该有形状torch.Size([4500, 784])
,但它的形状是torch.Size([40500, 784])
有什么提示可以让这个正确吗?