PaddlePaddle / Paddle

PArallel Distributed Deep LEarning: Machine Learning Framework from Industrial Practice (『飞桨』核心框架,深度学习&机器学习高性能单机、分布式训练和跨平台部署)
http://www.paddlepaddle.org/
Apache License 2.0
22.23k stars 5.58k forks source link

paddle fluid 调用load_inference_model,infer时报错 ValueError: var temp1_var not in this block #11089

Closed seanxh closed 6 years ago

seanxh commented 6 years ago
place = fluid.CPUPlace()
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
program, feed_target_names, fetch_targets = fluid.io.load_inference_model('fluid_model', exe,params_filename='param')

for i in range(0,len(program.blocks)):
    print i,program.blocks[i].vars.keys()

输出

0 ['dynamic_rnn_0.tmp_0', 'blstm_1.tmp_0', 'blstm_0.tmp_2', 'final_output.w_0', 'dynamic_rnn_mem_array_1', 'blstm_0.b_0', 'attention_output.b_0', 'blstm_1.tmp_2', 'blstm_0.tmp_0', 'attention_fc.b_0', 'fill_constant_1.tmp_0', 'c.b_0', 'attention_fc.w_0', 'cell_init', 'fetch', 'data_lod_attention', 'dynamic_rnn_max_seq_len_0', 'temp1_var', 'blstm_0.tmp_1', 'input.w_0', 'blstm_0.w_0', 'relu_fc.tmp_0', 'lod_reset_0.tmp_0', 'output.w_0', 'input.w_1', 'blstm_0.tmp_3', 'dynamic_rnn_mem_array_0', 'fc_1.tmp_0', 'final_output.b_0', 'output.b_0', 'feed', 'c.w_1', 'forget.w_1', 'final_output.tmp_1', 'sequence_expand_2.tmp_0', 'final_output.tmp_0', 'fc_0.w_0', 'output.w_1', 'forget.w_0', 'forget.b_0', 'sequence_expand_3.tmp_0', 'fc_1.w_0', 'fill_constant_0.tmp_0', 'blstm_1.b_0', 'array_to_lod_tensor_0.tmp_0', 'attention_output.w_0', 'relu_fc.w_0', 'concat_0.tmp_0', 'blstm_1.w_0', '_generated_var_0', 'input.b_0', 'blstm_1.tmp_1', 'lod_rank_table_0', 'dynamic_rnn_mem_init_reordered_0', 'blstm_1.tmp_3', 'fc_0.tmp_0', 'hidden_init', 'sequence_expand_0.tmp_0', 'dynamic_rnn_input_array_0', 'c.w_0', 'concat_2.tmp_0', 'dynamic_rnn_0_output_array_elementwise_mul_3.tmp_0_0', 'relu_fc.tmp_1', 'relu_fc.b_0', 'data', 'temp2_var']

1 ['sequence_pool_0.tmp_0', 'attention_output.tmp_0', 'attention_output.tmp_1', 'c.tmp_1', 'input.tmp_2', 'output.tmp_0', 'attention_fc.tmp_0', 'output.tmp_1', 'elementwise_mul_2.tmp_0', 'tanh_0.tmp_0', 'sum_0.tmp_0', 'shrink_memory_1.tmp_0', 'shrink_memory_0.tmp_0', 'sequence_softmax_0.tmp_0', 'c.tmp_2', 'sequence_expand_1.tmp_0', 'concat_1.tmp_0', 'elementwise_mul_0.tmp_0', 'elementwise_mul_1.tmp_0', 'c.tmp_3', 'forget.tmp_0', 'forget.tmp_1', 'attention_fc.tmp_1', 'forget.tmp_3', 'forget.tmp_2', 'input.tmp_0', 'sigmoid_0.tmp_0', 'input.tmp_1', 'array_read_0.tmp_0', 'input.tmp_3', 'array_read_1.tmp_0', 'c.tmp_0', 'sigmoid_2.tmp_0', 'array_read_2.tmp_0', 'elementwise_mul_3.tmp_0', 'sigmoid_1.tmp_0', 'tanh_1.tmp_0', 'output.tmp_2', 'sequence_pool_0.tmp_1', 'output.tmp_3']
print feed_target_names

输出

[u'data', u'data_lod_attention', u'hidden_init', u'cell_init', u'week', u'minute']
for i in range(0,len(fetch_targets)):
    print fetch_targets[i].name

