【问题标题】:Tweet emotion prediction using CNN使用 CNN 进行推文情绪预测
【发布时间】:2020-05-30 17:57:00
【问题描述】:

我已经建立了一个用于推文情感检测的 CNN 模型,最后一步如下:

tweets_emotion = model.predict(val_tweets, verbose= 0)

这给了我这样的预测输出

array([[3.1052819e-01, 2.7634043e-01, 1.6270137e-03, 7.7674150e-01],
       [5.0230421e-02, 7.7430069e-01, 7.7313791e-09, 2.0278792e-01],
       [9.9952579e-01, 1.3450404e-03, 5.8804121e-20, 3.2991991e-07],
       ...,
       [3.9727339e-01, 2.8888196e-01, 1.9649005e-02, 2.1239746e-01],
       [1.2528910e-01, 3.2127723e-01, 3.2503495e-03, 5.5401272e-01],
       [5.8543805e-02, 4.5720499e-05, 2.9060062e-12, 9.3766922e-01]],
      dtype=float32)

我的实际输出应该是这样的:

array([[1., 0., 0., 0.],
       [1., 0., 0., 0.],
       [1., 0., 0., 0.],
       ...,
       [0., 0., 0., 1.],
       [0., 0., 0., 1.],
       [0., 0., 0., 1.]], dtype=float32)

有没有办法将我的预测输出 (tweets_emotion) 转换为我预期的输出?

【问题讨论】:

  • 您的输出看起来不正确,即它看起来不像 softmax 的输出;每个数组加起来应该是 1.0,这不是这里的情况。请出示您的实际型号。
  • 我在这里使用了sigmoid激活。
  • Sigmoid 在具有单热编码标签的单标签多类设置中没有意义,就像您的设置一样。

标签: python machine-learning keras conv-neural-network


【解决方案1】:

使用您在此处显示的 6 个预测示例:

import numpy as np

tweets_emotion = np.array([[3.1052819e-01, 2.7634043e-01, 1.6270137e-03, 7.7674150e-01],
                           [5.0230421e-02, 7.7430069e-01, 7.7313791e-09, 2.0278792e-01],
                           [9.9952579e-01, 1.3450404e-03, 5.8804121e-20, 3.2991991e-07],
                           [3.9727339e-01, 2.8888196e-01, 1.9649005e-02, 2.1239746e-01],
                           [1.2528910e-01, 3.2127723e-01, 3.2503495e-03, 5.5401272e-01],
                           [5.8543805e-02, 4.5720499e-05, 2.9060062e-12, 9.3766922e-01]])

tweets_emotion_class = np.argmax(tweets_emotion, axis=1)
tweets_emotion_class
# array([3, 1, 0, 0, 3, 3])

您应该能够通过简单的视觉检查来验证每个数组元素的最大值确实是tweets_emotion_class中显示的值。

与您的问题无关,但正如 cmets 中所述,最后一个网络层的 sigmoid 激活在具有单热编码标签的单标签多类设置中没有意义,因为您的设置似乎是 -你应该把它改成softmax

【讨论】:

    猜你喜欢
    • 2013-04-28
    • 1970-01-01
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2019-11-16
    • 2018-12-29
    相关资源
    最近更新 更多