当前位置: 首页 > news >正文

成功解决 AttributeError: ‘Field‘ object has no attribute ‘vocab‘

最近复现代码过程中,需要用到 torchtext.data 中的 Field 类。本篇博客记录使用过程中的问题及解决方式。

  1. 注意 torchtext 版本不宜过新

在较新版本的 torchtext.data 里面并没有 Field 方法,这一点需要注意。

启示:在复现别人代码时,应同时复制他们使用环境的版本信息。

  1. 运行下述代码:
from torchtext.data import FieldSRC = Field(tokenize = tokenize_en, init_token = '<sos>', eos_token = '<eos>',fix_length = max_length,lower = True, batch_first = True,sequential=True)TRG = Field(tokenize = tokenize_en, init_token = '<sos>', eos_token = '<eos>', fix_length = max_length,lower = True, batch_first = True,sequential=True)print(SRC.vocab.stoi["<sos>"])
print(TRG.vocab.stoi["<sos>"])

报错信息:

print(SRC.vocab.stoi["<sos>"])  # 2
AttributeError: 'Field' object has no attribute 'vocab'

于是查看 Field 类的定义,寻找和词表建立相关的函数,发现其 build_vocab() 函数中有建立词表的操作, build_vocab() 函数定义如下:

class Field(RawField):...def build_vocab(self, *args, **kwargs):"""Construct the Vocab object for this field from one or more datasets.Arguments:Positional arguments: Dataset objects or other iterable datasources from which to construct the Vocab object thatrepresents the set of possible values for this field. Ifa Dataset object is provided, all columns correspondingto this field are used; individual columns can also beprovided directly.Remaining keyword arguments: Passed to the constructor of Vocab."""counter = Counter()sources = []for arg in args:if isinstance(arg, Dataset):sources += [getattr(arg, name) for name, field inarg.fields.items() if field is self]else:sources.append(arg)for data in sources:for x in data:if not self.sequential:x = [x]try:counter.update(x)except TypeError:counter.update(chain.from_iterable(x))specials = list(OrderedDict.fromkeys(tok for tok in [self.unk_token, self.pad_token, self.init_token,self.eos_token] + kwargs.pop('specials', [])if tok is not None))self.vocab = self.vocab_cls(counter, specials=specials, **kwargs)...

解决方式:在程序中 Field 定义后添加 SRC.build_vocab()TRG.build_vocab(),程序变成:

SRC.build_vocab()
TRG.build_vocab()print(SRC.vocab.stoi["<sos>"])  # 输出结果:2
print(TRG.vocab.stoi["<sos>"])  # 输出结果:2

至此,程序就会顺利执行啦!


参考资料

  1. python - BucketIterator 抛出 ‘Field’ 对象没有属性 ‘vocab’ - IT工具网 (coder.work)
  2. ImportError: cannot import name ‘Field‘ from ‘torchtext.data‘, No module named “legacy“_no module named 'torchtext.legacy_御用厨师的博客-CSDN博客
http://www.lryc.cn/news/94660.html

相关文章:

  • ikbc键盘2.4G接收器丢失,重新对码
  • STM32 Proteus仿真医用仓库环境控制系统紫外线消毒RS232上传CO2 -0066
  • Docker(二)之容器技术所涉及Linux内核关键技术
  • 计算机网络_ 1.3 网络核心 (数据交换_电路交换)
  • Kafka高性能集群部署与优化
  • Lucene介绍与入门使用
  • 【最短路+状压】CF1846 G
  • vue+elementui实现英雄联盟道具城
  • ruby注释
  • 2023(WAIC)智能驾驶科技峰会丨拓数派大模型下的数据计算系统,助力汽车智能化产业数据增值
  • 牛客周赛 Round 2
  • Git 命令提交和分支控制
  • 2023 node 接入腾讯云短信服务,实现发送短信功能
  • Devops系列四(使用argocd部署java应用到k8s容器)
  • 如何在Microsoft Excel中进行不连续区域批量填充
  • k8s+springboot+CronJob 定时任务部署
  • Spring5 中更优雅的第三方 Bean 注入
  • Yolov5-Face 原理解析及算法解析
  • 通俗易懂讲解CPU、GPU、FPGA的特点
  • PIC18 DataRAM 笔记
  • 【FFMPEG】AVFilter使用流程
  • 爬虫入门06——了解cookie和session
  • Ubuntu 的移动梦醒了
  • RabbitMQ的集群
  • 超长上下文处理:基于Transformer上下文处理常见方法梳理
  • ChatGPT爆火 但生成式AI并非全新产物
  • 深度学习循环神经网络
  • 如何规范的设计数据库表
  • 【CSS】跳动文字
  • arm海思启动udev的错误