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

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

我正在尝试使用 Cifar10 数据集计算多 class class 化问题的 f1 分数。我正在从 sklearn 库中导入 f1 metirics。但是我不断收到以下错误消息:

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

下面是我在验证集上测试模型的函数。有人能解释一下在执行 multi-class classification 时如何计算 f1 吗?我越来越糊涂了。

@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


问题出在这里:

val_pred = model(x_val)

您需要转换加载模型的方式。例如你的情况:

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