TypeError: step() missing 1 required positional argument: 'metrics'

I’m trying to add scheduler ReduceLRonPlateau, following instructions from documentation, but I get error “TypeError: step() missing 1 required positional argument: ‘metrics’” when I run it
Can you please tell me what should I pass to the optimizer?

learning_rate = 0.0001
num_epochs = 300
n_total_steps = len(train_loader)

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
scheduler = ReduceLROnPlateau(optimizer, mode='min', factor=0.5, patience=15, threshold=0.00001)

for epoch in range(num_epochs):
    for i, (images, shfl_labels, rot_labels) in enumerate(train_loader):
        # Origin shape: image [batch size, 3, 224, 224]
        #               shfl_labels [batch size, 224, 224]
        #               rot_labels [batch size, 4, 4]


        # Load data in GPU
        images = images.to(device)
        shfl_labels = shfl_labels.to(device)
        rot_labels = rot_labels.to(device)

        # Forward pass
        shfl_output, rot_output = model(images)
        loss_shfl = criterion(shfl_output, shfl_labels.long())
        loss_rot = criterion(rot_output, rot_labels.long())
        loss = loss_shfl + loss_rot

        #Backward pass and optimize
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
        scheduler.step()

The step() function of ReduceLROnPlateau expects a metric value as described in the docs, which is used to determine if the learning rate should be decreased or not.

5 Likes

Oh you are right. Sorry for my inattention. Thanks for your help

1 Like

I am also getting the same error, I saw the documentation but couldn’t identify the issue here. Please help me know what exactly should I look for in the documentation. should I mention a loss value in the step()?

Yes, the step() method expects a metric value as explained in my previous post.