简体   繁体   English

Keras CNN model 精度没有随着时间的推移而提高和降低?

[英]Keras CNN model accuracy not improving and decreasing over epoch?

Newbie to machine learning here.机器学习的新手在这里。 I'm currently working on a diagnostic machine learning framework using 3D-CNNs on fMRI imaging.我目前正在研究使用 3D-CNN 进行 fMRI 成像的诊断机器学习框架。 My dataset consists of 636 images right now, and I'm trying to distinguish between control and affected (binary classification).我的数据集现在包含 636 张图像,我正在尝试区分控制和受影响(二进制分类)。 However, when I tried to train my model, after every epoch, my accuracy remains at 48.13%, no matter what I do.但是,当我尝试训练我的 model 时,在每个 epoch 之后,无论我做什么,我的准确率都保持在 48.13%。 Additionally, over the epoch, the accuracy decreases from 56% to 48.13%.此外,在整个 epoch 中,准确率从 56% 下降到 48.13%。 So far, I have tried:到目前为止,我已经尝试过:

  • changing my loss functions (poisson, categorical cross entropy, binary cross entropy, sparse categorical cross entropy, mean squared error, mean absolute error, hinge, hinge squared)改变我的损失函数(泊松、分类交叉熵、二元交叉熵、稀疏分类交叉熵、均方误差、平均绝对误差、铰链、铰链平方)
  • changing my optimizer (I've tried Adam and SGD)改变我的优化器(我试过 Adam 和 SGD)
  • changing the number of layers改变层数
  • using weight regularization使用权重正则化
  • changing from ReLU to leaky ReLU (I thought perhaps that could help if this was a case of overfitting)从 ReLU 更改为leaky ReLU(我认为如果这是过度拟合的情况,这可能会有所帮助)

Nothing has worked so far.到目前为止没有任何效果。

Any tips?有小费吗? Here's my code:这是我的代码:

#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)

The main issue is that you are using softmax activation with 1 neuron.主要问题是您正在使用带有 1 个神经元的softmax激活。 Change it to sigmoid with binary_crossentropy as a loss function.将其更改为sigmoid ,使用binary_crossentropy作为损失 function。

At the same time, bear in mind that you are using Poisson loss function, which is suitable for regression problems not classification ones.同时,请记住,您使用的是Poisson损失 function,它适用于回归问题而不是分类问题。 Ensure that you detect the exact scenario that your are trying to solve.确保您检测到您正在尝试解决的确切方案。

Softmax with one neuron makes the model illogical and only use one of the sigmoid activation functions or Softmax in the last layer具有一个神经元的 Softmax 使 model 不合逻辑,并且在最后一层仅使用一个 sigmoid 激活函数或 Softmax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM