[英]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:到目前为止,我已经尝试过:
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.