Keras 中的训练准确率是如何确定每个 epoch 的? [英] How is the training accuracy in Keras determined for every epoch?
问题描述
我正在 Keras 中训练一个模型,如下所示:
I am training a model in Keras with as follows:
model.fit(Xtrn, ytrn batch_size=16, epochs=50, verbose=1, shuffle=True,
callbacks=[model_checkpoint], validation_data=(Xval, yval))
拟合输出如下所示:
如 model.fit
所示,我的批次大小为 16,总共有 8000
个训练样本,如输出所示.因此,根据我的理解,每 16
个批次都会进行训练.这也意味着训练在单个时期内运行 500
次(即,8000/16 =500)
As shown in the model.fit
I have a batch size of 16 and a total of 8000
training samples as shown in the output. So from my understanding, training takes place every 16
batches. Which also means training is ran 500
times for a single epoch (i.e., 8000/16 =500)
因此,让我们采用 Epoch 1/50 输出中打印的训练准确度,在本例中为 0.9381
.我想知道 0.9381
的训练精度是如何得出的.
So let's take the training accuracy printed in the output for Epoch 1/50, which in this case is 0.9381
. I would like to know how is this training accuracy of 0.9381
derived.
是吗:
- mean 训练准确率是
500
次训练的平均值吗?
- Is the mean training accuracy, taken as the average from the
500
times training, performed for every batch?
或,
- 在运行训练过程的
500
个实例中,它是最佳(或 最大)训练准确度吗?
- Is it the best (or max) training accuracy from out of the
500
instances the training procedure is run?
推荐答案
看看 Keras
中的 BaseLogger
,他们在那里计算运行平均值.对于每个 epoch,准确率是该 epoch 中之前看到的所有批次的平均值.
Take a look at the BaseLogger
in Keras
where they're computing a running mean.
For each epoch the accuracy is the average of all the batches seen before in that epoch.
class BaseLogger(Callback):
"""Callback that accumulates epoch averages of metrics.
This callback is automatically applied to every Keras model.
"""
def on_epoch_begin(self, epoch, logs=None):
self.seen = 0
self.totals = {}
def on_batch_end(self, batch, logs=None):
logs = logs or {}
batch_size = logs.get('size', 0)
self.seen += batch_size
for k, v in logs.items():
if k in self.totals:
self.totals[k] += v * batch_size
else:
self.totals[k] = v * batch_size
def on_epoch_end(self, epoch, logs=None):
if logs is not None:
for k in self.params['metrics']:
if k in self.totals:
# Make value available to next callbacks.
logs[k] = self.totals[k] / self.seen
这篇关于Keras 中的训练准确率是如何确定每个 epoch 的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!