Skip to content

Commit

Permalink
limit progress bar update rate (#2860)
Browse files Browse the repository at this point in the history
* limit progress bar update rate

Limit progress bar update rate in verbose=1 mode. This patch allows to
reduce terminal I/O throughput while keeping reasonable high visual
update rate (defaults to 100 refreshes per second). It helps greatly
when working with large but simple data sets with small batches, which
leads to millions of relatively useless screen updates per second. Also
it helps to keep network traffic at reasonable rates, which
exceptionally useful within laggy networking conditions when using
keras over telnet/ssh, and improve web browser responsibility when
using keras within Jupyter Notebook.

* add docstrings for 'interval' and 'force' arguments
  • Loading branch information
stromnov authored and fchollet committed Jun 2, 2016
1 parent 76cae0e commit 5a71090
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion keras/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def on_epoch_end(self, epoch, logs={}):
if k in logs:
self.log_values.append((k, logs[k]))
if self.verbose:
self.progbar.update(self.seen, self.log_values)
self.progbar.update(self.seen, self.log_values, force=True)


class History(Callback):
Expand Down
13 changes: 11 additions & 2 deletions keras/utils/generic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,28 @@ def make_tuple(*args):


class Progbar(object):
def __init__(self, target, width=30, verbose=1):
def __init__(self, target, width=30, verbose=1, interval=0.01):
'''
@param target: total number of steps expected
@param interval: minimum visual progress update interval (in seconds)
'''
self.width = width
self.target = target
self.sum_values = {}
self.unique_values = []
self.start = time.time()
self.last_update = 0
self.interval = interval
self.total_width = 0
self.seen_so_far = 0
self.verbose = verbose

def update(self, current, values=[]):
def update(self, current, values=[], force=False):
'''
@param current: index of current step
@param values: list of tuples (name, value_for_last_step).
The progress bar will display averages for these values.
@param force: force visual progress update
'''
for k, v in values:
if k not in self.sum_values:
Expand All @@ -64,6 +68,9 @@ def update(self, current, values=[]):

now = time.time()
if self.verbose == 1:
if not force and (now - self.last_update) < self.interval:
return

prev_total_width = self.total_width
sys.stdout.write("\b" * prev_total_width)
sys.stdout.write("\r")
Expand Down Expand Up @@ -127,6 +134,8 @@ def update(self, current, values=[]):
info += ' %.4e' % avg
sys.stdout.write(info + "\n")

self.last_update = now

def add(self, n, values=[]):
self.update(self.seen_so_far + n, values)

Expand Down

0 comments on commit 5a71090

Please sign in to comment.