Vertex AI SDK for Python
=================================================
Gemini API and Generative AI on Vertex AI
-----------------------------------------
.. note::
For Gemini API and Generative AI on Vertex AI, please reference `Vertex Generative AI SDK for Python`_
.. _Vertex Generative AI SDK for Python: https://cloud.google.com/vertex-ai/generative-ai/docs/reference/python/latest
-----------------------------------------
|GA| |pypi| |versions| |unit-tests| |system-tests| |sample-tests|
`Vertex AI`_: Google Vertex AI is an integrated suite of machine learning tools and services for building and using ML models with AutoML or custom code. It offers both novices and experts the best workbench for the entire machine learning development lifecycle.
- `Client Library Documentation`_
- `Product Documentation`_
.. |GA| image:: https://img.shields.io/badge/support-ga-gold.svg
:target: https://github.com/googleapis/google-cloud-python/blob/main/README.rst#general-availability
.. |pypi| image:: https://img.shields.io/pypi/v/google-cloud-aiplatform.svg
:target: https://pypi.org/project/google-cloud-aiplatform/
.. |versions| image:: https://img.shields.io/pypi/pyversions/google-cloud-aiplatform.svg
:target: https://pypi.org/project/google-cloud-aiplatform/
.. |unit-tests| image:: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-unit-tests.svg
:target: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-unit-tests.html
.. |system-tests| image:: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-system-tests.svg
:target: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-system-tests.html
.. |sample-tests| image:: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-sample-tests.svg
:target: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-sample-tests.html
.. _Vertex AI: https://cloud.google.com/vertex-ai/docs
.. _Client Library Documentation: https://cloud.google.com/python/docs/reference/aiplatform/latest
.. _Product Documentation: https://cloud.google.com/vertex-ai/docs
Quick Start
-----------
In order to use this library, you first need to go through the following steps:
1. `Select or create a Cloud Platform project.`_
2. `Enable billing for your project.`_
3. `Enable the Vertex AI API.`_
4. `Setup Authentication.`_
.. _Select or create a Cloud Platform project.: https://console.cloud.google.com/project
.. _Enable billing for your project.: https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_a_project
.. _Enable the Vertex AI API.: https://cloud.google.com/vertex-ai/docs/start/use-vertex-ai-python-sdk
.. _Setup Authentication.: https://googleapis.dev/python/google-api-core/latest/auth.html
Installation
~~~~~~~~~~~~
Install this library in a `virtualenv`_ using pip. `virtualenv`_ is a tool to
create isolated Python environments. The basic problem it addresses is one of
dependencies and versions, and indirectly permissions.
With `virtualenv`_, it's possible to install this library without needing system
install permissions, and without clashing with the installed system
dependencies.
.. _virtualenv: https://virtualenv.pypa.io/en/latest/
Mac/Linux
^^^^^^^^^
.. code-block:: console
pip install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install google-cloud-aiplatform
Windows
^^^^^^^
.. code-block:: console
pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-cloud-aiplatform
Supported Python Versions
^^^^^^^^^^^^^^^^^^^^^^^^^
Python >= 3.8
Deprecated Python Versions
^^^^^^^^^^^^^^^^^^^^^^^^^^
Python <= 3.7.
The last version of this library compatible with Python 3.6 is google-cloud-aiplatform==1.12.1.
Overview
~~~~~~~~
This section provides a brief overview of the Vertex AI SDK for Python. You can also reference the notebooks in `vertex-ai-samples`_ for examples.
.. _vertex-ai-samples: https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/community/sdk
All publicly available SDK features can be found in the :code:`google/cloud/aiplatform` directory.
Under the hood, Vertex SDK builds on top of GAPIC, which stands for Google API CodeGen.
The GAPIC library code sits in :code:`google/cloud/aiplatform_v1` and :code:`google/cloud/aiplatform_v1beta1`,
and it is auto-generated from Google's service proto files.
For most developers' programmatic needs, they can follow these steps to figure out which libraries to import:
1. Look through :code:`google/cloud/aiplatform` first -- Vertex SDK's APIs will almost always be easier to use and more concise comparing with GAPIC
2. If the feature that you are looking for cannot be found there, look through :code:`aiplatform_v1` to see if it's available in GAPIC
3. If it is still in beta phase, it will be available in :code:`aiplatform_v1beta1`
If none of the above scenarios could help you find the right tools for your task, please feel free to open a github issue and send us a feature request.
Importing
^^^^^^^^^
Vertex AI SDK resource based functionality can be used by importing the following namespace:
.. code-block:: Python
from google.cloud import aiplatform
Initialization
^^^^^^^^^^^^^^
Initialize the SDK to store common configurations that you use with the SDK.
.. code-block:: Python
aiplatform.init(
# your Google Cloud Project ID or number
# environment default used is not set
project='my-project',
# the Vertex AI region you will use
# defaults to us-central1
location='us-central1',
# Google Cloud Storage bucket in same region as location
# used to stage artifacts
staging_bucket='gs://my_staging_bucket',
# custom google.auth.credentials.Credentials
# environment default credentials used if not set
credentials=my_credentials,
# customer managed encryption key resource name
# will be applied to all Vertex AI resources if set
encryption_spec_key_name=my_encryption_key_name,
# the name of the experiment to use to track
# logged metrics and parameters
experiment='my-experiment',
# description of the experiment above
experiment_description='my experiment description'
)
Datasets
^^^^^^^^
Vertex AI provides managed tabular, text, image, and video datasets. In the SDK, datasets can be used downstream to
train models.
To create a tabular dataset:
.. code-block:: Python
my_dataset = aiplatform.TabularDataset.create(
display_name="my-dataset", gcs_source=['gs://path/to/my/dataset.csv'])
You can also create and import a dataset in separate steps:
.. code-block:: Python
from google.cloud import aiplatform
my_dataset = aiplatform.TextDataset.create(
display_name="my-dataset")
my_dataset.import_data(
gcs_source=['gs://path/to/my/dataset.csv'],
import_schema_uri=aiplatform.schema.dataset.ioformat.text.multi_label_classification
)
To get a previously created Dataset:
.. code-block:: Python
dataset = aiplatform.ImageDataset('projects/my-project/location/us-central1/datasets/{DATASET_ID}')
Vertex AI supports a variety of dataset schemas. References to these schemas are available under the
:code:`aiplatform.schema.dataset` namespace. For more information on the supported dataset schemas please refer to the
`Preparing data docs`_.
.. _Preparing data docs: https://cloud.google.com/ai-platform-unified/docs/datasets/prepare
Training
^^^^^^^^
The Vertex AI SDK for Python allows you train Custom and AutoML Models.
You can train custom models using a custom Python script, custom Python package, or container.
**Preparing Your Custom Code**
Vertex AI custom training enables you to train on Vertex AI datasets and produce Vertex AI models. To do so your
script must adhere to the following contract:
It must read datasets from the environment variables populated by the training service:
.. code-block:: Python
os.environ['AIP_DATA_FORMAT'] # provides format of data
os.environ['AIP_TRAINING_DATA_URI'] # uri to training split
os.environ['AIP_VALIDATION_DATA_URI'] # uri to validation split
os.environ['AIP_TEST_DATA_URI'] # uri to test split
Please visit `Using a managed dataset in a custom training application`_ for a detailed overview.
.. _Using a managed dataset in a custom training application: https://cloud.google.com/vertex-ai/docs/training/using-managed-datasets
It must write the model artifact to the environment variable populated by the training service:
.. code-block:: Python
os.environ['AIP_MODEL_DIR']
**Running Training**
.. code-block:: Python
job = aiplatform.CustomTrainingJob(
display_name="my-training-job",
script_path="training_script.py",
container_uri="us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-2:latest",
requirements=["gcsfs==0.7.1"],
model_serving_container_image_uri="us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-2:latest",
)
model = job.run(my_dataset,
replica_count=1,
machine_type="n1-standard-4",
accelerator_type='NVIDIA_TESLA_K80',
accelerator_count=1)
In the code block above `my_dataset` is managed dataset created in the `Dataset` section above. The `model` variable is a managed Vertex AI model that can be deployed or exported.
AutoMLs
-------
The Vertex AI SDK for Python supports AutoML tabular, image, text, video, and forecasting.
To train an AutoML tabular model:
.. code-block:: Python
dataset = aiplatform.TabularDataset('projects/my-project/location/us-central1/datasets/{DATASET_ID}')
job = aiplatform.AutoMLTabularTrainingJob(
display_name="train-automl",
optimization_prediction_type="regression",
optimization_objective="minimize-rmse",
)
model = job.run(
dataset=dataset,
target_column="target_column_name",
training_fraction_split=0.6,
validation_fraction_split=0.2,
test_fraction_split=0.2,
budget_milli_node_hours=1000,
model_display_name="my-automl-model",
disable_early_stopping=False,
)
Models
------
To get a model:
.. code-block:: Python
model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')
To upload a model:
.. code-block:: Python
model = aiplatform.Model.upload(
display_name='my-model',
artifact_uri="gs://python/to/my/model/dir",
serving_container_image_uri="us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-2:latest",
)
To deploy a model:
.. code-block:: Python
endpoint = model.deploy(machine_type="n1-standard-4",
min_replica_count=1,
max_replica_count=5
machine_type='n1-standard-4',
accelerator_type='NVIDIA_TESLA_K80',
accelerator_count=1)
Please visit `Importing models to Vertex AI`_ for a detailed overview:
.. _Importing models to Vertex AI: https://cloud.google.com/vertex-ai/docs/general/import-model
Model Evaluation
----------------
The Vertex AI SDK for Python currently supports getting model evaluation metrics for all AutoML models.
To list all model evaluations for a model:
.. code-block:: Python
model = aiplatform.Model('projects/my-project/locations/us-central1/models/{MODEL_ID}')
evaluations = model.list_model_evaluations()
To get the model evaluation resource for a given model:
.. code-block:: Python
model = aiplatform.Model('projects/my-project/locations/us-central1/models/{MODEL_ID}')
# returns the first evaluation with no arguments, you can also pass the evaluation ID
evaluation = model.get_model_evaluation()
eval_metrics = evaluation.metrics
You can also create a reference to your model evaluation directly by passing in the resource name of the model evaluation:
.. code-block:: Python
evaluation = aiplatform.ModelEvaluation(
evaluation_name='projects/my-project/locations/us-central1/models/{MODEL_ID}/evaluations/{EVALUATION_ID}')
Alternatively, you can create a reference to your evaluation by passing in the model and evaluation IDs:
.. code-block:: Python
evaluation = aiplatform.ModelEvaluation(
evaluation_name={EVALUATION_ID},
model_id={MODEL_ID})
Batch Prediction
----------------
To create a batch prediction job:
.. code-block:: Python
model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')
batch_prediction_job = model.batch_predict(
job_display_name='my-batch-prediction-job',
instances_format='csv',
machine_type='n1-standard-4',
gcs_source=['gs://path/to/my/file.csv'],
gcs_destination_prefix='gs://path/to/my/batch_prediction/results/',
service_account='my-sa@my-project.iam.gserviceaccount.com'
)
You can also create a batch prediction job asynchronously by including the `sync=False` argument:
.. code-block:: Python
batch_prediction_job = model.batch_predict(..., sync=False)
# wait for resource to be created
batch_prediction_job.wait_for_resource_creation()
# get the state
batch_prediction_job.state
# block until job is complete
batch_prediction_job.wait()
Endpoints
---------
To create an endpoint:
.. code-block:: Python
endpoint = aiplatform.Endpoint.create(display_name='my-endpoint')
To deploy a model to a created endpoint:
.. code-block:: Python
model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')
endpoint.deploy(model,
min_replica_count=1,
max_replica_count=5,
machine_type='n1-standard-4',
accelerator_type='NVIDIA_TESLA_K80',
accelerator_count=1)
To get predictions from endpoints:
.. code-block:: Python
endpoint.predict(instances=[[6.7, 3.1, 4.7, 1.5], [4.6, 3.1, 1.5, 0.2]])
To undeploy models from an endpoint:
.. code-block:: Python
endpoint.undeploy_all()
To delete an endpoint:
.. code-block:: Python
endpoint.delete()
Pipelines
---------
To create a Vertex AI Pipeline run and monitor until completion:
.. code-block:: Python
# Instantiate PipelineJob object
pl = PipelineJob(
display_name="My first pipeline",
# Whether or not to enable caching
# True = always cache pipeline step result
# False = never cache pipeline step result
# None = defer to cache option for each pipeline component in the pipeline definition
enable_caching=False,
# Local or GCS path to a compiled pipeline definition
template_path="pipeline.json",
# Dictionary containing input parameters for your pipeline
parameter_values=parameter_values,
# GCS path to act as the pipeline root
pipeline_root=pipeline_root,
)
# Execute pipeline in Vertex AI and monitor until completion
pl.run(
# Email address of service account to use for the pipeline run
# You must have iam.serviceAccounts.actAs permission on the service account to use it
service_account=service_account,
# Whether this function call should be synchronous (wait for pipeline run to finish before terminating)
# or asynchronous (return immediately)
sync=True
)
To create a Vertex AI Pipeline without monitoring until completion, use `submit` instead of `run`:
.. code-block:: Python
# Instantiate PipelineJob object
pl = PipelineJob(
display_name="My first pipeline",
# Whether or not to enable caching
# True = always cache pipeline step result
# False = never cache pipeline step result
# None = defer to cache option for each pipeline component in the pipeline definition
enable_caching=False,
# Local or GCS path to a compiled pipeline definition
template_path="pipeline.json",
# Dictionary containing input parameters for your pipeline
parameter_values=parameter_values,
# GCS path to act as the pipeline root
pipeline_root=pipeline_root,
)
# Submit the Pipeline to Vertex AI
pl.submit(
# Email address of service account to use for the pipeline run
# You must have iam.serviceAccounts.actAs permission on the service account to use it
service_account=service_account,
)
Explainable AI: Get Metadata
----------------------------
To get metadata in dictionary format from TensorFlow 1 models:
.. code-block:: Python
from google.cloud.aiplatform.explain.metadata.tf.v1 import saved_model_metadata_builder
builder = saved_model_metadata_builder.SavedModelMetadataBuilder(
'gs://python/to/my/model/dir', tags=[tf.saved_model.tag_constants.SERVING]
)
generated_md = builder.get_metadata()
To get metadata in dictionary format from TensorFlow 2 models:
.. code-block:: Python
from google.cloud.aiplatform.explain.metadata.tf.v2 import saved_model_metadata_builder
builder = saved_model_metadata_builder.SavedModelMetadataBuilder('gs://python/to/my/model/dir')
generated_md = builder.get_metadata()
To use Explanation Metadata in endpoint deployment and model upload:
.. code-block:: Python
explanation_metadata = builder.get_metadata_protobuf()
# To deploy a model to an endpoint with explanation
model.deploy(..., explanation_metadata=explanation_metadata)
# To deploy a model to a created endpoint with explanation
endpoint.deploy(..., explanation_metadata=explanation_metadata)
# To upload a model with explanation
aiplatform.Model.upload(..., explanation_metadata=explanation_metadata)
Cloud Profiler
----------------------------
Cloud Profiler allows you to profile your remote Vertex AI Training jobs on demand and visualize the results in Vertex AI Tensorboard.
To start using the profiler with TensorFlow, update your training script to include the following:
.. code-block:: Python
from google.cloud.aiplatform.training_utils import cloud_profiler
...
cloud_profiler.init()
Next, run the job with with a Vertex AI TensorBoard instance. For full details on how to do this, visit https://cloud.google.com/vertex-ai/docs/experiments/tensorboard-overview
Finally, visit your TensorBoard in your Google Cloud Console, navigate to the "Profile" tab, and click the `Capture Profile` button. This will allow users to capture profiling statistics for the running jobs.
Next Steps
~~~~~~~~~~
- Read the `Client Library Documentation`_ for Vertex AI
API to see other available methods on the client.
- Read the `Vertex AI API Product documentation`_ to learn
more about the product and see How-to Guides.
- View this `README`_ to see the full list of Cloud
APIs that we cover.
.. _Vertex AI API Product documentation: https://cloud.google.com/vertex-ai/docs
.. _README: https://github.com/googleapis/google-cloud-python/blob/main/README.rst
Raw data
{
"_id": null,
"home_page": "https://github.com/googleapis/python-aiplatform",
"name": "google-cloud-aiplatform",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Google LLC",
"author_email": "googleapis-packages@google.com",
"download_url": "https://files.pythonhosted.org/packages/1b/af/0fa64dbc7508b45ec82175752fb27641ba7f08fd179cdb811b29311cddf8/google_cloud_aiplatform-1.73.0.tar.gz",
"platform": "Posix; MacOS X; Windows",
"description": "Vertex AI SDK for Python\n=================================================\n\n\nGemini API and Generative AI on Vertex AI\n-----------------------------------------\n\n.. note::\n\n For Gemini API and Generative AI on Vertex AI, please reference `Vertex Generative AI SDK for Python`_\n.. _Vertex Generative AI SDK for Python: https://cloud.google.com/vertex-ai/generative-ai/docs/reference/python/latest\n\n-----------------------------------------\n\n|GA| |pypi| |versions| |unit-tests| |system-tests| |sample-tests|\n\n`Vertex AI`_: Google Vertex AI is an integrated suite of machine learning tools and services for building and using ML models with AutoML or custom code. It offers both novices and experts the best workbench for the entire machine learning development lifecycle.\n\n- `Client Library Documentation`_\n- `Product Documentation`_\n\n.. |GA| image:: https://img.shields.io/badge/support-ga-gold.svg\n :target: https://github.com/googleapis/google-cloud-python/blob/main/README.rst#general-availability\n.. |pypi| image:: https://img.shields.io/pypi/v/google-cloud-aiplatform.svg\n :target: https://pypi.org/project/google-cloud-aiplatform/\n.. |versions| image:: https://img.shields.io/pypi/pyversions/google-cloud-aiplatform.svg\n :target: https://pypi.org/project/google-cloud-aiplatform/\n.. |unit-tests| image:: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-unit-tests.svg\n :target: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-unit-tests.html\n.. |system-tests| image:: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-system-tests.svg\n :target: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-system-tests.html\n.. |sample-tests| image:: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-sample-tests.svg\n :target: https://storage.googleapis.com/cloud-devrel-public/python-aiplatform/badges/sdk-sample-tests.html\n.. _Vertex AI: https://cloud.google.com/vertex-ai/docs\n.. _Client Library Documentation: https://cloud.google.com/python/docs/reference/aiplatform/latest\n.. _Product Documentation: https://cloud.google.com/vertex-ai/docs\n\nQuick Start\n-----------\n\nIn order to use this library, you first need to go through the following steps:\n\n1. `Select or create a Cloud Platform project.`_\n2. `Enable billing for your project.`_\n3. `Enable the Vertex AI API.`_\n4. `Setup Authentication.`_\n\n.. _Select or create a Cloud Platform project.: https://console.cloud.google.com/project\n.. _Enable billing for your project.: https://cloud.google.com/billing/docs/how-to/modify-project#enable_billing_for_a_project\n.. _Enable the Vertex AI API.: https://cloud.google.com/vertex-ai/docs/start/use-vertex-ai-python-sdk\n.. _Setup Authentication.: https://googleapis.dev/python/google-api-core/latest/auth.html\n\nInstallation\n~~~~~~~~~~~~\n\nInstall this library in a `virtualenv`_ using pip. `virtualenv`_ is a tool to\ncreate isolated Python environments. The basic problem it addresses is one of\ndependencies and versions, and indirectly permissions.\n\nWith `virtualenv`_, it's possible to install this library without needing system\ninstall permissions, and without clashing with the installed system\ndependencies.\n\n.. _virtualenv: https://virtualenv.pypa.io/en/latest/\n\n\nMac/Linux\n^^^^^^^^^\n\n.. code-block:: console\n\n pip install virtualenv\n virtualenv <your-env>\n source <your-env>/bin/activate\n <your-env>/bin/pip install google-cloud-aiplatform\n\n\nWindows\n^^^^^^^\n\n.. code-block:: console\n\n pip install virtualenv\n virtualenv <your-env>\n <your-env>\\Scripts\\activate\n <your-env>\\Scripts\\pip.exe install google-cloud-aiplatform\n\n\nSupported Python Versions\n^^^^^^^^^^^^^^^^^^^^^^^^^\nPython >= 3.8\n\nDeprecated Python Versions\n^^^^^^^^^^^^^^^^^^^^^^^^^^\nPython <= 3.7.\n\nThe last version of this library compatible with Python 3.6 is google-cloud-aiplatform==1.12.1.\n\nOverview\n~~~~~~~~\nThis section provides a brief overview of the Vertex AI SDK for Python. You can also reference the notebooks in `vertex-ai-samples`_ for examples.\n\n.. _vertex-ai-samples: https://github.com/GoogleCloudPlatform/vertex-ai-samples/tree/main/notebooks/community/sdk\n\nAll publicly available SDK features can be found in the :code:`google/cloud/aiplatform` directory.\nUnder the hood, Vertex SDK builds on top of GAPIC, which stands for Google API CodeGen.\nThe GAPIC library code sits in :code:`google/cloud/aiplatform_v1` and :code:`google/cloud/aiplatform_v1beta1`,\nand it is auto-generated from Google's service proto files.\n\nFor most developers' programmatic needs, they can follow these steps to figure out which libraries to import:\n\n1. Look through :code:`google/cloud/aiplatform` first -- Vertex SDK's APIs will almost always be easier to use and more concise comparing with GAPIC\n2. If the feature that you are looking for cannot be found there, look through :code:`aiplatform_v1` to see if it's available in GAPIC\n3. If it is still in beta phase, it will be available in :code:`aiplatform_v1beta1`\n\nIf none of the above scenarios could help you find the right tools for your task, please feel free to open a github issue and send us a feature request.\n\nImporting\n^^^^^^^^^\nVertex AI SDK resource based functionality can be used by importing the following namespace:\n\n.. code-block:: Python\n\n from google.cloud import aiplatform\n\nInitialization\n^^^^^^^^^^^^^^\nInitialize the SDK to store common configurations that you use with the SDK.\n\n.. code-block:: Python\n\n aiplatform.init(\n # your Google Cloud Project ID or number\n # environment default used is not set\n project='my-project',\n\n # the Vertex AI region you will use\n # defaults to us-central1\n location='us-central1',\n\n # Google Cloud Storage bucket in same region as location\n # used to stage artifacts\n staging_bucket='gs://my_staging_bucket',\n\n # custom google.auth.credentials.Credentials\n # environment default credentials used if not set\n credentials=my_credentials,\n\n # customer managed encryption key resource name\n # will be applied to all Vertex AI resources if set\n encryption_spec_key_name=my_encryption_key_name,\n\n # the name of the experiment to use to track\n # logged metrics and parameters\n experiment='my-experiment',\n\n # description of the experiment above\n experiment_description='my experiment description'\n )\n\nDatasets\n^^^^^^^^\nVertex AI provides managed tabular, text, image, and video datasets. In the SDK, datasets can be used downstream to\ntrain models.\n\nTo create a tabular dataset:\n\n.. code-block:: Python\n\n my_dataset = aiplatform.TabularDataset.create(\n display_name=\"my-dataset\", gcs_source=['gs://path/to/my/dataset.csv'])\n\nYou can also create and import a dataset in separate steps:\n\n.. code-block:: Python\n\n from google.cloud import aiplatform\n\n my_dataset = aiplatform.TextDataset.create(\n display_name=\"my-dataset\")\n\n my_dataset.import_data(\n gcs_source=['gs://path/to/my/dataset.csv'],\n import_schema_uri=aiplatform.schema.dataset.ioformat.text.multi_label_classification\n )\n\nTo get a previously created Dataset:\n\n.. code-block:: Python\n\n dataset = aiplatform.ImageDataset('projects/my-project/location/us-central1/datasets/{DATASET_ID}')\n\nVertex AI supports a variety of dataset schemas. References to these schemas are available under the\n:code:`aiplatform.schema.dataset` namespace. For more information on the supported dataset schemas please refer to the\n`Preparing data docs`_.\n\n.. _Preparing data docs: https://cloud.google.com/ai-platform-unified/docs/datasets/prepare\n\nTraining\n^^^^^^^^\nThe Vertex AI SDK for Python allows you train Custom and AutoML Models.\n\nYou can train custom models using a custom Python script, custom Python package, or container.\n\n**Preparing Your Custom Code**\n\nVertex AI custom training enables you to train on Vertex AI datasets and produce Vertex AI models. To do so your\nscript must adhere to the following contract:\n\nIt must read datasets from the environment variables populated by the training service:\n\n.. code-block:: Python\n\n os.environ['AIP_DATA_FORMAT'] # provides format of data\n os.environ['AIP_TRAINING_DATA_URI'] # uri to training split\n os.environ['AIP_VALIDATION_DATA_URI'] # uri to validation split\n os.environ['AIP_TEST_DATA_URI'] # uri to test split\n\nPlease visit `Using a managed dataset in a custom training application`_ for a detailed overview.\n\n.. _Using a managed dataset in a custom training application: https://cloud.google.com/vertex-ai/docs/training/using-managed-datasets\n\nIt must write the model artifact to the environment variable populated by the training service:\n\n.. code-block:: Python\n\n os.environ['AIP_MODEL_DIR']\n\n**Running Training**\n\n.. code-block:: Python\n\n job = aiplatform.CustomTrainingJob(\n display_name=\"my-training-job\",\n script_path=\"training_script.py\",\n container_uri=\"us-docker.pkg.dev/vertex-ai/training/tf-cpu.2-2:latest\",\n requirements=[\"gcsfs==0.7.1\"],\n model_serving_container_image_uri=\"us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-2:latest\",\n )\n\n model = job.run(my_dataset,\n replica_count=1,\n machine_type=\"n1-standard-4\",\n accelerator_type='NVIDIA_TESLA_K80',\n accelerator_count=1)\n\nIn the code block above `my_dataset` is managed dataset created in the `Dataset` section above. The `model` variable is a managed Vertex AI model that can be deployed or exported.\n\n\nAutoMLs\n-------\nThe Vertex AI SDK for Python supports AutoML tabular, image, text, video, and forecasting.\n\nTo train an AutoML tabular model:\n\n.. code-block:: Python\n\n dataset = aiplatform.TabularDataset('projects/my-project/location/us-central1/datasets/{DATASET_ID}')\n\n job = aiplatform.AutoMLTabularTrainingJob(\n display_name=\"train-automl\",\n optimization_prediction_type=\"regression\",\n optimization_objective=\"minimize-rmse\",\n )\n\n model = job.run(\n dataset=dataset,\n target_column=\"target_column_name\",\n training_fraction_split=0.6,\n validation_fraction_split=0.2,\n test_fraction_split=0.2,\n budget_milli_node_hours=1000,\n model_display_name=\"my-automl-model\",\n disable_early_stopping=False,\n )\n\n\nModels\n------\nTo get a model:\n\n\n.. code-block:: Python\n\n model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')\n\n\n\nTo upload a model:\n\n.. code-block:: Python\n\n model = aiplatform.Model.upload(\n display_name='my-model',\n artifact_uri=\"gs://python/to/my/model/dir\",\n serving_container_image_uri=\"us-docker.pkg.dev/vertex-ai/prediction/tf2-cpu.2-2:latest\",\n )\n\n\n\nTo deploy a model:\n\n\n.. code-block:: Python\n\n endpoint = model.deploy(machine_type=\"n1-standard-4\",\n min_replica_count=1,\n max_replica_count=5\n machine_type='n1-standard-4',\n accelerator_type='NVIDIA_TESLA_K80',\n accelerator_count=1)\n\n\nPlease visit `Importing models to Vertex AI`_ for a detailed overview:\n\n.. _Importing models to Vertex AI: https://cloud.google.com/vertex-ai/docs/general/import-model\n\nModel Evaluation\n----------------\n\nThe Vertex AI SDK for Python currently supports getting model evaluation metrics for all AutoML models.\n\nTo list all model evaluations for a model:\n\n.. code-block:: Python\n\n model = aiplatform.Model('projects/my-project/locations/us-central1/models/{MODEL_ID}')\n\n evaluations = model.list_model_evaluations()\n\n\nTo get the model evaluation resource for a given model:\n\n.. code-block:: Python\n\n model = aiplatform.Model('projects/my-project/locations/us-central1/models/{MODEL_ID}')\n\n # returns the first evaluation with no arguments, you can also pass the evaluation ID\n evaluation = model.get_model_evaluation()\n\n eval_metrics = evaluation.metrics\n\n\nYou can also create a reference to your model evaluation directly by passing in the resource name of the model evaluation:\n\n.. code-block:: Python\n\n evaluation = aiplatform.ModelEvaluation(\n evaluation_name='projects/my-project/locations/us-central1/models/{MODEL_ID}/evaluations/{EVALUATION_ID}')\n\nAlternatively, you can create a reference to your evaluation by passing in the model and evaluation IDs:\n\n.. code-block:: Python\n\n evaluation = aiplatform.ModelEvaluation(\n evaluation_name={EVALUATION_ID},\n model_id={MODEL_ID})\n\n\nBatch Prediction\n----------------\n\nTo create a batch prediction job:\n\n.. code-block:: Python\n\n model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')\n\n batch_prediction_job = model.batch_predict(\n job_display_name='my-batch-prediction-job',\n instances_format='csv',\n machine_type='n1-standard-4',\n gcs_source=['gs://path/to/my/file.csv'],\n gcs_destination_prefix='gs://path/to/my/batch_prediction/results/',\n service_account='my-sa@my-project.iam.gserviceaccount.com'\n )\n\nYou can also create a batch prediction job asynchronously by including the `sync=False` argument:\n\n.. code-block:: Python\n\n batch_prediction_job = model.batch_predict(..., sync=False)\n\n # wait for resource to be created\n batch_prediction_job.wait_for_resource_creation()\n\n # get the state\n batch_prediction_job.state\n\n # block until job is complete\n batch_prediction_job.wait()\n\n\nEndpoints\n---------\n\nTo create an endpoint:\n\n.. code-block:: Python\n\n endpoint = aiplatform.Endpoint.create(display_name='my-endpoint')\n\nTo deploy a model to a created endpoint:\n\n.. code-block:: Python\n\n model = aiplatform.Model('/projects/my-project/locations/us-central1/models/{MODEL_ID}')\n\n endpoint.deploy(model,\n min_replica_count=1,\n max_replica_count=5,\n machine_type='n1-standard-4',\n accelerator_type='NVIDIA_TESLA_K80',\n accelerator_count=1)\n\nTo get predictions from endpoints:\n\n.. code-block:: Python\n\n endpoint.predict(instances=[[6.7, 3.1, 4.7, 1.5], [4.6, 3.1, 1.5, 0.2]])\n\nTo undeploy models from an endpoint:\n\n.. code-block:: Python\n\n endpoint.undeploy_all()\n\nTo delete an endpoint:\n\n.. code-block:: Python\n\n endpoint.delete()\n\n\nPipelines\n---------\n\nTo create a Vertex AI Pipeline run and monitor until completion:\n\n.. code-block:: Python\n\n # Instantiate PipelineJob object\n pl = PipelineJob(\n display_name=\"My first pipeline\",\n\n # Whether or not to enable caching\n # True = always cache pipeline step result\n # False = never cache pipeline step result\n # None = defer to cache option for each pipeline component in the pipeline definition\n enable_caching=False,\n\n # Local or GCS path to a compiled pipeline definition\n template_path=\"pipeline.json\",\n\n # Dictionary containing input parameters for your pipeline\n parameter_values=parameter_values,\n\n # GCS path to act as the pipeline root\n pipeline_root=pipeline_root,\n )\n\n # Execute pipeline in Vertex AI and monitor until completion\n pl.run(\n # Email address of service account to use for the pipeline run\n # You must have iam.serviceAccounts.actAs permission on the service account to use it\n service_account=service_account,\n\n # Whether this function call should be synchronous (wait for pipeline run to finish before terminating)\n # or asynchronous (return immediately)\n sync=True\n )\n\nTo create a Vertex AI Pipeline without monitoring until completion, use `submit` instead of `run`:\n\n.. code-block:: Python\n\n # Instantiate PipelineJob object\n pl = PipelineJob(\n display_name=\"My first pipeline\",\n\n # Whether or not to enable caching\n # True = always cache pipeline step result\n # False = never cache pipeline step result\n # None = defer to cache option for each pipeline component in the pipeline definition\n enable_caching=False,\n\n # Local or GCS path to a compiled pipeline definition\n template_path=\"pipeline.json\",\n\n # Dictionary containing input parameters for your pipeline\n parameter_values=parameter_values,\n\n # GCS path to act as the pipeline root\n pipeline_root=pipeline_root,\n )\n\n # Submit the Pipeline to Vertex AI\n pl.submit(\n # Email address of service account to use for the pipeline run\n # You must have iam.serviceAccounts.actAs permission on the service account to use it\n service_account=service_account,\n )\n\n\nExplainable AI: Get Metadata\n----------------------------\n\nTo get metadata in dictionary format from TensorFlow 1 models:\n\n.. code-block:: Python\n\n from google.cloud.aiplatform.explain.metadata.tf.v1 import saved_model_metadata_builder\n\n builder = saved_model_metadata_builder.SavedModelMetadataBuilder(\n 'gs://python/to/my/model/dir', tags=[tf.saved_model.tag_constants.SERVING]\n )\n generated_md = builder.get_metadata()\n\nTo get metadata in dictionary format from TensorFlow 2 models:\n\n.. code-block:: Python\n\n from google.cloud.aiplatform.explain.metadata.tf.v2 import saved_model_metadata_builder\n\n builder = saved_model_metadata_builder.SavedModelMetadataBuilder('gs://python/to/my/model/dir')\n generated_md = builder.get_metadata()\n\nTo use Explanation Metadata in endpoint deployment and model upload:\n\n.. code-block:: Python\n\n explanation_metadata = builder.get_metadata_protobuf()\n\n # To deploy a model to an endpoint with explanation\n model.deploy(..., explanation_metadata=explanation_metadata)\n\n # To deploy a model to a created endpoint with explanation\n endpoint.deploy(..., explanation_metadata=explanation_metadata)\n\n # To upload a model with explanation\n aiplatform.Model.upload(..., explanation_metadata=explanation_metadata)\n\n\nCloud Profiler\n----------------------------\n\nCloud Profiler allows you to profile your remote Vertex AI Training jobs on demand and visualize the results in Vertex AI Tensorboard.\n\nTo start using the profiler with TensorFlow, update your training script to include the following:\n\n.. code-block:: Python\n\n from google.cloud.aiplatform.training_utils import cloud_profiler\n ...\n cloud_profiler.init()\n\nNext, run the job with with a Vertex AI TensorBoard instance. For full details on how to do this, visit https://cloud.google.com/vertex-ai/docs/experiments/tensorboard-overview\n\nFinally, visit your TensorBoard in your Google Cloud Console, navigate to the \"Profile\" tab, and click the `Capture Profile` button. This will allow users to capture profiling statistics for the running jobs.\n\n\nNext Steps\n~~~~~~~~~~\n\n- Read the `Client Library Documentation`_ for Vertex AI\n API to see other available methods on the client.\n- Read the `Vertex AI API Product documentation`_ to learn\n more about the product and see How-to Guides.\n- View this `README`_ to see the full list of Cloud\n APIs that we cover.\n\n.. _Vertex AI API Product documentation: https://cloud.google.com/vertex-ai/docs\n.. _README: https://github.com/googleapis/google-cloud-python/blob/main/README.rst\n",
"bugtrack_url": null,
"license": "Apache 2.0",
"summary": "Vertex AI API client library",
"version": "1.73.0",
"project_urls": {
"Homepage": "https://github.com/googleapis/python-aiplatform"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1a5c6858327c9344561699d2bbe81f4104374df3defc67f97df233142153c6e0",
"md5": "e7ec0a7baac851b3202fdaf680c77bb8",
"sha256": "6f9aebc1cb2277048093f17214c5f4ec9129fa347b8b22d784f780b12b8865a9"
},
"downloads": -1,
"filename": "google_cloud_aiplatform-1.73.0-py2.py3-none-any.whl",
"has_sig": false,
"md5_digest": "e7ec0a7baac851b3202fdaf680c77bb8",
"packagetype": "bdist_wheel",
"python_version": "py2.py3",
"requires_python": ">=3.8",
"size": 6270768,
"upload_time": "2024-11-19T21:20:26",
"upload_time_iso_8601": "2024-11-19T21:20:26.567487Z",
"url": "https://files.pythonhosted.org/packages/1a/5c/6858327c9344561699d2bbe81f4104374df3defc67f97df233142153c6e0/google_cloud_aiplatform-1.73.0-py2.py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "1baf0fa64dbc7508b45ec82175752fb27641ba7f08fd179cdb811b29311cddf8",
"md5": "0be73ad2ff2dff2544a2e140d8e757f9",
"sha256": "687d4d6dd26439db42d38b835ea0da7ebb75c20ca8e17666669536b253637e74"
},
"downloads": -1,
"filename": "google_cloud_aiplatform-1.73.0.tar.gz",
"has_sig": false,
"md5_digest": "0be73ad2ff2dff2544a2e140d8e757f9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 7543164,
"upload_time": "2024-11-19T21:20:30",
"upload_time_iso_8601": "2024-11-19T21:20:30.558389Z",
"url": "https://files.pythonhosted.org/packages/1b/af/0fa64dbc7508b45ec82175752fb27641ba7f08fd179cdb811b29311cddf8/google_cloud_aiplatform-1.73.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-19 21:20:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "googleapis",
"github_project": "python-aiplatform",
"travis_ci": false,
"coveralls": true,
"github_actions": false,
"lcname": "google-cloud-aiplatform"
}