Почему в Керасе есть только один вход в модель внимания? - PullRequest
0 голосов
/ 25 июня 2019

В этом коде автор определил 2 входа, но есть только один входной поток для модели.Там должно быть несколько ошибок, однако, я могу запустить его.Интересно, почему я могу успешно запустить этот код.

def han():
    # refer to 4.2 in the paper whil reading the following code

    # Input for one day : max article per day =40, dim_vec=200
    input1 = Input(shape=(40, 200), dtype='float32')

    # Attention Layer
    dense_layer = Dense(200, activation='tanh')(input1)
    softmax_layer = Activation('softmax')(dense_layer)
    attention_mul = multiply([softmax_layer,input1])
    #end attention layer


    vec_sum = Lambda(lambda x: K.sum(x, axis=1))(attention_mul)
    pre_model1 = Model(input1, vec_sum)
    pre_model2 = Model(input1, vec_sum)

    # Input of the HAN shape (None,11,40,200)
    # 11 = Window size = N in the paper 40 = max articles per day, dim_vec = 200
    input2 = Input(shape=(11, 40, 200), dtype='float32')

    # TimeDistributed is used to apply a layer to every temporal slice of an input 
    # So we use it here to apply our attention layer ( pre_model ) to every article in one day
    # to focus on the most critical article
    pre_gru = TimeDistributed(pre_model1)(input2)

# bidirectional gru
    l_gru = Bidirectional(GRU(100, return_sequences=True))(pre_gru)

    # We apply attention layer to every day to focus on the most critical day   
    post_gru = TimeDistributed(pre_model2)(l_gru)

# MLP to perform classification
    dense1 = Dense(100, activation='tanh')(post_gru)
    dense2 = Dense(3, activation='tanh')(dense1)
    final = Activation('softmax')(dense2)
    final_model = Model(input2, final)
    final_model.summary()

    return final_model

1 Ответ

0 голосов
/ 25 июня 2019

Модели Keras можно использовать в качестве слоев.В приведенном выше коде input1 используется для определения pre_model{1,2}.Затем модели называются ниже моделью с именем final_model.

final_model имеет один входной слой.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...