Train ML model on Cloud AI Platform

This notebook shows how to:

TODO: Export the data from BigQuery to GCS

  1. Navigate to export_data.ipynb
  2. Update 'your-gcs-project-here' to your GCP project name
  3. Run all the notebook cells

TODO: Edit notebook parameters

  1. Navigate to notebook_params.yaml
  2. Replace the bucket name with your own bucket containing your model (likely gcp-project with -ml at the end)
  3. Save the notebook
  4. Return to this notebook and continue

Export code from notebook

This notebook extracts code from a notebook and creates a Python file suitable for use as model.py


In [9]:
import logging
import nbformat
import sys
import yaml

def write_parameters(cell_source, params_yaml, outfp):
    with open(params_yaml, 'r') as ifp:
        y = yaml.safe_load(ifp)
        # print out all the lines in notebook
        write_code(cell_source, 'PARAMS from notebook', outfp)
        # print out YAML file; this will override definitions above
        formats = [
            '{} = {}', # for integers and floats
            '{} = "{}"', # for strings
        ]
        write_code(
            '\n'.join([
                formats[type(value) is str].format(key, value) for key, value in y.items()]),
            'PARAMS from YAML',
            outfp
        )

def write_code(cell_source, comment, outfp):
    lines = cell_source.split('\n')
    if len(lines) > 0 and lines[0].startswith('%%'):
        prefix = '#'
    else:
        prefix = ''
    
    print("### BEGIN {} ###".format(comment), file=outfp)
    for line in lines:
        line = prefix + line.replace('print(', 'logging.info(')
        if len(line) > 0 and (line[0] == '!' or line[0] == '%'):
            print('#' + line, file=outfp)
        else:
            print(line, file=outfp)
    print("### END {} ###\n".format(comment), file=outfp)
            
def convert_notebook(notebook_filename, params_yaml, outfp):
    write_code('import logging', 'code added by notebook conversion', outfp)
    with open(INPUT) as ifp:
        nb = nbformat.reads(ifp.read(), nbformat.NO_CONVERT)
        for cell in nb.cells:
            if cell.cell_type == 'code':
                if 'tags' in cell.metadata and 'display' in cell.metadata.tags:
                    logging.info('Ignoring cell # {} with display tag'.format(cell.execution_count))
                elif 'tags' in cell.metadata and 'parameters' in cell.metadata.tags:
                    logging.info('Writing params cell # {}'.format(cell.execution_count))
                    write_parameters(cell.source, PARAMS, outfp)
                else:
                    logging.info('Writing model cell # {}'.format(cell.execution_count))
                    write_code(cell.source, 'Cell #{}'.format(cell.execution_count), outfp)

In [10]:
import os

INPUT='../../06_feateng_keras/solution/taxifare_fc.ipynb'
PARAMS='./notebook_params.yaml'
OUTDIR='./container/trainer'

!mkdir -p $OUTDIR
OUTFILE=os.path.join(OUTDIR, 'model.py')
!touch $OUTDIR/__init__.py
with open(OUTFILE, 'w') as ofp:
    #convert_notebook(INPUT, PARAMS, sys.stdout)
    convert_notebook(INPUT, PARAMS, ofp)
#!cat $OUTFILE

Try out model file

Note Once the training starts, Interrupt the Kernel (from the notebook ribbon bar above). Because it processes the entire dataset, this will take a long time on the relatively small machine on which you are running Notebooks.


In [ ]:
!python3 $OUTFILE


2019-09-23 03:30:16.322199: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations:  AVX2 FMA
To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags.
2019-09-23 03:30:16.330935: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2200000000 Hz
2019-09-23 03:30:16.331268: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x55d16e6c9710 executing computations on platform Host. Devices:
2019-09-23 03:30:16.331305: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): <undefined>, <undefined>
2019-09-23 03:30:16.331636: I tensorflow/core/common_runtime/process_util.cc:115] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
Model: "model"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
dropoff_latitude (InputLayer)   [(None,)]            0                                            
__________________________________________________________________________________________________
dropoff_longitude (InputLayer)  [(None,)]            0                                            
__________________________________________________________________________________________________
pickup_longitude (InputLayer)   [(None,)]            0                                            
__________________________________________________________________________________________________
pickup_latitude (InputLayer)    [(None,)]            0                                            
__________________________________________________________________________________________________
pickup_datetime (InputLayer)    [(None,)]            0                                            
__________________________________________________________________________________________________
scale_dropoff_latitude (Lambda) (None,)              0           dropoff_latitude[0][0]           
__________________________________________________________________________________________________
scale_dropoff_longitude (Lambda (None,)              0           dropoff_longitude[0][0]          
__________________________________________________________________________________________________
euclidean (Lambda)              (None,)              0           pickup_longitude[0][0]           
                                                                 pickup_latitude[0][0]            
                                                                 dropoff_longitude[0][0]          
                                                                 dropoff_latitude[0][0]           
__________________________________________________________________________________________________
hourofday (Lambda)              (None,)              0           pickup_datetime[0][0]            
__________________________________________________________________________________________________
passenger_count (InputLayer)    [(None,)]            0                                            
__________________________________________________________________________________________________
scale_pickup_latitude (Lambda)  (None,)              0           pickup_latitude[0][0]            
__________________________________________________________________________________________________
scale_pickup_longitude (Lambda) (None,)              0           pickup_longitude[0][0]           
__________________________________________________________________________________________________
dense_features (DenseFeatures)  (None, 130)          1000000     scale_dropoff_latitude[0][0]     
                                                                 scale_dropoff_longitude[0][0]    
                                                                 euclidean[0][0]                  
                                                                 hourofday[0][0]                  
                                                                 passenger_count[0][0]            
                                                                 scale_pickup_latitude[0][0]      
                                                                 scale_pickup_longitude[0][0]     
__________________________________________________________________________________________________
h1 (Dense)                      (None, 32)           4192        dense_features[0][0]             
__________________________________________________________________________________________________
h2 (Dense)                      (None, 8)            264         h1[0][0]                         
__________________________________________________________________________________________________
fare (Dense)                    (None, 1)            9           h2[0][0]                         
==================================================================================================
Total params: 1,004,465
Trainable params: 1,004,465
Non-trainable params: 0
__________________________________________________________________________________________________
Epoch 1/5
2019-09-23 03:30:19.192346: W tensorflow/compiler/jit/mark_for_compilation_pass.cc:1483] (One-time warning): Not using XLA:CPU for cluster because envvar TF_XLA_FLAGS=--tf_xla_cpu_global_jit was not set.  If you want XLA:CPU, either set that envvar, or use experimental_jit_scope to enable XLA:CPU.  To confirm that XLA is active, pass --vmodule=xla_compilation_cache=1 (as a proper command-line flag, not via TF_XLA_FLAGS) or set the envvar XLA_FLAGS=--xla_hlo_profile.

Create Docker container

Package up the trainer file into a Docker container and submit the image.


In [1]:
%%writefile container/Dockerfile
FROM gcr.io/deeplearning-platform-release/tf2-cpu

#RUN python3 -m pip install --upgrade --quiet tf-nightly-2.0-preview
RUN python3 -m pip install --upgrade --quiet cloudml-hypertune

COPY trainer /trainer
CMD ["python3", "/trainer/model.py"]


Overwriting container/Dockerfile

In [2]:
%%writefile container/push_docker.sh
export PROJECT_ID=$(gcloud config list project --format "value(core.project)")
export IMAGE_REPO_NAME=serverlessml_training_container
#export IMAGE_TAG=$(date +%Y%m%d_%H%M%S)
#export IMAGE_URI=gcr.io/$PROJECT_ID/$IMAGE_REPO_NAME:$IMAGE_TAG
export IMAGE_URI=gcr.io/$PROJECT_ID/$IMAGE_REPO_NAME

