【问题标题】:Keras CNN model accuracy not improving and decreasing over epoch?Keras CNN 模型的准确度没有随着时间的推移而提高和降低?
【发布时间】:2020-11-22 12:19:31
【问题描述】:

机器学习新手在这里。 我目前正在研究使用 3D-CNN 进行 fMRI 成像的诊断机器学习框架。我的数据集现在包含 636 张图像,我正在尝试区分控制和受影响(二进制分类)。然而,当我尝试训练我的模型时,在每个 epoch 之后,无论我做什么,我的准确率都保持在 48.13%。此外,在整个 epoch 中,准确率从 56% 下降到 48.13%。 到目前为止,我已经尝试过:

  • 更改我的损失函数(泊松、分类交叉熵、二元交叉熵、稀疏分类交叉熵、均方误差、平均绝对误差、铰链、铰链平方)
  • 更改我的优化器(我尝试过 Adam 和 SGD)
  • 改变层数
  • 使用权重正则化
  • 从 ReLU 更改为leaky ReLU(我认为如果这是过度拟合的情况,这可能会有所帮助)

到目前为止没有任何效果。

有什么建议吗?这是我的代码:

#importing important packages
import tensorflow as tf
import os
import keras
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv3D, MaxPooling3D, Dropout, BatchNormalization, LeakyReLU
import numpy as np
from keras.regularizers import l2
from sklearn.utils import compute_class_weight
from keras.optimizers import SGD

BATCH_SIZE = 64
input_shape=(64, 64, 40, 20)

# Create the model
model = Sequential()

model.add(Conv3D(64, kernel_size=(3,3,3), activation='relu', input_shape=input_shape, kernel_regularizer=l2(0.005), bias_regularizer=l2(0.005), data_format = 'channels_first', padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Conv3D(64, kernel_size=(3,3,3), activation='relu', input_shape=input_shape, kernel_regularizer=l2(0.005), bias_regularizer=l2(0.005), data_format = 'channels_first', padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(BatchNormalization(center=True, scale=True))

model.add(Conv3D(64, kernel_size=(3,3,3), activation='relu', input_shape=input_shape, kernel_regularizer=l2(0.005), bias_regularizer=l2(0.005), data_format = 'channels_first', padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Conv3D(64, kernel_size=(3,3,3), activation='relu', input_shape=input_shape, kernel_regularizer=l2(0.005), bias_regularizer=l2(0.005), data_format = 'channels_first', padding='same'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(BatchNormalization(center=True, scale=True))

model.add(Flatten())
model.add(BatchNormalization(center=True, scale=True))
model.add(Dense(128, activation='relu', kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)))
model.add(Dropout(0.5))
model.add(Dense(128, activation='sigmoid', kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)))
model.add(Dense(1, activation='softmax', kernel_regularizer=l2(0.01), bias_regularizer=l2(0.01)))
 
# Compile the model
model.compile(optimizer = keras.optimizers.sgd(lr=0.000001), loss='poisson', metrics=['accuracy', tf.keras.metrics.Precision(), tf.keras.metrics.Recall()])

# Model Testing 
history = model.fit(X_train, y_train, batch_size=BATCH_SIZE, epochs=50, verbose=1, shuffle=True)

【问题讨论】:

  • 你的学习率太小lr=0.000001。试试lr=0.01lr=0.001。学习率非常低会导致您的模型无法学习。
  • 我只是尝试更改学习率 - 没有任何变化。
  • 带有一个神经元的 Softmax 绝对没有意义,如果你想做二进制分类,你必须在最后一层使用 sigmoid,一个神经元和二进制交叉熵损失。

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


【解决方案1】:

具有一个神经元的 Softmax 使模型不合逻辑,并且仅在最后一层使用 sigmoid 激活函数或 Softmax 之一

【讨论】:

    【解决方案2】:

    主要问题是您正在使用带有 1 个神经元的 softmax 激活。将其更改为sigmoid,并将binary_crossentropy 作为损失函数。

    同时,请记住您使用的是Poisson 损失函数,它适用于回归问题而不是分类问题。确保您检测到您正在尝试解决的确切场景。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2022-10-15
      • 2018-10-09
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 2016-11-06
      相关资源
      最近更新 更多