does the learning rate adjust if the loss doesn't drop after 2 epochs ? import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils.rnn import pad_sequence
from torch.utils.data import DataLoader, Dataset
from collections import Counter
import json
from tqdm import tqdm
import math
import torch
import torch.optim.lr_scheduler as lr_scheduler
Check if CUDA is available
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def forward(self, x):
x = self.input_fc(x) + positional_encoding(x.size(1), self.d_model, x.device)
transformer_output = self.transformer_encoder(x)
output = self.output_fc(transformer_output) # Apply output_fc to each time step in the sequence
return output
# If hidden layers are specified, create them
if hidden_dims is not None:
for hidden_dim in hidden_dims:
layers.append(nn.Linear(last_dim, hidden_dim))
layers.append(nn.ReLU()) # You could make this a hyperparameter as well
if dropout_rate > 0.0:
layers.append(nn.Dropout(dropout_rate))
last_dim = hidden_dim
# Final layer projecting to the number of experts
layers.append(nn.Linear(last_dim, num_experts))
self.fc_layers = nn.Sequential(*layers)
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
# Assuming x is of shape [batch_size, seq_len, d_model], aggregate across the sequence length
x = x.mean(dim=1) # Aggregate feature per instance
x = self.fc_layers(x) # Pass through gating network layers
return self.softmax(x)
Define hyperparameters specific to the transformer
add learning rate to show up with val loss and loss to see if it is getting adjusted
Assistant
model_name = "Alpha_Talk-V04-X"
Mixture of Experts Model
class MixtureOfTransformerExperts(nn.Module):
def init(self, input_size, d_model, output_size, nhead, dim_feedforward, num_experts, num_encoder_layers=1):
super(MixtureOfTransformerExperts, self).init()
self.num_experts = num_experts
self.output_size = output_size # Store output_size as an instance variable
self.experts = nn.ModuleList([TransformerExpert(input_size, d_model, output_size, nhead, dim_feedforward, num_encoder_layers) for _ in range(num_experts)])
self.gating_network = GatingNetwork(d_model, num_experts)
def forward(self, x):
gating_scores = self.gating_network(x) # [batch_size, num_experts]
expert_outputs = [expert(x) for expert in self.experts] # List of [batch_size, seq_len, output_size]
stacked_expert_outputs = torch.stack(expert_outputs) # Shape: [num_experts, batch_size, seq_len, output_size]
# Expand gating scores
expanded_gating_scores = gating_scores.unsqueeze(2).unsqueeze(3) # Shape: [batch_size, num_experts, 1, 1]
expanded_gating_scores = expanded_gating_scores.expand(-1, -1, x.size(1), self.output_size)
expanded_gating_scores = expanded_gating_scores.transpose(0, 1) # Shape: [num_experts, batch_size, seq_len, output_size]
# Now the shape of expanded_gating_scores matches stacked_expert_outputs, and broadcasting will work
mixed_output = torch.sum(stacked_expert_outputs * expanded_gating_scores, dim=0) # Sum weighted expert outputs for each time step
return mixed_output
class QAJsonlDataset(Dataset):
def init(self, path, seq_len):
self.seq_len = seq_len
self.pairs = self.load_data(path)
# Flatten the pairs completely before passing them to build_vocab
self.vocab, self.idx2token = self.build_vocab([word for pair in self.pairs for sublist in pair for word in sublist])
self.tokenized_pairs = [(self.tokenize(q), self.tokenize(a)) for q, a in self.pairs]
def load_data(self, path):
pairs = []
with open(path, "r", encoding="utf-8") as f:
for line in f:
data = json.loads(line.strip())
question, answer = data.get("user", ""), data.get("content", "")
pairs.append((question.split(), answer.split()))
return pairs
def tokenize(self, words):
# Tokenize a sentence and pad if necessary
# Add <eos> token at the end if there’s room
tokens = [self.vocab.get(w, self.vocab["<unk>"]) for w in words]
if len(tokens) < self.seq_len:
tokens.append(self.vocab["<eos>"]) # Add <eos> token
tokens.extend([self.vocab["<pad>"]] * (self.seq_len - len(tokens))) # Pad the rest
else:
tokens = tokens[:self.seq_len - 1] + [self.vocab["<eos>"]]
return tokens
def build_vocab(self, words):
# Start with special tokens with fixed indices
vocab = {"<unk>": 0, "<pad>": 1, "<eos>": 2}
start_index = len(vocab)
# Use Counter to count word frequencies in the corpus
counts = Counter(words)
# Create the vocab dictionary with all words, starting indices after the special tokens
for word, _ in counts.most_common():
if word not in vocab: # Skip special tokens
vocab[word] = len(vocab)
# Create the reverse mapping from indices to words
idx2token = {idx: token for token, idx in vocab.items()}
return vocab, idx2token
def __len__(self):
return len(self.tokenized_pairs)
def __getitem__(self, idx):
tokenized_question, tokenized_answer = self.tokenized_pairs[idx]
return torch.tensor(tokenized_question, dtype=torch.long), torch.tensor(tokenized_answer, dtype=torch.long)
Set the path to your jsonl file and define sequence length
path_to_text = 'Real_talk.jsonl' # replace with the path to your jsonl file
seq_len = 32 # sequence length
Create a dataset and data loader
dataset = QAJsonlDataset(path_to_text, seq_len)
Save vocabulary to a text file
vocab_file = f"{model_name}_vocab.txt"
with open(vocab_file, "w", encoding="utf-8") as f:
for token, id in dataset.vocab.items():
f.write(f"{token}\t{id}\n")
Training loop - added gradient clipping to avoid exploding gradients
def train_model(model, criterion, optimizer, scheduler, num_epochs, data_loader, val_data_loader=None):
"""
Trains the MoE Transformer model with learning rate scheduler and validation loss calculation.
Args:
model: The MoE Transformer model to train.
criterion: The loss function (e.g., CrossEntropyLoss).
optimizer: The optimizer (e.g., AdamW).
scheduler: The learning rate scheduler (e.g., ReduceLROnPlateau).
num_epochs: The number of epochs to train for.
data_loader: The data loader for training data.
val_data_loader (optional): The data loader for validation data (if desired).
Returns:
None
"""
model.train()
for epoch in range(num_epochs):
total_loss = 0
learning_rate = optimizer.param_groups[0]['lr'] # Access current learning rate
progress_bar = tqdm(enumerate(data_loader), total=len(data_loader), desc=f"Epoch {epoch+1} - LR: {learning_rate:.6f}", leave=False)
for i, (inputs, targets) in progress_bar:
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
predictions = model(inputs)
predictions = predictions.view(-1, predictions.size(-1))
targets = targets.view(-1) # Flatten the targets to [batch_size * seq_len]
loss = criterion(predictions, targets)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) # Gradient clipping
optimizer.step()
total_loss += loss.item()
progress_bar.set_postfix({"Loss": loss.item()})
# Validation (if validation data loader provided)
if val_data_loader is not None:
# Set model to evaluation mode
model.eval()
with torch.no_grad():
running_val_loss = 0.0
for batch_index, (val_inputs, val_targets) in enumerate(val_data_loader):
val_inputs, val_targets = val_inputs.to(device), val_targets.to(device)
# Forward pass
val_outputs = model(val_inputs)
# Calculate validation loss
val_loss = criterion(val_outputs, val_targets)
running_val_loss += val_loss.item() * val_inputs.size(0) # Accumulate loss for the epoch
epoch_val_loss = running_val_loss / len(val_data_loader.dataset)
# Pass validation loss to scheduler
scheduler.step(metrics={'val_loss': epoch_val_loss})
# Set model back to training mode
model.train()
average_loss = total_loss / len(data_loader.dataset)
print(f"Epoch {epoch+1}, Average Loss: {average_loss}")
if val_data_loader is not None:
print(f"Epoch {epoch+1}, Validation Loss: {epoch_val_loss}")
# Update progress bar description to include current learning rate
progress_bar.set_description(f"Epoch {epoch+1} - LR: {learning_rate:.6f}")
def generate_text(model, dataset, seed_text, num_generate, temperature=1.0):
model.eval() # Put the model in evaluation mode
# List to store the generated tokens
generated_tokens = []
# Initial sequence (prefix) to start the generation process
input_sequence = [dataset.vocab.get(word, dataset.vocab["<pad>"]) for word in seed_text.split()] # Convert to token IDs
current_sequence = torch.tensor(input_sequence, dtype=torch.long).unsqueeze(0)
current_sequence = current_sequence.to(device)
# Generate num_generate tokens
for _ in range(num_generate):
# Forward pass through the model
with torch.no_grad():
output = model(current_sequence)
# Get probabilities, apply temperature scaling, and sample from the distribution
probabilities = F.softmax(output[:, -1, :] / temperature, dim=-1).detach()
next_token_idx = torch.multinomial(probabilities, 1).item()
# Append token to the current sequence and to the generated tokens
generated_tokens.append(next_token_idx)
current_sequence = torch.cat((current_sequence, torch.tensor([[next_token_idx]])), 1).to(device)
# Convert tokens to words
generated_text = " ".join([dataset.idx2token.get(token, "<unk>") for token in generated_tokens]) # Use .get() to provide a default value for missing keys
return generated_text
Function to count the number of tokens in the dataset
def count_tokens_in_dataset(dataset):
return sum([len(pair[0]) + len(pair[1]) for pair in dataset.pairs])
num_tokens = count_tokens_in_dataset(dataset)
print(f"Total number of tokens in the dataset: {num_tokens}")
vocab_size = len(dataset.vocab) # Assume dataset.vocab is defined in the QAJsonlDataset class
Instantiate resulting MoE transformer model and move it to device
Start a loop for the interactive chat-like text generation
while True:
try:
# Get user input
seed_text = input("Enter seed text (type 'quit' to stop): ")
# Check if user wants to quit the interaction
if seed_text.lower() == "quit":
print("Exiting text generation chat.")
break
# User input is not empty and not “quit”, generate text
if seed_text.strip():
num_generate = 64 # Number of words to generate
temperature = 1.0 # Sampling temperature, higher will increase diversity
# Use the trained model to generate text
generated_text = generate_text(moe_transformer_model, dataset, seed_text, num_generate, temperature)
print("Generated Text:", generated_text)
else:
print("Seed text cannot be empty.")
except KeyboardInterrupt:
# Handle KeyboardInterrupt (Ctrl+C) to gracefully exit
print("\nExiting text generation chat.")
break
In the provided Python code, which trains a Mixture Of Transformer Experts (MoE) model for text generation, the behavior of the learning rate in response to stagnation or increase in loss depends on how the learning rate scheduler is set up and used in the training loop.
The code snippet utilizes torch.optim.lr_scheduler.ReduceLROnPlateau as the learning rate scheduler. This scheduler adjusts the learning rate when a metric has stopped improving, specifically aiming to "reduce learning rate when a metric has stopped improving." The parameters passed to the scheduler determine the conditions under which the learning rate will be reduced.
mode='min': This means the scheduler is looking for the minimum validation loss to decide if improvement is happening.
factor=0.5: When adjustment happens, the learning rate will be reduced to half of its current value.
patience=2: This specifies that the scheduler will wait for 2 epochs where no improvement is observed before reducing the learning rate.
threshold=threshold_loss: This works along with threshold_mode='rel' to determine if the observed changes are significant based on a relative or absolute value.
threshold_mode='rel': Indicates that threshold is interpreted as a relative change.
verbose=True: This will print messages when the scheduler makes a learning rate adjustment.
Therefore, in response to your question: Yes, the learning rate will adjust if the validation loss does not drop (or does not significantly change within the specified threshold) after 2 epochs because of the configuration of the learning rate scheduler used (ReduceLROnPlateau with patience=2). Note that validation loss is used in this context since the scheduler updates based on val_loss provided to scheduler.step(metrics={'val_loss': epoch_val_loss}) inside the validation loop of the training method.
Keep in mind, to observe the adjustment of the learning rate in response to training/validation loss behavior, proper validation data must be fed into train_model() function, and the loss obtained from the validation part must be passed to scheduler.step() as shown in the training loop. The current configuration provided in the question shows a proper setup, but ensure that val_data_loader is non-None and correctly implemented to enable the scheduler's functionality.
To ensure the learning rate scheduling functionality is fully leveraged, you need a validation data loader (val_data_loader) to be passed into the train_model function. The example provided in the original question had the basis for validation checks but lacked the actual initialization and passing of a validation data loader to the training function.
Let's add an example validation dataset and adjust the training loop to use this validation data loader. We'll create a simplified version of a validation data loader setup and modify the train_model function to incorporate it. This is a hypothetical addition since the actual validation dataset is not provided in the question.
First, initialize a validation dataset and loader (similar to how the training dataset and loader are defined):
# Assuming the structure of QAJsonlDataset is applicable for both training and validation
# and assuming there's another file for validation or a mechanism to split the dataset.
# Use a different path or mechanism to ensure validation examples are separated from training
validation_dataset = QAJsonlDataset('validation_data.jsonl', seq_len)
val_data_loader = DataLoader(validation_dataset, batch_size=32, shuffle=False, collate_fn=collate_fn, pin_memory=True)
Now, update the train_model function signature and call within your script:
Update the function signature to include val_data_loader in the arguments list.
Call scheduler.step(epoch_val_loss) instead of passing a dictionary to it, as ReduceLROnPlateau expects the metric directly.
Updated train_model function to use the validation data loader:
def train_model(model, criterion, optimizer, scheduler, num_epochs, data_loader, val_data_loader=None):
model.train()
for epoch in range(num_epochs):
total_loss = 0
progress_bar = tqdm(enumerate(data_loader), total=len(data_loader), desc="Training", leave=False)
for i, (inputs, targets) in progress_bar:
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
predictions = model(inputs)
predictions = predictions.view(-1, predictions.size(-1))
targets = targets.view(-1)
loss = criterion(predictions, targets)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
total_loss += loss.item()
# Validation phase
val_loss = 0.0
if val_data_loader is not None:
model.eval()
with torch.no_grad():
for inputs, targets in val_data_loader:
inputs, targets = inputs.to(device), targets.to(device)
predictions = model(inputs)
predictions = predictions.view(-1, predictions.size(-1))
targets = targets.view(-1)
loss = criterion(predictions, targets)
val_loss += loss.item()
val_loss /= len(val_data_loader)
scheduler.step(val_loss) # Update learning rate based on validation loss
model.train() # Ensure model is back in training mode
print(f"Epoch {epoch+1}, Training Loss: {total_loss / len(data_loader)}, Validation Loss: {val_loss}")
# Make sure to pass 'val_data_loader' in the call to 'train_model'
train_model(moe_transformer_model, criterion, optimizer, scheduler, num_epochs, data_loader, val_data_loader)
These modifications ensure that the model now takes into account validation performance for learning rate adjustments. This can help in preventing overfitting and ensures that your learning rate is dynamically adjusted based on how your model is performing on data it hasn't been trained on, which is a more robust indicator of its generalizability.
To show the learning rate alongside training loss and validation loss, and to confirm if it's getting adjusted, we can modify the print statement within our training loop. This adjustment will help monitor the current learning rate after each epoch and observe its changes in response to the validation loss.
I'll enhance the train_model function by explicitly fetching and displaying the learning rate at the end of each epoch, ensuring visibility into how it changes over time based on our scheduler settings.
def train_model(model, criterion, optimizer, scheduler, num_epochs, data_loader, val_data_loader=None):
model.train()
for epoch in range(num_epochs):
total_loss = 0
# Initially, fetch the learning rate from the optimizer
learning_rate = optimizer.param_groups[0]['lr']
progress_bar = tqdm(enumerate(data_loader), total=len(data_loader), desc="Training", leave=False)
for i, (inputs, targets) in progress_bar:
inputs, targets = inputs.to(device), targets.to(device)
optimizer.zero_grad()
predictions = model(inputs)
predictions = predictions.view(-1, predictions.size(-1))
targets = targets.view(-1)
loss = criterion(predictions, targets)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
total_loss += loss.item()
# Validation phase
val_loss = 0.0
if val_data_loader is not None:
model.eval()
with torch.no_grad():
for inputs, targets in val_data_loader:
inputs, targets = inputs.to(device), targets.to(device)
predictions = model(inputs)
predictions = predictions.view(-1, predictions.size(-1))
targets = targets.view(-1)
loss = criterion(predictions, targets)
val_loss += loss.item()
val_loss /= len(val_data_loader)
scheduler.step(val_loss) # Update learning rate based on validation loss
# Fetch the adjusted learning rate after the scheduler step
adjusted_learning_rate = optimizer.param_groups[0]['lr']
model.train() # Ensure model is back in training mode
# Print epoch, learning rate, training loss, and validation loss
print(f"Epoch {epoch+1}, Learning Rate: {adjusted_learning_rate:.6f}, Training Loss: {total_loss / len(data_loader.dataset)}, Validation Loss: {val_loss}")
# Make sure to pass 'val_data_loader' to 'train_model'
train_model(moe_transformer_model, criterion, optimizer, scheduler, num_epochs, data_loader, val_data_loader)
This enhancement in the print statement not only shows the training and validation losses but also prominently displays the learning rate at the start of each epoch and its adjustment if any. Specifically, the scheduler.step(val_loss) call may adjust the learning rate based on the validation loss performance, so comparing the learning_rate before and adjusted_learning_rate after this call can highlight the scheduler's impact.
Such visibility is crucial for understanding your model's learning dynamics and fine-tuning the learning rate schedule for better performance or more stable convergence.