|
| 1 | +# Third Party |
| 2 | +import tensorflow as tf |
| 3 | +from tensorflow.keras.layers import Conv2D, Dense, Dropout, Flatten, Input, MaxPooling2D |
| 4 | + |
| 5 | +# First Party |
| 6 | +import smdebug.tensorflow as smd |
| 7 | + |
| 8 | + |
| 9 | +def create_dataset(): |
| 10 | + # Download and load MNIST dataset. |
| 11 | + (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data("MNIST-data") |
| 12 | + x_train, x_test = x_train / 255.0, x_test / 255.0 |
| 13 | + |
| 14 | + # Add a channels dimension |
| 15 | + x_train = x_train[..., tf.newaxis] |
| 16 | + x_test = x_test[..., tf.newaxis] |
| 17 | + |
| 18 | + train_ds = ( |
| 19 | + tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000, seed=123).batch(2) |
| 20 | + ) |
| 21 | + test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(2) |
| 22 | + |
| 23 | + return train_ds, test_ds |
| 24 | + |
| 25 | + |
| 26 | +def test_functional_model(out_dir, tf_eager_mode): |
| 27 | + if tf_eager_mode is False: |
| 28 | + tf.compat.v1.disable_eager_execution() |
| 29 | + else: |
| 30 | + return |
| 31 | + num_classes = 10 |
| 32 | + train_ds, test_ds = create_dataset() |
| 33 | + |
| 34 | + # Input image dimensions |
| 35 | + img_rows, img_cols = 28, 28 |
| 36 | + |
| 37 | + img_inputs = Input(shape=(28, 28, 1)) |
| 38 | + x = Conv2D(32, kernel_size=(3, 3), activation="relu")(img_inputs) |
| 39 | + x1 = Conv2D(64, (3, 3), activation="relu")(x) |
| 40 | + x = MaxPooling2D(pool_size=(2, 2))(x1) |
| 41 | + x = Dropout(0.25)(x) |
| 42 | + x = Flatten()(x) |
| 43 | + x = Dense(128, activation="relu")(x) |
| 44 | + x = Dropout(0.5)(x) |
| 45 | + out = Dense(num_classes, activation="softmax")(x) |
| 46 | + |
| 47 | + model = tf.keras.models.Model(inputs=img_inputs, outputs=out) |
| 48 | + |
| 49 | + smd_callback = smd.KerasHook( |
| 50 | + export_tensorboard=False, out_dir=out_dir, include_collections=["custom"] |
| 51 | + ) |
| 52 | + |
| 53 | + smd_callback.get_collection("custom").add_for_mode([x1], mode=smd.modes.TRAIN) |
| 54 | + smd_callback.save_config = smd.SaveConfig(save_interval=1) |
| 55 | + opt = tf.keras.optimizers.Adadelta(1.0) |
| 56 | + |
| 57 | + model.compile( |
| 58 | + loss=tf.keras.losses.sparse_categorical_crossentropy, |
| 59 | + optimizer=opt, |
| 60 | + experimental_run_tf_function=False, |
| 61 | + ) |
| 62 | + |
| 63 | + callbacks = [smd_callback] |
| 64 | + model.fit(train_ds, epochs=1, steps_per_epoch=100, callbacks=callbacks) |
| 65 | + |
| 66 | + trial = smd.create_trial(out_dir) |
| 67 | + assert len(trial.tensor_names(collection="custom")) == 1 |
0 commit comments