我创建了一个Conv1D模型用于文本分类。
在最后一次密集使用softmax / Sigmoid时,结果为
softmax => [0.98502016 0.0149798 ]
sigmoid => [0.03902826 0.00037046]
我只希望S型结果的第一个索引应该至少大于0.8 。只希望多类应该有独立的结果。我该如何实现?
模型摘要:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding (Embedding) (None, 128, 100) 600
_________________________________________________________________
conv1d (Conv1D) (None, 126, 128) 38528
_________________________________________________________________
max_pooling1d (MaxPooling1D) (None, 63, 128) 0
_________________________________________________________________
conv1d_1 (Conv1D) (None, 61, 128) 49280
_________________________________________________________________
max_pooling1d_1 (MaxPooling1 (None, 30, 128) 0
_________________________________________________________________
conv1d_2 (Conv1D) (None, 28, 128) 49280
_________________________________________________________________
max_pooling1d_2 (MaxPooling1 (None, 14, 128) 0
_________________________________________________________________
flatten (Flatten) (None, 1792) 0
_________________________________________________________________
dense (Dense) (None, 2) 3586
=================================================================
Total params: 141,274
Trainable params: 141,274
Non-trainable params: 0
_________________________________________________________________
model.add(keras.layers.Dense(num_class, activation='sigmoid'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop', metrics=['acc'])
答案 0 :(得分:2)
我同意@ blue-phoenox的评论,即您不应该将Sigmoid与交叉熵一起使用,因为类的概率之和不等于1。但是,如果您有使用ReferenceError: "email" is not defined. (line 7, file "Code")
的理由,则可以通过矢量元素的总和对输出进行归一化,使其等于1:
sigmoid
您会得到:
output = output/tf.reshape(tf.reduce_sum(output, 1), (-1, 1))
要在import tensorflow as tf
output = tf.Variable([[0.03902826, 0.00037046]])
output = output/tf.reshape(tf.reduce_sum(output, 1), (-1, 1))
summedup = tf.reduce_sum(output, axis=1)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(output.eval()) # [[0.9905971 0.00940284]] - new output
print(summedup.eval()) # [1.] - summs up to 1
中实现它,您可以像这样创建keras
的子类:
tf.keras.layers.Layer
然后在您的from tensorflow.keras import layers
class NormLayer(layers.Layer):
def __init__(self):
super(NormLayer, self).__init__()
def call(self, inputs):
return inputs / tf.reshape(tf.reduce_sum(inputs, 1), (-1, 1))
模型中使用它:
Sequential()
答案 1 :(得分:1)
Sigmoid产生0到1之间的输出。如果对softmax和Sigmoid使用相同的损失函数,则它将不起作用。尝试使用binary_crossentropy。而且,如果您有两个以上的班级,我认为您不会在寻找S型。