forked from JustGlowing/minisom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
45 lines (42 loc) · 1.72 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import numpy as np
import sys
from time import time
from datetime import timedelta
def _wrap_index__in_verbose(iterations):
"""Yields the values in iterations printing the status on the stdout."""
m = len(iterations)
digits = len(str(m))
progress = '\r [ {s:{d}} / {m} ] {s:3.0f}% - ? it/s'
progress = progress.format(m=m, d=digits, s=0)
sys.stdout.write(progress)
beginning = time()
sys.stdout.write(progress)
for i, it in enumerate(iterations):
yield it
sec_left = ((m-i+1) * (time() - beginning)) / (i+1)
time_left = str(timedelta(seconds=sec_left))[:7]
progress = '\r [ {i:{d}} / {m} ]'.format(i=i+1, d=digits, m=m)
progress += ' {p:3.0f}%'.format(p=100*(i+1)/m)
progress += ' - {time_left} left '.format(time_left=time_left)
sys.stdout.write(progress)
def _build_iteration_indexes(data_len, num_iterations,
verbose=False, random_generator=None,
use_epochs=False):
"""Returns an iterable with the indexes of the samples
to pick at each iteration of the training.
If random_generator is not None, it must be an instance
of numpy.random.RandomState and it will be used
to randomize the order of the samples."""
if use_epochs:
iterations_per_epoch = np.arange(data_len)
if random_generator:
random_generator.shuffle(iterations_per_epoch)
iterations = np.tile(iterations_per_epoch, num_iterations)
else:
iterations = np.arange(num_iterations) % data_len
if random_generator:
random_generator.shuffle(iterations)
if verbose:
return _wrap_index__in_verbose(iterations)
else:
return iterations