2019-12-20 18:29:03 +00:00
|
|
|
from __future__ import division
|
2020-10-24 20:56:26 +00:00
|
|
|
|
2019-11-09 02:49:16 +00:00
|
|
|
from tqdm import tqdm
|
2020-10-24 20:56:26 +00:00
|
|
|
from .tests_tqdm import pretest_posttest # NOQA
|
|
|
|
from .tests_tqdm import importorskip, StringIO, closing
|
2019-11-09 02:49:16 +00:00
|
|
|
|
|
|
|
|
2020-10-24 18:36:45 +00:00
|
|
|
def test_keras():
|
|
|
|
"""Test tqdm.keras.TqdmCallback"""
|
2020-10-24 18:37:35 +00:00
|
|
|
TqdmCallback = importorskip("tqdm.keras").TqdmCallback
|
|
|
|
np = importorskip("numpy")
|
2020-10-24 18:36:45 +00:00
|
|
|
try:
|
2020-10-24 18:37:35 +00:00
|
|
|
import keras as K
|
|
|
|
except ImportError:
|
|
|
|
K = importorskip("tensorflow.keras")
|
2019-11-09 02:49:16 +00:00
|
|
|
|
2020-10-24 18:36:45 +00:00
|
|
|
# 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
|
2019-11-09 02:49:16 +00:00
|
|
|
|
2020-10-24 18:36:45 +00:00
|
|
|
with closing(StringIO()) as our_file:
|
2019-11-09 02:49:16 +00:00
|
|
|
|
2020-10-24 18:36:45 +00:00
|
|
|
class Tqdm(tqdm):
|
|
|
|
"""redirected I/O class"""
|
2019-11-09 02:49:16 +00:00
|
|
|
|
2020-10-24 18:36:45 +00:00
|
|
|
def __init__(self, *a, **k):
|
|
|
|
k.setdefault("file", our_file)
|
|
|
|
super(Tqdm, self).__init__(*a, **k)
|
2019-11-09 02:49:16 +00:00
|
|
|
|
2020-10-24 18:36:45 +00:00
|
|
|
# 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
|
2019-11-09 02:49:16 +00:00
|
|
|
|
2020-10-24 18:36:45 +00:00
|
|
|
# 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
|
2019-12-19 21:52:48 +00:00
|
|
|
|
2020-10-24 18:36:45 +00:00
|
|
|
# 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
|