简体   繁体   English

在训练期间如何在每个 epoch 结束时调用测试集? 我正在使用张量流

[英]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.

相关问题 tensorflow-keras 如何计算每个 epoch 的训练成本? - How does tensorflow-keras calculate the cost during training in each epoch? 如何使用文件列表作为 Sagemaker 上的训练集和 Tensorflow? - How can I use a list of files as the training set on Sagemaker with Tensorflow? 使用 tensorflow,我如何找到拟合期间一个时期所花费的时间? - Using tensorflow, how do I find the time taken for an epoch during fitting? 如何在 tensorflow 1.x 的每个训练时期保持模型的输出? - How to hold the output of a model at each training epoch in tensorflow 1.x? 如何在训练期间修改每个时期的损失函数内的变量? - How to modify a variable inside the loss function in each epoch during training? 如何在不使用和拆分测试集的情况下将我的数据集拆分为训练和验证? - How can i split my dataset into training and validation with no using and spliting test set? 如何停止 tensorflow 中的培训工作? - How can I stop a training job in tensorflow? TensorFlow:如何在培训期间多次评估验证数据队列? - TensorFlow: How can I evaluate a validation data queue multiple times during training? 如何快速检查训练期间哪些张量流变量已更新以及哪些已冻结? - How can I quickly check which tensorflow variables are updated during training and which are frozen? 如何在训练/评估期间访问Tensorflow中标签的值? - How do I access the value of a label in Tensorflow during training/eval?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM