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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
from __future__ import division
from tqdm import tqdm
from tests_tqdm import with_setup, pretest, posttest, SkipTest, StringIO, \
closing
@with_setup(pretest, posttest)
def test_keras():
"""Test tqdm.keras.TqdmCallback"""
try:
from tqdm.keras import TqdmCallback
import numpy as np
try:
import keras as K
except ImportError:
from tensorflow import keras as K
except ImportError:
raise SkipTest
# 1D autoencoder
dtype = np.float32
model = K.models.Sequential(
[K.layers.InputLayer((1, 1), dtype=dtype), K.layers.Conv1D(1, 1)]
)
model.compile("adam", "mse")
x = np.random.rand(100, 1, 1).astype(dtype)
batch_size = 10
batches = len(x) / batch_size
epochs = 5
with closing(StringIO()) as our_file:
class Tqdm(tqdm):
"""redirected I/O class"""
def __init__(self, *a, **k):
k.setdefault("file", our_file)
super(Tqdm, self).__init__(*a, **k)
# just epoch (no batch) progress
model.fit(
x,
x,
epochs=epochs,
batch_size=batch_size,
verbose=False,
callbacks=[
TqdmCallback(
epochs,
data_size=len(x),
batch_size=batch_size,
verbose=0,
tqdm_class=Tqdm,
)
],
)
res = our_file.getvalue()
assert "{epochs}/{epochs}".format(epochs=epochs) in res
assert "{batches}/{batches}".format(batches=batches) not in res
# full (epoch and batch) progress
our_file.seek(0)
our_file.truncate()
model.fit(
x,
x,
epochs=epochs,
batch_size=batch_size,
verbose=False,
callbacks=[
TqdmCallback(
epochs,
data_size=len(x),
batch_size=batch_size,
verbose=2,
tqdm_class=Tqdm,
)
],
)
res = our_file.getvalue()
assert "{epochs}/{epochs}".format(epochs=epochs) in res
assert "{batches}/{batches}".format(batches=batches) in res
# auto-detect epochs and batches
our_file.seek(0)
our_file.truncate()
model.fit(
x,
x,
epochs=epochs,
batch_size=batch_size,
verbose=False,
callbacks=[TqdmCallback(verbose=2, tqdm_class=Tqdm)],
)
res = our_file.getvalue()
assert "{epochs}/{epochs}".format(epochs=epochs) in res
assert "{batches}/{batches}".format(batches=batches) in res
|