我一直坚持使用多标签分类(我必须说我对神经网络很新).首先,我将解释我正在努力训练的网络.我在网络中有1000个类,它们有多标签输出.对于每个训练示例,正输出的数量相同(即10),但是它们可以分配给1000个类中的任何一个.所以10个类有输出1而其余990有输出0.对于多标签分类,我使用'二进制交叉熵'作为成本函数,'sigmoid'作为激活函数.当我尝试0.5的这个规则作为1或0的截止时.所有这些都是0.我明白这是一个类不平衡问题.从这个链接,我理解,我可能不得不创建额外的输出标签.遗憾的是,我还没有弄清楚如何将其纳入keras中的简单神经网络.
nclasses = 1000
# if we wanted to maximize an imbalance problem!
#class_weight = {k: len(Y_train)/(nclasses*(Y_train==k).sum()) for k in range(nclasses)}
#print(class_weight)
# building neural network model
inp = Input(shape=[X_train.shape[1]])
x = Dense(5000, activation='relu')(inp)
x = Dense(4000, activation='relu')(x)
x = Dense(3000, activation='relu')(x)
x = Dense(2000, activation='relu')(x)
x = Dense(nclasses, activation='sigmoid')(x)
model = Model(inputs=[inp], outputs=[x])
print(model.summary())
adam=keras.optimizers.adam(lr=0.00001)
model.compile('adam', 'binary_crossentropy')
history = model.fit(
X_train, Y_train, batch_size=32, epochs=50,verbose=0,shuffle=False)
plt.plot(history.history['loss'])
#plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
model.save('model2.h5')
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我处理这里的代码,如果你能为这个问题提出一个好的"准确度"指标,我也非常感谢你们. …
我只找到MaxPooling2D并AveragePooling2D与tensorflow后端keras。一直在寻找MinimumPooling2D。这个 github链接建议使用这样的东西来进行最小池化 ( pool2d(-x))
在输入之前使用负号时出现错误。我在 keras 中使用的以下行
MaxPooling2D((3, 3), strides=(2, 2), padding='same')(-inputs)
Run Code Online (Sandbox Code Playgroud)