Skip to content

Commit 6424515

Browse files
googlyrahmanbhandarivijay-pnggurusai-voleti
authored
Migrate gsutil usage to gcloud storage (#4339)
* Migrate gsutil usage to gcloud storage * Manual Changes * Manual Changes * Fix: Updated gcloud storage command without formatting * Manual Changes * Manual Changes * Revert "Manual Changes" This reverts commit a7a7bda. * Manual Changes * Revert "Manual Changes" This reverts commit a7a7bda. * Manual Changes * Revert "Manual Changes" This reverts commit 71c777d. * Manual Changes * Manual changes * Changes for 4339 * Changes for 4339 * Changes for 4339 * Fix: Applied linter formatting and resolved style issues * gcloud to gsutilchanges for 4339 * removed gsutil to gcloud migration * Manual changes --------- Co-authored-by: bhandarivijay <bhandarivijay@google.com> Co-authored-by: gurusai-voleti <gvoleti@google.com>
1 parent 8d22b22 commit 6424515

8 files changed

+159
-140
lines changed

community-content/tf_agents_bandits_movie_recommendation_with_kfp_and_vertex_sdk/step_by_step_sdk_tf_agents_bandits_movie_recommendation/step_by_step_sdk_tf_agents_bandits_movie_recommendation.ipynb

Lines changed: 75 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@
398398
"if not IS_GOOGLE_CLOUD_NOTEBOOK:\n",
399399
" if \"google.colab\" in sys.modules:\n",
400400
" from google.colab import auth as google_auth\n",
401+
"\n",
401402
" google_auth.authenticate_user()\n",
402403
"\n",
403404
" # If you are running this notebook locally, replace the string below with the\n",
@@ -472,7 +473,7 @@
472473
},
473474
"outputs": [],
474475
"source": [
475-
"! gsutil mb -l $REGION $BUCKET_NAME"
476+
"! gcloud storage buckets create --location $REGION $BUCKET_NAME"
476477
]
477478
},
478479
{
@@ -492,7 +493,7 @@
492493
},
493494
"outputs": [],
494495
"source": [
495-
"! gsutil ls -al $BUCKET_NAME"
496+
"! gcloud storage ls --all-versions --long $BUCKET_NAME"
496497
]
497498
},
498499
{
@@ -565,7 +566,7 @@
565566
"outputs": [],
566567
"source": [
567568
"# Copy the sample data into your DATA_PATH\n",
568-
"! gsutil cp \"gs://cloud-samples-data/vertex-ai/community-content/tf_agents_bandits_movie_recommendation_with_kfp_and_vertex_sdk/u.data\" $DATA_PATH"
569+
"! gcloud storage cp \"gs://cloud-samples-data/vertex-ai/community-content/tf_agents_bandits_movie_recommendation_with_kfp_and_vertex_sdk/u.data\" $DATA_PATH"
569570
]
570571
},
571572
{
@@ -579,11 +580,15 @@
579580
"# Set hyperparameters.\n",
580581
"BATCH_SIZE = 8 # @param {type:\"integer\"} Training and prediction batch size.\n",
581582
"TRAINING_LOOPS = 5 # @param {type:\"integer\"} Number of training iterations.\n",
582-
"STEPS_PER_LOOP = 2 # @param {type:\"integer\"} Number of driver steps per training iteration.\n",
583+
"STEPS_PER_LOOP = (\n",
584+
" 2 # @param {type:\"integer\"} Number of driver steps per training iteration.\n",
585+
")\n",
583586
"\n",
584587
"# Set MovieLens simulation environment parameters.\n",
585588
"RANK_K = 20 # @param {type:\"integer\"} Rank for matrix factorization in the MovieLens environment; also the observation dimension.\n",
586-
"NUM_ACTIONS = 20 # @param {type:\"integer\"} Number of actions (movie items) to choose from.\n",
589+
"NUM_ACTIONS = (\n",
590+
" 20 # @param {type:\"integer\"} Number of actions (movie items) to choose from.\n",
591+
")\n",
587592
"PER_ARM = False # Use the non-per-arm version of the MovieLens environment.\n",
588593
"\n",
589594
"# Set agent parameters.\n",
@@ -621,7 +626,8 @@
621626
"source": [
622627
"# Define RL environment.\n",
623628
"env = movielens_py_environment.MovieLensPyEnvironment(\n",
624-
" DATA_PATH, RANK_K, BATCH_SIZE, num_movies=NUM_ACTIONS, csv_delimiter=\"\\t\")\n",
629+
" DATA_PATH, RANK_K, BATCH_SIZE, num_movies=NUM_ACTIONS, csv_delimiter=\"\\t\"\n",
630+
")\n",
625631
"environment = tf_py_environment.TFPyEnvironment(env)\n",
626632
"\n",
627633
"# Define RL agent/algorithm.\n",
@@ -631,15 +637,17 @@
631637
" tikhonov_weight=TIKHONOV_WEIGHT,\n",
632638
" alpha=AGENT_ALPHA,\n",
633639
" dtype=tf.float32,\n",
634-
" accepts_per_arm_features=PER_ARM)\n",
640+
" accepts_per_arm_features=PER_ARM,\n",
641+
")\n",
635642
"print(\"TimeStep Spec (for each batch):\\n\", agent.time_step_spec, \"\\n\")\n",
636643
"print(\"Action Spec (for each batch):\\n\", agent.action_spec, \"\\n\")\n",
637644
"print(\"Reward Spec (for each batch):\\n\", environment.reward_spec(), \"\\n\")\n",
638645
"\n",
639646
"# Define RL metric.\n",
640647
"optimal_reward_fn = functools.partial(\n",
641648
" environment_utilities.compute_optimal_reward_with_movielens_environment,\n",
642-
" environment=environment)\n",
649+
" environment=environment,\n",
650+
")\n",
643651
"regret_metric = tf_bandit_metrics.RegretMetric(optimal_reward_fn)\n",
644652
"metrics = [regret_metric]"
645653
]
@@ -704,35 +712,38 @@
704712
" if training_data_spec_transformation_fn is None:\n",
705713
" data_spec = agent.policy.trajectory_spec\n",
706714
" else:\n",
707-
" data_spec = training_data_spec_transformation_fn(\n",
708-
" agent.policy.trajectory_spec)\n",
709-
" replay_buffer = trainer.get_replay_buffer(data_spec, environment.batch_size,\n",
710-
" steps_per_loop)\n",
715+
" data_spec = training_data_spec_transformation_fn(agent.policy.trajectory_spec)\n",
716+
" replay_buffer = trainer.get_replay_buffer(\n",
717+
" data_spec, environment.batch_size, steps_per_loop\n",
718+
" )\n",
711719
"\n",
712720
" # `step_metric` records the number of individual rounds of bandit interaction;\n",
713721
" # that is, (number of trajectories) * batch_size.\n",
714722
" step_metric = tf_metrics.EnvironmentSteps()\n",
715723
" metrics = [\n",
716724
" tf_metrics.NumberOfEpisodes(),\n",
717-
" tf_metrics.AverageEpisodeLengthMetric(batch_size=environment.batch_size)\n",
725+
" tf_metrics.AverageEpisodeLengthMetric(batch_size=environment.batch_size),\n",
718726
" ]\n",
719727
" if additional_metrics:\n",
720728
" metrics += additional_metrics\n",
721729
"\n",
722730
" if isinstance(environment.reward_spec(), dict):\n",
723-
" metrics += [tf_metrics.AverageReturnMultiMetric(\n",
724-
" reward_spec=environment.reward_spec(),\n",
725-
" batch_size=environment.batch_size)]\n",
726-
" else:\n",
727731
" metrics += [\n",
728-
" tf_metrics.AverageReturnMetric(batch_size=environment.batch_size)]\n",
732+
" tf_metrics.AverageReturnMultiMetric(\n",
733+
" reward_spec=environment.reward_spec(), batch_size=environment.batch_size\n",
734+
" )\n",
735+
" ]\n",
736+
" else:\n",
737+
" metrics += [tf_metrics.AverageReturnMetric(batch_size=environment.batch_size)]\n",
729738
"\n",
730739
" # Store intermediate metric results, indexed by metric names.\n",
731740
" metric_results = defaultdict(list)\n",
732741
"\n",
733742
" if training_data_spec_transformation_fn is not None:\n",
734-
" def add_batch_fn(data): return replay_buffer.add_batch(training_data_spec_transformation_fn(data)) \n",
735-
" \n",
743+
"\n",
744+
" def add_batch_fn(data):\n",
745+
" return replay_buffer.add_batch(training_data_spec_transformation_fn(data))\n",
746+
"\n",
736747
" else:\n",
737748
" add_batch_fn = replay_buffer.add_batch\n",
738749
"\n",
@@ -742,10 +753,12 @@
742753
" env=environment,\n",
743754
" policy=agent.collect_policy,\n",
744755
" num_steps=steps_per_loop * environment.batch_size,\n",
745-
" observers=observers)\n",
756+
" observers=observers,\n",
757+
" )\n",
746758
"\n",
747759
" training_loop = trainer.get_training_loop_fn(\n",
748-
" driver, replay_buffer, agent, steps_per_loop)\n",
760+
" driver, replay_buffer, agent, steps_per_loop\n",
761+
" )\n",
749762
" saver = policy_saver.PolicySaver(agent.policy)\n",
750763
"\n",
751764
" for _ in range(training_loops):\n",
@@ -783,7 +796,8 @@
783796
" environment=environment,\n",
784797
" training_loops=TRAINING_LOOPS,\n",
785798
" steps_per_loop=STEPS_PER_LOOP,\n",
786-
" additional_metrics=metrics)\n",
799+
" additional_metrics=metrics,\n",
800+
")\n",
787801
"\n",
788802
"tf.profiler.experimental.stop()"
789803
]
@@ -1092,11 +1106,15 @@
10921106
},
10931107
"outputs": [],
10941108
"source": [
1095-
"RUN_HYPERPARAMETER_TUNING = True # Execute hyperparameter tuning instead of regular training.\n",
1109+
"RUN_HYPERPARAMETER_TUNING = (\n",
1110+
" True # Execute hyperparameter tuning instead of regular training.\n",
1111+
")\n",
10961112
"TRAIN_WITH_BEST_HYPERPARAMETERS = False # Do not train.\n",
10971113
"\n",
10981114
"HPTUNING_RESULT_DIR = \"hptuning/\" # @param {type: \"string\"} Directory to store the best hyperparameter(s) in `BUCKET_NAME` and locally (temporarily).\n",
1099-
"HPTUNING_RESULT_PATH = os.path.join(HPTUNING_RESULT_DIR, \"result.json\") # @param {type: \"string\"} Path to the file containing the best hyperparameter(s)."
1115+
"HPTUNING_RESULT_PATH = os.path.join(\n",
1116+
" HPTUNING_RESULT_DIR, \"result.json\"\n",
1117+
") # @param {type: \"string\"} Path to the file containing the best hyperparameter(s)."
11001118
]
11011119
},
11021120
{
@@ -1124,7 +1142,7 @@
11241142
" image_uri: str,\n",
11251143
" args: List[str],\n",
11261144
" location: str = \"us-central1\",\n",
1127-
" api_endpoint: str = \"us-central1-aiplatform.googleapis.com\"\n",
1145+
" api_endpoint: str = \"us-central1-aiplatform.googleapis.com\",\n",
11281146
") -> None:\n",
11291147
" \"\"\"Creates a hyperparameter tuning job using a custom container.\n",
11301148
"\n",
@@ -1197,8 +1215,8 @@
11971215
"\n",
11981216
" # Create job\n",
11991217
" response = client.create_hyperparameter_tuning_job(\n",
1200-
" parent=parent,\n",
1201-
" hyperparameter_tuning_job=hyperparameter_tuning_job)\n",
1218+
" parent=parent, hyperparameter_tuning_job=hyperparameter_tuning_job\n",
1219+
" )\n",
12021220
" job_id = response.name.split(\"/\")[-1]\n",
12031221
" print(\"Job ID:\", job_id)\n",
12041222
" print(\"Job config:\", response)\n",
@@ -1242,7 +1260,8 @@
12421260
" image_uri=f\"gcr.io/{PROJECT_ID}/{HPTUNING_TRAINING_CONTAINER}:latest\",\n",
12431261
" args=args,\n",
12441262
" location=REGION,\n",
1245-
" api_endpoint=f\"{REGION}-aiplatform.googleapis.com\")"
1263+
" api_endpoint=f\"{REGION}-aiplatform.googleapis.com\",\n",
1264+
")"
12461265
]
12471266
},
12481267
{
@@ -1292,7 +1311,8 @@
12921311
" name = client.hyperparameter_tuning_job_path(\n",
12931312
" project=project,\n",
12941313
" location=location,\n",
1295-
" hyperparameter_tuning_job=hyperparameter_tuning_job_id)\n",
1314+
" hyperparameter_tuning_job=hyperparameter_tuning_job_id,\n",
1315+
" )\n",
12961316
" response = client.get_hyperparameter_tuning_job(name=name)\n",
12971317
" return response"
12981318
]
@@ -1313,7 +1333,8 @@
13131333
" location=REGION,\n",
13141334
" api_endpoint=f\"{REGION}-aiplatform.googleapis.com\")\n",
13151335
" if response.state.name == 'JOB_STATE_SUCCEEDED':\n",
1316-
" print(\"Job succeeded.\\nJob Time:\", response.update_time - response.create_time)\n",
1336+
" print(\"Job succeeded.\n",
1337+
"Job Time:\", response.update_time - response.create_time)\n",
13171338
" trials = response.trials\n",
13181339
" print(\"Trials:\", trials)\n",
13191340
" break\n",
@@ -1348,8 +1369,8 @@
13481369
"if trials:\n",
13491370
" # Dict mapping from metric names to the best metric values seen so far\n",
13501371
" best_objective_values = dict.fromkeys(\n",
1351-
" [metric.metric_id for metric in trials[0].final_measurement.metrics],\n",
1352-
" -np.inf)\n",
1372+
" [metric.metric_id for metric in trials[0].final_measurement.metrics], -np.inf\n",
1373+
" )\n",
13531374
" # Dict mapping from metric names to a list of the best combination(s) of\n",
13541375
" # hyperparameter(s). Each combination is a dict mapping from hyperparameter\n",
13551376
" # names to their values.\n",
@@ -1358,12 +1379,13 @@
13581379
" # `final_measurement` and `parameters` are `RepeatedComposite` objects.\n",
13591380
" # Reference the structure above to extract the value of your interest.\n",
13601381
" for metric in trial.final_measurement.metrics:\n",
1361-
" params = {\n",
1362-
" param.parameter_id: param.value for param in trial.parameters}\n",
1382+
" params = {param.parameter_id: param.value for param in trial.parameters}\n",
13631383
" if metric.value > best_objective_values[metric.metric_id]:\n",
13641384
" best_params[metric.metric_id] = [params]\n",
13651385
" elif metric.value == best_objective_values[metric.metric_id]:\n",
1366-
" best_params[param.parameter_id].append(params) # Handle cases where multiple hyperparameter values lead to the same performance.\n",
1386+
" best_params[param.parameter_id].append(\n",
1387+
" params\n",
1388+
" ) # Handle cases where multiple hyperparameter values lead to the same performance.\n",
13671389
" print(\"Best hyperparameter value(s):\")\n",
13681390
" for metric, params in best_params.items():\n",
13691391
" print(f\"Metric={metric}: {sorted(params)}\")\n",
@@ -1443,7 +1465,9 @@
14431465
},
14441466
"outputs": [],
14451467
"source": [
1446-
"PREDICTION_CONTAINER = \"prediction-custom-container\" # @param {type:\"string\"} Name of the container image."
1468+
"PREDICTION_CONTAINER = (\n",
1469+
" \"prediction-custom-container\" # @param {type:\"string\"} Name of the container image.\n",
1470+
")"
14471471
]
14481472
},
14491473
{
@@ -1475,7 +1499,7 @@
14751499
" machineType: 'E2_HIGHCPU_8'\"\"\".format(\n",
14761500
" PROJECT_ID=PROJECT_ID,\n",
14771501
" PREDICTION_CONTAINER=PREDICTION_CONTAINER,\n",
1478-
" ARTIFACTS_DIR=ARTIFACTS_DIR\n",
1502+
" ARTIFACTS_DIR=ARTIFACTS_DIR,\n",
14791503
")\n",
14801504
"\n",
14811505
"with open(\"cloudbuild.yaml\", \"w\") as fp:\n",
@@ -1592,8 +1616,12 @@
15921616
},
15931617
"outputs": [],
15941618
"source": [
1595-
"RUN_HYPERPARAMETER_TUNING = False # Execute regular training instead of hyperparameter tuning.\n",
1596-
"TRAIN_WITH_BEST_HYPERPARAMETERS = True # @param {type:\"bool\"} Whether to use learned hyperparameters in training."
1619+
"RUN_HYPERPARAMETER_TUNING = (\n",
1620+
" False # Execute regular training instead of hyperparameter tuning.\n",
1621+
")\n",
1622+
"TRAIN_WITH_BEST_HYPERPARAMETERS = (\n",
1623+
" True # @param {type:\"bool\"} Whether to use learned hyperparameters in training.\n",
1624+
")"
15971625
]
15981626
},
15991627
{
@@ -1633,10 +1661,12 @@
16331661
"job = aiplatform.CustomContainerTrainingJob(\n",
16341662
" display_name=\"train-movielens\",\n",
16351663
" container_uri=f\"gcr.io/{PROJECT_ID}/{HPTUNING_TRAINING_CONTAINER}:latest\",\n",
1636-
" command=[\"python3\", \"-m\", \"src.training.task\"] + args, # Pass in training arguments, including hyperparameters.\n",
1664+
" command=[\"python3\", \"-m\", \"src.training.task\"]\n",
1665+
" + args, # Pass in training arguments, including hyperparameters.\n",
16371666
" model_serving_container_image_uri=f\"gcr.io/{PROJECT_ID}/{PREDICTION_CONTAINER}:latest\",\n",
16381667
" model_serving_container_predict_route=\"/predict\",\n",
1639-
" model_serving_container_health_route=\"/health\")\n",
1668+
" model_serving_container_health_route=\"/health\",\n",
1669+
")\n",
16401670
"\n",
16411671
"print(\"Training Spec:\", job._managed_model)\n",
16421672
"\n",
@@ -1645,7 +1675,8 @@
16451675
" replica_count=1,\n",
16461676
" machine_type=\"n1-standard-4\",\n",
16471677
" accelerator_type=\"ACCELERATOR_TYPE_UNSPECIFIED\",\n",
1648-
" accelerator_count=0)"
1678+
" accelerator_count=0,\n",
1679+
")"
16491680
]
16501681
},
16511682
{
@@ -1784,7 +1815,7 @@
17841815
"! gcloud ai models delete $model.name --quiet\n",
17851816
"\n",
17861817
"# Delete Cloud Storage objects that were created\n",
1787-
"! gsutil -m rm -r $ARTIFACTS_DIR"
1818+
"! gcloud storage rm --recursive $ARTIFACTS_DIR"
17881819
]
17891820
}
17901821
],

0 commit comments

Comments
 (0)