Paddle Tensor holds the wrong type, it holds int, but desires to be int64_t

siv3szwd  于 2021-12-07  发布在  Java
关注(0)|答案(4)|浏览(552)

paddlepaddle 1.8.0 python3.7

预测时报错:
c:\temp\classify>python infer.py
C:\Users\eashow\AppData\Local\Programs\Python\Python37\lib\site-packages\paddle\fluid\executor.py:1093: UserWarning: There are no operators in the program to be executed. If you pass Program manually, please use fluid.program_guard to ensure the current Program is being used.
warnings.warn(error_info)
C:\Users\eashow\AppData\Local\Programs\Python\Python37\lib\site-packages\paddle\fluid\executor.py:1070: UserWarning: The following exception is not an EOF exception.
"The following exception is not an EOF exception.")
Traceback (most recent call last):
File "infer.py", line 53, in
fetch_list=target_var)
File "C:\Users\eashow\AppData\Local\Programs\Python\Python37\lib\site-packages\paddle\fluid\executor.py", line 1071, in run
six.reraise(*sys.exc_info())
File "C:\Users\eashow\AppData\Local\Programs\Python\Python37\lib\site-packages\six.py", line 703, in reraise
raise value
File "C:\Users\eashow\AppData\Local\Programs\Python\Python37\lib\site-packages\paddle\fluid\executor.py", line 1066, in run
return_merged=return_merged)
File "C:\Users\eashow\AppData\Local\Programs\Python\Python37\lib\site-packages\paddle\fluid\executor.py", line 1154, in _run_impl
use_program_cache=use_program_cache)
File "C:\Users\eashow\AppData\Local\Programs\Python\Python37\lib\site-packages\paddle\fluid\executor.py", line 1229, in _run_program
fetch_var_name)
paddle.fluid.core_avx.EnforceNotMet:

C++ Call Stacks (More useful to developers):

Windows not support stack backtrace yet.

Python Call Stacks (More useful to users):

File "C:\Users\eashow\AppData\Local\Programs\Python\Python37\lib\site-packages\paddle\fluid\framework.py", line 2610, in append_op
attrs=kwargs.get("attrs", None))
File "C:\Users\eashow\AppData\Local\Programs\Python\Python37\lib\site-packages\paddle\fluid\layer_helper.py", line 43, in append_op
return self.main_program.current_block().append_op(*args,**kwargs)
File "C:\Users\eashow\AppData\Local\Programs\Python\Python37\lib\site-packages\paddle\fluid\layers\nn.py", line 1873, in embedding
'padding_idx': padding_idx
File "drill.py", line 43, in CNN_net
size=[dict_dim, emb_dim])
File "drill.py", line 76, in
model = CNN_net(words, dict_dim)

Error Message Summary:

InvalidArgumentError: Tensor holds the wrong type, it holds int, but desires to be int64_t.
[Hint: Expected valid == true, but received valid:0 != true:1.] at (D:\1.8.0cpu\paddle\paddle/fluid/framework/tensor_impl.h:33)
[operator < lookup_table > error]

原代码如下:import os
from multiprocessing import cpu_count
import numpy as np
import shutil
import paddle
import paddle.fluid as fluid

用训练好的模型进行预测并输出预测结果

创建执行器

place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())

save_path = 'data/infer_model/'

从模型中获取预测程序、输入数据名称列表、分类器

[infer_program, feeded_var_names, target_var] = fluid.io.load_inference_model(dirname=save_path, executor=exe)

获取数据

def get_data(sentence):

读取数据字典

with open('data/dict_txt.txt', 'r', encoding='utf-8') as f_data:
dict_txt = eval(f_data.readlines()[0])
dict_txt = dict(dict_txt)

把字符串数据转换成列表数据

keys = dict_txt.keys()
data = []
for s in sentence:

判断是否存在未知字符

if not s in keys:
s = ''
data.append(int(dict_txt[s]))
return data

data = []

获取图片数据

data1 = get_data('在获得诺贝尔文学奖7年之后,莫言15日晚间在山西汾阳贾家庄如是说')
data2 = get_data('综合“今日美国”、《世界日报》等当地媒体报道,芝加哥河滨警察局表示,')
data.append(data1)
data.append(data2)

获取每句话的单词数量

base_shape = len(c) for c in data

生成预测数据

tensor_words = fluid.create_lod_tensor(data, base_shape, place)

执行预测

result = exe.run(program=infer_program,
feed={feeded_var_names[0]: tensor_words},
fetch_list=target_var)

分类名称

names = [ '文化', '娱乐', '体育', '财经','房产', '汽车', '教育', '科技', '国际', '证券']

获取结果概率最大的label

for i in range(len(data)):
lab = np.argsort(result)[0][i][-1]
print('预测结果标签为:%d, 名称为:%s, 概率为:%f' % (lab, names[lab], result[0][i][lab]))

ykejflvf

ykejflvf1#

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网API文档常见问题历史IssueAI社区来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

icomxhvb

icomxhvb2#

从报错信息看,是Tensor的数据类型不匹配。Tensor的类型是int,但是需要的是int64,排查下组网吧。

w8ntj3qf

w8ntj3qf3#

原码来源于 =》课程5-深度学习入门NLP-文本分类
https://aistudio.baidu.com/aistudio/projectdetail/1342778?forkThirdPart=1

库都是下现成的,怎么排查下组网?

n3h0vuf2

n3h0vuf24#

不好意思,上述连接无法打开。你可以查一下embeding的输入数据类型,需要是int64类型。检查下数据输入类型是不是不一致。

相关问题