简体   繁体   English

Keras - 绘制训练、验证和测试集准确性

[英]Keras - Plot training, validation and test set accuracy

I want to plot the output of this simple neural network:我想绘制这个简单神经网络的输出:

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(x_test, y_test, nb_epoch=10, validation_split=0.2, shuffle=True)

model.test_on_batch(x_test, y_test)
model.metrics_names

I have plotted accuracy and loss of training and validation:我绘制了训练和验证的准确性损失

print(history.history.keys())
#  "Accuracy"
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()
# "Loss"
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'validation'], loc='upper left')
plt.show()

Now I want to add and plot test set's accuracy from model.test_on_batch(x_test, y_test) , but from model.metrics_names I obtain the same value 'acc' utilized for plotting accuracy on training data plt.plot(history.history['acc']) .现在我想从model.test_on_batch(x_test, y_test)添加和绘制测试集的准确性,但是从model.metrics_names我获得相同的值'acc'用于绘制训练数据的准确性plt.plot(history.history['acc']) How could I plot test set's accuracy?我如何绘制测试集的准确性?

import keras
from matplotlib import pyplot as plt
history = model1.fit(train_x, train_y,validation_split = 0.1, epochs=50, batch_size=4)
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc'])
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()

模型精度

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'val'], loc='upper left')
plt.show()

模型损失

It is the same because you are training on the test set, not on the train set.这是一样的,因为你是在测试集上训练,而不是在训练集上。 Don't do that, just train on the training set:不要那样做,只需在训练集上训练:

history = model.fit(x_test, y_test, nb_epoch=10, validation_split=0.2, shuffle=True)

Change into:变成:

history = model.fit(x_train, y_train, nb_epoch=10, validation_split=0.2, shuffle=True)

Try试试

pd.DataFrame(history.history).plot(figsize=(8,5))
plt.show()

This builds a graph with the available metrics of the history for all datasets of the history.这将构建一个图表,其中包含所有历史数据集的可用历史指标。 Example:例子:

在此处输入图片说明

Validate the model on the test data as shown below and then plot the accuracy and loss在测试数据上验证模型,如下所示,然后绘制精度和损失

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(X_train, y_train, nb_epoch=10, validation_data=(X_test, y_test), shuffle=True)

You could do it this way also....你也可以这样做....

regressor.compile(optimizer = 'adam', loss = 'mean_squared_error',metrics=['accuracy'])
earlyStopCallBack = EarlyStopping(monitor='loss', patience=3)
history=regressor.fit(X_train, y_train, validation_data=(X_test, y_test), epochs = EPOCHS, batch_size = BATCHSIZE, callbacks=[earlyStopCallBack])

For the plotting - I like plotly... so对于策划 - 我喜欢策划......所以

import plotly.graph_objects as go
from plotly.subplots import make_subplots

# Create figure with secondary y-axis
fig = make_subplots(specs=[[{"secondary_y": True}]])

# Add traces
fig.add_trace(
    go.Scatter( y=history.history['val_loss'], name="val_loss"),
    secondary_y=False,
)

fig.add_trace(
    go.Scatter( y=history.history['loss'], name="loss"),
    secondary_y=False,
)

fig.add_trace(
    go.Scatter( y=history.history['val_accuracy'], name="val accuracy"),
    secondary_y=True,
)

fig.add_trace(
    go.Scatter( y=history.history['accuracy'], name="val accuracy"),
    secondary_y=True,
)

# Add figure title
fig.update_layout(
    title_text="Loss/Accuracy of LSTM Model"
)

# Set x-axis title
fig.update_xaxes(title_text="Epoch")

# Set y-axes titles
fig.update_yaxes(title_text="<b>primary</b> Loss", secondary_y=False)
fig.update_yaxes(title_text="<b>secondary</b> Accuracy", secondary_y=True)

fig.show()

在此处输入图像描述

Nothing wrong with either of the proceeding methods.两种处理方法都没有错。 Please note the Plotly graph has two scales, 1 for loss the other for accuracy.请注意,Plotly 图有两个尺度,一个用于损失,另一个用于准确性。

Use accuracy and val_accuracy while plotting chart绘制图表时使用accuracyval_accuracy


Plotting accuracy in : 中的绘图accuracy

plt.plot(model.history.history["accuracy"], label="training accuracy")
plt.plot(model.history.history["val_accuracy"], label="validation accuracy")
plt.legend()
plt.show()

精度图

Plotting loss in :中绘制loss

plt.plot(model.history.history["loss"], label="training loss")
plt.plot(model.history.history["val_loss"], label="validation loss")
plt.legend()
plt.show()

损失图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM