Introduction to AI Platform

Google Cloud AI Platform is a comprehensive suite of tools and services designed to help developers and data scientists build, deploy, and manage machine learning models at scale. It integrates seamlessly with other Google Cloud services, providing a robust environment for end-to-end machine learning workflows.

Key Concepts

  1. AI Platform Notebooks: Managed Jupyter notebooks that provide a collaborative environment for data exploration and model development.
  2. AI Platform Training: A service for training machine learning models using custom code or pre-built algorithms.
  3. AI Platform Prediction: A service for deploying trained models to make predictions on new data.
  4. AI Platform Pipelines: A service for orchestrating and automating machine learning workflows.
  5. AI Platform Data Labeling: A service for creating and managing labeled datasets for training machine learning models.

Setting Up AI Platform

Prerequisites

  • A Google Cloud Platform account.
  • Basic knowledge of machine learning concepts.
  • Familiarity with Python programming.

Steps to Set Up AI Platform

  1. Enable AI Platform APIs:

    • Go to the GCP Console.
    • Navigate to the "APIs & Services" section.
    • Enable the "AI Platform" API.
  2. Create a New Project:

    • In the GCP Console, click on the project dropdown and select "New Project".
    • Name your project and click "Create".
  3. Set Up Billing:

    • Ensure that billing is enabled for your project to use AI Platform services.

Using AI Platform Notebooks

AI Platform Notebooks provide a managed Jupyter notebook environment for data exploration and model development.

Creating a Notebook Instance

  1. Navigate to AI Platform Notebooks:

    • In the GCP Console, go to "AI Platform" > "Notebooks".
  2. Create a New Instance:

    • Click "New Instance".
    • Choose a configuration (e.g., TensorFlow, PyTorch).
    • Configure the instance settings (e.g., machine type, disk size).
    • Click "Create".
  3. Access the Notebook:

    • Once the instance is created, click "Open JupyterLab" to start working in the notebook environment.

Example: Loading Data in a Notebook

import pandas as pd

# Load data from a CSV file stored in Google Cloud Storage
data = pd.read_csv('gs://your-bucket-name/your-data-file.csv')

# Display the first few rows of the dataset
data.head()

Explanation

  • import pandas as pd: Imports the pandas library, which is used for data manipulation and analysis.
  • pd.read_csv: Reads a CSV file from Google Cloud Storage.
  • data.head(): Displays the first few rows of the dataset.

Training a Model with AI Platform

AI Platform Training allows you to train machine learning models using custom code or pre-built algorithms.

Steps to Train a Model

  1. Prepare Your Training Code:

    • Write your training code in Python.
    • Ensure that your code is compatible with AI Platform Training.
  2. Package Your Code:

    • Package your training code into a Python package.
    • Include a setup.py file to specify dependencies.
  3. Submit a Training Job:

    • Use the gcloud command-line tool to submit a training job.

Example: Submitting a Training Job

gcloud ai-platform jobs submit training my_training_job \
    --region us-central1 \
    --module-name trainer.task \
    --package-path ./trainer \
    --job-dir gs://your-bucket-name/job-dir \
    --runtime-version 2.3 \
    --python-version 3.7

Explanation

  • gcloud ai-platform jobs submit training: Submits a training job to AI Platform.
  • my_training_job: The name of the training job.
  • --region us-central1: The region where the job will run.
  • --module-name trainer.task: The entry point of your training code.
  • --package-path ./trainer: The path to your training code package.
  • --job-dir gs://your-bucket-name/job-dir: The Google Cloud Storage location for job outputs.
  • --runtime-version 2.3: The runtime version for the training job.
  • --python-version 3.7: The Python version for the training job.

Deploying a Model with AI Platform Prediction

AI Platform Prediction allows you to deploy trained models to make predictions on new data.

Steps to Deploy a Model

  1. Export Your Trained Model:

    • Save your trained model to Google Cloud Storage.
  2. Create a Model Resource:

    • Use the gcloud command-line tool to create a model resource.
  3. Create a Model Version:

    • Use the gcloud command-line tool to create a version of the model.

Example: Deploying a Model

gcloud ai-platform models create my_model

gcloud ai-platform versions create v1 \
    --model my_model \
    --origin gs://your-bucket-name/model-dir \
    --runtime-version 2.3 \
    --python-version 3.7

Explanation

  • gcloud ai-platform models create my_model: Creates a model resource named "my_model".
  • gcloud ai-platform versions create v1: Creates a version of the model named "v1".
  • --model my_model: Specifies the model resource.
  • --origin gs://your-bucket-name/model-dir: The Google Cloud Storage location of the model.
  • --runtime-version 2.3: The runtime version for the model.
  • --python-version 3.7: The Python version for the model.

Practical Exercise

Exercise: Train and Deploy a Simple Model

  1. Objective: Train a simple linear regression model and deploy it using AI Platform.
  2. Steps:
    • Create a new AI Platform Notebook instance.
    • Load a dataset from Google Cloud Storage.
    • Train a linear regression model using scikit-learn.
    • Export the trained model to Google Cloud Storage.
    • Deploy the model using AI Platform Prediction.

Solution

Step 1: Create a New AI Platform Notebook Instance

Follow the steps outlined in the "Creating a Notebook Instance" section.

Step 2: Load a Dataset

import pandas as pd

# Load data from a CSV file stored in Google Cloud Storage
data = pd.read_csv('gs://your-bucket-name/your-data-file.csv')

# Display the first few rows of the dataset
data.head()

Step 3: Train a Linear Regression Model

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import joblib

# Split the data into training and testing sets
X = data[['feature1', 'feature2']]
y = data['target']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Train a linear regression model
model = LinearRegression()
model.fit(X_train, y_train)

# Save the trained model to a file
joblib.dump(model, 'model.joblib')

Step 4: Export the Model to Google Cloud Storage

gsutil cp model.joblib gs://your-bucket-name/model-dir/

Step 5: Deploy the Model

gcloud ai-platform models create my_linear_model

gcloud ai-platform versions create v1 \
    --model my_linear_model \
    --origin gs://your-bucket-name/model-dir \
    --runtime-version 2.3 \
    --python-version 3.7

Conclusion

In this section, you learned about Google Cloud AI Platform and its key components, including AI Platform Notebooks, Training, and Prediction. You also learned how to set up AI Platform, create and use notebooks, train and deploy machine learning models. By completing the practical exercise, you gained hands-on experience in training and deploying a simple linear regression model using AI Platform. This knowledge will serve as a foundation for more advanced machine learning and AI topics in Google Cloud Platform.

© Copyright 2024. All rights reserved