-
Notifications
You must be signed in to change notification settings - Fork 163
SM integration test for TF 1.x #307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bf0a76d
SM integration test for TF 1.x
vandanavk 8d4c402
Merge branch 'master' into tf_1x_test
saravsak 42b747f
add skip py2 fixture
vandanavk af65546
Merge branch 'master' into tf_1x_test
saravsak d80b004
Merge branch 'master' into tf_1x_test
YYStreet 4fc5040
SM integration test for TF 1.x
vandanavk 404e5aa
add skip py2 fixture
vandanavk 14573f2
remove py2 fixture
vandanavk ff661b1
Merge commit 'refs/pull/307/head' of https://github.com/aws/sagemaker…
vandanavk cbcb153
remove py2 fixture
vandanavk fbbc9f0
Merge branch 'master' into tf_1x_test
YYStreet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import argparse | ||
| import json | ||
| import os | ||
| import sys | ||
|
|
||
| import numpy as np | ||
| import tensorflow.compat.v1 as tf | ||
|
|
||
| import smdebug.tensorflow as smd | ||
| from smdebug.core.collection import CollectionKeys | ||
| from smdebug.core.reduction_config import ALLOWED_NORMS, ALLOWED_REDUCTIONS | ||
| from smdebug.tensorflow import ReductionConfig, SaveConfig | ||
| from smdebug.trials import create_trial | ||
|
|
||
|
|
||
| def _parse_args(): | ||
|
|
||
| parser = argparse.ArgumentParser() | ||
|
|
||
| # hyperparameters sent by the client are passed as command-line arguments to the script. | ||
| parser.add_argument('--epochs', type=int, default=1) | ||
| # Data, model, and output directories | ||
| parser.add_argument('--model-dir', type=str, default=os.environ['SM_MODEL_DIR']) | ||
| parser.add_argument( | ||
| "--smdebug_path", | ||
| type=str, | ||
| default=None, | ||
| help="S3 URI of the bucket where tensor data will be stored.", | ||
| ) | ||
| parser.add_argument('--train', type=str, default=os.environ['SM_CHANNEL_TRAINING']) | ||
| parser.add_argument('--hosts', type=list, default=json.loads(os.environ['SM_HOSTS'])) | ||
| parser.add_argument('--current-host', type=str, default=os.environ['SM_CURRENT_HOST']) | ||
|
|
||
| return parser.parse_known_args() | ||
|
|
||
|
|
||
| def _load_training_data(base_dir): | ||
| x_train = np.load(os.path.join(base_dir, 'train', 'x_train.npy')) | ||
| y_train = np.load(os.path.join(base_dir, 'train', 'y_train.npy')) | ||
| return x_train, y_train | ||
|
|
||
|
|
||
| def _load_testing_data(base_dir): | ||
| x_test = np.load(os.path.join(base_dir, 'test', 'x_test.npy')) | ||
| y_test = np.load(os.path.join(base_dir, 'test', 'y_test.npy')) | ||
| return x_test, y_test | ||
|
|
||
|
|
||
| def create_smdebug_hook(out_dir): | ||
| include_collections = [ | ||
| CollectionKeys.WEIGHTS, | ||
| CollectionKeys.BIASES, | ||
| CollectionKeys.GRADIENTS, | ||
| CollectionKeys.LOSSES, | ||
| CollectionKeys.OUTPUTS, | ||
| CollectionKeys.METRICS, | ||
| CollectionKeys.LOSSES, | ||
| CollectionKeys.OPTIMIZER_VARIABLES, | ||
| ] | ||
| save_config = SaveConfig(save_interval=3) | ||
| hook = smd.KerasHook( | ||
| out_dir, | ||
| save_config=save_config, | ||
| include_collections=include_collections, | ||
| reduction_config=ReductionConfig(norms=ALLOWED_NORMS, reductions=ALLOWED_REDUCTIONS), | ||
| ) | ||
| return hook | ||
|
|
||
|
|
||
| args, unknown = _parse_args() | ||
|
|
||
| hook = create_smdebug_hook(args.smdebug_path) | ||
| hooks = [hook] | ||
|
|
||
| model = tf.keras.models.Sequential([ | ||
| tf.keras.layers.Flatten(input_shape=(28, 28)), | ||
| tf.keras.layers.Dense(512, activation=tf.nn.relu), | ||
| tf.keras.layers.Dropout(0.2), | ||
| tf.keras.layers.Dense(10, activation=tf.nn.softmax) | ||
| ]) | ||
|
|
||
| model.compile(optimizer='adam', | ||
| loss='sparse_categorical_crossentropy', | ||
| metrics=['accuracy']) | ||
| x_train, y_train = _load_training_data(args.train) | ||
| x_test, y_test = _load_testing_data(args.train) | ||
| model.fit(x_train, y_train, epochs=args.epochs, callbacks=hooks) | ||
| model.evaluate(x_test, y_test, callbacks=hooks) | ||
|
|
||
| if args.current_host == args.hosts[0]: | ||
| model.save(os.path.join('/opt/ml/model', 'my_model.h5')) | ||
|
|
||
| print("Created the trial with out_dir {0}".format(args.smdebug_path)) | ||
| trial = create_trial(args.smdebug_path) | ||
| assert trial | ||
|
|
||
| print(f"trial.tensor_names() = {trial.tensor_names()}") | ||
|
|
||
| weights_tensors = hook.collection_manager.get("weights").tensor_names | ||
| assert len(weights_tensors) > 0 | ||
|
|
||
| losses_tensors = hook.collection_manager.get("losses").tensor_names | ||
| assert len(losses_tensors) > 0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs this test fixture:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this required in tf2 branch as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. There are py2 containers for tf2, so this will be required for tf2 as well.