簡體   English   中英

Keras model 總是預測 0

[英]Keras model always predicting 0

所以我有類似(10000, 178, 178, 3)形狀的數據,其中我有 10000 個樣本,每個都有 3 個不同的顏色通道(不是 RGB 通道),其中我有大約8k個樣本,其中 label 0和 Z65E8800B5C6800AAD896F888B2AkA62 為label 1 這是我的示例數據之一:

array([[[[1.79844797e-01, 1.73587397e-01, 1.73587397e-01, ...,
          4.84393053e-02, 5.15680127e-02, 5.46967126e-02],
         [1.76716089e-01, 1.79844797e-01, 1.82973504e-01, ...,
          5.15680127e-02, 5.31323589e-02, 5.15680127e-02],
         [1.81409150e-01, 1.86102197e-01, 1.81409150e-01, ...,
          5.15680127e-02, 5.31323589e-02, 5.15680127e-02]]],


       [[[2.51065755e+00, 2.53197193e+00, 2.53197193e+00, ...,
          1.88543844e+00, 1.89964795e+00, 1.90675282e+00],
         [2.51776242e+00, 2.52486706e+00, 2.53197193e+00, ...,
          1.89964795e+00, 1.90675282e+00, 1.90675282e+00],
         [2.53197193e+00, 2.51776242e+00, 2.52486706e+00, ...,
          1.91385746e+00, 1.90675282e+00, 1.90675282e+00]]],


       [[[7.13270283e+00, 7.11016369e+00, 7.13270283e+00, ...,
          4.85625362e+00, 4.90133190e+00, 4.94641018e+00],
         [7.08762503e+00, 7.08762503e+00, 7.08762503e+00, ...,
          4.92387104e+00, 4.96894932e+00, 4.96894932e+00],
         [7.08762503e+00, 7.08762503e+00, 7.06508589e+00, ...,
          4.99148846e+00, 4.96894932e+00, 4.96894932e+00]]],
      dtype=float32)

現在首先我試圖通過顏色通道進行標准化。 由於每個顏色通道完全不同,所以我按如下方式按顏色通道進行歸一化, dara_array是我的整個數據集:

def nan(index):
    data_array[:, :, :, index] = (data_array[:, :, :, index] - np.min(data_array[:, :, :, index]))/(np.max(data_array[:, :, :, index]) - np.min(data_array[:, :, : ,index]))
    

拆分訓練和測試:

rand_indices = np.random.permutation(len(data))
train_indices = rand_indices[0:7460]
valid_indices = rand_indices[7460:len(data)]

x_test = data_array[valid_indices, :]
y_test = EDR[[valid_indices]].astype('float')

x_train = data_array[train_indices, :]
y_train = EDR[[train_indices]].astype('float')

然后我使用這個神經網絡來訓練這個數據集:

weight_decay = 1e-4
model = Sequential()
model.add(Conv2D(32, (20,20), padding='same', kernel_regularizer=regularizers.l2(weight_decay), input_shape=x_tr.shape[1:]))
model.add(LeakyReLU(alpha=0.01))
model.add(BatchNormalization())
model.add(Conv2D(32, (30,30), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
model.add(LeakyReLU(alpha=0.01))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Dropout(0.2))

model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))

model.summary()

然后我在這里訓練它:

def lr_schedule(epoch):
    lrate = 0.001
    if epoch > 75:
        lrate = 0.0005
    elif epoch > 100:
        lrate = 0.0003        
    return lrate

batch_size = 128

opt_rms = tf.keras.optimizers.Adam()

model.compile(loss= 'binary_crossentropy', optimizer = opt_rms, metrics=['accuracy', tf.keras.metrics.Precision(), tf.keras.metrics.Recall()])

history = model.fit(x_train, y_train, batch_size, epochs=10, verbose=1,
                   callbacks=[LearningRateScheduler(lr_schedule)])

這是我所有時代的結果:

Epoch 1/10
59/59 [==============================] - 420s 7s/step - loss: 6.7227 - accuracy: 0.7263 - precision_2: 0.2697 - recall_2: 0.2846 - lr: 0.0010
Epoch 2/10
59/59 [==============================] - 399s 7s/step - loss: 2.7919 - accuracy: 0.7440 - precision_2: 0.3027 - recall_2: 0.2991 - lr: 0.0010
Epoch 3/10
59/59 [==============================] - 399s 7s/step - loss: 2.9244 - accuracy: 0.7484 - precision_2: 0.3210 - recall_2: 0.3282 - lr: 0.0010
Epoch 4/10
59/59 [==============================] - 399s 7s/step - loss: 3.5013 - accuracy: 0.7509 - precision_2: 0.3246 - recall_2: 0.3261 - lr: 0.0010
Epoch 5/10
59/59 [==============================] - 398s 7s/step - loss: 3.1829 - accuracy: 0.7413 - precision_2: 0.3137 - recall_2: 0.3406 - lr: 0.0010
Epoch 6/10
59/59 [==============================] - 398s 7s/step - loss: 4.9515 - accuracy: 0.7592 - precision_2: 0.3307 - recall_2: 0.2999 - lr: 0.0010
Epoch 7/10
59/59 [==============================] - 398s 7s/step - loss: 2.3082 - accuracy: 0.7613 - precision_2: 0.3539 - recall_2: 0.3588 - lr: 0.0010
Epoch 8/10
59/59 [==============================] - 399s 7s/step - loss: 1.8624 - accuracy: 0.7520 - precision_2: 0.3273 - recall_2: 0.3282 - lr: 0.0010
Epoch 9/10
59/59 [==============================] - 398s 7s/step - loss: 2.7749 - accuracy: 0.7579 - precision_2: 0.3344 - recall_2: 0.3173 - lr: 0.0010
Epoch 10/10
59/59 [==============================] - 399s 7s/step - loss: 2.5800 - accuracy: 0.7513 - precision_2: 0.3288 - recall_2: 0.3362 - lr: 0.0010

現在,當我打印分類報告時,一切都將變為0

y_pred = model.predict(x_test)
y_pred_bool = np.argmax(y_pred, axis=1)

print(classification_report(y_test, y_pred_bool))

Output:

    precision    recall  f1-score   support

         0.0       0.82      1.00      0.90      2030
         1.0       0.00      0.00      0.00       453

    accuracy                           0.82      2483
   macro avg       0.41      0.50      0.45      2483
weighted avg       0.67      0.82      0.74      2483

有人可以告訴我我做錯了什么或我錯過了什么,我在規范化數據或訓練時做錯了什么,或者我的 model 有什么問題嗎?

這是我的數據中的一張示例圖片:

在此處輸入圖像描述

您在單個 output 上使用 argmax,因此它只會預測 class 0。您可以通過分析 y_pred 變量或通過切換 ZA2F2ED4F8EBC2CBB4C21A29DC40 標簽並看到 it1DZ6 標簽來看到這種情況。

要解決此問題,請使用閾值而不是 argmax 或更改 model 以預測每個 class 1 output。 有一些方法可以優化這一點,但對於初始測試,您可以使用 0.5 左右的閾值,如下所示:

T=0.5
y_pred = model.predict(x_test)
y_pred_bool = y_pred>=T

print(classification_report(y_test, y_pred_bool))

暫無
暫無

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

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