【问题标题】:Keras model always predicting 0Keras 模型总是预测 0
【发布时间】:2021-04-23 04:24:51
【问题描述】:

所以我有类似(10000, 178, 178, 3) 形状的数据,其中我有 10000 个样本,每个样本都有 3 个不同的颜色通道(不是 RGB 通道),其中我有大约 8k 带有标签 0 的样本,其余 2k作为标签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))

输出:

    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

谁能告诉我我做错了什么或我错过了什么,我在规范化数据或训练时做错了什么,还是我的模型有问题?

这是我数据中的一张示例图片:

【问题讨论】:

  • 尝试运行 100 个 epoch,使用设置学习率调度程序的方式,它始终为 0.001。或者换成tf.keras.optimizers.schedules.ExponentialDecay()会更合适。
  • @yudhiesh 好的,我会试试的,谢谢你让我看到!
  • 加上@yudhiesh,看来你的训练损失有点波动。您可能想尝试不同的参数设置,例如增加批量大小,降低初始学习率,或者使用更复杂的模型。但训练更多时期也是我的第一次尝试。
  • 作为健全性检查,您可以查看 y_pred 以查看少数输出是否为 0 到 0.5 之间的小数,而不仅仅是零。
  • @lpounng 好的,我会试试看,实际上我没有尝试训练更多的 epoch,因为每个 epoch 都在 P2XLarge 实例上采用 420s。但是,是的,我现在就试试,谢谢!

标签: python tensorflow machine-learning keras deep-learning


【解决方案1】:

您在单个输出上使用 argmax,因此它只会预测类别 0。您可以通过分析 y_pred 变量或切换类别标签并看到它会继续趋势来看到这种情况。

要解决此问题,请使用阈值而不是 argmax 或更改您的模型以预测每个类 1 个输出。有办法对此进行优化,但对于初始测试,您可以使用 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))

【讨论】:

  • 好的,我现在就试试,训练模型需要时间,所以我会及时通知你。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-07-29
  • 2019-01-28
  • 1970-01-01
  • 2018-02-22
  • 1970-01-01
  • 2019-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多