unim192745
unim192745

Reputation: 79

ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets

I am trying to calculate the f1 score for a multi-class classification problem using the Cifar10 dataset. I am importing f1 metirics from the sklearn library. However I keep getting the following error message:

ValueError: Classification metrics can't handle a mix of multiclass and continuous-multioutput targets

Below is my function for testing the model on my validation set. Would someone be able to explain how to calculate f1 when performing multi-class classification. I am getting quite confused.

@torch.no_grad()
def valid_function(model, optimizer, val_loader):
  model.eval()

  val_loss = 0.0
  val_accu = 0.0
  f_one = []
  for i, (x_val, y_val) in enumerate(val_loader):

    x_val, y_val = x_val.to(device), y_val.to(device)

    val_pred = model(x_val)
    loss = criterion(val_pred, y_val)

    val_loss += loss.item()
    val_accu += accuracy(val_pred, y_val)
    f_one.append(f1_score(y_val.cpu(), val_pred.cpu()))


  val_loss /= len(val_loader)
  val_accu /= len(val_loader)
      
  print('Val Loss: %.3f | Val Accuracy: %.3f'%(val_loss,val_accu))

  return val_loss, val_accu


Upvotes: 1

Views: 8683

Answers (1)

Gilbert Ronoh
Gilbert Ronoh

Reputation: 76

The problem is here:

val_pred = model(x_val)

You need to convert how you load the model. For example in your case:

val_pred = np.argmax(model.predict(x_val), axis=-1)

Upvotes: 5

Related Questions