2017-07-27 97 views
0

在keras序列模型中绘制模型损失和模型准确性似乎很简单。但是如果我们将数据拆分为X_train,Y_train, X_test, Y_test,并且使用交叉验证,他们又怎么能够绘制?我收到错误,因为它找不到'val_acc'。这意味着我不能在测试集上绘制结果。从history.history绘制模型损失和模型准确性Keras sequential

这里是我的代码:

# 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() 

我将不胜感激它的一些必要的修改,以获得这些地块也为测试。

+0

我们可以看到您收到的错误吗?关于'val_acc'的一些事情? – cosinepenguin

+0

当然。 plt.plot(history.history ['val_acc']) KeyError:'val_acc'。如果我删除线条plt.plot(history.history ['val_acc']),它会返回每个交叉验证数据集(火车)的图表。 –

回答

0

根据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'

希望它能帮助!

+0

这将是一个解决方案,但我正在使用交叉验证。但我试图使用:'history = model.fit(X_train,Y_train,epochs = 42,batch_size = 50,validation_data =(X_test,Y_test),verbose = 0)'这个工作。 –

+0

太棒了!很高兴你找到了一个可行的解决方案! – cosinepenguin