%%bash
cd cifar10

# Clean old builds
rm -rf build dist

# Build wheel distribution
python setup.py bdist_wheel --universal

# Check the built package
ls -al dist

"""
Explanation: Training a CNN model with the CIFAR-10 dataset in ML Engine
The trainer package source is inside the cifar10 directory. It was based from Tensorflow's CNN tutorial and one of the Datalab image classification example.
Enable the ML Engine API
We need to enable the ML Engine API since it isn't by default.

Head back to the web console.
Search for "API Manager" using the bar on the top middle of the page.
Select Library from the sidebar.
Search for "ML Engine" and select Google Cloud Machine Learning Engine.
Click ENABLE

Build the trainer package
End of explanation
"""


%%bash
cd cifar10

# Set some variables
JOB_NAME=cifar10_train_$(date +%s)
BUCKET_NAME=dost_deeplearning_cifar10 # Change this to your own!
TRAINING_PACKAGE_PATH=dist/trainer-0.0.0-py2.py3-none-any.whl

# Submit the job through the gcloud tool
gcloud ml-engine jobs submit training \
  $JOB_NAME \
  --region us-east1 \
  --job-dir gs://$BUCKET_NAME/$JOB_NAME \
  --packages $TRAINING_PACKAGE_PATH \
  --module-name trainer.task \
  --config config.yaml

"""
Explanation: Submit the training job to ML Engine
End of explanation
"""


import os.path
from google.datalab.ml import TensorBoard

bucket_path = 'gs://dost_deeplearning_cifar10'  # Change this to your own bucket
job_name = 'cifar10_train_1499874404'           # Change this to your own job name
train_dir = os.path.join(bucket_path, job_name, 'train')

TensorBoard.start(train_dir)

"""
Explanation: It will take a few minutes for ML Engine to provision a training instance for our job. While that's happening, let's talk about pricing!
TensorBoard
End of explanation
"""


