使用以下Keras网络训练和分类9个班级时:
from keras.models import Model
from keras.layers import Convolution1D, Input, Dropout, GlobalMaxPooling1D, Dense, merge
input_window3 = Input(shape=(MEANLEN, W2VLEN))
input_window4 = Input(shape=(MEANLEN, W2VLEN))
conv_w3 = Convolution1D(MEANLEN*2, 3, activation='tanh', border_mode='valid')(input_window3)
drop_w3 = Dropout(0.7)(conv_w3),
pool_w3 = GlobalMaxPooling1D(name='pool_w3')(drop_w3[0])
conv_w4 = Convolution1D(MEANLEN, 5, activation='tanh', border_mode='valid')(input_window4)
drop_w4 = Dropout(0.7)(conv_w4),
pool_w4 = GlobalMaxPooling1D(name='pool_w4')(drop_w4[0])
print(conv_w4.shape)
x = merge([pool_w3, pool_w4], mode='concat', concat_axis=1)
print(x.shape)
x = Dense(MEANLEN*3, activation='relu')(x)
drop_dense = Dropout(0.5)(x)
main_output = Dense(num_categories, activation='sigmoid', name='main_output')(drop_dense)
model = Model(input=[input_window3, input_window4], output=[main_output])
model.compile(optimizer='adam', loss='mse', metrics=['accuracy', f1_score])
测算:
result = model.predict([X_test, X_test])
将类似的向量数组返回到这些向量:
array([[ 0.08401331, 0.1911521 , 0.14310306, 0.07138534, 0.19428432,
0.15808958, 0.16400988, 0.27708355, 0.09983496],
[ 0.02074078, 0.08897329, 0.03244834, 0.00112842, 0.04122255,
0.03494435, 0.17535761, 0.55671334, 0.04375785],
[ 0.04897207, 0.06169643, 0.00313113, 0.002085 , 0.00275023,
0.00131959, 0.09961601, 0.56414878, 0.02338091]], dtype=float32)
数组中的值,我认为是类概率,不总结为1.如何获得类概率?
答案 0 :(得分:3)
根据您发布的数组,您有9个类别。在这种情况下,您应该使用softmax而不是sigmoid替换最终的激活函数。此外,如果您尚未完成,则需要将标签转换为单热矢量。您可以使用函数to_categorical执行此操作。最后,作为一个损失函数,你应该使用categorical_crossentropy损失,而不是mse。提供了使用keras进行分类的教程(使用上述功能)here。
答案 1 :(得分:2)
通常,当您希望输出类似于分类概率分布时,在最后一层使用softmax activation function而不是sigmoid:
main_output = Dense(num_categories, activation='softmax', name='main_output')(drop_dense)