Introduction
RNN-based text generation helps computers write text by learning patterns from example sentences. It can create new sentences that sound like the original text.
Jump into concepts and practice - no test required
model = Sequential() model.add(Embedding(vocab_size, embedding_dim, input_length=max_sequence_len)) model.add(SimpleRNN(units)) model.add(Dense(vocab_size, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model = Sequential() model.add(Embedding(1000, 64, input_length=10)) model.add(SimpleRNN(128)) model.add(Dense(1000, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model = Sequential() model.add(Embedding(5000, 100, input_length=20)) model.add(SimpleRNN(256)) model.add(Dense(5000, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
import numpy as np from tensorflow.keras.models import Sequential from tensorflow.keras.layers import Embedding, SimpleRNN, Dense from tensorflow.keras.utils import to_categorical # Sample text data text = "hello world hello machine learning" # Create a simple word index words = sorted(set(text.split())) word_to_index = {w: i for i, w in enumerate(words)} index_to_word = {i: w for w, i in word_to_index.items()} # Prepare sequences (input and target) sequence_length = 2 sequences = [] tokens = text.split() for i in range(len(tokens) - sequence_length): seq = tokens[i:i+sequence_length+1] sequences.append([word_to_index[w] for w in seq]) sequences = np.array(sequences) X, y = sequences[:, :-1], sequences[:, -1] y = to_categorical(y, num_classes=len(words)) # Build the model model = Sequential() model.add(Embedding(input_dim=len(words), output_dim=10, input_length=sequence_length)) model.add(SimpleRNN(20)) model.add(Dense(len(words), activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Train the model model.fit(X, y, epochs=100, verbose=0) # Generate text function def generate_text(seed_text, next_words=3): result = seed_text.split() for _ in range(next_words): encoded = [word_to_index[w] for w in result[-sequence_length:]] encoded = np.array(encoded).reshape(1, sequence_length) pred = model.predict(encoded, verbose=0) next_index = np.argmax(pred) next_word = index_to_word[next_index] result.append(next_word) return ' '.join(result) # Generate new text output_text = generate_text('hello world') print(output_text)
X if the vocabulary size is 5000, sequence length is 20, and batch size is 32?model = Sequential() model.add(Embedding(input_dim=5000, output_dim=50, input_length=20)) model.add(SimpleRNN(100)) model.add(Dense(5000, activation='softmax')) X = np.random.randint(0, 5000, (32, 20))
model = Sequential() model.add(Embedding(input_dim=10000, output_dim=64, input_length=15)) model.add(SimpleRNN(128)) model.add(Dense(10000, activation='softmax')) X = np.random.randint(0, 10000, (64, 15)) y = np.random.randint(0, 10000, (64, 15)) # target labels model.compile(loss='sparse_categorical_crossentropy', optimizer='adam') model.fit(X, y, epochs=5)