<jupyter_start><jupyter_text>dados -

tutorial: https://keras.io/examples/lstm_text_generation/


https://www.neuralnine.com/generating-texts-with-recurrent-neural-networks-in-python/<jupyter_code>import random
import numpy as np

import tensorflow as tf
#shakespeare em ingles
filepath = tf.keras.utils.get_file('shakespeare.txt',
        'https://storage.googleapis.com/download.tensorflow.org/data/shakespeare.txt')
text = open(filepath, 'rb').read().decode(encoding='utf-8')

text
filepath = tf.keras.utils.get_file('cpm_musicas.txt',
        'https://raw.githubusercontent.com/EduardoMoraesRitter/NLP-GENERATING-TEXTS/main/cpm_musicas.txt')
text = open(filepath, 'rb').read().decode(encoding='utf-8')

#text = text[300000:800000]

text
characters = sorted(set(text))

char_to_index = dict((c, i) for i, c in enumerate(characters))
index_to_char = dict((i, c) for i, c in enumerate(characters))

char_to_index
#index_to_char
SEQ_LENGTH = 40
STEP_SIZE = 3

sentences = []
next_char = []
for i in range(0, len(text) - SEQ_LENGTH, STEP_SIZE):
    sentences.append(text[i: i + SEQ_LENGTH])
    next_char.append(text[i + SEQ_LENGTH])

sentences
x = np.zeros((len(sentences), SEQ_LENGTH,
              len(characters)), dtype=np.bool)
y = np.zeros((len(sentences),
              len(characters)), dtype=np.bool)

y
for i, satz in enumerate(sentences):
    for t, char in enumerate(satz):
        x[i, t, char_to_index[char]] = 1
    y[i, char_to_index[next_char[i]]] = 1

y<jupyter_output><empty_output><jupyter_text>modelo LSTM<jupyter_code>import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Activation
from tensorflow.keras.optimizers import RMSprop
model = Sequential()
model.add(LSTM(128,
               input_shape=(SEQ_LENGTH,
                            len(characters))))
model.add(Dense(len(characters)))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer=RMSprop(lr=0.01))

model.fit(x, y, batch_size=256, epochs=400)
model.save('model_nlp_generating')
def sample(preds, temperature=1.0):
    preds = np.asarray(preds).astype('float64')
    preds = np.log(preds) / temperature
    exp_preds = np.exp(preds)
    preds = exp_preds / np.sum(exp_preds)
    probas = np.random.multinomial(1, preds, 1)
    return np.argmax(probas)
def generate_text(length, temperature):
    start_index = random.randint(0, len(text) - SEQ_LENGTH - 1)
    generated = ''
    sentence = text[start_index: start_index + SEQ_LENGTH]
    generated += sentence
    for i in range(length):
        x_predictions = np.zeros((1, SEQ_LENGTH, len(characters)))
        for t, char in enumerate(sentence):
            x_predictions[0, t, char_to_index[char]] = 1

        predictions = model.predict(x_predictions, verbose=0)[0]
        next_index = sample(predictions,
                                 temperature)
        next_character = index_to_char[next_index]

        generated += next_character
        sentence = sentence[1:] + next_character
    return generated
#print(generate_text(300, 0.2))
#print(generate_text(400, 0.4))
#print(generate_text(300, 0.5))
print(generate_text(200, 0.9))
#print(generate_text(300, 0.7))
#print(generate_text(300, 0.8))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13123515439429928, 'fraction_numerical': 0.022565320665083134, 'mean_word_length': 3.68567454798331, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '343010', 'license': 'MIT', 'n_tokens_mistral': 1261, 'n_tokens_neox': 1184, 'n_words': 209, 'path': 'Generating_Texts_Recurrent_Neural_Networks_LSTM.ipynb', 'repo_name': 'EduardoMoraesRitter/NLP---GENERATING-TEXTS-', 'size': '3368'}