首页 > 解决方案 > "module 'torchtext.data' has no attribute 'Field'"

问题描述

import torchtext

ENGLISH = torchtext.data.Field(tokenize=tokenizer_english, lower=True, init_token="<sos>", eos_token="<eos>")

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-2a3d11c77e7d> in <module>
----> 1 ENGLISH = torchtext.data.Field(tokenize=tokenizer_english, lower=True, init_token="<sos>", eos_token="<eos>")

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

It won't import torchtext.data.Field for some reason even though that's whats in the docs

标签: pythonnlppytorchtorchtext

解决方案


[BC Breaking] Legacy 在 v0.9.0 版本中,我们将以下遗留代码移至 torchtext.legacy。这是改造 torchtext 库工作的一部分,其动机已在问题 #664 中讨论:

torchtext.legacy.data.field
torchtext.legacy.data.batch
torchtext.legacy.data.example
torchtext.legacy.data.iterator
torchtext.legacy.data.pipeline
torchtext.legacy.datasets

我们有一个迁移教程来帮助用户在 v0.9.0 版本中切换到 torchtext 数据集。对于仍然需要旧版组件的用户,他们可以将旧版添加到导入路径中。

试试看ENGLISH = torchtext.legacy.data.field(tokenize=tokenizer_english, lower=True, init_token="<sos>", eos_token="<eos>")


推荐阅读