Пример #1
0
def test_attention_mv2(batch_size, word_embed_size, sent_embed_size,
                       doc_embed_size, vocab_size, max_words, max_sents,
                       num_classes, should_fit_model):
    """ AttentionMV: full """

    # sentence encoder
    E = np.random.random((vocab_size, word_embed_size))

    sent_inputs = Input(shape=(max_words, ), dtype="int32")
    sent_emb = Embedding(input_dim=vocab_size,
                         output_dim=word_embed_size,
                         weights=[E])(sent_inputs)
    sent_enc = Bidirectional(GRU(sent_embed_size,
                                 return_sequences=True))(sent_emb)

    sent_vec = GlobalAveragePooling1D()(sent_enc)
    sent_att = custom_attn.AttentionMV()([sent_enc, sent_vec])

    sent_model = Model(inputs=sent_inputs, outputs=sent_att)

    #    sent_enc = Bidirectional(GRU(sent_embed_size,
    #                                 return_sequences=False))(sent_emb)
    #    sent_model = Model(inputs=sent_inputs, outputs=sent_enc)

    # document pipeline
    doc_inputs = Input(shape=(max_sents, max_words), dtype="int32")

    doc_emb = TimeDistributed(sent_model)(doc_inputs)

    doc_enc = Bidirectional(GRU(doc_embed_size,
                                return_sequences=True))(doc_emb)

    doc_vec = GlobalAveragePooling1D()(doc_enc)
    doc_att = custom_attn.AttentionMV()([doc_enc, doc_vec])

    fc1_dropout = Dropout(0.2)(doc_att)
    fc1 = Dense(50, activation="relu")(fc1_dropout)
    fc2_dropout = Dropout(0.2)(fc1)
    doc_pred = Dense(num_classes, activation="softmax")(fc2_dropout)

    model = Model(inputs=doc_inputs, outputs=doc_pred)
    model.summary()

    if should_fit_model:
        X = np.random.random((batch_size * 2, max_sents, max_words))
        y = np.random.randint(0, num_classes, batch_size * 2)
        Y = np_utils.to_categorical(y, num_classes=num_classes)
        model.compile(optimizer="adam", loss="categorical_crossentropy")
        model.fit(X, Y, batch_size=batch_size, epochs=1)

    return
Пример #2
0
def test_attention_mv1(batch_size, word_embed_size, sent_embed_size,
                       num_classes, max_words, vocab_size, should_fit_model):
    """ AttentionMV: sentence only """

    E = np.random.random((vocab_size, word_embed_size))

    sent_inputs = Input(shape=(max_words, ), dtype="int32")
    sent_emb = Embedding(input_dim=vocab_size,
                         output_dim=word_embed_size,
                         weights=[E])(sent_inputs)
    sent_enc = Bidirectional(GRU(sent_embed_size,
                                 return_sequences=True))(sent_emb)

    # generate summary vector
    sent_vec = GlobalAveragePooling1D()(sent_enc)

    sent_att = custom_attn.AttentionMV()([sent_enc, sent_vec])

    fc1_dropout = Dropout(0.2)(sent_att)
    fc1 = Dense(50, activation="relu")(fc1_dropout)
    fc2_dropout = Dropout(0.2)(fc1)
    sent_pred = Dense(num_classes, activation="softmax")(fc2_dropout)

    model = Model(inputs=sent_inputs, outputs=sent_pred)
    model.summary()

    if should_fit_model:
        X = np.random.random((batch_size * 2, max_words))
        y = np.random.randint(0, num_classes, batch_size * 2)
        Y = np_utils.to_categorical(y, num_classes=num_classes)
        model.compile(optimizer="adam", loss="categorical_crossentropy")
        model.fit(X, Y, batch_size=batch_size, epochs=1)

    return