def __padding_lstm_rnn(
self,
input_embedding,
num_layers,
num_steps,
hidden_size,
initializer,
scope,
init_hidden,
init_cell,
dropout=None):
weight_1_arr = []
bias_arr = []
hidden_array = []
cell_array = []
for i in range(num_layers):
weight_1 = layers.create_parameter([hidden_size + input_embedding.shape[-1], hidden_size * 4], dtype="float32",
name=scope + "/fc_weight1_" + str(i),
default_initializer=initializer)
weight_1_arr.append(weight_1)
bias_1 = layers.create_parameter(
[hidden_size * 4],
dtype="float32",
name=scope + "/fc_bias1_" + str(i),
default_initializer=fluid.initializer.Constant(0.0))
bias_arr.append(bias_1)
pre_hidden = layers.slice(
init_hidden, axes=[0], starts=[i], ends=[i + 1])
pre_cell = layers.slice(
init_cell, axes=[0], starts=[i], ends=[i + 1])
pre_hidden = layers.reshape(pre_hidden, shape=[-1, hidden_size])
pre_cell = layers.reshape(pre_cell, shape=[-1, hidden_size])
hidden_array.append(pre_hidden)
cell_array.append(pre_cell)
input_embedding = layers.transpose(input_embedding, perm=[1, 0, 2])
rnn = StaticRNN(name=scope)
with rnn.step():
input = rnn.step_input(input_embedding)
for k in range(num_layers):
pre_hidden = rnn.memory(init=hidden_array[k])
pre_cell = rnn.memory(init=cell_array[k])
weight_1 = weight_1_arr[k]
bias = bias_arr[k]
nn = layers.concat([input, pre_hidden], 1)
gate_input = layers.matmul(x=nn, y=weight_1)
gate_input = layers.elementwise_add(gate_input, bias)
# i, j, f, o = layers.split(gate_input, num_or_sections=4, dim=-1)
i = layers.slice(
gate_input, axes=[1], starts=[0], ends=[hidden_size])
j = layers.slice(
gate_input,
axes=[1],
starts=[hidden_size],
ends=[hidden_size * 2])
f = layers.slice(
gate_input,
axes=[1],
starts=[hidden_size * 2],
ends=[hidden_size * 3])
o = layers.slice(
gate_input,
axes=[1],
starts=[hidden_size * 3],
ends=[hidden_size * 4])
c = pre_cell * layers.sigmoid(f) + layers.sigmoid(
i) * layers.tanh(j)
m = layers.tanh(c) * layers.sigmoid(o)
rnn.update_memory(pre_hidden, m)
rnn.update_memory(pre_cell, c)
rnn.step_output(m)
rnn.step_output(c)
input = m
if dropout != None and dropout > 0.0:
input = layers.dropout(
input,
dropout_prob=dropout,
dropout_implementation='upscale_in_train')
rnn.step_output(input)
# real_res = layers.concat(res, 0)
rnnout = rnn()
last_hidden_array = []
last_cell_array = []
real_res = rnnout[-1]
for i in range(num_layers):
m = rnnout[i * 2]
c = rnnout[i * 2 + 1]
m.stop_gradient = True
c.stop_gradient = True
last_h = layers.slice(
m, axes=[0], starts=[num_steps - 1], ends=[num_steps])
last_hidden_array.append(last_h)
last_c = layers.slice(
c, axes=[0], starts=[num_steps - 1], ends=[num_steps])
last_cell_array.append(last_c)
real_res = layers.transpose(x=real_res, perm=[1, 0, 2])
last_hidden = layers.concat(last_hidden_array, 0)
last_cell = layers.concat(last_cell_array, 0)
return real_res, last_hidden, last_cell
我总共调用了这个函数三次,只有第一次可以使用layers.Print()查看最终结果,到了第二次的时候,出现paddle.fluid.core.EnforceNotMet: holder_ should not be null的错误
10条答案
按热度按时间0sgqnhkj1#
并且当我编辑完问题之后马上又重新启动了一遍程序,这次的报错是
感觉有点奇怪,是什么原因造成的呢
n53p2ov02#
@dianhuasici 看log
Input tensors should have the same elements except the specify axis
说的是concat时输入的维度不不匹配,你的代码中有三处concat,能先定位到是哪一个concat有问题么?谢谢。7fhtutme3#
@hjchen2 我是这么调用的
其中第一处layers.Print("cell_fw")是可以输出结果的,但是相同的操作和相同的参数(除了第二个rnn的input我做了一下reverse),第二个layers.Print("cell_bw")就无法输出,并且报错了
gijlo24d4#
并且我是根据官方的modelhttps://github.com/PaddlePaddle/models/blob/develop/fluid/PaddleNLP/language_model/lstm/lm_model.py这里面的代码做了一些小改动,基本上没有很大的变化,所以我感觉这个函数的内部应该concat不会出错,是不是我是用的时候哪里不太对呢 @hjchen2
wvmv3b1j5#
问题找到了,当我使用图中被注解的layers.Print(input, message=scope + "/input", summarize=0)时,会报paddle.fluid.core.EnforceNotMet: holder_ should not be null,如果注解掉这句话,那在nn = layers.concat([input, pre_hidden], 1)这句就会concat出错,所以我是不是应该在reverse之后同样把lodlevel设置为1呢 @hjchen2
sf6xfgos6#
reverse应该是不会用到lod的,你对input的self.encode_emb做reverse,axis=1是不是有问题,应该是axis=0?
dphi5xsq7#
所以现在的问题就是,我把同样的输入做一个reverse之后,
我打印了一下这个其中的input,然后发现其实是空的(这时调用layers.Print报错),所以下面的concat中使用了input,会报维度的错误,因为这时候input的维度是0 @hjchen2
a1o7rhls8#
事实上,我现在只是把原来的输入(input_embedding)没做改变又重新输入再次调用函数,效果仍然是一样的。。这个input还是空的
nzkunb0c9#
@dianhuasici 能把第一次调用后的self.encode_emb shape打印一下么,并跟调用之前比较一下。我比较怀疑是调用后self.encode_emb发生了改变。
0yg35tkg10#
搞清楚了,是由于https://github.com/PaddlePaddle/Paddle/issues/15446这个问题导致的 @hjchen2 ,我猜是由于变量名字相同,在step_input的时候无法create_param