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
{ | |
"displayName": "e2e-tutorial-viz", | |
"jobSpec": { | |
"workerPoolSpecs": [ | |
{ | |
"replicaCount": 1, | |
"machineSpec": { | |
"machineType": "n1-standard-4", | |
"acceleratorType": "NVIDIA_TESLA_V100", | |
"acceleratorCount": 2 |
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 os | |
tensorboard_callback = tf.keras.callbacks.TensorBoard( | |
log_dir=os.environ['AIP_TENSORBOARD_LOG_DIR'], | |
histogram_freq=1 | |
) | |
model.fit(train_dataset, epochs=args.epochs, validation_data=val_dataset, callbacks=[tensorboard_callback]) |
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
studySpec: | |
metrics: | |
# Correspond to the metrics we use the hypertune library to report. | |
- metricId: val_accuracy | |
goal: MAXIMIZE | |
parameters: | |
# Correspond to the command line argument our Python code expects. | |
- parameterId: dropout_rate | |
doubleValueSpec: | |
minValue: 0.01 |
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 tensorflow as tf | |
import hypertune | |
hpt = hypertune.HyperTune() | |
class CustomCallback(tf.keras.callbacks.Callback): | |
def on_epoch_end(self, epoch, logs=None): | |
hpt.report_hyperparameter_tuning_metric( | |
hyperparameter_metric_tag='val_accuracy', | |
metric_value=logs['val_accuracy'], |
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
from tensorflow.keras import layers, models, losses | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--epochs', dest='epochs', type=int, default=5) | |
parser.add_argument('--dropout_rate', dest='dropout_rate', type=float, default=0.1) | |
args = parser.parse_args() | |
def create_model(): |
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
workerPoolSpecs: | |
machineSpec: | |
# Machines and GPUs: https://cloud.google.com/vertex-ai/docs/training/configure-compute#specifying_gpus | |
machineType: n1-standard-4 | |
acceleratorType: NVIDIA_TESLA_V100 | |
acceleratorCount: 2 | |
replicaCount: 1 | |
pythonPackageSpec: | |
# Executors: https://cloud.google.com/vertex-ai/docs/training/pre-built-containers | |
executorImageUri: us-docker.pkg.dev/vertex-ai/training/tf-gpu.2-3:latest |
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
cloud_model = tf.keras.models.load_model(GCS_PATH_FOR_SAVED_MODEL) | |
# Compile the model to make sure it uses the correct accuracy metric. | |
cloud_model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(), metrics=['accuracy']) | |
cloud_model.evaluate(test_dataset, verbose=2) | |
# Output loss: 0.917 - accuracy: 0.6620 |
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
from setuptools import find_packages | |
from setuptools import setup | |
setup( | |
name='trainer', | |
version='0.1', | |
packages=find_packages(), | |
include_package_data=True, | |
description='An E2E Tutorial of Vertex AI' | |
) |
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 argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--epochs', dest='epochs', type=int, default=5) | |
args = parser.parse_args() | |
# Refer to the argument as args.epochs. |
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
# A distributed strategy to take advantage of available hardward. | |
# No-op otherwise. | |
mirrored_strategy = tf.distribute.MirroredStrategy() | |
with mirrored_strategy.scope(): | |
model = create_model() | |
# Restore from the latest checkpoint if available. | |
latest_ckpt = tf.train.latest_checkpoint(GCS_PATH_FOR_CHECKPOINTS) | |
if latest_ckpt: | |
model.load_weights(latest_ckpt) |