Created
December 11, 2017 18:08
-
-
Save lucasdavid/5c3553c9322ba574874555e09bb34e19 to your computer and use it in GitHub Desktop.
Demonstrates correct behavior of TensorBoard callback on model re-training.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from keras import Input | |
from keras.layers import Dense | |
from keras.models import Model | |
from keras import callbacks | |
from sklearn.datasets import load_digits | |
from sklearn.model_selection import train_test_split | |
def build_model(shape, name=None): | |
x = Input(shape) | |
y = Dense(128, activation='relu')(x) | |
model = Model(inputs=x, outputs=y, name=name) | |
model.compile('adam', 'sparse_categorical_crossentropy') | |
return model | |
x, y = load_digits(return_X_y=True) | |
x, x_test, y, y_test = train_test_split(x, y) | |
samples, features = x.shape | |
models = [build_model([features], name=name) for name in ('alpha', 'beta')] | |
for model in models: | |
# Training "fresh" models. | |
model.fit(x, y, | |
epochs=2, | |
batch_size=None, | |
validation_data=(x_test, y_test), | |
callbacks=[callbacks.TensorBoard('./tf-log/' + model.name)]) | |
for model in models: | |
# Training "trained" models. | |
model.fit(x, y, | |
epochs=4, | |
batch_size=None, | |
validation_data=(x_test, y_test), | |
callbacks=[callbacks.TensorBoard('./tf-log/' + model.name)], | |
initial_epoch=2) | |
for model in models: | |
# Training "fresh, but trained" models. | |
model = Model(model.inputs, model.outputs, name=model.name) | |
model.compile('adam', 'sparse_categorical_crossentropy') | |
model.fit(x, y, | |
epochs=6, | |
batch_size=None, | |
validation_data=(x_test, y_test), | |
callbacks=[callbacks.TensorBoard('./tf-log/' + model.name)], | |
initial_epoch=4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment