关于新版本torchtext == 0.12.0 出现的无法调用TabularDataset()的问题

题主运行以下代码:

from torchtext import data

Dataset = data.TabularDataset(path='./drugsCom_raw/drugsComTrain_raw.tsv', format = 'TSV')

出现以下问题:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [23], in ()
----> 1 Dataset = data.TabularDataset(path='./drugsCom_raw/drugsComTrain_raw.tsv', format = 'TSV')

AttributeError: module 'torchtext.data' has no attribute 'TabularDataset'

由于版本是0.12.0,使用网上的方法同样出现错误:

from torchtext.legacy import data


---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [25], in ()
----> 1 from torchtext.legacy import data

ModuleNotFoundError: No module named 'torchtext.legacy'

于是查阅文档,官方给的案例如下:

>>> from torchtext.datasets import AG_NEWS
>>> train_iter = AG_NEWS(split='train')
>>> # Iterate with for loop
>>> for (label, line) in train_iter:
>>>     print(label, line)
>>> # Or send to DataLoader
>>> from torch.utils.data import DataLoader
>>> train_iter = AG_NEWS(split='train')
>>> dataloader = DataLoader(train_iter, batch_size=8, shuffle=False)

此处附上官方链接:torchtext · PyPI

这个库问题很多,而且版本更新后很多方法调整很大,大家最好尽量查阅官方链接。。。

(p.s.为啥一堆人要用这个破玩意啊啊啊啊啊)

省流:

所以,解决方法如下:

改用

from torch.utils.data import DataLoader

你可能感兴趣的:(深度学习,python,人工智能,神经网络,pytorch)