简体   繁体   English

如何使用 keras 进行多标签多类分类

[英]How to use keras for mutli label multiclass classification

I have a data set with 6 possible labels of the type:我有一个数据集,其中包含 6 个可能的类型标签:

Class 1: Near-large 
Class 2: Far- Large 
Class 3: Near - Medium 
Class 4: Far - Medium 
Class 5: Near - small 
Class 6: far - small 

And I would like to modify the problem to separate the labels so each sample would be classified as far/near and small/medium/small independently, given different features for each classification as input.我想修改问题以分离标签,以便每个样本都可以独立地分类为远/近和小/中/小,给定每个分类的不同特征作为输入。

My first idea was to train 2 different models for each sublabel and then make a custom function to join the predictions , but I wonder if there's a quicker way of doing it within the Keras framework.我的第一个想法是为每个子标签训练 2 个不同的模型,然后创建一个自定义函数来加入预测,但我想知道在 Keras 框架内是否有更快的方法。

I know I can use the functional API to create two models with independent inputs and two independent outputs.我知道我可以使用函数式 API 来创建两个具有独立输入和两个独立输出的模型。 This would give me 2 predictions for 2 different sublabels.这会给我 2 个不同子标签的 2 个预测。 If I one hot encode the sublabels the output of those models would be something like this:如果我对子标签进行热编码,这些模型的输出将是这样的:

Model1.output = [ 0,1 ] or [1,0]  ( far/near) 
Model2.output = [ 1, 0, 0 ] or [0,1,0] or [0,0,1](small/medium/large)

But then how can I merge these two outputs to create a 6 dim vector for the complete labels ?但是,如何合并这两个输出以创建完整标签的 6 个暗向量?

Model_merged.output = [1, 0, 0, 0, 0 ,0 ] , [010000], ...., [000001] (class1,... ,Class6) 

You can reshape model1 output to extend the axis, mulitply that with model2 output and flatten them both.您可以reshape 1 输出以扩展轴,将其与模型 2 输出相乘并将它们都展平。

from keras.models import Model

reshaped = keras.layers.Reshape((2,-1))(model1.output)
combined = keras.layers.Multiply()([reshaped,model2.output])
flattened = keras.layers.Reshape((6,))(combined)


Combined_model = Model([model1.input,model2.input], flattened)

A simple numpy example of the above would be:上面的一个简单的 numpy 示例是:

model1_output = np.array([0,1])[:,None] #Reshaped

#array([[0],
#       [1]])

model2_output = np.array([1,0,0])

# array([1, 0, 0])

combined = model1_output*model2_output

#array([[0, 0, 0],
#       [1, 0, 0]])

combined.ravel()

#array([0, 0, 0, 1, 0, 0])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM