Python 根据历史绘制模型损失和模型精度。历史Keras序列
使用keras在序列模型中绘制模型损失和模型精度似乎很简单。然而,如果我们将数据分成Python 根据历史绘制模型损失和模型精度。历史Keras序列,python,matplotlib,plot,machine-learning,keras,Python,Matplotlib,Plot,Machine Learning,Keras,使用keras在序列模型中绘制模型损失和模型精度似乎很简单。然而,如果我们将数据分成X\u序列,Y\u序列,X\u测试,Y\u测试,并使用交叉验证,如何绘制它们?我得到错误是因为它找不到'val\u acc'。这意味着我无法在测试集上绘制结果 这是我的密码: # Create the model def create_model(neurons = 379, init_mode = 'uniform', activation='relu', inputDim = 8040, dropout_ra
X\u序列
,Y\u序列
,X\u测试
,Y\u测试
,并使用交叉验证,如何绘制它们?我得到错误是因为它找不到'val\u acc'
。这意味着我无法在测试集上绘制结果
这是我的密码:
# Create the model
def create_model(neurons = 379, init_mode = 'uniform', activation='relu', inputDim = 8040, dropout_rate=1.1, learn_rate=0.001, momentum=0.7, weight_constraint=6): #weight_constraint=
model = Sequential()
model.add(Dense(neurons, input_dim=inputDim, kernel_initializer=init_mode, activation=activation, kernel_constraint=maxnorm(weight_constraint), kernel_regularizer=regularizers.l2(0.002))) #, activity_regularizer=regularizers.l1(0.0001))) # one inner layer
#model.add(Dense(200, input_dim=inputDim, activation=activation)) # second inner layer
#model.add(Dense(60, input_dim=inputDim, activation=activation)) # second inner layer
model.add(Dropout(dropout_rate))
model.add(Dense(1, activation='sigmoid'))
optimizer = RMSprop(lr=learn_rate)
# compile model
model.compile(loss='binary_crossentropy', optimizer='RmSprop', metrics=['accuracy']) #weight_constraint=weight_constraint
return model
model = create_model() #weight constraint= 3 or 4
seed = 7
# Define k-fold cross validation test harness
kfold = StratifiedKFold(n_splits=3, shuffle=True, random_state=seed)
cvscores = []
for train, test in kfold.split(X_train, Y_train):
print("TRAIN:", train, "VALIDATION:", test)
# Fit the model
history = model.fit(X_train, Y_train, epochs=40, batch_size=50, verbose=0)
# Plot Model Loss and Model accuracy
# list all data in history
print(history.history.keys())
# summarize history for accuracy
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc']) # RAISE ERROR
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
# summarize history for loss
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss']) #RAISE ERROR
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
plt.show()
我希望对其进行一些必要的更改,以获得测试所需的绘图。根据,似乎为了能够使用'val\u acc'
和'val\u loss'
您需要启用验证和准确性监控。这样做很简单,只需将验证分割添加到模型中即可。将放入您的代码中
而不是:
history = model.fit(X_train, Y_train, epochs=40, batch_size=50, verbose=0)
您需要执行以下操作:
history = model.fit(X_train, Y_train, validation_split=0.33, epochs=40, batch_size=50, verbose=0)
这是因为验证通常在列车组的1/3期间进行
以下是另一个可能有用的来源:
希望有帮助 我们能看到你的错误吗?关于val\u acc
?当然。plt.plot(history.history['val_acc'])键错误:“val_acc”。如果我删除plt.plot(history.history['val_acc'])行,它将返回每个cros验证数据集(train)的绘图。这将是一个解决方案,但我使用的是cros验证。但是我试着使用:history=model.fit(X\u-train,Y\u-train,epochs=42,batch\u-size=50,validation\u-data=(X\u-test,Y\u-test),verbose=0)
。