繁体   English   中英

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

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

我有一个数据集,其中包含 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 

我想修改问题以分离标签,以便每个样本都可以独立地分类为远/近和小/中/小,给定每个分类的不同特征作为输入。

我的第一个想法是为每个子标签训练 2 个不同的模型,然后创建一个自定义函数来加入预测,但我想知道在 Keras 框架内是否有更快的方法。

我知道我可以使用函数式 API 来创建两个具有独立输入和两个独立输出的模型。 这会给我 2 个不同子标签的 2 个预测。 如果我对子标签进行热编码,这些模型的输出将是这样的:

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)

但是,如何合并这两个输出以创建完整标签的 6 个暗向量?

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

您可以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)

上面的一个简单的 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