|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from typing import List, Optional |
| 16 | + |
| 17 | +from google.cloud import aiplatform |
| 18 | + |
| 19 | + |
| 20 | +# [START aiplatform_sdk_create_training_pipeline_forecasting_tft_sample] |
| 21 | +def create_training_pipeline_forecasting_temporal_fusion_transformer_sample( |
| 22 | + project: str, |
| 23 | + display_name: str, |
| 24 | + dataset_id: str, |
| 25 | + location: str = "us-central1", |
| 26 | + model_display_name: str = "my_model", |
| 27 | + target_column: str = "target_column", |
| 28 | + time_column: str = "date", |
| 29 | + time_series_identifier_column: str = "time_series_id", |
| 30 | + unavailable_at_forecast_columns: List[str] = [], |
| 31 | + available_at_forecast_columns: List[str] = [], |
| 32 | + forecast_horizon: int = 1, |
| 33 | + data_granularity_unit: str = "week", |
| 34 | + data_granularity_count: int = 1, |
| 35 | + training_fraction_split: float = 0.8, |
| 36 | + validation_fraction_split: float = 0.1, |
| 37 | + test_fraction_split: float = 0.1, |
| 38 | + budget_milli_node_hours: int = 8000, |
| 39 | + timestamp_split_column_name: str = "timestamp_split", |
| 40 | + weight_column: str = "weight", |
| 41 | + time_series_attribute_columns: List[str] = [], |
| 42 | + context_window: int = 0, |
| 43 | + export_evaluated_data_items: bool = False, |
| 44 | + export_evaluated_data_items_bigquery_destination_uri: Optional[str] = None, |
| 45 | + export_evaluated_data_items_override_destination: bool = False, |
| 46 | + validation_options: Optional[str] = None, |
| 47 | + predefined_split_column_name: Optional[str] = None, |
| 48 | + sync: bool = True, |
| 49 | +): |
| 50 | + aiplatform.init(project=project, location=location) |
| 51 | + |
| 52 | + # Create training job |
| 53 | + forecasting_tft_job = aiplatform.TemporalFusionTransformerForecastingTrainingJob( |
| 54 | + display_name=display_name, |
| 55 | + optimization_objective="minimize-rmse", |
| 56 | + ) |
| 57 | + |
| 58 | + # Retrieve existing dataset |
| 59 | + dataset = aiplatform.TimeSeriesDataset(dataset_id) |
| 60 | + |
| 61 | + # Run training job |
| 62 | + model = forecasting_tft_job.run( |
| 63 | + dataset=dataset, |
| 64 | + target_column=target_column, |
| 65 | + time_column=time_column, |
| 66 | + time_series_identifier_column=time_series_identifier_column, |
| 67 | + unavailable_at_forecast_columns=unavailable_at_forecast_columns, |
| 68 | + available_at_forecast_columns=available_at_forecast_columns, |
| 69 | + forecast_horizon=forecast_horizon, |
| 70 | + data_granularity_unit=data_granularity_unit, |
| 71 | + data_granularity_count=data_granularity_count, |
| 72 | + training_fraction_split=training_fraction_split, |
| 73 | + validation_fraction_split=validation_fraction_split, |
| 74 | + test_fraction_split=test_fraction_split, |
| 75 | + predefined_split_column_name=predefined_split_column_name, |
| 76 | + timestamp_split_column_name=timestamp_split_column_name, |
| 77 | + weight_column=weight_column, |
| 78 | + time_series_attribute_columns=time_series_attribute_columns, |
| 79 | + context_window=context_window, |
| 80 | + export_evaluated_data_items=export_evaluated_data_items, |
| 81 | + export_evaluated_data_items_bigquery_destination_uri=export_evaluated_data_items_bigquery_destination_uri, |
| 82 | + export_evaluated_data_items_override_destination=export_evaluated_data_items_override_destination, |
| 83 | + validation_options=validation_options, |
| 84 | + budget_milli_node_hours=budget_milli_node_hours, |
| 85 | + model_display_name=model_display_name, |
| 86 | + sync=sync, |
| 87 | + ) |
| 88 | + |
| 89 | + model.wait() |
| 90 | + |
| 91 | + print(model.display_name) |
| 92 | + print(model.resource_name) |
| 93 | + print(model.uri) |
| 94 | + return model |
| 95 | + |
| 96 | + |
| 97 | +# [END aiplatform_sdk_create_training_pipeline_forecasting_tft_sample] |
0 commit comments