import numpy as np
import tensorflow as tf
from tensorflow.keras .models import Sequential
from tensorflow.keras .layers import Embedding, SimpleRNN, Dense
text = "This is a sample text for language modeling using RNN."
chars = sorted ( set ( text) )
char_to_index = { char: index for index, char in enumerate ( chars) }
index_to_char = { index: char for index, char in enumerate ( chars) }
text_indices = [ char_to_index[ char] for char in text]
seq_length, sequences, next_char = 20 , [ ] , [ ]
for i in range ( 0 , len ( text_indices) - seq_length) :
sequences.append ( text_indices[ i : i + seq_length] )
next_char.append ( text_indices[ i + seq_length] )
X = np.array ( sequences)
y = np.array ( next_char)
17
model = Sequential( [ Embedding( input_dim= len ( chars) , output_dim= 50 , input_length= seq_length) , SimpleRNN( 100 , return_sequences= False ) , Dense( len ( chars) , activation= "softmax" ) ] )
model.compile ( loss= "sparse_categorical_crossentropy" , optimizer= "adam" )
model.fit ( X, y, batch_size= 64 , epochs= 50 )
seed_text = "This is a sample te"
generated_text = seed_text
num_chars_to_generate = 100
for _ in range ( num_chars_to_generate) :
seed_indices = [ char_to_index[ char] for char in seed_text]
if len ( seed_indices) < seq_length:
diff = seq_length - len ( seed_indices)
seed_indices = [ 0 ] * diff + seed_indices
seed_indices = np.array ( seed_indices) .reshape ( 1 , -1 )
next_index = model.predict ( seed_indices) .argmax ( )
next_char = index_to_char[ next_index]
generated_text += next_char
seed_text = seed_text[ 1 :] + next_char
print ( generated_text)
aW1wb3J0IG51bXB5IGFzIG5wCmltcG9ydCB0ZW5zb3JmbG93IGFzIHRmCmZyb20gdGVuc29yZmxvdy5rZXJhcy5tb2RlbHMgaW1wb3J0IFNlcXVlbnRpYWwKZnJvbSB0ZW5zb3JmbG93LmtlcmFzLmxheWVycyBpbXBvcnQgRW1iZWRkaW5nLCBTaW1wbGVSTk4sIERlbnNlCnRleHQgPSAiVGhpcyBpcyBhIHNhbXBsZSB0ZXh0IGZvciBsYW5ndWFnZSBtb2RlbGluZyB1c2luZyBSTk4uIgpjaGFycyA9IHNvcnRlZChzZXQodGV4dCkpCmNoYXJf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