Pytorch training and testing loop confusion

username: Usman1
cooked: <p>Hi everyone, I’m working on a deep learning model where I need to predict (X, Y) coordinates . I’ve encountered an issue I’ve been advised to plot the predicted positions at each epoch, but it’s only necessary to generate this plot once at the end of the training. Could someone guide me on how to best structure this so that i can move forward with.my traing loop is as follow:</p>
<p>total_train_losses = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
total_test_losses = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
all_predicted_outputs = <span class="chcklst-box fa fa-square-o fa-fw"></span><br>
all_actual_outputs = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<h1><a name="p-459214-training-and-evaluation-process-1" class="anchor" href="#p-459214-training-and-evaluation-process-1"></a>Training and evaluation process</h1>
<p>for epoch in range(num_epochs):<br>
# --------Training---------<br>
model.train()<br>
running_train_loss = 0.0  # Initialize running training loss for each epoch</p>
<pre><code>for batch_data, batch_target in train_loader:
    
    optimizer.zero_grad()

    # Forward pass
    predicted_output = model(batch_data)  # Predict X, Y coordinates for the batch

    # Compute loss
    train_loss = criterion(predicted_output, batch_target)

    # Backward pass and optimization
    train_loss.backward()
    optimizer.step()

    running_train_loss += train_loss.item()

# Compute average training loss
avg_train_loss = running_train_loss / len(train_loader)
# Compute average test loss
    
total_train_losses.append(avg_train_loss)  # Append to total train losses
# --------Evaluation---------
model.eval()
running_test_loss = 0.0
predicted_outputs = []
actual_outputs = []
with torch.no_grad():
    

    for batch_data, batch_target in test_loader:
        #print("Batch Data (Input):", batch_data)
        #print("Batch Target (Label):", batch_target)
        #break  # Only print the first batch to avoid flooding output
        predicted_output = model(batch_data)
        test_loss = criterion(predicted_output, batch_target)
        running_test_loss += test_loss.item()

        # Store the predicted and actual outputs for analysis
        predicted_outputs.append(predicted_output.cpu().numpy())
        actual_outputs.append(batch_target.cpu().numpy())

    # Compute average test loss
    avg_test_loss = running_test_loss / len(test_loader)
    
    total_test_losses.append(avg_test_loss)  # Append to total train losses
# Print train and test loss every 100 epochs
if (epoch + 1) % 100 == 0:
    print(f"Epoch {epoch+1}/{num_epochs}, Train Loss: {avg_train_loss:.4f}, Test Loss: {avg_test_loss:.4f}")
</code></pre>

username: ptrblck
cooked: <p>I don’t fully understand your question and where exactly you are stuck.<br>
In your current code snippet you are already appending the predicted coordinates during the validation run to <code>predicted_outputs</code> and should be able to plot these afterwards.</p>

username: Usman1
cooked: <p>sorry for confusion.<br>
in my training and testing loop i am predicted positions (predicted output for training)and predicted output for testing.now i need to calculate the average position error.so for that  i will calculate eucludean distance between predicted output and ground truth values(for both training and testing).i am stuck will I use these actual and predicted positions<br>
predicted_outputs.append(predicted_output.cpu().numpy())<br>
actual_outputs.append(batch_target.cpu().numpy())<br>
or  do i need to take the  the shuffled data(train target and test target) i load to train_test loader?<br>
hope this clear you my confusion</p>

username: ptrblck
cooked: <p>You can use the appended batch output and target for the loss or accuracy calculation.</p>