Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fluid.layers.lstm接口参数is_bidirec不能发挥双向的作用 #22979

Closed
remarkableJ opened this issue Mar 12, 2020 · 4 comments
Closed

fluid.layers.lstm接口参数is_bidirec不能发挥双向的作用 #22979

remarkableJ opened this issue Mar 12, 2020 · 4 comments

Comments

@remarkableJ
Copy link

fluid.layers.lstm这个接口的参数is_bidirec貌似不能发挥双向的作用,输出的结果维度跟描述也不一致

@Aurelius84
Copy link
Contributor

请问您可否提供下发现问题的代码?

@3wGTA
Copy link

3wGTA commented May 1, 2020

我也遇到同样的问题,如下代码确实没有发挥双向作用

import numpy as np
import paddle.fluid as fluid

weight = np.random.randint(0,2,(32,5))
# weight = np.ones((32,5))
weight[0] = 0
print(weight[:2])

batch_size = 5
def yield_data():
    for i in range(100):
        max_len = 7
        s_x = np.random.randint(2,5)
        s_y = np.random.randint(2,8)
        label = np.random.randint(2)
        x = np.random.randint(0,10,(1,s_x)).tolist()
        y = np.random.randint(0,10,(1,s_y)).tolist()
        yield x[0],y[0] ,label
#         yield np.random.randint(0,10,(1,max_len)),np.random.randint(0,10,(1,max_len)),np.array(label)
#         yield np.random.randint(0,10,(1,s_x))[0],np.random.randint(0,10,(1,s_y))[0],np.array(label)

train_reader = fluid.io.batch(reader=yield_data,batch_size=batch_size)

for i,data in enumerate(train_reader()):
    print(data)
#     print(type(data[0][0][0]))
    break

V = 32
h = 3
emb_size=5
max_len = 7 #最大为5,每个句子最长为5
hidden_size = 2
num_layers = 1

label = fluid.data(name='label', shape=[None, 1], dtype='int64')
x = fluid.data(name='t', shape=[None], dtype='int64',lod_level=1)
y = fluid.data(name='h', shape=[None], dtype='int64',lod_level=1)


w = fluid.ParamAttr(name='emb_vec', initializer=fluid.initializer.NumpyArrayInitializer(weight), trainable=False)

emb_x = fluid.embedding(input=x, size=[32,5], param_attr=w)
emb_y = fluid.embedding(input=y, size=[32,5], param_attr=w)

pad_value = fluid.layers.assign(input=np.array([0.0], dtype=np.float32))
pad_x,info_x = fluid.layers.sequence_pad(emb_x,pad_value)
pad_y,info_y = fluid.layers.sequence_pad(emb_y,pad_value)

init_h = fluid.layers.fill_constant([num_layers, batch_size, hidden_size], 'float32', 0)
init_c = fluid.layers.fill_constant([num_layers, batch_size, hidden_size], 'float32', 0)
# lstm 网络

# 返回的形状是 batch_size, seq_len, hiddensize
lstm_x, x_last_h, x_last_c = fluid.layers.lstm(pad_x, init_h, init_c, max_len, hidden_size, num_layers,is_bidirec=True)


temp_x = lstm_x[:,:1]
# print("lstm_shape:", lstm_x.shape)
# lstm_y, y_last_h, y_last_c = fluid.layers.lstm(emb_y, init_h, init_c, max_len, hidden_size, num_layers)

# # 输出是 senq, batch_size, hiddensize
# lstm_bi_x, bi_x_last_h, bi_x_last_c = fluid.layers.lstm(
#                 input=pad_x, init_h=init_h, init_c=init_c, 
#                 max_len=max_len, hidden_size=hidden_size, num_layers=num_layers, 
#                 dropout_prob=0.2,
#                 is_bidirec=True,
#                 is_test=False,
#                 name='bidirec')

# print(lstm_x.shape)
# print(lstm_y.shape)

use_gpu = True
place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)
main_program = fluid.default_main_program()
feeder = fluid.DataFeeder(feed_list=['t', 'h','label'], place=place)
exe.run(fluid.default_startup_program())

fetch_var = [x, y, emb_x, emb_y, pad_x, pad_y, lstm_x, x_last_h]

for i, data in enumerate(train_reader()):
    print(data)
    result = exe.run(
        main_program,
        feeder.feed(data),
        fetch_list=fetch_var,
        return_numpy=False
        )
    break

for i, data in enumerate(result):
    print(fetch_var[i].name)
    temp = np.array(data)
    print(data.lod())
    print(temp)
    print(temp.shape)
    print("_____"*10)

毫无双向作用,经过 许多次测试,没有发挥双向作用,而且输出和官方文档描述的不符合!!!
输出形状不符合
版本 1.72-gpu

@3wGTA 3wGTA mentioned this issue May 2, 2020
@guoshengCS
Copy link
Contributor

fluid.layers.lstm问题较多,已经在高优重新实现了 #25502 ,还望见谅

还请先行使用paddle.fluid.layers.rnn接口 https://www.paddlepaddle.org.cn/documentation/docs/zh/api_cn/layers_cn/rnn_cn.html#rnn 完成,双向的话暂时需要is_reverse=True和False的两个RNN。

@paddle-bot-old
Copy link

Since you haven't replied for more than a year, we have closed this issue/pr.
If the problem is not solved or there is a follow-up one, please reopen it at any time and we will continue to follow up.
由于您超过一年未回复,我们将关闭这个issue/pr。
若问题未解决或有后续问题,请随时重新打开,我们会继续跟进。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants