Keras-培训和验证损失

时间:2020-01-18 16:43:19

标签: validation keras neural-network training-data epoch

根据下面链接中的训练和验证损失线图,我的模型是否过度拟合?最佳时期数是多少?以及如何缩小培训与验证损失之间的差距? 下面是我的代码,数据集是6列自变量(整数)和1列因变量(二进制结果,即0或1)。

Training and Validation Loss Plot

from keras.models import Sequential
from keras.layers.core import Dense, Dropout
from numpy import loadtxt

from keras.optimizers import SGD, RMSprop
#from keras.utils import np_utils, generic_utils

*#import theano (*not sure if this is required to plot the validation and #training loss. I've tested the code without this import function, seems #to work)*
#import os *(Same remark as above *)*
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
#import cv2 *(Same remark as above *)*
from sklearn.model_selection import train_test_split
from sklearn.model_selection import cross_validate


# load the dataset
dataset = loadtxt('numbers.csv', delimiter=',')
# split into input (X) and output (y) variables
X = dataset[:,0:6]
y = dataset[:,6]

# define the keras model
model = Sequential()
model.add(Dense(32, input_dim=6, activation='relu'))
model.add(Dropout(0.40))
model.add(Dense(16, activation='sigmoid'))
model.add(Dense(1, activation='sigmoid'))
# compile the keras model
model.compile(loss='binary_crossentropy', optimizer='SGD', metrics=['accuracy'])

# fit the keras model on the dataset
epochs=50
nb_epoch=epochs
batch_size=100
batch_size=batch_size

model.fit(X, y, nb_epoch, batch_size)

# Split the data
X_train_new, X_val_new, y_train_new,y_val_new =  train_test_split(X, y, 
                                                                  test_size=0.2, random_state=4)


# Train the model
hist = model.fit(X_train_new, y_train_new, validation_data=(X_val_new,y_val_new),
          batch_size=batch_size,nb_epoch = nb_epoch, shuffle=True, verbose=2)


# Evaluate the model
score = model.evaluate(X_val_new, y_val_new, batch_size=batch_size)
print('Test score:', score[0])
print('Test accuracy:', score[1])

# Plot the results
train_loss=hist.history['loss']
val_loss=hist.history['val_loss']
train_acc=hist.history['accuracy']
val_acc=hist.history['val_accuracy']
xc=range(50)

plt.figure(1,figsize=(7,5))
plt.plot(xc,train_loss)
plt.plot(xc,val_loss)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
plt.grid(True)
plt.legend(['train','val'])
plt.style.use(['classic'])

plt.figure(2,figsize=(7,5))
plt.plot(xc,train_acc)
plt.plot(xc,val_acc)
plt.xlabel('num of Epochs')
plt.ylabel('accuracy')
plt.title('train_acc vs val_acc')
plt.grid(True)
plt.legend(['train','val'],loc=4)
plt.style.use(['classic'])

0 个答案:

没有答案