我创建了一个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'])

最佳答案

我同意@ blue-phoenox的评论,即您不应该使用具有交叉熵的Sigmoid,因为类的概率之和不等于1。但是,如果您有使用sigmoid的理由,则可以通过矢量元素的总和对输出进行归一化,使其等于1:

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()模型中使用它:

# using dummy data to illustrate
x_train = np.array([[-1.551, -1.469], [1.022, 1.664]], dtype=np.float32)
y_train = np.array([[0, 1], [1, 0]], dtype=np.int32)

model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(units=2, activation=tf.nn.sigmoid, input_shape=(2, )))
model.add(NormLayer())

model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

model.fit(x=x_train,
          y=y_train,
          epochs=2,
          batch_size=2)
# ...

关于python - 增加S型预测输出值?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55469601/

10-13 07:13