DynamicRNN中对hidden嵌套循环
Created by: DominicXWang
- paddle 1.5, python3.6
seq2seq自定义解码过程遇到问题:
我希望实现如下操作:给定m*n的矩阵X,将X中的m个独立同质的n维向量用共享参数的方式与DynamicRNN 的 hidden 状态为 S分别进行计算。最后得到m个结果,进而求出m个概率值。
尝试了两种方法:
一是复制hidden,二是DynamicRNN里面嵌套循环,结果都是报错如下
Exception: /paddle/paddle/fluid/memory/detail/meta_cache.cc:33 Assertion desc->check_guards()
failed.
Traceback (most recent call last):
File "./train.py", line 187, in
cli()
File "/home/wangxin/tools/py367gcc48_paddle15/lib/python3.6/site-packages/click/core.py", line 764, in call
return self.main(*args, **kwargs)
File "/home/wangxin/tools/py367gcc48_paddle15/lib/python3.6/site-packages/click/core.py", line 717, in main
rv = self.invoke(ctx)
File "/home/wangxin/tools/py367gcc48_paddle15/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/home/wangxin/tools/py367gcc48_paddle15/lib/python3.6/site-packages/click/core.py", line 956, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/home/wangxin/tools/py367gcc48_paddle15/lib/python3.6/site-packages/click/core.py", line 555, in invoke
return callback(*args, **kwargs)
File "/home/wangxin/tools/py367gcc48_paddle15/lib/python3.6/site-packages/click_config/init.py", line 45, in wrapper
return fn(**kwargs_to_forward)
File "./train.py", line 151, in train
return_numpy=False)
File "/home/wangxin/tools/py367gcc48_paddle15/lib/python3.6/site-packages/paddle/fluid/executor.py", line 650, in run
use_program_cache=use_program_cache)
File "/home/wangxin/tools/py367gcc48_paddle15/lib/python3.6/site-packages/paddle/fluid/executor.py", line 748, in _run
exe.run(program.desc, scope, 0, True, True, fetch_var_name)
RuntimeError: Exception encounter.
Exception: /paddle/paddle/fluid/memory/detail/meta_cache.cc:33 Assertion desc->check_guards()
failed.
terminate called after throwing an instance of 'std::runtime_error'
what(): Exception encounter.
train_old.sh: line 6: 28038 Aborted
- DynamicRNN中复制hidden方式:
DynamicRNN外定义
param = self.helper.create_parameter(
attr=self.helper.param_attr, shape=[256, 1],
dtype="float32", is_bias=False)
DynamicRNN中
state = fluid.layers.fc(input=state, size=256)
state = fluid.layers.squeeze(state, axes=[0])
z1 = fluid.layers.elementwise_mul(X, state)
z2 = fluid.layers.mul(z1, param)
z3 = fluid.layers.squeeze(z2, axes=[-1])
prob = fluid.layers.softmax(input=z3, axis=-1)
prob = fluid.layers.reshape(prob, shape=[-1, 58])
- DynamicRNN中嵌套循环的方式:
y_array = fluid.layers.create_array('float32')
i = fluid.layers.fill_constant(shape=[1], dtype='int64', value=0)
i.stop_gradient = True
limit = fluid.layers.fill_constant(shape=[1], dtype='int64', value=58)
limit.stop_gradient = True
cond = fluid.layers.less_than(x=i, y=limit)
while_op = fluid.layers.While(cond=cond)
with while_op.block():
activation = fluid.layers.gather(X, i)
activation = fluid.layers.reshape(activation, shape=[-1, 256])
activation = fluid.layers.fc(input=activation, size=64,
param_attr='pre_y_pr', bias_attr='pre_y_bs')
z = fluid.layers.elementwise_mul(activation, state)
y = fluid.layers.fc(input=z, size=1,
param_attr='y_pr', bias_attr='y_bs')
fluid.layers.array_write(y, i=i, array=y_array)
fluid.layers.increment(x=i, value=1, in_place=True)
fluid.layers.less_than(x=i, y=limit, cond=cond)
y_array, y_array_index = fluid.layers.tensor_array_to_tensor(y_array)
y_array = fluid.layers.reshape(y_array, shape=[-1, 58])
y_array = fluid.layers.softmax(input=y_array, axis=-1)
prob = fluid.layers.softmax(input=y_array, axis=-1)
两种写法报错一致,请问是什么原因导致的?