diff --git a/tensorboard_profiling_keras.py b/tensorboard_profiling_keras.py new file mode 100755 index 00000000..bdcd4cfb --- /dev/null +++ b/tensorboard_profiling_keras.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python + +#@title Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from datetime import datetime +import os +import tensorflow as tf + +print("TensorFlow version: ", tf.__version__) + +device_name = tf.test.gpu_device_name() +if not device_name: + raise SystemError('GPU device not found') +print('Found GPU at: {}'.format(device_name)) + +import tensorflow_datasets as tfds +tfds.disable_progress_bar() + + +# Limit to a short training time tfor demonstration purposes +Epochs = 2 + + +(ds_train, ds_test), ds_info = tfds.load( + 'mnist', + split=['train', 'test'], + shuffle_files=True, + as_supervised=True, + with_info=True, +) + +def normalize_img(image, label): + """Normalizes images: `uint8` -> `float32`.""" + return tf.cast(image, tf.float32) / 255., label + +ds_train = ds_train.map(normalize_img) +ds_train = ds_train.batch(128) + +ds_test = ds_test.map(normalize_img) +ds_test = ds_test.batch(128) + +model = tf.keras.models.Sequential([ + tf.keras.layers.Flatten(input_shape=(28, 28, 1)), + tf.keras.layers.Dense(128,activation='relu'), + tf.keras.layers.Dense(10, activation='softmax') +]) +model.compile( + loss='sparse_categorical_crossentropy', + optimizer=tf.keras.optimizers.Adam(0.001), + metrics=['accuracy'] +) + +# Create a TensorBoard callback +logs = "logs/" + datetime.now().strftime("%Y%m%d-%H%M%S") + +tboard_callback = tf.keras.callbacks.TensorBoard(log_dir = logs, + histogram_freq = 1, + profile_batch = (1, Epochs)) # Note: 1-indexed + +model.fit(ds_train, + epochs=Epochs, + validation_data=ds_test, + callbacks = [tboard_callback]) + + +(ds_train, ds_test), ds_info = tfds.load( + 'mnist', + split=['train', 'test'], + shuffle_files=True, + as_supervised=True, + with_info=True, +) + +ds_train = ds_train.map(normalize_img) +ds_train = ds_train.batch(128) +ds_train = ds_train.cache() +ds_train = ds_train.prefetch(tf.data.experimental.AUTOTUNE) + +ds_test = ds_test.map(normalize_img) +ds_test = ds_test.batch(128) +ds_test = ds_test.cache() +ds_test = ds_test.prefetch(tf.data.experimental.AUTOTUNE) + +model.fit(ds_train, + epochs=Epochs, + validation_data=ds_test, + callbacks = [tboard_callback]) + +