Exemplo n.º 1
0

    model.add(Embedding(words, 10, input_length=input_len))


    model.add(LSTM(100))


    model.add(Dense(words, activation='softmax'))

    model.compile(loss='categorical_crossentropy', optimizer='adam')

    return model

model = create_model(max_len, words)
model.summary()

model.fit(predictors, label, epochs=50, verbose=2)

# Generating headlines based on input and LSTM

def headline_generator(text, next_words, model, max_len):
    for _ in range(next_words):
        token_list = tokenizer.texts_to_sequences([text])[0]
        token_list = pad_sequences([token_list], maxlen=max_len-1, padding='pre')
        predicted = model.predict_classes(token_list, verbose=0)

        output_word = ""
        for word,index in tokenizer.word_index.items():
            if index == predicted:
                output_word = word