|
| 1 | +# Standard Library |
| 2 | +# Third Party |
| 3 | +import pytest |
| 4 | +import tensorflow.compat.v2 as tf |
| 5 | +from tests.zero_code_change.tf_utils import get_estimator, get_input_fns |
| 6 | + |
| 7 | +# First Party |
| 8 | +import smdebug.tensorflow as smd |
| 9 | + |
| 10 | + |
| 11 | +@pytest.mark.parametrize("saveall", [True, False]) |
| 12 | +def test_estimator(out_dir, tf_eager_mode, saveall): |
| 13 | + """ Works as intended. """ |
| 14 | + if tf_eager_mode is False: |
| 15 | + tf.compat.v1.disable_eager_execution() |
| 16 | + tf.compat.v1.reset_default_graph() |
| 17 | + tf.keras.backend.clear_session() |
| 18 | + mnist_classifier = get_estimator() |
| 19 | + train_input_fn, eval_input_fn = get_input_fns() |
| 20 | + |
| 21 | + # Train and evaluate |
| 22 | + train_steps, eval_steps = 8, 2 |
| 23 | + hook = smd.EstimatorHook(out_dir=out_dir, save_all=saveall) |
| 24 | + hook.set_mode(mode=smd.modes.TRAIN) |
| 25 | + mnist_classifier.train(input_fn=train_input_fn, steps=train_steps, hooks=[hook]) |
| 26 | + hook.set_mode(mode=smd.modes.EVAL) |
| 27 | + mnist_classifier.evaluate(input_fn=eval_input_fn, steps=eval_steps, hooks=[hook]) |
| 28 | + |
| 29 | + # Check that hook created and tensors saved |
| 30 | + trial = smd.create_trial(path=out_dir) |
| 31 | + tnames = trial.tensor_names() |
| 32 | + assert len(trial.steps()) > 0 |
| 33 | + if saveall: |
| 34 | + assert len(tnames) >= 301 |
| 35 | + else: |
| 36 | + assert len(tnames) == 1 |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("saveall", [True, False]) |
| 40 | +def test_linear_classifier(out_dir, tf_eager_mode, saveall): |
| 41 | + """ Works as intended. """ |
| 42 | + if tf_eager_mode is False: |
| 43 | + tf.compat.v1.disable_eager_execution() |
| 44 | + tf.compat.v1.reset_default_graph() |
| 45 | + tf.keras.backend.clear_session() |
| 46 | + train_input_fn, eval_input_fn = get_input_fns() |
| 47 | + x_feature = tf.feature_column.numeric_column("x", shape=(28, 28)) |
| 48 | + estimator = tf.estimator.LinearClassifier( |
| 49 | + feature_columns=[x_feature], model_dir="/tmp/mnist_linear_classifier", n_classes=10 |
| 50 | + ) |
| 51 | + hook = smd.EstimatorHook(out_dir=out_dir, save_all=saveall) |
| 52 | + estimator.train(input_fn=train_input_fn, steps=10, hooks=[hook]) |
| 53 | + |
| 54 | + # Check that hook created and tensors saved |
| 55 | + trial = smd.create_trial(path=out_dir) |
| 56 | + tnames = trial.tensor_names() |
| 57 | + assert len(trial.steps()) > 0 |
| 58 | + if saveall: |
| 59 | + assert len(tnames) >= 224 |
| 60 | + else: |
| 61 | + assert len(tnames) == 2 |
0 commit comments