File size: 5,005 Bytes
6e66252 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
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) |