【问题标题】:Multi-class classification find probability of all classes多类分类找到所有类的概率
【发布时间】:2019-11-10 00:34:15
【问题描述】:

我从 keras 中举了一个例子。

https://github.com/keras-team/keras/blob/master/examples/pretrained_word_embeddings.py

sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
x = Conv1D(128, 5, activation='relu')(embedded_sequences)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = GlobalMaxPooling1D()(x)
x = Dense(128, activation='relu')(x)
preds = Dense(len(labels_index), activation='softmax')(x)

model = Model(sequence_input, preds)
model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['acc'])

该模型预测的类概率小于 0。我知道 softmax 会将其总和为 1。使用np.argmax(pre) 在概率中我可以看到的唯一一个输出 我希望其他类的概率至少是可读的。

Prediction output:
    [2.8300792e-06 4.5637703e-03 7.2316222e-02 6.7710824e-02 5.2243233e-01
     3.7763064e-04 1.2326813e-02 2.9277834e-01 4.1662962e-03 1.0876421e-05
     2.3830748e-06 1.3590348e-04 2.3074823e-02 3.3520879e-05 4.0551484e-05
     1.9896568e-06 1.0994432e-05 4.7518920e-06 2.3408763e-06 6.7659844e-06]

所有这些都产生小于 0 的概率。当我使用 np.argmax 时,我得到了 4如何获得高于 0 的概率结果?相反,我应该使用 softmax 哪个激活来获得更多的正概率?

【问题讨论】:

  • 我不明白你的问题。您所有预测的类概率都大于05.2243233e-01 = 0.522432336.7710824e-02 = 0.067710824.
  • 这些数字是科学计数法,例如2.83e-6 = 2.83 x 10^(-6),这些数字都不是负数。
  • 我明白了。我缺乏格式化这个 numpy 结果。谢谢指点.. 6.7710824e-02 = 0.067710824 你是怎么得到的>?
  • @giser_yugang 你是怎么转换的>?
  • @GiriAnnamalaiM 你可以参考stackoverflow.com/a/46635273

标签: python keras prediction


【解决方案1】:

试试这个代码。 从 sklearn.metrics 导入分类报告 将 numpy 导入为 np

Y_test = np.argmax(y_test, axis=1) # Convert one-hot to index
y_pred = model.predict_classes(X_test)
print(classification_report(Y_test, y_pred))

此代码的输出 (Cifar 10)

     classes  precision    recall  f1-score   support

       0       0.82      0.40      0.54      1000
       1       0.84      0.66      0.74      1000
       2       0.47      0.51      0.49      1000
       3       0.41      0.50      0.45      1000
       4       0.44      0.72      0.55      1000
       5       0.56      0.43      0.49      1000
       6       0.69      0.71      0.70      1000
       7       0.80      0.52      0.63      1000
       8       0.62      0.85      0.72      1000
       9       0.73      0.73      0.73      1000

   micro avg       0.60      0.60      0.60     10000
   macro avg       0.64      0.60      0.60     10000
weighted avg       0.64      0.60      0.60     10000

【讨论】:

    【解决方案2】:

    格式化上述预测结果

    pred = ["2.8300792e-06","4.5637703e-03", "7.2316222e-02"," 6.7710824e-02"," 5.2243233e-01",
     "3.7763064e-04","1.2326813e-02","2.9277834e-01", "4.1662962e-03", "1.0876421e-05",
     "2.3830748e-06", "1.3590348e-04", "2.3074823e-02","3.3520879e-05", "4.0551484e-05",
     "1.9896568e-06" ,"1.0994432e-05", "4.7518920e-06" ,"2.3408763e-06" ,"6.7659844e-06"]
    
    
     pred_ = ["{:f}".format(float(x)) for x in pred])
    
     #np.argmax give you the position which have maximum value and not probability
     #o/p
     ['0.000003', '0.004564', '0.072316', '0.067711', '0.522432', '0.000378', '0.012327', 
     '0.292778', '0.004166', '0.000011', '0.000002', '0.000136', '0.023075', '0.000034', 
     '0.000041', '0.000002', '0.000011', '0.000005', '0.000002', '0.000007']
    
    
    
     np.argmax(pred_)
     #o/p
     4
    

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2020-05-23
      • 2018-10-12
      • 2016-01-05
      • 2017-06-12
      • 2017-08-13
      • 2015-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多