如何使用 pytorch 的 cat 函数进行 K 折验证(即将 pytorch 块列表连接在一起)
How to use pytorch's cat function for K-Fold Validation (i.e. concatenate a list of pytorch chunks together)
我正在为 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 的不同值和各种折叠,同时始终从训练集中排除一个折叠以在 test/validation 时间使用:
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
有人有什么建议吗?
非常感谢
-德鲁
尝试:
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)
torch.cat
接收到一个元素为 torch.Tensor
类型的元组。但是,元组 x_train_folds[:fold]
中的元素仍然是 tuple
。因此,您需要删除张量的 tuple
'decorator'。
我正在为 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 的不同值和各种折叠,同时始终从训练集中排除一个折叠以在 test/validation 时间使用:
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
有人有什么建议吗?
非常感谢 -德鲁
尝试:
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)
torch.cat
接收到一个元素为 torch.Tensor
类型的元组。但是,元组 x_train_folds[:fold]
中的元素仍然是 tuple
。因此,您需要删除张量的 tuple
'decorator'。