import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader import numpy as np from tqdm.auto import tqdm import matplotlib.pyplot as plt from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score # Custom training loop def train_model(model, train_loader, val_loader, num_epochs=10, learning_rate=0.001): """ Train the temperature prediction model Args: model: The model to train train_loader: DataLoader for training data val_loader: DataLoader for validation data num_epochs: Number of training epochs learning_rate: Learning rate for optimizer Returns: Trained model and training history """ # Set device device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print(f"Using device: {device}") # Move model to device model = model.to(device) # Loss function and optimizer criterion = nn.MSELoss() optimizer = optim.AdamW(model.parameters(), lr=learning_rate) # Learning rate scheduler scheduler = optim.lr_scheduler.ReduceLROnPlateau( optimizer, mode='min', factor=0.5, patience=2, verbose=True ) # Training history history = { 'train_loss': [], 'val_loss': [], 'val_mae': [] } # Best model tracking best_val_loss = float('inf') best_model_state = None # Training loop for epoch in range(num_epochs): # Training phase model.train() train_losses = [] progress_bar = tqdm(train_loader, desc=f"Epoch {epoch+1}/{num_epochs} [Train]") for batch in progress_bar: # Move batch to device input_ids = batch['input_ids'].to(device) attention_mask = batch['attention_mask'].to(device) features = batch['features'].to(device) targets = batch['target'].to(device) # Forward pass optimizer.zero_grad() outputs = model(input_ids, attention_mask, features) # Calculate loss loss = criterion(outputs, targets) # Backward pass and optimize loss.backward() optimizer.step() # Track loss train_losses.append(loss.item()) progress_bar.set_postfix({'loss': np.mean(train_losses[-100:])}) # Calculate average training loss for the epoch avg_train_loss = np.mean(train_losses)