echo "Building  $IMAGE_URI"
docker build -f Dockerfile -t $IMAGE_URI ./
echo "Pushing $IMAGE_URI"
docker push $IMAGE_URI


Overwriting container/push_docker.sh

In [3]:
!find container


container
container/Dockerfile
container/push_docker.sh
container/trainer
container/trainer/model.py
container/trainer/__init__.py

Note: If you get a permissions error when running push_docker.sh from Notebooks, do it from CloudShell:

  • Open CloudShell on the GCP Console
  • git clone https://github.com/GoogleCloudPlatform/training-data-analyst
  • cd training-data-analyst/quests/serverlessml/07_caip/solution/container
  • bash push_docker.sh

This next step takes 5 - 10 minutes to run


In [4]:
%%bash
cd container
bash push_docker.sh


Building  gcr.io/qwiklabs-gcp-bdc77450c97b4bf6/serverlessml_training_container
Sending build context to Docker daemon  17.92kB
Step 1/4 : FROM gcr.io/deeplearning-platform-release/tf2-cpu
 ---> 5fb6bb1ebda9
Step 2/4 : COPY trainer /trainer
 ---> 8eb46e62f49d
Step 3/4 : RUN apt update &&     apt install --yes python3-pip &&     pip3 install --upgrade --quiet tf-nightly-2.0-preview
 ---> Running in 5d9135ef7e5a

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Get:1 http://packages.cloud.google.com/apt cloud-sdk-bionic InRelease [6372 B]
Get:2 http://packages.cloud.google.com/apt cloud-sdk-bionic/main amd64 Packages [86.7 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:4 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:5 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [662 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Get:8 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [769 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [186 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages [1344 kB]
Get:11 http://security.ubuntu.com/ubuntu bionic-security/restricted amd64 Packages [9585 B]
Get:12 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [5230 B]
Get:13 http://archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [11.3 MB]
Get:14 http://archive.ubuntu.com/ubuntu bionic/restricted amd64 Packages [13.5 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [959 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [8284 B]
Get:17 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [20.3 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [1287 kB]
Get:19 http://archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [4227 B]
Get:20 http://archive.ubuntu.com/ubuntu bionic-backports/main amd64 Packages [2496 B]
Fetched 17.2 MB in 10s (1761 kB/s)
Reading package lists...
Building dependency tree...
Reading state information...
41 packages can be upgraded. Run 'apt list --upgradable' to see them.

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
Building dependency tree...
Reading state information...
The following additional packages will be installed:
  dh-python libexpat1 libexpat1-dev libpython3-dev libpython3.6
  libpython3.6-dev libpython3.6-minimal libpython3.6-stdlib python-pip-whl
  python3-asn1crypto python3-cffi-backend python3-crypto python3-cryptography
  python3-dev python3-distutils python3-idna python3-keyring
  python3-keyrings.alt python3-lib2to3 python3-pkg-resources
  python3-secretstorage python3-setuptools python3-six python3-wheel
  python3-xdg python3.6 python3.6-dev python3.6-minimal
Suggested packages:
  python-crypto-doc python-cryptography-doc python3-cryptography-vectors
  gnome-keyring libkf5wallet-bin gir1.2-gnomekeyring-1.0
  python-secretstorage-doc python-setuptools-doc python3.6-venv python3.6-doc
  binfmt-support
The following NEW packages will be installed:
  dh-python libexpat1-dev libpython3-dev libpython3.6-dev python-pip-whl
  python3-asn1crypto python3-cffi-backend python3-crypto python3-cryptography
  python3-dev python3-distutils python3-idna python3-keyring
  python3-keyrings.alt python3-lib2to3 python3-pip python3-pkg-resources
  python3-secretstorage python3-setuptools python3-six python3-wheel
  python3-xdg python3.6-dev
The following packages will be upgraded:
  libexpat1 libpython3.6 libpython3.6-minimal libpython3.6-stdlib python3.6
  python3.6-minimal
6 upgraded, 23 newly installed, 0 to remove and 35 not upgraded.
Need to get 54.2 MB of archives.
After this operation, 89.1 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libexpat1 amd64 2.2.5-3ubuntu0.2 [80.5 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6 amd64 3.6.8-1~18.04.2 [1414 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3.6 amd64 3.6.8-1~18.04.2 [202 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6-stdlib amd64 3.6.8-1~18.04.2 [1710 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3.6-minimal amd64 3.6.8-1~18.04.2 [1609 kB]
Get:6 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6-minimal amd64 3.6.8-1~18.04.2 [532 kB]
Get:7 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-lib2to3 all 3.6.8-1~18.04 [76.5 kB]
Get:8 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-distutils all 3.6.8-1~18.04 [141 kB]
Get:9 http://archive.ubuntu.com/ubuntu bionic/main amd64 dh-python all 3.20180325ubuntu2 [89.2 kB]
Get:10 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libexpat1-dev amd64 2.2.5-3ubuntu0.2 [122 kB]
Get:11 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3.6-dev amd64 3.6.8-1~18.04.2 [44.8 MB]
Get:12 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython3-dev amd64 3.6.7-1~18.04 [7328 B]
Get:13 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 python-pip-whl all 9.0.1-2.3~ubuntu1.18.04.1 [1653 kB]
Get:14 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-asn1crypto all 0.24.0-1 [72.8 kB]
Get:15 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-cffi-backend amd64 1.11.5-1 [64.6 kB]
Get:16 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-crypto amd64 2.6.1-8ubuntu2 [244 kB]
Get:17 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-idna all 2.6-1 [32.5 kB]
Get:18 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-six all 1.11.0-2 [11.4 kB]
Get:19 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-cryptography amd64 2.1.4-1ubuntu1.3 [221 kB]
Get:20 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3.6-dev amd64 3.6.8-1~18.04.2 [508 kB]
Get:21 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 python3-dev amd64 3.6.7-1~18.04 [1288 B]
Get:22 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-secretstorage all 2.3.1-2 [12.1 kB]
Get:23 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-keyring all 10.6.0-1 [26.7 kB]
Get:24 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-keyrings.alt all 3.0-1 [16.6 kB]
Get:25 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 python3-pip all 9.0.1-2.3~ubuntu1.18.04.1 [114 kB]
Get:26 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-pkg-resources all 39.0.1-2 [98.8 kB]
Get:27 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-setuptools all 39.0.1-2 [248 kB]
Get:28 http://archive.ubuntu.com/ubuntu bionic/universe amd64 python3-wheel all 0.30.0-0.2 [36.5 kB]
Get:29 http://archive.ubuntu.com/ubuntu bionic/main amd64 python3-xdg all 0.25-4ubuntu1 [31.4 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 54.2 MB in 23s (2309 kB/s)
(Reading database ... 84724 files and directories currently installed.)
Preparing to unpack .../00-libexpat1_2.2.5-3ubuntu0.2_amd64.deb ...
Unpacking libexpat1:amd64 (2.2.5-3ubuntu0.2) over (2.2.5-3ubuntu0.1) ...
Preparing to unpack .../01-libpython3.6_3.6.8-1~18.04.2_amd64.deb ...
Unpacking libpython3.6:amd64 (3.6.8-1~18.04.2) over (3.6.8-1~18.04.1) ...
Preparing to unpack .../02-python3.6_3.6.8-1~18.04.2_amd64.deb ...
Unpacking python3.6 (3.6.8-1~18.04.2) over (3.6.8-1~18.04.1) ...
Preparing to unpack .../03-libpython3.6-stdlib_3.6.8-1~18.04.2_amd64.deb ...
Unpacking libpython3.6-stdlib:amd64 (3.6.8-1~18.04.2) over (3.6.8-1~18.04.1) ...
Preparing to unpack .../04-python3.6-minimal_3.6.8-1~18.04.2_amd64.deb ...
Unpacking python3.6-minimal (3.6.8-1~18.04.2) over (3.6.8-1~18.04.1) ...
Preparing to unpack .../05-libpython3.6-minimal_3.6.8-1~18.04.2_amd64.deb ...
Unpacking libpython3.6-minimal:amd64 (3.6.8-1~18.04.2) over (3.6.8-1~18.04.1) ...
Selecting previously unselected package python3-lib2to3.
Preparing to unpack .../06-python3-lib2to3_3.6.8-1~18.04_all.deb ...
Unpacking python3-lib2to3 (3.6.8-1~18.04) ...
Selecting previously unselected package python3-distutils.
Preparing to unpack .../07-python3-distutils_3.6.8-1~18.04_all.deb ...
Unpacking python3-distutils (3.6.8-1~18.04) ...
Selecting previously unselected package dh-python.
Preparing to unpack .../08-dh-python_3.20180325ubuntu2_all.deb ...
Unpacking dh-python (3.20180325ubuntu2) ...
Selecting previously unselected package libexpat1-dev:amd64.
Preparing to unpack .../09-libexpat1-dev_2.2.5-3ubuntu0.2_amd64.deb ...
Unpacking libexpat1-dev:amd64 (2.2.5-3ubuntu0.2) ...
Selecting previously unselected package libpython3.6-dev:amd64.
Preparing to unpack .../10-libpython3.6-dev_3.6.8-1~18.04.2_amd64.deb ...
Unpacking libpython3.6-dev:amd64 (3.6.8-1~18.04.2) ...
Selecting previously unselected package libpython3-dev:amd64.
Preparing to unpack .../11-libpython3-dev_3.6.7-1~18.04_amd64.deb ...
Unpacking libpython3-dev:amd64 (3.6.7-1~18.04) ...
Selecting previously unselected package python-pip-whl.
Preparing to unpack .../12-python-pip-whl_9.0.1-2.3~ubuntu1.18.04.1_all.deb ...
Unpacking python-pip-whl (9.0.1-2.3~ubuntu1.18.04.1) ...
Selecting previously unselected package python3-asn1crypto.
Preparing to unpack .../13-python3-asn1crypto_0.24.0-1_all.deb ...
Unpacking python3-asn1crypto (0.24.0-1) ...
Selecting previously unselected package python3-cffi-backend.
Preparing to unpack .../14-python3-cffi-backend_1.11.5-1_amd64.deb ...
Unpacking python3-cffi-backend (1.11.5-1) ...
Selecting previously unselected package python3-crypto.
Preparing to unpack .../15-python3-crypto_2.6.1-8ubuntu2_amd64.deb ...
Unpacking python3-crypto (2.6.1-8ubuntu2) ...
Selecting previously unselected package python3-idna.
Preparing to unpack .../16-python3-idna_2.6-1_all.deb ...
Unpacking python3-idna (2.6-1) ...
Selecting previously unselected package python3-six.
Preparing to unpack .../17-python3-six_1.11.0-2_all.deb ...
Unpacking python3-six (1.11.0-2) ...
Selecting previously unselected package python3-cryptography.
Preparing to unpack .../18-python3-cryptography_2.1.4-1ubuntu1.3_amd64.deb ...
Unpacking python3-cryptography (2.1.4-1ubuntu1.3) ...
Selecting previously unselected package python3.6-dev.
Preparing to unpack .../19-python3.6-dev_3.6.8-1~18.04.2_amd64.deb ...
Unpacking python3.6-dev (3.6.8-1~18.04.2) ...
Selecting previously unselected package python3-dev.
Preparing to unpack .../20-python3-dev_3.6.7-1~18.04_amd64.deb ...
Unpacking python3-dev (3.6.7-1~18.04) ...
Selecting previously unselected package python3-secretstorage.
Preparing to unpack .../21-python3-secretstorage_2.3.1-2_all.deb ...
Unpacking python3-secretstorage (2.3.1-2) ...
Selecting previously unselected package python3-keyring.
Preparing to unpack .../22-python3-keyring_10.6.0-1_all.deb ...
Unpacking python3-keyring (10.6.0-1) ...
Selecting previously unselected package python3-keyrings.alt.
Preparing to unpack .../23-python3-keyrings.alt_3.0-1_all.deb ...
Unpacking python3-keyrings.alt (3.0-1) ...
Selecting previously unselected package python3-pip.
Preparing to unpack .../24-python3-pip_9.0.1-2.3~ubuntu1.18.04.1_all.deb ...
Unpacking python3-pip (9.0.1-2.3~ubuntu1.18.04.1) ...
Selecting previously unselected package python3-pkg-resources.
Preparing to unpack .../25-python3-pkg-resources_39.0.1-2_all.deb ...
Unpacking python3-pkg-resources (39.0.1-2) ...
Selecting previously unselected package python3-setuptools.
Preparing to unpack .../26-python3-setuptools_39.0.1-2_all.deb ...
Unpacking python3-setuptools (39.0.1-2) ...
Selecting previously unselected package python3-wheel.
Preparing to unpack .../27-python3-wheel_0.30.0-0.2_all.deb ...
Unpacking python3-wheel (0.30.0-0.2) ...
Selecting previously unselected package python3-xdg.
Preparing to unpack .../28-python3-xdg_0.25-4ubuntu1_all.deb ...
Unpacking python3-xdg (0.25-4ubuntu1) ...
Setting up python-pip-whl (9.0.1-2.3~ubuntu1.18.04.1) ...
Setting up libexpat1:amd64 (2.2.5-3ubuntu0.2) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Setting up python3-cffi-backend (1.11.5-1) ...
Setting up python3-crypto (2.6.1-8ubuntu2) ...
Setting up python3-idna (2.6-1) ...
Setting up python3-xdg (0.25-4ubuntu1) ...
Setting up python3-six (1.11.0-2) ...
Setting up python3-wheel (0.30.0-0.2) ...
Setting up python3-pkg-resources (39.0.1-2) ...
Setting up libpython3.6-minimal:amd64 (3.6.8-1~18.04.2) ...
Setting up python3-asn1crypto (0.24.0-1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Setting up libexpat1-dev:amd64 (2.2.5-3ubuntu0.2) ...
Setting up python3-lib2to3 (3.6.8-1~18.04) ...
Setting up python3-distutils (3.6.8-1~18.04) ...
Setting up python3-cryptography (2.1.4-1ubuntu1.3) ...
Setting up libpython3.6-stdlib:amd64 (3.6.8-1~18.04.2) ...
Setting up python3-keyrings.alt (3.0-1) ...
Setting up python3.6-minimal (3.6.8-1~18.04.2) ...
Setting up python3-pip (9.0.1-2.3~ubuntu1.18.04.1) ...
Setting up python3-setuptools (39.0.1-2) ...
Setting up python3-secretstorage (2.3.1-2) ...
Setting up dh-python (3.20180325ubuntu2) ...
Setting up libpython3.6:amd64 (3.6.8-1~18.04.2) ...
Setting up python3.6 (3.6.8-1~18.04.2) ...
Setting up python3-keyring (10.6.0-1) ...
Setting up libpython3.6-dev:amd64 (3.6.8-1~18.04.2) ...
Setting up python3.6-dev (3.6.8-1~18.04.2) ...
Setting up libpython3-dev:amd64 (3.6.7-1~18.04) ...
Setting up python3-dev (3.6.7-1~18.04) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Removing intermediate container 5d9135ef7e5a
 ---> c25211bc8d1f
Step 4/4 : CMD ["python3", "/trainer/model.py"]
 ---> Running in 4d1cddc80dce
Removing intermediate container 4d1cddc80dce
 ---> 005b3a997797
Successfully built 005b3a997797
Successfully tagged gcr.io/qwiklabs-gcp-bdc77450c97b4bf6/serverlessml_training_container:latest
Pushing gcr.io/qwiklabs-gcp-bdc77450c97b4bf6/serverlessml_training_container
The push refers to repository [gcr.io/qwiklabs-gcp-bdc77450c97b4bf6/serverlessml_training_container]
9781db4bf0e2: Preparing
8150a3c43d14: Preparing
a71e72a550ba: Preparing
c23179f29c5e: Preparing
7e4890a23200: Preparing
54d2eb300334: Preparing
9464e4075ebf: Preparing
d811f4e3bddb: Preparing
219acea3b74e: Preparing
add8d5b9d28b: Preparing
aa35d6dde7c2: Preparing
d6a152cedf91: Preparing
920c1e5a5ee9: Preparing
0cfda733c555: Preparing
a0e77507151e: Preparing
baf51ba9f2bc: Preparing
122be11ab4a2: Preparing
7beb13bce073: Preparing
f7eae43028b3: Preparing
6cebf3abed5f: Preparing
d6a152cedf91: Waiting
920c1e5a5ee9: Waiting
0cfda733c555: Waiting
a0e77507151e: Waiting
baf51ba9f2bc: Waiting
122be11ab4a2: Waiting
7beb13bce073: Waiting
f7eae43028b3: Waiting
6cebf3abed5f: Waiting
54d2eb300334: Waiting
9464e4075ebf: Waiting
d811f4e3bddb: Waiting
219acea3b74e: Waiting
aa35d6dde7c2: Waiting
add8d5b9d28b: Waiting
c23179f29c5e: Layer already exists
7e4890a23200: Layer already exists
a71e72a550ba: Layer already exists
54d2eb300334: Layer already exists
d811f4e3bddb: Layer already exists
9464e4075ebf: Layer already exists
219acea3b74e: Layer already exists
add8d5b9d28b: Layer already exists
aa35d6dde7c2: Layer already exists
d6a152cedf91: Layer already exists
920c1e5a5ee9: Layer already exists
0cfda733c555: Layer already exists
a0e77507151e: Layer already exists
122be11ab4a2: Layer already exists
baf51ba9f2bc: Layer already exists
6cebf3abed5f: Layer already exists
7beb13bce073: Layer already exists
f7eae43028b3: Layer already exists
8150a3c43d14: Pushed
9781db4bf0e2: Pushed
latest: digest: sha256:46eb0ddf5851cfb187f87c974b31dff77b89bb3111550235fa92c1cc8863cf74 size: 4505

Deploy to AI Platform

Submit a training job using this custom container that we have just built. After you submit the job, monitor it here.


In [5]:
%%bash
JOBID=serverlessml_$(date +%Y%m%d_%H%M%S)
REGION=us-central1
PROJECT_ID=$(gcloud config list project --format "value(core.project)")
BUCKET=$(gcloud config list project --format "value(core.project)")-ml

#IMAGE=gcr.io/deeplearning-platform-release/tf2-cpu
IMAGE=gcr.io/$PROJECT_ID/serverlessml_training_container

gcloud beta ai-platform jobs submit training $JOBID \
   --staging-bucket=gs://$BUCKET  --region=$REGION \
   --master-image-uri=$IMAGE \
   --master-machine-type=n1-standard-4 --scale-tier=CUSTOM


jobId: serverlessml_20190923_034220
state: QUEUED
Job [serverlessml_20190923_034220] submitted successfully.
Your job is still active. You may view the status of your job with the command

  $ gcloud ai-platform jobs describe serverlessml_20190923_034220

or continue streaming the logs with the command

  $ gcloud ai-platform jobs stream-logs serverlessml_20190923_034220

The training job will take 35 - 45 minutes to complete on the dataset. You can cancel the job once you confirm it started and have inspected the logs.

Copyright 2019 Google Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


In [ ]: