首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >当要查找`start`分数最高的令牌时,torch.argmax()中的TypeError

当要查找`start`分数最高的令牌时,torch.argmax()中的TypeError
EN

Stack Overflow用户
提问于 2021-09-18 18:52:23
回答 2查看 103关注 0票数 1

我想运行这段代码,使用拥抱面孔转换器回答问题。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import torch
from transformers import BertForQuestionAnswering
from transformers import BertTokenizer

#Model
model = BertForQuestionAnswering.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')

#Tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-large-uncased-whole-word-masking-finetuned-squad')

question = '''Why was the student group called "the Methodists?"'''

paragraph = ''' The movement which would become The United Methodist Church began in the mid-18th century within the Church of England.
            A small group of students, including John Wesley, Charles Wesley and George Whitefield, met on the Oxford University campus.
            They focused on Bible study, methodical study of scripture and living a holy life.
            Other students mocked them, saying they were the "Holy Club" and "the Methodists", being methodical and exceptionally detailed in their Bible study, opinions and disciplined lifestyle.
            Eventually, the so-called Methodists started individual societies or classes for members of the Church of England who wanted to live a more religious life. '''
            
encoding = tokenizer.encode_plus(text=question,text_pair=paragraph)

inputs = encoding['input_ids']  #Token embeddings
sentence_embedding = encoding['token_type_ids']  #Segment embeddings
tokens = tokenizer.convert_ids_to_tokens(inputs) #input tokens

start_scores, end_scores = model(input_ids=torch.tensor([inputs]), token_type_ids=torch.tensor([sentence_embedding]))

start_index = torch.argmax(start_scores)

但是我在最后一行得到了这个错误:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Exception has occurred: TypeError
argmax(): argument 'input' (position 1) must be Tensor, not str
  File "D:\bert\QuestionAnswering.py", line 33, in <module>
    start_index = torch.argmax(start_scores)

我不知道出了什么问题。有谁可以帮我?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-18 19:17:18

BertForQuestionAnswering返回一个QuestionAnsweringModelOutput对象。

由于您将BertForQuestionAnswering的输出设置为start_scores, end_scores,因此将强制将返回的QuestionAnsweringModelOutput对象转换为字符串元组('start_logits', 'end_logits'),从而导致类型不匹配错误。

下面的代码应该可以工作:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
outputs = model(input_ids=torch.tensor([inputs]), token_type_ids=torch.tensor([sentence_embedding]))

start_index = torch.argmax(outputs.start_logits)
票数 1
EN

Stack Overflow用户

发布于 2021-09-18 19:38:34

Huggingface转换器提供了一种运行模型的简单的高级方法,如此guide所示

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from transformers import pipeline

nlp = pipeline('question-answering', model=model, tokenizer=tokenizer)
print(nlp(question=question, context=paragraph, topk=5))

topk允许选择几个得分最高的答案。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69239925

