在Keras中分批训练期间显示每个时期的进度条 [英] Show progress bar for each epoch during batchwise training in Keras
问题描述
当我将整个数据集加载到内存中并使用以下代码在Keras中训练网络时:
When I load the whole dataset in memory and train the network in Keras using following code:
model.fit(X, y, nb_epoch=40, batch_size=32, validation_split=0.2, verbose=1)
这会在每个时期生成带有ETA,准确性,损失等指标的进度条
This generates a progress bar per epoch with metrics like ETA, accuracy, loss, etc
当我分批训练网络时,我正在使用以下代码
When I train the network in batches, I'm using the following code
for e in range(40):
for X, y in data.next_batch():
model.fit(X, y, nb_epoch=1, batch_size=data.batch_size, verbose=1)
这将为每个批次而不是每个时期生成一个进度条.在分批训练期间是否可以为每个时期生成进度条?
This will generate a progress bar for each batch instead of each epoch. Is it possible to generate a progress bar for each epoch during batchwise training?
推荐答案
1.
model.fit(X, y, nb_epoch=40, batch_size=32, validation_split=0.2, verbose=1)
在上述对verbose=2
的更改中,如文档中所述:详细:0表示不记录到stdout,1表示进度条记录,2 for one log line per epoch
."
In the above change to verbose=2
, as it is mentioned in the documentation: "verbose: 0 for no logging to stdout, 1 for progress bar logging, 2 for one log line per epoch
."
它将显示您的输出为:
Epoch 1/100
0s - loss: 0.2506 - acc: 0.5750 - val_loss: 0.2501 - val_acc: 0.3750
Epoch 2/100
0s - loss: 0.2487 - acc: 0.6250 - val_loss: 0.2498 - val_acc: 0.6250
Epoch 3/100
0s - loss: 0.2495 - acc: 0.5750 - val_loss: 0.2496 - val_acc: 0.6250
.....
.....
2.
如果您想显示进度条以完成纪元,请保留verbose=0
(关闭记录到stdout的日志)并以以下方式实现:
If you want to show a progress bar for completion of epochs, keep verbose=0
(which shuts out logging to stdout) and implement in the following manner:
from time import sleep
import sys
epochs = 10
for e in range(epochs):
sys.stdout.write('\r')
for X, y in data.next_batch():
model.fit(X, y, nb_epoch=1, batch_size=data.batch_size, verbose=0)
# print loss and accuracy
# the exact output you're looking for:
sys.stdout.write("[%-60s] %d%%" % ('='*(60*(e+1)/10), (100*(e+1)/10)))
sys.stdout.flush()
sys.stdout.write(", epoch %d"% (e+1))
sys.stdout.flush()
输出如下:
[============================================== =============] 100%,时代10
[============================================================] 100%, epoch 10
3.
如果要每n个批次显示一次损失,可以使用:
If you want to show loss after every n batches, you can use:
out_batch = NBatchLogger(display=1000)
model.fit([X_train_aux,X_train_main],Y_train,batch_size=128,callbacks=[out_batch])
不过,我以前从未尝试过.上面的示例摘自该keras github问题:每N批显示损失#2850
Though, I haven't ever tried it before. The above example was taken from this keras github issue: Show Loss Every N Batches #2850
您还可以在此处关注NBatchLogger
的演示:
You can also follow a demo of NBatchLogger
here:
class NBatchLogger(Callback):
def __init__(self, display):
self.seen = 0
self.display = display
def on_batch_end(self, batch, logs={}):
self.seen += logs.get('size', 0)
if self.seen % self.display == 0:
metrics_log = ''
for k in self.params['metrics']:
if k in logs:
val = logs[k]
if abs(val) > 1e-3:
metrics_log += ' - %s: %.4f' % (k, val)
else:
metrics_log += ' - %s: %.4e' % (k, val)
print('{}/{} ... {}'.format(self.seen,
self.params['samples'],
metrics_log))
4.
您也可以使用progbar
获取进度,但是它将分批打印进度
You can also use progbar
for progress, but it'll print progress batchwise
from keras.utils import generic_utils
progbar = generic_utils.Progbar(X_train.shape[0])
for X_batch, Y_batch in datagen.flow(X_train, Y_train):
loss, acc = model_test.train([X_batch]*2, Y_batch, accuracy=True)
progbar.add(X_batch.shape[0], values=[("train loss", loss), ("acc", acc)])
这篇关于在Keras中分批训练期间显示每个时期的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!