我正在为 k 折交叉验证拆分数据集,但在使用 Pytorch 的 stack/cat 函数连接张量列表时遇到问题。
首先,我使用 .chunk 方法将训练集和测试集分成块,如下所示
x_train_folds = torch.chunk(x_train, num_folds)
y_train_folds = torch.chunk(y_train, num_folds)
其中 x_train 是 torch.Size([5000, 3, 32, 32]) 的张量,y_train 是 torch.Size([5000]) 的张量
x_train_folds 和 y_train_folds 现在是 num_folds 张量的元组
然后,我需要设置一系列嵌套循环来遍历 K 的不同值和各种折叠,同时始终从训练集中排除一个折叠以在测试/验证时使用:
for k in k_choices:
k_to_accuracies[k] = [] # create empty space to append for a given k-value
for fold in range(num_folds):
# create training sets by excluding the current loop index fold and using that as the test set
x_train_cross_val = torch.cat((x_train_folds[:fold], x_train_folds[fold+1:]), 0)
y_train_cross_val = torch.cat((y_train_folds[:fold], y_train_folds[fold+1:]), 0)
classifier = KnnClassifier(x_train_cross_val, y_train_cross_val)
k_to_accuracies[k].append(classifier.check_accuracy(x_train_folds[fold], y_train_folds[fold], k=k))
如您所见,我总是从原始训练集中跳过一倍以用于验证。这是标准的 K 折交叉验证。
不幸的是,我收到以下错误,我似乎无法弄清楚:
TypeError: expected Tensor as element 0 in argument 0, but got tuple
正如您在 API 列表中看到的那样,.cat 似乎需要一个张量元组,这就是我所拥有的。 https://pytorch.org/docs/stable/torch.html#torch.cat
有没有人有什么建议?
非常感谢 - 德鲁