From c49bebb3d68115edcebff2e1098da4a4f1788538 Mon Sep 17 00:00:00 2001 From: Ashley Xu Date: Thu, 9 May 2024 22:28:23 +0000 Subject: [PATCH 1/3] docs: add code snippets for imported tensorflow model --- .../imported_tensorflow_model_test.py | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 samples/snippets/imported_tensorflow_model_test.py diff --git a/samples/snippets/imported_tensorflow_model_test.py b/samples/snippets/imported_tensorflow_model_test.py new file mode 100644 index 0000000000..cc2dfb5500 --- /dev/null +++ b/samples/snippets/imported_tensorflow_model_test.py @@ -0,0 +1,42 @@ +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (t +# you may not use this file except in compliance wi +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in +# distributed under the License is distributed on a +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, eit +# See the License for the specific language governi +# limitations under the License. + + +def test_imported_tensorflow_model() -> None: + # Determine project id, in this case prefer the one set in the environment + # variable GOOGLE_CLOUD_PROJECT (if any) + import os + + PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "bigframes-dev") + LOCATION = "US" + MODEL_PATH = "gs://cloud-training-demos/txtclass/export/exporter/1549825580/*" + + # [START bigquery_dataframes_imported_tensorflow_tutorial_import_tensorflow_models] + import bigframes + from bigframes.ml.imported import TensorFlowModel + + bigframes.options.bigquery.project = PROJECT_ID + bigframes.options.bigquery.location = LOCATION + + imported_tensorflow_model = TensorFlowModel(model_path=MODEL_PATH) + # [END bigquery_dataframes_imported_tensorflow_tutorial_import_tensorflow_models] + assert imported_tensorflow_model is not None + + # [START bigquery_dataframes_imported_tensorflow_tutorial_make_predictions] + import bigframes.pandas as bpd + + df = bpd.read_gbq("bigquery-public-data.hacker_news.full") + predictions = imported_tensorflow_model.predict(df) + predictions.head(5) + # [END bigquery_dataframes_imported_tensorflow_tutorial_make_predictions] From 826a930b310740c001d6366a2b8a2655aa48c696 Mon Sep 17 00:00:00 2001 From: Ashley Xu Date: Mon, 13 May 2024 21:17:42 +0000 Subject: [PATCH 2/3] address comments --- samples/snippets/imported_tensorflow_model_test.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/samples/snippets/imported_tensorflow_model_test.py b/samples/snippets/imported_tensorflow_model_test.py index cc2dfb5500..767ee5ed5f 100644 --- a/samples/snippets/imported_tensorflow_model_test.py +++ b/samples/snippets/imported_tensorflow_model_test.py @@ -19,7 +19,6 @@ def test_imported_tensorflow_model() -> None: import os PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "bigframes-dev") - LOCATION = "US" MODEL_PATH = "gs://cloud-training-demos/txtclass/export/exporter/1549825580/*" # [START bigquery_dataframes_imported_tensorflow_tutorial_import_tensorflow_models] @@ -27,9 +26,13 @@ def test_imported_tensorflow_model() -> None: from bigframes.ml.imported import TensorFlowModel bigframes.options.bigquery.project = PROJECT_ID - bigframes.options.bigquery.location = LOCATION + # You can change the location to one of the valid locations: https://cloud.google.com/bigquery/docs/locations#supported_locations + bigframes.options.bigquery.location = "US" - imported_tensorflow_model = TensorFlowModel(model_path=MODEL_PATH) + imported_tensorflow_model = TensorFlowModel( + # e.g. "gs://bucket/path/to/saved_model/*" + model_path=MODEL_PATH + ) # [END bigquery_dataframes_imported_tensorflow_tutorial_import_tensorflow_models] assert imported_tensorflow_model is not None From 43fef69ef888dcd2f79c4903c42fc849b7fda0c3 Mon Sep 17 00:00:00 2001 From: Ashley Xu Date: Mon, 13 May 2024 21:29:00 +0000 Subject: [PATCH 3/3] address comments --- samples/snippets/imported_tensorflow_model_test.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/samples/snippets/imported_tensorflow_model_test.py b/samples/snippets/imported_tensorflow_model_test.py index 767ee5ed5f..4913c635c2 100644 --- a/samples/snippets/imported_tensorflow_model_test.py +++ b/samples/snippets/imported_tensorflow_model_test.py @@ -19,7 +19,6 @@ def test_imported_tensorflow_model() -> None: import os PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "bigframes-dev") - MODEL_PATH = "gs://cloud-training-demos/txtclass/export/exporter/1549825580/*" # [START bigquery_dataframes_imported_tensorflow_tutorial_import_tensorflow_models] import bigframes @@ -30,8 +29,7 @@ def test_imported_tensorflow_model() -> None: bigframes.options.bigquery.location = "US" imported_tensorflow_model = TensorFlowModel( - # e.g. "gs://bucket/path/to/saved_model/*" - model_path=MODEL_PATH + model_path="gs://cloud-training-demos/txtclass/export/exporter/1549825580/*" ) # [END bigquery_dataframes_imported_tensorflow_tutorial_import_tensorflow_models] assert imported_tensorflow_model is not None