输出

final_output.tmp_1

但当我输入数据进行predict时报错

def create_lodtensor(data,lod,place):
    res = fluid.LoDTensor()
    res.set(data, place)
    res.set_lod(lod)
    return res

d = np.array([
    [0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.8447, 0.877, -0.359, 3.679, 1.382, -0.291, 0.093, 0.4038293957710266, 0.35241472721099854, 0.5131370425224304, 0.0248238705098629, 0.2605356276035309, 0.48855382204055786, -0.3628966510295868, 0.03824082762002945, 0.3911936581134796, -0.2226775735616684],
    [0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -0.4161, -0.636, -0.359, -0.341, 1.382, -0.291, 0.093, 0.048032496124506, 0.5262567400932312, 0.3271188735961914, 0.344055712223053, 0.040524303913116455, 0.46039748191833496, -0.5069547891616821, -0.10679615288972855, 0.25316208600997925, -0.0658646896481514], 
    [0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -0.5342, -0.779, -0.359, -0.75, 1.382, -0.291, 0.093, 0.05232345312833786, 0.12918666005134583, -0.36327382922172546, 0.23112450540065765, -0.042830973863601685, 0.38096097111701965, -0.24081705510616302, -0.059611719101667404, -0.041615620255470276, -0.28868567943573]] 
,dtype=np.float32)

lod_tensor = create_lodtensor(d,[[0,3]], place)

lod_attention = create_lodtensor(np.array([[0,0]],dtype=np.float32),[[0,3],[0,3,6,9]], place)

init_zero = create_lodtensor( np.zeros((1,15),dtype=np.float32), [[0,1]] , place)

temp1_var = create_lodtensor( np.array( [ [ 0.6708747,-0.65609026,0.6203626,-0.66443706,0.6562011,-0.6406015,
   0.662779,0.6728267,-0.6428651,0.6196991 ] ] ,dtype=np.float32),[[0,1]] , place)
temp2_var = create_lodtensor( np.array([
    [ 0.23115854,0.09782156,0.1631787,-0.096836,-0.11749872, -0.18711221,
  -0.06191225,0.10609391,-0.03271886,-0.1656364 ]
],dtype=np.float32),[[0,1]], place)

results = exe.run(fluid.default_main_program(),
                          feed={'data':lod_tensor,'hidden_init':init_zero,
                                'cell_init':init_zero,'data_lod_attention':lod_attention,
                                'temp1_var':temp1_var,'temp2_var':temp2_var},
                          fetch_list=['final_output.tmp_1'],return_numpy=False)
print results[0].lod()
print np.array( results[0] )

输出报错

ValueErrorTraceback (most recent call last)
<ipython-input-6-8dca9fe8d449> in <module>()
     29                                 'cell_init':init_zero,'data_lod_attention':lod_attention,
     30                                 'temp1_var':temp1_var,'temp2_var':temp2_var},
---> 31                           fetch_list=['final_output.tmp_1'],return_numpy=False)
     32 print results[0].lod()
     33 print np.array( results[0] )

/lib/python2.7/site-packages/paddle/fluid/executor.pyc in run(self, program, feed, fetch_list, feed_var_name, fetch_var_name, scope, return_numpy, use_program_cache)
    335                 fetch_list=fetch_list,
    336                 feed_var_name=feed_var_name,
--> 337                 fetch_var_name=fetch_var_name)
    338 
    339         self._feed_data(program, feed, feed_var_name, scope)

lib/python2.7/site-packages/paddle/fluid/executor.pyc in _add_feed_fetch_ops(self, program, feed, fetch_list, feed_var_name, fetch_var_name)
    230         if not has_feed_operators(global_block, feed, feed_var_name):
    231             for i, name in enumerate(feed):
--> 232                 out = global_block.var(name)
    233                 global_block.prepend_op(
    234                     type='feed',

/home/map/xuhao/code/tmp/python-gcc482-paddle/lib/python2.7/site-packages/paddle/fluid/framework.pyc in var(self, name)
    722         v = self.vars.get(name, None)
    723         if v is None:
--> 724             raise ValueError("var %s not in this block" % name)
    725         return v
    726 

ValueError: var temp1_var not in this block
panyx0718 commented 6 years ago

可否提供复现环境?

panyx0718 commented 6 years ago

线下讨论

  1. 需要设置正确的program
  2. fetch_list中需要用var object,而不是string name