hi, i’m pretty new on using pytorch for training a model and i’m trying to debug this code, 'cause the accuracy seems pretty strange (some run 0.02 and others 1.0). can someone help me spotting some bug in the code?
the following is the code for training
num_epochs = 150
batch_size = 1024
batch_start = torch.arange(0, len(X_train), batch_size)
precision_train_ = []
accuracy_train_ = []
recall_train_ = []
f1_train_ = []
loss_train_ = []
precision_val_ = []
accuracy_val_ = []
recall_val_ = []
f1_val_ = []
loss_val_ = []
print("Starting training...")
for Epoch in range(num_epochs):
for start in batch_start:
X_train_batch = X_train[start:start+batch_size]
y_train_batch = y_train[start:start+batch_size]
# Forward pass
outputs = model(X_train_batch)
loss = criterion(outputs, y_train_batch.view(-1, 1))
# Backpropagation and optimization
optimizer.zero_grad()
loss.backward()
optimizer.step()
with torch.no_grad():
model.eval()
# Predictions on training set
predictions_train = model(X_train_batch)
loss_train = criterion(predictions_train, y_train_batch.view(-1, 1))
predictions_train = (predictions_train > 0.5).float()
# Metrics on training set
accuracy_train = torch.sum(predictions_train == y_train_batch.view(-1, 1)).item() / y_train.shape[0]
accuracy_train_.append(accuracy_train)
precision_train = precision_score(y_train_batch, predictions_train, average='weighted')
precision_train_.append(precision_train)
recall_train = recall_score(y_train_batch, predictions_train, average='weighted')
recall_train_.append(recall_train)
f1_train = f1_score(y_train_batch, predictions_train, average='weighted')
f1_train_.append(f1_train)
loss_train_.append(loss_train)
# Predictions on validation set
predictions_val = model(X_test)
loss_val = criterion(predictions_val, y_test.view(-1, 1))
predictions_val = (predictions_val > 0.5).float()
# Metrics on validation set
accuracy_val = torch.sum(predictions_val == y_test.view(-1, 1)).item() / y_test.shape[0]
accuracy_val_.append(accuracy_val)
precision_val = precision_score(y_test, predictions_val, average='weighted')
precision_val_.append(precision_val)
recall_val = recall_score(y_test, predictions_val, average='weighted')
recall_val_.append(recall_val)
f1_val = f1_score(y_test, predictions_val, average='weighted')
f1_val_.append(f1_val)
loss_val_.append(loss_val)
before_lr = optimizer.param_groups[0]["lr"]
#scheduler.step()
after_lr = optimizer.param_groups[0]["lr"]
if (Epoch + 1) % 10 == 0:
print(f"Epoch [{Epoch+1}/{num_epochs}] | Loss_train: {loss_train.item():.4f} | Loss_val: {loss_val.item():.4f} | Adam lr {before_lr:.4f} -> {after_lr:.4f} | Accuracy_train: {accuracy_train:.2f} | Accuracy_val: {accuracy_val:.2f}")