gpt4 book ai didi

python - 从history.history Keras顺序绘制模型损失和模型准确性

转载 作者:行者123 更新时间:2023-11-30 08:59:41 25 4
gpt4 key购买 nike

使用 keras 绘制序列模型中的模型损失和模型精度似乎很简单。但是,如果我们将数据分成 X_trainY_trainX_testY_test 并使用,如何绘制它们交叉验证?我收到错误是因为它找不到 'val_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()

我希望对其进行一些必要的更改,以便获得这些图来进行测试。

最佳答案

根据Keras.io documentation ,似乎为了能够使用 'val_acc''val_loss' 您需要启用验证和准确性监控。这样做就像将validation_split添加到代码中的model.fit一样简单!

而不是:

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 期间。

这是一个额外的可能有用的来源:

Plotting learning curve in keras gives KeyError: 'val_acc'

希望对你有帮助!

关于python - 从history.history Keras顺序绘制模型损失和模型准确性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45355603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com