TensorFlow.org에서 보기 | Google Colab에서 실행하기 | GitHub에서 소스 보기 | 노트북 다운로드하기 |
개요
이 노트북은 TensorFlow 애드온에서 TQDMCallback을 사용하는 방법을 보여줍니다.
설정
pip install -U tensorflow-addons
!pip install -q "tqdm>=4.36.1"
import tensorflow as tf
import tensorflow_addons as tfa
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
import tqdm
# quietly deep-reload tqdm
import sys
from IPython.lib import deepreload
stdout = sys.stdout
sys.stdout = open('junk','w')
deepreload.reload(tqdm)
sys.stdout = stdout
tqdm.__version__
'4.51.0'
데이터 가져오기 및 정규화
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# normalize data
x_train, x_test = x_train / 255.0, x_test / 255.0
간단한 MNIST CNN 모델 빌드하기
# build the model using the Sequential API
model = Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))
model.compile(optimizer='adam',
loss = 'sparse_categorical_crossentropy',
metrics=['accuracy'])
기본 TQDMCallback 사용법
# initialize tqdm callback with default parameters
tqdm_callback = tfa.callbacks.TQDMProgressBar()
# train the model with tqdm_callback
# make sure to set verbose = 0 to disable
# the default progress bar.
model.fit(x_train, y_train,
batch_size=64,
epochs=10,
verbose=0,
callbacks=[tqdm_callback],
validation_data=(x_test, y_test))
HBox(children=(HTML(value='Training'), FloatProgress(value=0.0, layout=Layout(flex='2'), max=10.0), HTML(value… Epoch 1/10 HBox(children=(HTML(value=''), FloatProgress(value=0.0, layout=Layout(flex='2'), max=938.0), HTML(value='')), … Epoch 2/10 HBox(children=(HTML(value=''), FloatProgress(value=0.0, layout=Layout(flex='2'), max=938.0), HTML(value='')), … Epoch 3/10 HBox(children=(HTML(value=''), FloatProgress(value=0.0, layout=Layout(flex='2'), max=938.0), HTML(value='')), … Epoch 4/10 HBox(children=(HTML(value=''), FloatProgress(value=0.0, layout=Layout(flex='2'), max=938.0), HTML(value='')), … Epoch 5/10 HBox(children=(HTML(value=''), FloatProgress(value=0.0, layout=Layout(flex='2'), max=938.0), HTML(value='')), … Epoch 6/10 HBox(children=(HTML(value=''), FloatProgress(value=0.0, layout=Layout(flex='2'), max=938.0), HTML(value='')), … Epoch 7/10 HBox(children=(HTML(value=''), FloatProgress(value=0.0, layout=Layout(flex='2'), max=938.0), HTML(value='')), … Epoch 8/10 HBox(children=(HTML(value=''), FloatProgress(value=0.0, layout=Layout(flex='2'), max=938.0), HTML(value='')), … Epoch 9/10 HBox(children=(HTML(value=''), FloatProgress(value=0.0, layout=Layout(flex='2'), max=938.0), HTML(value='')), … Epoch 10/10 HBox(children=(HTML(value=''), FloatProgress(value=0.0, layout=Layout(flex='2'), max=938.0), HTML(value='')), … <tensorflow.python.keras.callbacks.History at 0x7f5f02d57208>
아래는 위의 셀을 실행할 때 예상되는 출력입니다.
# TQDMProgressBar() also works with evaluate()
model.evaluate(x_test, y_test, batch_size=64, callbacks=[tqdm_callback], verbose=0)
HBox(children=(HTML(value='Evaluating'), FloatProgress(value=0.0, layout=Layout(flex='2'), max=157.0), HTML(va… [0.07110489159822464, 0.9794999957084656]
아래는 위의 셀을 실행할 때 예상되는 출력입니다.