{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# How to upgrade existing AmazonForecast predictor to AutoPredictors\n",
"\n",
"Helps advanced users start with Amazon Forecast Upgrade to AutoPredictor Feature. \n",
"\n",
"Predictors created with AutoML or manual selection can be upgraded to an AutoPredictor. Upgrading an existing to AutoPredictor will transfer all the relevant predictor configuration settings and create an AutoPredictor using the same datasets.\n",
"\n",
"After Upgrading to AutoPredictor, the original predictor will remain active and the upgraded predictor will have a separate Predictor ARN. This enables you to compare accuracy metrics between the two predictors, and you can still generate forecasts with the original predictor.\n",
"\n",
"This allows users to conviently upgrade existing predictor(s) without having the need to pass all the exiting configurations.\n",
"\n",
"The demo notebook provides guidance to upgrade an existing Predictors which were created via CreatePredictor API to upgrade to more advances AutoPredictor which offer various benifits. Refer https://docs.aws.amazon.com/forecast/latest/dg/howitworks-predictor.html for details.\n",
"\n",
"Prerequisites: \n",
"[AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/installing.html) . \n",
"\n",
"For more informations about APIs, please check the [documentation](https://docs.aws.amazon.com/forecast/latest/dg/what-is-forecast.html)\n",
"\n",
"**Note that this notebook is meant for customers who already have existing Predictors and want to move to AutoPredictor for the added functionalities.**\n",
"\n",
"\n",
"## Table Of Contents\n",
"* [Setting up](#setup)\n",
"* [Check Predictors eligible for Upgrade](#upgradableCheck)\n",
"* [Upgrading to AutoPredictor](#upgradeToAuto)\n",
"\n",
"**Read Every Cell FULLY before executing it**\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"import time\n",
"\n",
"import boto3\n",
"\n",
"# importing forecast notebook utility from notebooks/common directory\n",
"sys.path.insert( 0, os.path.abspath(\"../../common\") )\n",
"import util"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"text_widget_region = util.create_text_widget( \"region\", \"input region name.\", default_value=\"us-west-2\" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"region = text_widget_region.value\n",
"assert region, \"region not set.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"session = boto3.Session(region_name=region)\n",
"BETA = 'https://seer-beta-service.us-west-2.amazonaws.com'\n",
"forecast = session.client(service_name='forecast-auto', endpoint_url=BETA)\n",
"#forecast = session.client(service_name='forecast')\n",
"forecastquery = session.client(service_name='forecastquery')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Check Predictors eligible for Upgrade\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this section, we go over the Predictors in your account to see if a Predictor is upgradable and if there is, we upgrade that Predictor."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we list the Predictor for your account. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"li=forecast.list_predictors()['Predictors']\n",
"li"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we look for a Predictor for which is eligible for upgrade.\n",
"\n",
"Notice that **IsAutoPredictor** is **False** for Predictors created earlier through CreatePredictor API\n",
"\n",
"All such Predictors are eligible for upgrade using the new [CreateAutoPredictor API]()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"upgradablePredictor = next((x for x in li if x[\"IsAutoPredictor\"] == False), None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If no predictor is elegible for upgrade, we terminate the notebook exeuction"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if upgradablePredictor == None:\n",
" print(\"No predictor is eligible for upgrade! You can create a new AutoPredictor using https://github.com/aws-samples/amazon-forecast-samples/tree/main/notebooks/basic/Getting_Started\")\n",
" sys.exit(\"Not executing anything further!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below we list the Predictor Arn which is eligible for Upgrade and describe that to see the Predictor charecteristics."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"upgradablePredictor"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"forecast.describe_predictor(PredictorArn=upgradablePredictor[\"PredictorArn\"])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Upgrading to AutoPredictor "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this section, we pick the existing predictor selected in the previous section and upgrate that to an AutoPredictor."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"project = 'ws_upgrade_predictor' # Replace this with a unique name here, make sure the entire name is < 30 characters."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"newUpgradePredictorName=project"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For Upgrading the predictor, we pass the referencePredictor Arn an an input to CreateAutoPredictor API. You can pass additional inputs like tags but rest of the predictor configuration cannot be changed.\n",
"\n",
"Here referencePredictorArn will be the predictorArn which is upgradable above."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"referencePredictorArn=upgradablePredictor[\"PredictorArn\"]\n",
"\n",
"print(\"ReferencePredictorArn: \", referencePredictorArn)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Upgrading an existing Predictor is very simple and does not require a lot of input.\n",
"\n",
"Amazon Forecast takes the old predictor as a reference and creates a new AutoPredictor using the datasets for the existing predictor."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"create_auto_predictor_response=forecast.create_auto_predictor(PredictorName=newUpgradePredictorName, \n",
" ReferencePredictorArn=referencePredictorArn)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"upgradedPredictor=create_auto_predictor_response['PredictorArn']"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"New Predictor Arn: \", upgradedPredictor)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we wait for the new AutoPredictor to complete and it can take upto 2-3 hours."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Waiting for Predictor creation to complete.\")\n",
"\n",
"status = util.wait(lambda: forecast.describe_auto_predictor(PredictorArn=upgradedPredictor))\n",
"\n",
"describe_auto_predictor_response = forecast.describe_auto_predictor(PredictorArn=upgradedPredictor)\n",
"print(f\"\\n\\nThe Predictor with ARN **{upgradedPredictor}** is now **{describe_auto_predictor_response['Status']}**.\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we describe the new AutoPredictor Accuracy metrics and details about the auto predictor"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"describe_auto_predictor_response"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"forecast.get_accuracy_metrics(PredictorArn=upgradedPredictor)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Cleanup\n",
"\n",
"Once we have completed the above steps, we can start to cleanup the resources we created. All the created resources can be deleted using `delete_resource_tree` and it is an asynchronous operation, so we have added the helpful `wait_till_delete` function. To learn more about deleting a parent resource and all its child resources, visit [DeleteResourceTree](https://docs.aws.amazon.com/forecast/latest/dg/API_DeleteResourceTree.html) API. \n",
"Resource Limits documented here.\n",
"\n",
"Note that below cells have been intentionally commented out to prevent resource from being deleted. Uncomment below cells for deleting resources."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#util.wait_till_delete(lambda: forecast.delete_resource_tree(ResourceArn=upgradedPredictor))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
},
"toc": {
"collapse_to_match_collapsible_headings": false,
"colors": {
"hover_highlight": "#DAA520",
"navigate_num": "#000000",
"navigate_text": "#333333",
"running_highlight": "#FF0000",
"selected_highlight": "#FFD700",
"sidebar_border": "#EEEEEE",
"wrapper_background": "#FFFFFF"
},
"moveMenuLeft": true,
"nav_menu": {
"height": "253px",
"width": "254px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"threshold": 4,
"toc_cell": false,
"toc_section_display": "block",
"toc_window_display": false,
"widenNotebook": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}