python - torch.utils.data.dataloader 中的 _DataLoaderIter 错误

标签 python pytorch

我想运行需要从 torch.utils.data.dataloader 导入 _DataLoaderIter 的代码。通过检查source code对于数据加载器类,存在该方法。但是,我收到错误:

Traceback (most recent call last):

File "main.py", line 4, in

import data

File "D:\Hyperspectral Data\RCAN\RCAN_TrainCode\code\data\__init__.py", line 3, in module

from dataloader import MSDataLoader

File "D:\Hyperspectral Data\RCAN\RCAN_TrainCode\code\dataloader.py", line 14, in module

from torch.utils.data.dataloader import _DataLoaderIter

ImportError: cannot import name '_DataLoaderIter'

为什么会发生这种情况?

最佳答案

您的评论回答了问题:_DataLoaderIter1.0.0中有吗(您正在链接文档)但不在 0.3.1 中,您可以查看here - 它的名字前面没有 _ .

这是一个教科书示例,为什么访问其他包的私有(private)类/函数(通常带有下划线前缀)是一个坏主意 - 您对其实现和行为的稳定性的保证为零。如果您需要他们的代码,通常最好将代码复制粘贴到您自己的文件中,因为至少可以保证在 torch 的更新和错误修复之间不会更改。 .

关于python - torch.utils.data.dataloader 中的 _DataLoaderIter 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54467696/

相关文章:

python - 使用 Qt Designer 在 PySide2 中实现信号/槽时出错

python - Django 相关领域总是仅在生产中抛出完整性错误

python - 从一维张量中提取前 k 个值索引

python - 用 pytorch 计算预测函数的损失

machine-learning - 在 PyTorch 中使用 Dataloader 迭代数据集时出现 IndexError

python - 如何升级Python venv版本

Python:如何模拟类属性初始化函数

python - 如何将键值管道分隔文件转换为带标题的完美 csv 文件

python - 如何确保所有 PyTorch 代码在 Google Colab 上充分利用 GPU

python - 在 Azure ML 中保存 torch 模型