[英]How can I call a test set at the end of each epoch during the training? I am using tensorflow
I am using Tensorflow-Keras to develop a CNN model in which I have split the data set into train, validation, and test sets.我正在使用 Tensorflow-Keras 开发一个 CNN 模型,其中我将数据集拆分为训练、验证和测试集。 I need to call the test set at the end of each epoch as well as train and validation sets to evaluate the model performance.我需要在每个时期结束时调用测试集以及训练和验证集来评估模型性能。 Below is my code to track train and validation sets.下面是我跟踪训练和验证集的代码。
result_dic = {"epochs": []}
json_logging_callback = LambdaCallback(
on_epoch_begin=lambda epoch, logs: [learning_rate],
on_epoch_end=lambda epoch, logs:
result_dic["epochs"].append({
'epoch': epoch + 1,
'acc': str(logs['acc']),
'val_acc': str(logs['val_acc'])
}))
model.fit(x_train, y_train,
validation_data=(x_val, y_val),
batch_size=batch_size,
epochs=epochs,
callbacks=[json_logging_callback])
output:输出:
Epoch 1/5
1/1 [==============================] - 4s 4s/step - acc: 0.8611 - val_acc: 0.8333
However, I'm not sure how to add the test set to my callback to produce the following output.但是,我不确定如何将测试集添加到我的回调中以产生以下输出。
Expected output:预期输出:
Epoch 1/5
1/1 [==============================] - 4s 4s/step - acc: 0.8611 - val_acc: 0.8333 - test_acc: xxx
To display your test accuracy after each epoch, you could customize your fit
function to display this metric.要在每个 epoch 后显示您的测试准确率,您可以自定义fit
函数以显示此指标。 Check out this documentation or you could, as shown here , define a simple callback for your test dataset and pass it into your fit
function:看看这个文档,或者你可以,如图所示这里,定义了一个简单的回调为您的测试数据集,并把它传递到您的fit
函数:
model.fit(x_train, y_train,
validation_data=(x_val, y_val),
batch_size=batch_size,
epochs=epochs,
callbacks=[json_logging_callback,
your_test_callback((X_test, Y_test))])
If you want complete flexibility, you could try using a training loop .如果你想要完全的灵活性,你可以尝试使用训练循环。
Update: Since you want to have a single JSON for all metrics, you should do the following:更新:由于您希望为所有指标使用单个 JSON,您应该执行以下操作:
Define your TestCallBack
and add your test accuracy (and loss
if you want) to your logs
dictionary:定义您的TestCallBack
并将您的测试准确性(如果需要,还包括loss
)添加到您的logs
字典中:
import tensorflow as tf
class TestCallback(tf.keras.callbacks.Callback):
def __init__(self, test_data):
self.test_data = test_data
def on_epoch_end(self, epoch, logs):
x, y = self.test_data
loss, acc = self.model.evaluate(x, y, verbose=0)
logs['test_accuracy'] = acc
Then add the test accuracy to your results dictionary:然后将测试准确度添加到您的结果字典中:
result_dic = {"epochs": []}
json_logging_callback = tf.keras.callbacks.LambdaCallback(
on_epoch_begin=lambda epoch, logs: [learning_rate],
on_epoch_end=lambda epoch, logs:
result_dic["epochs"].append({
'epoch': epoch + 1,
'acc': str(logs['accuracy']),
'val_acc': str(logs['val_accuracy']),
'test_acc': str(logs['test_accuracy'])
}))
And then just use both callbacks in your fit
function but note the order of the callbacks:然后在您的fit
函数中使用这两个回调,但请注意回调的顺序:
model.fit(x_train, y_train,
validation_data=(x_val, y_val),
batch_size=batch_size,
epochs=epochs,
callbacks=callbacks=[TestCallback((x_test, y_test)), json_logging_callback])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.