复制
相关文章
02:输出最高分数的学生姓名
02:输出最高分数的学生姓名 查看 提交 统计 提问 总时间限制:1000ms内存限制:65536kB描述 输入学生的人数,然后再输入每位学生的分数和姓名,求获得最高分数的学生的姓名。 输入第一行输入一个正整数N(N <= 100),表示学生人数。接着输入N行,每行格式如下: 分数 姓名 分数是一个非负整数,且小于等于100; 姓名为一个连续的字符串,中间没有空格,长度不超过20。 数据保证最高分只有一位同学。输出获得最高分数同学的姓名。样例输入 5 87 lilei 99 hanmeimei 97 lil
attack
2018/04/03
2.3K0
当gdb看到一行行的??时,你要冷静!
下面这段,初看一定会脑大,实际原因非常明确,所以遇到时要先观察,不一定是头大的问题。 gdb -p 1461 GNU gdb 6.6 Copyright (C) 2006 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB.  Type "show warranty" for details. This GDB was configured as "x86_64-suse-linux". Attaching to process 14614 Reading symbols from /home/zhangsan/bin/test...done. Using host libthread_db library "/lib64/libthread_db.so.1". Error while mapping shared library sections: ./libtest.so: No such file or directory. Reading symbols from /lib64/libdl.so.2...done. Loaded symbols for /lib64/libdl.so.2 Reading symbols from /lib64/libz.so.1...done. Loaded symbols for /lib64/libz.so.1 Reading symbols from /usr/lib64/libaio.so.1...done. Loaded symbols for /usr/lib64/libaio.so.1 Symbol file not found for ./libtest.so Reading symbols from /lib64/libc.so.6...done. Loaded symbols for /lib64/libc.so.6 Reading symbols from /lib64/ld-linux-x86-64.so.2...done. Loaded symbols for /lib64/ld-linux-x86-64.so.2 Reading symbols from /usr/lib64/libstdc++.so.6...done. Loaded symbols for /usr/lib64/libstdc++.so.6 Reading symbols from /lib64/libm.so.6...done. Loaded symbols for /lib64/libm.so.6 Reading symbols from /lib64/libgcc_s.so.1...done. Loaded symbols for /lib64/libgcc_s.so.1 Reading symbols from /lib64/libpthread.so.0...done. [Thread debugging using libthread_db enabled] [New Thread 47461298698832 (LWP 14614)] [New Thread 1082132800 (LWP 14618)] Symbol file not found for ./libapr-1.so.0 Reading symbols from /lib64/libcrypt.so.1...done. Loaded symbols for /lib64/libcrypt.so.1 Reading symbols from /lib64/libnss_files.so.2...done. Loaded symbols for /lib64/libnss_files.so.2 0x00002b2a709a9ec1 in free () from /lib64/libc.so.6 (gdb) t 2 [Switching to thread 2 (Thread 1082132800 (LWP 14618))]#0  0x00002b2a709cf476 in poll () from /lib64/libc.so.6 (gdb) bt #0  0x00002b2a709cf476 in poll () from
一见
2018/08/07
1K0
Python迭代DataLoader时出现TypeError: Caught TypeError in DataLoader worker process 0.错误。
迭代 DataLoader时出现以下错误,暂时不知道怎么解决,向大家求救,是一个比较稀罕的错误,也分享给大家一个奇葩的问题一起讨论。
全栈程序员站长
2022/08/22
3.9K0
当flex容器中包含absolute元素时
我们设置了justify-content: center;,不同机型显示区别如下:
celineWong7
2020/11/05
3.7K0
jspm init 报错 err TypeError: must start with number, buffer, array or string
研究了一下午啊,发现原来是后期本地安装git以及用git创建了项目造成了在git时需要配置用户名密码才能正常下载数据:
李维亮
2021/07/09
4030
jspm init 报错 err TypeError: must start with number, buffer, array or string
通过GiHub 查找 API 密钥、令牌和密码
关键词: Passwords api_key “api keys” authorization_bearer: oauth auth authentication client_secret api_token: “api token” client_id password user_password user_pass passcode client_secret secret password hash OTP user auth
Khan安全团队
2022/05/17
1.1K0
通过GiHub 查找 API 密钥、令牌和密码
当JSONObject中的value=null时,key竟然会消失?
value为Date类型时,JSON.toJSONString打印出来的是时间戳,取出来的却是Date?等。
陈哈哈
2020/07/03
2.9K0
镜像打包时Dockerfile中要COPY的文件找不到
docker镜像打包的COPY 指令将从构建上下文目录中 <源路径> 的文件/目录复制到新的一层的镜像内的 <目标路径> 位置。 实际操作中正确的方式和错误的方式参考如下:
院长技术
2020/11/24
8.1K0
【提问帖】GO中调用redis时,当并发量比较大时出现的问题。
GO中调用redis时,实始化一个连接池,做法也是一般的用户: &redis.Pool{ MaxIdle: beego.AppConfig.DefaultInt("redis::maxidle", 1), MaxActive: beego.AppConfig.DefaultInt("redis::maxactive", 10), IdleTimeout: time.Duration(beego.AppConfig.DefaultInt64("redis::idle_tim
李海彬
2018/03/27
2.2K0
LInux中的查找
Global Regular Expression Print,表示全局正则表达式版本。grep 命令用于查找文件里符合条件的字符串的一列。若不指定任何文件名称,或是所给予的文件名为 -,则 grep 指令会从标准输入设备读取数据。
matt
2022/10/25
16.2K0
VUE:解决 [Vue warn]: Error in render: “TypeError: item.slice is not a function“ (取部分数据)
4. 解决: 要么不用这个方法 ,要么把 bodyInfoList 改成 2 维数组,方可用 slice 方法解析 。
微风-- 轻许--
2022/04/13
1.6K0
VUE:解决 [Vue warn]: Error in render: “TypeError: item.slice is not a function“ (取部分数据)
当使用 jquery 插件操作 input 时同步 vue 中绑定的变量办法
为什么要同步到 vue 上绑定的变量呢,因为如果我们不更新绑定的变量的值,vue 下次刷新组件的时候,就会将旧的值更新到 input 上。
前Thoughtworks-杨焱
2022/02/19
1.7K0
当AI泡沫破裂时……
很显然我们目前处于一个不稳定的状态。这到底是一场泡沫还是一次革命?答案是当然包含一点革命——深度神经架构所创造的实实在在的成功已经颠覆了视觉和语音识别领域,更通用的机器学习也已经有了大量真实世界用例。
刀刀老高
2018/07/24
3600
当AI泡沫破裂时……
很显然我们目前处于一个不稳定的状态。这到底是一场泡沫还是一次革命?答案是当然包含一点革命——深度神经架构所创造的实实在在的成功已经颠覆了视觉和语音识别领域,更通用的机器学习也已经有了大量真实世界用例。
机器之心
2018/07/26
3760
oracle 中start with ... connect by prior 子句的用法
忍不了了,必须要搞清楚 oracle 中start with  ... connect by prior 子句的用法,每次都会忘记,必须写出来加深印象!!! connect by 是结构化查询中用到的,其基本语法是:  select ... from tablename start with 条件1 connect by 条件2 where 条件3; 假设table这张表是一个树形表 表中存在两个字段:org_id,parent_id 那么通过表示每一条记录的parent是谁,就可以兴城一个树状结构,用
小小明童鞋
2018/06/13
1.9K0
当elementui的el-dialog组件中包含子组件时,用refs调用子组件时的undefined问题
今天用elementui写了一个el-dialog组件里面包着一个el-transfer穿梭框组件, 代码
李维亮
2021/07/08
1.5K0
LeetCode 1838. 最高频元素的频数(二分查找)
给你一个整数数组 nums 和一个整数 k 。 在一步操作中,你可以选择 nums 的一个下标,并将该下标对应元素的值增加 1 。
Michael阿明
2021/09/06
4320
JavaScript中的变量查找
众所周知,JavaScript变量是按照作用域链来进行查找的(作用域和作用域链相关知识可参看我的另一篇文章,《基于JavaScript作用域链的性能调优》), 那么,对于一个简单的赋值操作,等号左右两边变量的查找方式一样吗?让我们从一个简单例子讲起~
娜姐
2022/05/13
1.5K0
当expandlistview的getGroupView或者getChildView中包含checkbox时,前者点击不可用
当expandlistview的getGroupView或者getChildView中包含checkbox时,前者点击不可用。
103style
2022/12/19
2970
点击加载更多

相似问题

查找最高分数并打印

220

查找并仅显示表中最高的分数

32

在多个列中查找最高分数

32

尝试使用循环查找学生的最高分数

222

使用循环从列表中查找最高分数和最低分数

117
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文