完美解决 AttributeError: module ‘torch.utils‘ has no attribute ‘data‘

「爱情、让人受尽委屈。」 2023-09-27 20:14 162阅读 0赞

完美解决 AttributeError: module ‘torch.utils’ has no attribute ‘data’

在这里插入图片描述

文章目录

    • 报错问题
    • 解决方法
    • 声明

报错问题

之前在工作中遇到过这个坑,记录一下问题以及解决方法,不一定针对所有情况都能用,但是可以供大家参考。
问题描述如下:

AttributeError: module ‘torch.utils’ has no attribute ‘data’

属性错误:模块的’torch.utils’没有属性’data’

在这里插入图片描述

解决方法

在这里插入图片描述
只需在顶行添加此代码即可!
import torch.utils.data # 新添加代码

  1. import logging
  2. import numpy as np
  3. import torch
  4. from . import ava_helper as ava_helper
  5. from . import cv2_transform as cv2_transform
  6. from . import transform as transform
  7. from . import utils as utils
  8. from .build import DATASET_REGISTRY
  9. logger = logging.getLogger(__name__)
  10. @DATASET_REGISTRY.register()
  11. class Ava(torch.utils.data.Dataset):
  12. """
  13. AVA Dataset
  14. """
  15. def __init__(self, cfg, split):
  16. self.cfg = cfg
  17. self._split = split

更改为

  1. import logging
  2. import numpy as np
  3. import torch
  4. import torch.utils.data # 新添加代码
  5. from . import ava_helper as ava_helper
  6. from . import cv2_transform as cv2_transform
  7. from . import transform as transform
  8. from . import utils as utils
  9. from .build import DATASET_REGISTRY
  10. logger = logging.getLogger(__name__)
  11. @DATASET_REGISTRY.register()
  12. class Ava(torch.utils.data.Dataset):
  13. """
  14. AVA Dataset
  15. """
  16. def __init__(self, cfg, split):
  17. self.cfg = cfg
  18. self._split = split

声明

解决方法参考网络,如有侵权联系我删除

发表评论

表情:
评论列表 (有 0 条评论,162人围观)

还没有评论,来说两句吧...

相关阅读