首页 Paddle框架 帖子详情
tensor array 赋值错误
收藏
快速回复
Paddle框架 问答深度学习 1528 2
tensor array 赋值错误
收藏
快速回复
Paddle框架 问答深度学习 1528 2

我用fluid.layers.create_array(dtype='float32')建立了一个array
在 with while_op.block() 循环中赋值:
layers.array_write(weights, i=step_idx, array=weights_array)
并且这时候确保是有数据的(打印出来看过了)
但是在循环结束后,我尝试读取或操作这个array,比如
step_idx = layers.fill_constant(
shape=[1], dtype=tgt_ids.dtype, value=0, force_cpu=True)
weight_debug = layers.array_read(weights_array, i=step_idx)
layers.Print(weight_debug, message="weights_debug", summarize=100)
就报错了,报错信息如下,极大概率是整个array 都是空的:

Error Message Summary:

PaddleCheckError: holder_ should not be null
Tensor holds no memory. Call Tensor::mutable_data first. at [/paddle/paddle/fluid/framework/tensor.cc:23]
[operator < read_from_array > error]


详情代码如下:
weights_array = fluid.layers.create_array(dtype='float32')
global_src_id = src_ids
with while_op.block():
pre_ids = layers.array_read(array=ids, i=step_idx)
pre_ids = layers.reshape(pre_ids, (-1, 1, 1), inplace=True)
pre_scores = layers.array_read(array=scores, i=step_idx)

    tmp_tgt_input_mask = layers.array_read(tgt_masks, i=step_idx)
    tmp_tgt_input_mask_encoder = layers.array_read(tgt_masks_encoder, i=step_idx)
    append_mask = layers.fill_constant_batch_size_like(
            input=tmp_tgt_input_mask,
            value=1.0,
            shape=[-1, 1, 1],
            dtype=tmp_tgt_input_mask.dtype)
    append_mask_encoder = layers.fill_constant_batch_size_like(
            input=tmp_tgt_input_mask,
            value=0.0,
            shape=[-1, 1, 1],
            dtype=tmp_tgt_input_mask.dtype)
    tmp_tgt_input_mask = layers.concat([tmp_tgt_input_mask, append_mask], axis=2)
    tmp_tgt_input_mask_encoder = layers.concat([tmp_tgt_input_mask_encoder, append_mask_encoder], axis=2)
    pre_mask = layers.gather(input=tmp_tgt_input_mask, index=parent_idx)
    pre_mask_encoder = layers.gather(input=tmp_tgt_input_mask_encoder, index=parent_idx)
    pre_pos = layers.elementwise_mul(
        x=layers.fill_constant_batch_size_like(
            input=pre_mask,
            value=1,
            shape=[-1, 1, 1],
            dtype=pre_ids.dtype), y=step_idx, axis=0)
    type_ids = layers.fill_constant_batch_size_like(
        input=pre_mask,
        value=args.tgt_type_id,
        shape=[-1, 1, 1],
        dtype=pre_ids.dtype)
    dec_out, weights, gens = ernie.encode(pre_ids, pre_pos, type_ids, pre_mask, pre_mask_encoder, parent_idx)
    tmp_id = layers.gather(input=global_src_id, index=parent_idx)
    tmp_id =  layers.concat([tmp_id, pre_ids], axis=1)
    layers.assign(tmp_id, global_src_id)
    fc_out = cal_logit(tmp_id, dec_out, None, args, ernie_config, weights, gens)
    topk_scores, topk_indices = layers.topk(
        input=fc_out, k=args.beam_size)
    accu_scores = layers.elementwise_add(
        x=layers.log(topk_scores), y=pre_scores, axis=0)
    topk_indices = layers.lod_reset(topk_indices, pre_ids)
    accu_scores = layers.lod_reset(accu_scores, pre_ids)
    selected_ids, selected_scores, gather_idx = layers.beam_search(
        pre_ids=pre_ids,
        pre_scores=pre_scores,
        ids=topk_indices,
        scores=accu_scores,
        beam_size=args.beam_size,
        end_id=args.eos_idx,
        return_parent_idx=True)

    layers.increment(x=step_idx, value=1.0, in_place=True)
    layers.array_write(selected_ids, i=step_idx, array=ids)
    layers.array_write(selected_scores, i=step_idx, array=scores)
    layers.Print(weights, message="weights_here", summarize=100)
    layers.array_write(weights, i=step_idx, array=weights_array)
    weight_debug = layers.array_read(weights_array, i=step_idx)
    layers.Print(weight_debug, message="weights_debug", summarize=100)
    layers.array_write(pre_mask, i=step_idx, array=tgt_masks)
    layers.array_write(pre_mask_encoder, i=step_idx, array=tgt_masks_encoder)
    layers.assign(gather_idx, parent_idx)
    length_cond = layers.less_than(x=step_idx, y=max_len)
    finish_cond = layers.logical_not(layers.is_empty(x=selected_ids))
    layers.logical_and(x=length_cond, y=finish_cond, out=cond)

finished_ids, finished_scores = layers.beam_search_decode(
    ids, scores, beam_size=args.beam_size, end_id=args.eos_idx)
step_idx = layers.fill_constant(
        shape=[1], dtype=tgt_ids.dtype, value=0, force_cpu=True)
weight_debug =   layers.array_read(weights_array, i=step_idx)
layers.Print(weight_debug, message="weights_debug", summarize=100)
weights_output, _ = fluid.layers.tensor_array_to_tensor(input=weights_array, axis=0 , use_stack=True)
0
收藏
回复
全部评论(2)
时间顺序
AIStudio790259
#2 回复于2019-12

您好,我本地测试while op是可以为tensor array进行赋值的。您可以检查下您对tensor array的赋值过程。

import paddle.fluid as fluid

weights_array = fluid.layers.create_array(dtype='float32')

i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)
ii = fluid.layers.fill_constant(shape=[1], dtype='int64', value=9)
loop_len = fluid.layers.fill_constant(shape=[1],dtype='int64', value=10)
cond = fluid.layers.less_than(x=i, y=loop_len)
while_op = fluid.layers.While(cond=cond)

tmp = fluid.layers.fill_constant(shape=[3, 2], dtype='int64', value=5)

with while_op.block():
        weights_array = fluid.layers.array_write(tmp, i=i, array=weights_array)
        item1 = fluid.layers.array_read(weights_array, i=i)
        fluid.layers.Print(i, message="The index is")
        fluid.layers.Print(item1, message="The content of i-th LoDTensor:")
        i = fluid.layers.increment(x=i, value=1.0, in_place=True)
        fluid.layers.less_than(x=i, y=loop_len, cond=cond)

item = fluid.layers.array_read(weights_array, i=ii)
fluid.layers.Print(ii, message="The index is")
fluid.layers.Print(item, message="The content of i-th LoDTensor:")

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

res = exe.run(fluid.default_main_program(), feed={}, fetch_list=[i])
print(res)
0
回复
AIStudio790259
#3 回复于2019-12

解决结果:
tensor array的赋值过程有误,循环里首先对step_idx自增,然后给tensor array赋值,这样导致tensor array index为0时没有数据。

0
回复
需求/bug反馈?一键提issue告诉我们
发现bug?如果您知道修复办法,欢迎提pr直接参与建设飞桨~
在@后输入用户全名并按空格结束,可艾特全站任一用户