ysn-rfd's picture
Upload 6 files
6e66252 verified
import torch
import torch.nn as nn
import torch.optim as optim
# 1. Prepare the Dataset
def prepare_dataset(filepath, seq_length):
"""
Prepares the dataset for training from a text file.
Args:
filepath (str): Path to the text file (e.g., 'dataset.txt').
seq_length (int): The length of input sequences.
Returns:
tuple: vocab (set), char_to_index (dict), index_to_char (dict),
input_sequences (list), target_sequences (list)
"""
try:
with open(filepath, 'r', encoding='utf-8') as file:
text = file.read()
except FileNotFoundError:
print(f"Error: File '{filepath}' not found. Make sure the file exists in the correct directory.")
return None, None, None, None, None
vocab = sorted(list(set(text)))
char_to_index = {char: index for index, char in enumerate(vocab)}
index_to_char = {index: char for index, char in enumerate(vocab)}
input_sequences = []
target_sequences = []
for i in range(0, len(text) - seq_length):
input_seq = text[i:i + seq_length]
target_seq = text[i + seq_length]
input_sequences.append([char_to_index[char] for char in input_seq])
target_sequences.append(char_to_index[target_seq])
return vocab, char_to_index, index_to_char, input_sequences, target_sequences
# 2. Define the Language Model (Simple RNN)
class SimpleRNNLM(nn.Module):
def __init__(self, vocab_size, embedding_dim, hidden_dim, num_layers):
super(SimpleRNNLM, self).__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.rnn = nn.RNN(embedding_dim, hidden_dim, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_dim, vocab_size)
def forward(self, input_seq, hidden):
embedded = self.embedding(input_seq)
output, hidden = self.rnn(embedded, hidden)
output = self.fc(output[:, -1, :])
return output, hidden
def init_hidden(self, batch_size, num_layers, hidden_dim):
return torch.zeros(num_layers, batch_size, hidden_dim)
# Example Usage
dataset_filepath = 'dataset.txt' # Path to your dataset text file
seq_length = 64
vocab, char_to_index, index_to_char, input_seqs, target_seqs = prepare_dataset(dataset_filepath, seq_length)
if vocab is None:
exit()
print(f"Vocabulary Size: {len(vocab)}")
print(f"Number of Input Sequences: {len(input_seqs)}")
# 3. Instantiate Model, Loss Function, and Optimizer
vocab_size = len(vocab)
embedding_dim = 32
hidden_dim = 64
num_layers = 1
learning_rate = 0.01
num_epochs = 10
model = SimpleRNNLM(vocab_size, embedding_dim, hidden_dim, num_layers)
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate)
device = torch.device("cpu")
model.to(device)
criterion.to(device)
# 4. Training Loop
batch_size = 256
for epoch in range(num_epochs):
model.train()
total_loss = 0
for i in range(0, len(input_seqs), batch_size):
input_batch = input_seqs[i:i+batch_size]
target_batch = target_seqs[i:i+batch_size]
input_batch_tensor = torch.LongTensor(input_batch).to(device)
target_batch_tensor = torch.LongTensor(target_batch).to(device)
hidden = model.init_hidden(len(input_batch), num_layers, hidden_dim).to(device)
optimizer.zero_grad()
output, hidden = model(input_batch_tensor, hidden)
loss = criterion(output, target_batch_tensor)
loss.backward()
optimizer.step()
total_loss += loss.item()
average_loss = total_loss / (len(input_seqs) // batch_size + (len(input_seqs) % batch_size != 0))
print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {average_loss:.4f}")
# 5. Text Generation
def generate_text(model, start_text, predict_len, char_to_index, index_to_char, vocab, device):
model.eval()
generated_text = start_text
input_sequence = [char_to_index[char] for char in start_text]
input_tensor = torch.LongTensor([input_sequence]).to(device)
hidden = model.init_hidden(1, num_layers, hidden_dim).to(device)
with torch.no_grad():
for _ in range(predict_len):
output, hidden = model(input_tensor, hidden)
probabilities = torch.softmax(output, dim=1)
predicted_index = torch.multinomial(probabilities, 1).item()
predicted_char = index_to_char[predicted_index]
generated_text += predicted_char
input_sequence = input_sequence[1:] + [predicted_index]
input_tensor = torch.LongTensor([input_sequence]).to(device)
return generated_text
# Example Generation
start_text = "The "
predict_length = 500
generated_output = generate_text(model, start_text, predict_length, char_to_index, index_to_char, vocab, device)
print("\nGenerated Text:")
print(generated_output)