This is the code: import torch import torch.optim as optim import torch.nn as nn import…
- User
This is the code: import torch import torch.optim as optim import torch.nn as nn import torch.nn.functional as F from torch.utils.data import DataLoader from sklearn.model_selection import train_test_split
Assuming you have already defined your MoE model, DataLoader, and loss function
from moe import MOELayer from data_preprocessing import DataPreprocessor
def advanced_dynamic_expert_allocation(validation_losses, num_experts): """ Computes dynamic expert allocation based on validation losses.
Args: validation_losses (list): List of validation losses for each expert. num_experts (int): Number of experts. Returns: torch.Tensor: Expert weights (probabilities) for each expert. """ # Compute inverse validation losses (lower loss -> higher weight) inverse_losses = [1.0 / loss for loss in validation_losses] # Normalize the inverse losses to get expert weights total_inverse_loss = sum(inverse_losses) expert_weights = torch.tensor([loss / total_inverse_loss for loss in inverse_losses]) return expert_weightsdef train_moe_model(x_train, y_train, x_val, y_val, moe_model, batch_size, num_epochs, lr, device, dynamic_expert_allocation=False, validation_losses=None, num_experts=None): moe_model.to(device) optimizer = optim.Adam(moe_model.parameters(), lr=lr) criterion = nn.CrossEntropyLoss() train_loader = DataLoader(x_train, y_train, batch_size=batch_size, shuffle=True) valid_loader = DataLoader(x_val, y_val, batch_size=batch_size, shuffle=False)
for epoch in range(num_epochs): moe_model.train() total_loss = 0.0 for batch in train_loader: inputs, targets = batch.text.to(device), batch.label.to(device) optimizer.zero_grad() # Forward pass outputs = moe_model(inputs) # Compute dynamic expert allocation gate_outputs = F.softmax(moe_model.gating_network(inputs), dim=1) expert_allocation = advanced_dynamic_expert_allocation(validation_losses, num_experts) # Apply expert weights to expert outputs weighted_outputs = torch.sum(expert_allocation.unsqueeze(2) * outputs, dim=1) # Compute loss loss = criterion(weighted_outputs, targets) # Backward pass and optimization loss.backward() optimizer.step() total_loss += loss.item() avg_train_loss = total_loss / len(train_loader) # Validation moe_model.eval() valid_loss = 0.0 with torch.no_grad(): for batch in valid_loader: inputs, targets = batch.text.to(device), batch.label.to(device) outputs = moe_model(inputs) valid_loss += criterion(outputs, targets).item() avg_valid_loss = valid_loss / len(valid_loader) print(f"Epoch {epoch+1}/{num_epochs} | Train Loss: {avg_train_loss:.4f} | Valid Loss: {avg_valid_loss:.4f}") # Save model checkpoint torch.save(moe_model.state_dict(), f"moe_model_epoch{epoch+1}.pth") print("Training completed. Model checkpoints saved.")if == "": preprocessor = DataPreprocessor(max_vocab_size=25000, embedding_dim=100) X_train, X_test, y_train, y_test = preprocessor.load_data() X_val, X_test, y_val, y_test = train_test_split(X_test, y_test, test_size=0.5, random_state=42) batch_size = 32 num_epochs = 10 learning_rate = 0.001 num_experts = 2 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
- Assistant
The error you're encountering is because of how you're initializing the
DataLoaderobjects for training and validation in yourtrain_moe_modelfunction. When initializing a , you should pass a dataset object that encapsulates your features (inputs) and labels (targets). However, in the code you provided, you're attempting to pass the features and labels as separate arguments directly to the constructor, which is incorrect and leads to the . - User
Traceback (most recent call last): File "c:\Users\L14\Documents\Projets\Project-Hajax\train.py", line 116, in train_moe_model(train_dataset, val_dataset, moe_model, batch_size, num_epochs, learning_rate, device) File "c:\Users\L14\Documents\Projets\Project-Hajax\train.py", line 56, in train_moe_model inputs, targets = batch.text.to(device), batch.label.to(device) AttributeError: 'list' object has no attribute 'text'
- Assistant
The error message you're encountering indicates that you're trying to access attributes (
textandlabel) that don't exist on the items returned by your DataLoader. When you're using a custom dataset class with PyTorch's DataLoader, the items yielded by the DataLoader are whatever is returned by the method of your dataset class. Based on the custom dataset example I previously provided, each item should be a tuple consisting of .