首页 > 解决方案 > 用于多标签问题的 keras 模型的 scikit 学习链分类器的拟合方法错误

问题描述

我正在为使用 KerasClassifier 模型的多类问题构建链分类器。我有 17 个标签作为分类目标,X_train 的形状是 (111300,107),y_train 是 (111300,17) 我的代码在这里:

def create_model():
  input_size=length_long_sentence
  embedding_size=128
  lstm_size=64
  output_size=len(unique_tag_set)
    #----------------------------Model -------------------------------
  current_input=Input(shape=(input_size,)) 
  emb_current = Embedding(vocab_size, embedding_size, input_length=input_size)(current_input)
  out_current=Bidirectional(LSTM(units=lstm_size))(emb_current )
  #out_current = Reshape((1,2*lstm_size))(out_current)
  output = Dense(units=len(unique_tag_set), activation='softmax')(out_current)
  model = Model(inputs=current_input, outputs=output)
  model.compile(optimizer='Adam', loss='categorical_crossentropy', metrics=['accuracy'])
  print(model.summary())
  return model

   model = KerasClassifier(build_fn=create_model, epochs=1,batch_size=256)
   print(type(model))
   chain=ClassifierChain(model, order='random', random_state=42)
   history=chain.fit(X_train, y_train)

模型摘要在这里:

在此处输入图像描述

当尝试在 ClassifierChain 上使用 fit 方法时,我收到此错误:

在此处输入图像描述

任何人都可以指导我这个错误,什么是(无,2)?

标签: pythonkerasscikit-learnmultilabel-classification

解决方案


来自链分类器的文档:

将二元分类器排列成链的多标签模型。

因此,使用最后一层中的单个节点和损失函数将您的 keras 模型转换为二元分类器作为 binary_crossentropy


推荐阅读