本文介绍了测试精度评估回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算神经网络训练期间每个时期后的测试准确率.简单的解决方案是:

I would like to compute the test accuracy after each epoch during the training of a neural network. Simple solution would be:

for i in range(NUM_EPOCHS): 
    model.fit(train_dataset,epochs=1)
    accuracy = model.evaluate(test_dataset)
    test_accuracy_list.append(accuracy)

是否有更多使用回调的模块化解决方案?

Is there any more modular solution by using Callbacks?

推荐答案

你需要的是on_epoch_end

class EvaluateEpochEnd(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
        scores = self.model.evaluate(x, y, verbose=False)
        print('\nTesting loss: {}, accuracy: {}\n'.format(scores[0], scores[1]))

用法:

model.fit(x_train, y_train, epochs=5, callbacks=[EvaluateEpochEnd((x_test, y_test))])

会给:

Epoch 1/5
1875/1875 [==============================] - 6s 3ms/step - loss: 0.4784 - accuracy: 0.8619

Testing loss: 0.14208272099494934, accuracy: 0.9574000239372253

这篇关于测试精度评估回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 06:24