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, y= np.array ( sequences) , 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= 20 )
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)
aW1wb3J0IG51bXB5IGFzIG5wCmltcG9ydCB0ZW5zb3JmbG93IGFzIHRmCmZyb20gdGVuc29yZmxvdy5rZXJhcy5tb2RlbHMgaW1wb3J0IFNlcXVlbnRpYWwKZnJvbSB0ZW5zb3JmbG93LmtlcmFzLmxheWVycyBpbXBvcnQgRW1iZWRkaW5nLCBTaW1wbGVSTk4sIERlbnNlCnRleHQ9IlRoaXMgaXMgYSBzYW1wbGUgdGV4dCBmb3IgbGFuZ3VhZ2UgbW9kZWxpbmcgdXNpbmcgUk5OLiIKY2hhcnM9c29ydGVkKHNldCh0ZXh0KSkKY2hh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