从history.history Keras序列绘制模型损失和模型准确性

时间:2017-07-27 15:52:32

标签: python matplotlib plot machine-learning keras

绘制模型损失和模型准确性在具有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()

我希望对它进行一些必要的修改,以便将这些图也用于测试。

1 个答案:

答案 0 :(得分:1)

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

而不是:

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'

希望它有所帮助!