this is my training loop: This is Cell #13 for epoch in range(numepochs): totalloss,…
Rate quality
Private research annotation
Sign in to add a private research label or note.
- User
this is my training loop:
This is Cell #13
for epoch in range(num_epochs): total_loss, correct_predictions, total_predictions = 0, 0, 0
codehidden = model.init_hidden(batch_size) for batch_idx, (batch_inputs, batch_targets) in tqdm(enumerate(train_loader), total=total_batches, desc=f"Epoch {epoch+1}/{num_epochs}"): batch_inputs, batch_targets = batch_inputs.to(device), batch_targets.to(device) output, hidden = model(batch_inputs, hidden) hidden = hidden.detach() loss = criterion(output.view(-1, output_size), batch_targets.view(-1)) # Flatten the outputs and targets for CrossEntropyLoss optimizer.zero_grad() loss.backward() optimizer.step() with torch.no_grad(): # Calculate accuracy _, predicted_indices = torch.max(output, dim=2) # Predicted characters correct_predictions += (predicted_indices == batch_targets).sum().item() total_predictions += batch_targets.size(0) * batch_targets.size(1) # Total items in this batch total_loss += loss.item() avg_loss = total_loss / len(train_loader) accuracy = correct_predictions / total_predictions * 100 # Convert to percentage print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {avg_loss:.4f}, Accuracy: {accuracy:.2f}%")help me with testing:
This is Cell #15
with torch.no_grad(): #TODO: Write the testing loop for your trained model by refering to the training loop code given to you above
codeprint(f"Test Loss: {avg_loss:.4f}, Test Accuracy: {accuracy:.2f}%") - Assistant
Certainly! Below is a testing loop that mirrors the structure of your training loop. It calculates the total loss and accuracy on the test dataset after training the model.