[Python] ValueError: Classification metrics can't handle a mix of multiclass and...

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have Multi class labeled text classification problem with 2000 different labels. Doing classification using LSTM with Glove Embedding.

    1. Label Encoder of target variable
    2. LSTM layer with Embedd Layer
    3. Error metric is F2 score
    LabelEncoded target variable:


    le = LabelEncoder()
    le.fit(y)
    train_y = le.transform(y_train)
    test_y = le.transform(y_test)

    LSTM network is like below with Glove Embeddings


    np.random.seed(seed)
    K.clear_session()
    model = Sequential()
    model.add(Embedding(max_features, embed_dim, input_length = X_train.shape[1],
    weights=[embedding_matrix]))#,trainable=False
    model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
    model.add(Dense(num_classes, activation='softmax'))
    model.compile(optimizer='rmsprop', loss='sparse_categorical_crossentropy')
    print(model.summary())

    My error metric is F1 score. I build below function for Error metric


    class Metrics(Callback):
    def on_train_begin(self, logs={}):
    self.val_f1s = []
    self.val_recalls = []
    self.val_precisions = []

    def on_epoch_end(self, epoch, logs={}):
    val_predict = (np.asarray(self.model.predict(self.validation_data[0]))).round()
    val_targ = self.validation_data[1]
    _val_f1 = f1_score(val_targ, val_predict)
    _val_recall = recall_score(val_targ, val_predict)
    _val_precision = precision_score(val_targ, val_predict)
    self.val_f1s.append(_val_f1)
    self.val_recalls.append(_val_recall)
    self.val_precisions.append(_val_precision)
    print("— val_f1: %f — val_precision: %f — val_recall %f" % (_val_f1, _val_precision, _val_recall))
    return

    metrics = Metrics()


    ##Model fit is

    model.fit(X_train, train_y, validation_data=(X_test, test_y),epochs=10, batch_size=64, callbacks=[metrics])


    Getting below error after 1st epoch:

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


    Where is the mistake in my code?

    Continue reading...

Compartilhe esta Página