我正在训练一个在 3 个类之间进行分类的模型。我每个班级只有 120 张图片,总共有 3 个班级。我已经训练了模型,但是训练和测试精度之间的差异很大。它表明模型是否处于过度拟合或其他状态。
训练和测试数据准确性之间的差异各不相同。所以现在我得到了:
- 训练数据的模型评估 [0.29664946870370346, 0.91636366]
- 对测试数据的模型评估 [0.4278888224678881, 0.8088235]
损失也很大。我应该考虑哪些事情来减少损失并减少训练和测试数据之间的差异最后一件事是我制作了自己的数据集,对于多层感知器来说是否足够。
我将 120 张图像分成训练和测试。100 用于训练,20 用于测试。够了吗?
import time
import numpy as np
import tensorflow as tf
from tensorflow.keras.callbacks import TensorBoard
from sklearn.model_selection import cross_val_score
from keras.wrappers.scikit_learn import KerasClassifier
data = np.load('E:\Python Telusko\OpenCv\desc_feat.npy', allow_pickle=True)
from sklearn.model_selection import train_test_split
NAME="ITA MLP-{}".format(int(time.time()))
tensorboard=TensorBoard(log_dir="E:\\ITA MLP\\logs\\{}".format(NAME))
training_data = np.asarray([i[0] for i in data])
train_labels = data[:, -1]
print("Shape of training data", training_data.shape)
print("Labels of training data", train_labels.shape)
data = training_data.astype('float32')
data = data / 255
from tensorflow.keras import utils as np_utils
from sklearn.model_selection import KFold
one_hot_train_labels = np_utils.to_categorical(train_labels)
def create_model():
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(64, input_shape=(128,) , activation = 'relu'))
model.add(tf.keras.layers.Dense(3, activation = 'softmax'))
model.compile(loss = 'categorical_crossentropy' , optimizer = 'adam' , metrics = ['accuracy'] )
return model
n_split=5
for train_index,test_index in KFold(n_split).split(data):
x_train,x_test=data[train_index],data[test_index]
y_train,y_test=one_hot_train_labels[train_index],one_hot_train_labels[test_index]
model=create_model()
model.fit(x_train, y_train,epochs=30,batch_size=32,callbacks=[tensorboard])
print('Model evaluation on train data ',model.evaluate(x_train,y_train))
print('Model evaluation on test data',model.evaluate(x_test,y_test))
model.save('SuperClassPredictions1.model')