【问题标题】:Plot model loss and model accuracy from history.history Keras sequential从 history.history Keras 序列中绘制模型损失和模型准确性
【发布时间】:2018-01-03 11:13:03
【问题描述】:

在使用 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()

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

【问题讨论】:

  • 我们能看到您遇到的错误吗?关于val_acc 的事?
  • 当然。 plt.plot(history.history['val_acc']) KeyError: 'val_acc'。如果我删除 plt.plot(history.history['val_acc']) 行,它会返回每个交叉验证数据集(火车)的图。

标签: python matplotlib plot machine-learning keras


【解决方案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'

希望对你有帮助!

【讨论】:

  • 这将是一个解决方案,但我正在使用交叉验证。但我只是尝试使用:history = model.fit(X_train, Y_train, epochs=42, batch_size=50, validation_data=(X_test,Y_test), verbose=0),这很有效。
猜你喜欢
  • 1970-01-01
  • 2017-07-16
  • 2019-10-23
  • 1970-01-01
  • 2019-03-07
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多