导入torchtext.data.datasets_utils时出错



我试图导入torchtext,但在尝试不同版本后仍出现错误。

from torch.utils.data import DataLoader
from torch.nn.utils.rnn import pad_sequence
import math
from torch.nn import Transformer
import torch.nn as nn
import torch
from torch import Tensor
from torchtext.vocab import build_vocab_from_iterator
from typing import Iterable, List
from torchtext.data.datasets_utils import _RawTextIterableDataset
from torchtext.data.datasets_utils import _read_text_iterator

我得到了如下所示的错误。

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
13 from typing import Iterable, List
14 # from torchtext.data.datasets_utils import _RawTextIterableDataset
---> 15 from torchtext.data.datasets_utils import _read_text_iterator
16 import os
17 import csv
File c:Usersanaconda3envstorch_env1libsite-packagestorchtextdatadatasets_utils.py:7, in <module>
4 import os
6 from torch.utils.data import functional_datapipe, IterDataPipe
----> 7 from torch.utils.data.datapipes.utils.common import StreamWrapper
9 try:
10     import defusedxml.ElementTree as ET
ImportError: cannot import name 'StreamWrapper' from 'torch.utils.data.datapipes.utils.common'

我的torch和torchtext版本如下。

Successfully installed torch-1.12.1 torchtext-0.13.1

我使用torcht=1.10.2和torchtext=0.11.2解决了这个问题。

pip install torch==1.10.2
pip install torchtext=0.11.2

如果你想使用cuda或GPU作为火炬,那么

pip install torch==1.10.2+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
pip install torchtext==0.11.2

相关内容

最新更新