簡體   English   中英

神經網絡無法預測多類 output

[英]neural network not able to predict multiclass output

我正在為多類分類問題制作 neural.network model。 我只想使用 1 層,因為我需要為另一個問題集提取權重,並且權重應該只與自變量相同。 我一直面臨一個問題,我的 model 似乎很糟糕,並表明在我的混淆矩陣中,model 不會預測所有類別。 這是我的數據的樣子(自變量是 t1-t6,因變量是分數)。

評論編號 t1 t2 t3 t4 t5 t6 分數
01 -3 0 0 0 0 0 1個
02 0 0 38 0 0 0 5個
03 0 9 0 0 0 0 2個
等等

“t1 - t6”的范圍是 [-infinity 到 -infinity]。 “分數”的范圍是[1-5],我想在其中做一個多類分類。 這是我的代碼的樣子......

num_classes = 5
model_relu = Sequential()
model_relu.add(Dense(1, input_shape=(X.shape[1],), activation='relu')) # input shape is (features,). 1 hidden layer with 6 neurons
model_relu.add(Dense(num_classes, activation='softmax'))
model_relu.summary()
# compile the model
model_relu.compile(optimizer='rmsprop', 
              loss='categorical_crossentropy', # this is different instead of binary_crossentropy (for regular classification)
              metrics=['accuracy'])
# early stopping callback
# This callback will stop the training when there is no improvement in  
# the validation loss for 10 consecutive epochs.  
es = keras.callbacks.EarlyStopping(monitor='val_loss', 
                                   mode='min',
                                   patience=10, 
                                   restore_best_weights=True) # important - otherwise you just return the last weigths...

# now we just update our model fit call
history = model_relu.fit(X,
                    dummy_y,
                    callbacks=[es],
                    epochs=8000000, # you can set this to a big number!
                    batch_size=10000,
                    shuffle=True,
                    validation_split=0.2,
                    verbose=1)
preds = model_relu.predict(X) # see how the model did!
print(preds[0]) # i'm spreading that prediction across nodes and they sum to 1
print(np.sum(preds[0])) # sum it up! Should be 1
# confusion matrix
matrix = confusion_matrix(dummy_y.argmax(axis=1), preds.argmax(axis=1))
matrix

現在我的混淆矩陣結果顯示......

array([[ 32141,      0,      0,      0, 114766],
       [  3500,      0,      0,      0,  19625],
       [  2541,      0,      0,      0,  27617],
       [  1657,      0,      0,      0,  42156],
       [ 10333,      0,      0,      0, 724463]])

和model評價顯示...

              precision    recall  f1-score   support

           0       0.64      0.22      0.33    146907
           1       0.00      0.00      0.00     23125
           2       0.00      0.00      0.00     30158
           3       0.00      0.00      0.00     43813
           4       0.78      0.99      0.87    734796

    accuracy                           0.77    978799
   macro avg       0.28      0.24      0.24    978799
weighted avg       0.68      0.77      0.70    978799

我的代碼有什么錯誤嗎? 因為它只預測 1 和 5 的分數(不預測 2、3、4)。 或者您對其他可能更好的算法有什么建議嗎?

讓我們想象一下您的 model。

import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

model = Sequential()
model.add(Dense(1, input_shape=(6,), activation='relu'))
model.add(Dense(10, activation='softmax'))

tf.keras.utils.plot_model(model, show_shapes=True, show_layer_names=False, to_file='model.png')

結果:

在此處輸入圖像描述

Your.network 有兩層:一個有 10 個單元的 output 層,然后是一個有 1 個隱藏單元的隱藏層。 顯然,隱藏層是一個嚴重的瓶頸,會導致問題——根據您的描述,它甚至不應該存在。

但是,您訓練 a.network 並將權重用於其他事物的方法是否是一個好方法是另一個問題(請注意,如果您只訓練一個 output 層——即邏輯回歸 model——你將得到一個權重形狀為 (input_size, num_classes) 的矩陣,而不是形狀為 (input_size,) 的向量,就像你說的那樣)。 此外,這樣一個簡單的 model 可能根本不足以獲得良好的准確性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM