Paddle fluid.layers.dynamic_gru()自定义h_0报错

aurhwmvo  于 2022-04-21  发布在  Java
关注(0)|答案(2)|浏览(197)

paddle1.7.2 我把双向gru的输出


,作为另一个双向gru的h_0

最终执行的时候报错:

请问这是什么原因?

n3schb8v

n3schb8v1#

可否提供下完整的复现代码,我用简单的例子试了下直接加h0是可以的

import numpy as np
import paddle.fluid as fluid

dict_dim, emb_dim = 128, 64
data = fluid.data(name='sequence',
          shape=[None],
          dtype='int64',
          lod_level=1)
emb = fluid.embedding(input=data, size=[dict_dim, emb_dim])
hidden_dim = 512
x = fluid.layers.fc(input=emb, size=hidden_dim * 3)
h0 = fluid.layers.fill_constant_batch_size_like(input=x, value=0, shape=[-1, hidden_dim], dtype='float32')
hidden = fluid.layers.dynamic_gru(input=x, size=hidden_dim, h_0=h0)

def to_lodtensor(data, place, lod=None):
    data_tensor = fluid.LoDTensor()
    data_tensor.set(data, place)
    if lod is not None:
        data_tensor.set_lod(lod)
    return data_tensor

place = fluid.CUDAPlace(0)
exe = fluid.Executor(place)
exe.run(fluid.default_startup_program())
exe.run(feed={'sequence': to_lodtensor(np.array(list(range(10)), dtype='int64'), place, [[0, 3, 10]])})
z31licg0

z31licg02#

def _bigru_layer(input_feature):
    """
    define the bidirectional gru layer
    """
    pre_gru = fluid.layers.fc(
        input=input_feature,
        size=grnn_hidden_dim * 3,
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.Uniform(
                low=-init_bound, high=init_bound),
            regularizer=fluid.regularizer.L2DecayRegularizer(
                regularization_coeff=1e-4)))

    gru = fluid.layers.dynamic_gru(
        input=pre_gru,
        size=grnn_hidden_dim,
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.Uniform(
                low=-init_bound, high=init_bound),
            regularizer=fluid.regularizer.L2DecayRegularizer(
                regularization_coeff=1e-4)))

    pre_gru_r = fluid.layers.fc(
        input=input_feature,
        size=grnn_hidden_dim * 3,
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.Uniform(
                low=-init_bound, high=init_bound),
            regularizer=fluid.regularizer.L2DecayRegularizer(
                regularization_coeff=1e-4)))

    gru_r = fluid.layers.dynamic_gru(
        input=pre_gru_r,
        size=grnn_hidden_dim,
        is_reverse=True,
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.Uniform(
                low=-init_bound, high=init_bound),
            regularizer=fluid.regularizer.L2DecayRegularizer(
                regularization_coeff=1e-4)))

    bi_merge = fluid.layers.concat(input=[gru, gru_r], axis=1)

    return bi_merge, gru_r

def _bigru_layer_obj(input_feature, h_0):
    """
    define the bidirectional gru layer
    """
    pre_gru_obj = fluid.layers.fc(
        input=input_feature,
        size=grnn_hidden_dim * 3,
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.Uniform(
                low=-init_bound, high=init_bound),
            regularizer=fluid.regularizer.L2DecayRegularizer(
                regularization_coeff=1e-4)))

     gru_obj = fluid.layers.dynamic_gru(
         input=pre_gru_obj,
         size=grnn_hidden_dim,
         param_attr=fluid.ParamAttr(
             initializer=fluid.initializer.Uniform(
                 low=-init_bound, high=init_bound),
             regularizer=fluid.regularizer.L2DecayRegularizer(
                regularization_coeff=1e-4)),
         h_0=h_0)

    pre_gru_r_obj = fluid.layers.fc(
        input=input_feature,
        size=grnn_hidden_dim * 3,
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.Uniform(
                low=-init_bound, high=init_bound),
            regularizer=fluid.regularizer.L2DecayRegularizer(
                regularization_coeff=1e-4)))

    gru_r_obj = fluid.layers.dynamic_gru(
        input=pre_gru_r_obj,
        size=grnn_hidden_dim,
        is_reverse=True,
        param_attr=fluid.ParamAttr(
            initializer=fluid.initializer.Uniform(
                low=-init_bound, high=init_bound),
            regularizer=fluid.regularizer.L2DecayRegularizer(
                regularization_coeff=1e-4)))

    bi_merge_obj = fluid.layers.concat(input=[gru_obj, gru_r_obj], axis=1)

    return bi_merge_obj

def _net_conf(word):
    """
    Configure the network
    """

    word_embedding = fluid.embedding(
        input=word,
        size=[17966, 300],
        dtype='float32',
        is_sparse=IS_SPARSE,
        param_attr=fluid.ParamAttr(
            learning_rate=emb_lr,
            name="word_emb",
            initializer=fluid.initializer.Uniform(
                low=-init_bound, high=init_bound)))
    word_embedding = fluid.layers.dropout(word_embedding, dropout_prob=0.1, is_test=istest)

    # subject
    input_feature = word_embedding
    for i in range(bigru_num):
        bigru_output, sub_gru_r = _bigru_layer(input_feature)
        input_feature = bigru_output

    # object
    obj_input_feature = word_embedding
    for i in range(bigru_num):
        obj_bigru_output = _bigru_layer_obj(obj_input_feature, sub_gru_r)
        obj_input_feature = obj_bigru_output

相关问题