![](/img/trans.png)
[英]AttributeError: module 'torchtext.datasets' has no attribute 'text_classification'
[英]AttributeError: module 'torchtext' has no attribute 'legacy'
我正在嘗試使用 torchtext 來處理測試數據,但是,當我運行以下代碼時,出現錯誤:“AttributeError: module 'torchtext' has no attribute 'legacy'”。 誰能指導我這里的問題是什么? 我正在使用 python 3.10.4。 謝謝
import pandas as pd
import torch
import torchtext
import spacy
def prep_data(file_path):
TEXT=torchtext.legacy.data.Field(tokenize='spacy', tokenizer_language='en_core_web_sm')
LABEL=torchtext.legacy.data.LabelField(dtype=torch.long)
fields=[('clean_text', TEXT), ('label',LABEL)]
dataset = torchtext.legacy.data.TabularDataset(
path=file_path, format='csv',
skip_header=True, fields=fields)
print(dataset.examples[0])
if __name__=="__main__":
train_path='./data/train.csv'
test_path='./data/test.csv'
prep_data(train_path)
我通過更新 torchtext 解決了同樣的問題。
pip 安裝 torchtext==0.9
我也有同樣的問題。 我使用pytorch 穩定版解決了我的問題您可能使用的是 0.10 和 0.11 版本。 這些是使用舊版的版本。 請更新到最新版本 0.13 和 0.14。
pip install torchtext==<version>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.