Tensor holds the wrong type, it holds double, but desires to be int64_t at [D:\1.5.2\paddle\paddle/fluid/framework/tensor_impl.h:30] PaddlePaddle Call Stacks: Windows not support stack backtrace yet.
Created by: Milkmilkmilk-3
什么都不懂 所以挂代码和报错
下面是代码
#用训练好的模型进行预测并输出预测结果 #创建执行器 import json 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) infer_exe=fluid.CPUPlace() infer_exe=fluid.Executor(place) infer_exe.run(fluid.default_startup_program())
save_path='C:/Users/lenovo/Desktop/end/fenleiqi/'
#从模型中获取预测程序,输入数据名称列表、分类器 [infer_program,feeded_var_names,target_var]=fluid.io.load_inference_model(dirname=save_path,executor=infer_exe)
#获取数据 def get_data(sentence): #读取数据字典 dict_txt={} with open('C:/Users/lenovo/Desktop/end/dans.txt','r',encoding='utf-8') as f_data: dic_txt=json.loads(f_data.readline())
#把字符串数据转换成列表数据
keys=dict_txt.keys()
data=[]
for s in sentence:
#判断是否存在未知字符
if not s in keys:
s='<unk>'
else:
data.append(int(dict_txt[s]))
return data
data=[] #获取图片数据 data1=get_data('在获得诺贝尔文学奖7年之后,莫言15日晚在山西汾阳贾家庄如是说') print('data1') print(data1) data2=get_data('综合"今日美国"、《世界日报》等当地媒体报道,芝加哥河滨警察局表示,') data.append(data1) print('data2') print(data2) 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 )
#分类名称 def name(): with open('zuimings.txt', 'r', encoding='utf-8') as f: return json.loads(f.readline()) mynames=name().keys() names=[] for a in mynames: names.append(a)
#过去数据概率最大的lable for i in range(len(data)): lab=np.argsort(result)[0][i][-1] print('预测结果为: %d, 名称为: %s, 概率为: %f'%(lab,names[lab],result[0][1][lab]))
下面是报错 C:\Users\lenovo\Desktop\end>end.py data1 [] data2 [] Traceback (most recent call last): File "C:\Users\lenovo\Desktop\end\end.py", line 58, in fetch_list=target_var File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\paddle\fluid\executor.py", line 657, in run use_program_cache=use_program_cache) File "C:\Users\lenovo\AppData\Local\Programs\Python\Python36\lib\site-packages\paddle\fluid\executor.py", line 755, in _run exe.run(program.desc, scope, 0, True, True, fetch_var_name) paddle.fluid.core_avx.EnforceNotMet: Invoke operator lookup_table error. Python Call stacks: File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/fluid/framework.py", line 1771, in append_op attrs=kwargs.get("attrs", None)) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/fluid/layer_helper.py", line 43, in append_op return self.main_program.current_block().append_op(*args, **kwargs) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/paddle/fluid/layers/nn.py", line 414, in embedding 'padding_idx': padding_idx File "", line 59, in CNN_net emb = fluid.layers.embedding(input=data,size=[dict_dim, emb_dim]) File "", line 88, in train model = CNN_net(words, dict_dim) File "", line 161, in classify.train(classify) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2878, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2818, in run_ast_nodes if self.run_code(code, result): File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2714, in run_cell interactivity=interactivity, compiler=compiler, result=result) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/ipykernel/zmqshell.py", line 537, in run_cell return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 208, in do_execute res = shell.run_cell(code, store_history=store_history, silent=silent) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 399, in execute_request user_expressions, allow_stdin) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell handler(stream, idents, msg) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 283, in dispatcher return self.dispatch_shell(stream, msg) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/tornado/stack_context.py", line 300, in null_wrapper return fn(*args, **kwargs) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 438, in _run_callback callback(*args, **kwargs) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 486, in _handle_recv self._run_callback(callback, msg) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 456, in _handle_events self._handle_recv() File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 542, in self.io_loop.add_callback(lambda : self._handle_events(self.socket, 0)) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/tornado/stack_context.py", line 300, in null_wrapper return fn(*args, **kwargs) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/tornado/ioloop.py", line 758, in _run_callback ret = callback() File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/tornado/ioloop.py", line 1017, in start self._run_callback(self._callbacks.popleft()) File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/ipykernel/kernelapp.py", line 499, in start self.io_loop.start() File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/traitlets/config/application.py", line 658, in launch_instance app.start() File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/site-packages/ipykernel_launcher.py", line 16, in app.launch_new_instance() File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/opt/conda/envs/python27-paddle120-env/lib/python2.7/runpy.py", line 174, in _run_module_as_main "main", fname, loader, pkg_name) C++ Call stacks: Tensor holds the wrong type, it holds double, but desires to be int64_t at [D:\1.5.2\paddle\paddle/fluid/framework/tensor_impl.h:30] PaddlePaddle Call Stacks: Windows not support stack backtrace yet.