使用矩陣分解模型,根據明確的意見回饋建立推薦內容


本教學課程將說明如何建立矩陣分解模型,並根據 movielens1m 資料集中的客戶電影評分進行訓練。接著,您可以使用矩陣分解模型為使用者產生電影推薦內容。

使用客戶提供的評分來訓練模型,稱為使用明確回饋訓練。如果您使用明確的意見回饋做為訓練資料,系統會使用交替最小平方演算法訓練矩陣分解模型。

目標

本教學課程會逐步引導您完成下列工作:

費用

本教學課程使用 Google Cloud的計費元件,包括:

  • BigQuery
  • BigQuery ML

如要進一步瞭解 BigQuery 費用,請參閱 BigQuery 定價頁面。

如要進一步瞭解 BigQuery ML 費用,請參閱 BigQuery ML 定價

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Make sure that billing is enabled for your Google Cloud project.

  6. 新專案會自動啟用 BigQuery。如要在現有的專案中啟用 BigQuery,請前往

    Enable the BigQuery API.

    Enable the API

  7. 所需權限

    • 如要建立資料集,您必須具備 bigquery.datasets.create IAM 權限。

    • 您必須具備下列權限,才能建立模型:

      • bigquery.jobs.create
      • bigquery.models.create
      • bigquery.models.getData
      • bigquery.models.updateData
    • 如要執行推論,您需要具備下列權限:

      • bigquery.models.getData
      • bigquery.jobs.create

    如要進一步瞭解 BigQuery 中的 IAM 角色和權限,請參閱「IAM 簡介」。

建立資料集

建立 BigQuery 資料集來儲存機器學習模型。

控制台

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往「BigQuery」頁面

  2. 在「Explorer」窗格中,按一下專案名稱。

  3. 依序點選 「View actions」(查看動作) >「Create dataset」(建立資料集)

    「建立資料集」選單選項。

  4. 在「Create dataset」頁面上執行下列操作:

    • 在「Dataset ID」(資料集 ID) 中輸入 bqml_tutorial

    • 在「位置類型」中選取「多區域」,然後選取「美國 (多個美國區域)」

    • 保留其餘預設設定,然後點選「Create dataset」(建立資料集)

bq

如要建立新的資料集,請使用 bq mk 指令搭配 --location 旗標。如需可能參數的完整清單,請參閱 bq mk --dataset 指令參考資料。

  1. 建立名為 bqml_tutorial 的資料集,並將資料位置設為 US,說明為 BigQuery ML tutorial dataset

    bq --location=US mk -d \
     --description "BigQuery ML tutorial dataset." \
     bqml_tutorial

    這個指令採用 -d 捷徑,而不是使用 --dataset 旗標。如果您省略 -d--dataset,該指令預設會建立資料集。

  2. 確認資料集已建立:

    bq ls

API

請呼叫 datasets.insert 方法,搭配已定義的資料集資源

{
  "datasetReference": {
     "datasetId": "bqml_tutorial"
  }
}

BigQuery DataFrames

在嘗試這個範例之前,請先參閱 BigQuery 快速入門:使用 BigQuery DataFrames,按照 BigQuery DataFrames 設定說明進行操作。詳情請參閱 BigQuery DataFrames 參考資料說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定 ADC」。

import google.cloud.bigquery

bqclient = google.cloud.bigquery.Client()
bqclient.create_dataset("bqml_tutorial", exists_ok=True)

上傳 Movielens 資料

movielens1m 資料上傳至 BigQuery。

CLI

請按照下列步驟,使用 bq 指令列工具上傳 movielens1m 資料:

  1. 開啟 Cloud Shell:

    啟用 Cloud Shell

  2. 將評分資料上傳至 ratings 資料表。在指令列中貼上以下查詢,然後按下 Enter

    curl -O 'http://files.grouplens.org/datasets/movielens/ml-1m.zip'
    unzip ml-1m.zip
    sed 's/::/,/g' ml-1m/ratings.dat > ratings.csv
    bq load --source_format=CSV bqml_tutorial.ratings ratings.csv \
      user_id:INT64,item_id:INT64,rating:FLOAT64,timestamp:TIMESTAMP
    
  3. 將電影資料上傳至 movies 資料表。在指令列中貼上以下查詢,然後按下 Enter

    sed 's/::/@/g' ml-1m/movies.dat > movie_titles.csv
    bq load --source_format=CSV --field_delimiter=@ \
    bqml_tutorial.movies movie_titles.csv \
    movie_id:INT64,movie_title:STRING,genre:STRING
    

BigQuery DataFrames

在嘗試這個範例之前,請先參閱 BigQuery 快速入門:使用 BigQuery DataFrames,按照 BigQuery DataFrames 設定說明進行操作。詳情請參閱 BigQuery DataFrames 參考資料說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定 ADC」。

首先,請使用 bqclient = google.cloud.bigquery.Client() 建立 Client 物件,然後將 movielens1m 資料載入先前步驟中建立的資料集。

import io
import zipfile

import google.api_core.exceptions
import requests

try:
    # Check if you've already created the Movielens tables to avoid downloading
    # and uploading the dataset unnecessarily.
    bqclient.get_table("bqml_tutorial.ratings")
    bqclient.get_table("bqml_tutorial.movies")
except google.api_core.exceptions.NotFound:
    # Download the https://grouplens.org/datasets/movielens/1m/ dataset.
    ml1m = requests.get("http://files.grouplens.org/datasets/movielens/ml-1m.zip")
    ml1m_file = io.BytesIO(ml1m.content)
    ml1m_zip = zipfile.ZipFile(ml1m_file)

    # Upload the ratings data into the ratings table.
    with ml1m_zip.open("ml-1m/ratings.dat") as ratings_file:
        ratings_content = ratings_file.read()

    ratings_csv = io.BytesIO(ratings_content.replace(b"::", b","))
    ratings_config = google.cloud.bigquery.LoadJobConfig()
    ratings_config.source_format = "CSV"
    ratings_config.write_disposition = "WRITE_TRUNCATE"
    ratings_config.schema = [
        google.cloud.bigquery.SchemaField("user_id", "INT64"),
        google.cloud.bigquery.SchemaField("item_id", "INT64"),
        google.cloud.bigquery.SchemaField("rating", "FLOAT64"),
        google.cloud.bigquery.SchemaField("timestamp", "TIMESTAMP"),
    ]
    bqclient.load_table_from_file(
        ratings_csv, "bqml_tutorial.ratings", job_config=ratings_config
    ).result()

    # Upload the movie data into the movies table.
    with ml1m_zip.open("ml-1m/movies.dat") as movies_file:
        movies_content = movies_file.read()

    movies_csv = io.BytesIO(movies_content.replace(b"::", b"@"))
    movies_config = google.cloud.bigquery.LoadJobConfig()
    movies_config.source_format = "CSV"
    movies_config.field_delimiter = "@"
    movies_config.write_disposition = "WRITE_TRUNCATE"
    movies_config.schema = [
        google.cloud.bigquery.SchemaField("movie_id", "INT64"),
        google.cloud.bigquery.SchemaField("movie_title", "STRING"),
        google.cloud.bigquery.SchemaField("genre", "STRING"),
    ]
    bqclient.load_table_from_file(
        movies_csv, "bqml_tutorial.movies", job_config=movies_config
    ).result()

建立模型

建立矩陣因式化模型,並根據 ratings 資料表中的資料進行訓練。模型經過訓練後,可根據客戶提供的電影評分,預測每個使用者-項目組合的評分。

SQL

以下 CREATE MODEL 陳述式會使用這些資料欄產生推薦內容:

  • user_id:使用者 ID。
  • item_id:電影 ID。
  • rating:使用者給予項目的明確評分,從 1 到 5 顆星。

請按照下列步驟建立模型:

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往 BigQuery

  2. 在查詢編輯器中貼上以下查詢,然後點選「執行」

    CREATE OR REPLACE MODEL `bqml_tutorial.mf_explicit`
    OPTIONS (
      MODEL_TYPE = 'matrix_factorization',
      FEEDBACK_TYPE = 'explicit',
      USER_COL = 'user_id',
      ITEM_COL = 'item_id',
      L2_REG = 9.83,
      NUM_FACTORS = 34)
    AS
    SELECT
    user_id,
    item_id,
    rating
    FROM `bqml_tutorial.ratings`;

    查詢作業需要約 10 分鐘才能完成,完成後「探索器」窗格中就會顯示 mf_explicit 模型。由於查詢是使用 CREATE MODEL 陳述式建立模型,因此您不會看到查詢結果。

BigQuery DataFrames

在嘗試這個範例之前,請先參閱 BigQuery 快速入門:使用 BigQuery DataFrames,按照 BigQuery DataFrames 設定說明進行操作。詳情請參閱 BigQuery DataFrames 參考資料說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定 ADC」。

from bigframes.ml import decomposition
import bigframes.pandas as bpd

# Load data from BigQuery
bq_df = bpd.read_gbq(
    "bqml_tutorial.ratings", columns=("user_id", "item_id", "rating")
)

# Create the Matrix Factorization model
model = decomposition.MatrixFactorization(
    num_factors=34,
    feedback_type="explicit",
    user_col="user_id",
    item_col="item_id",
    rating_col="rating",
    l2_reg=9.83,
)
model.fit(bq_df)
model.to_gbq(
    your_model_id, replace=True  # For example: "bqml_tutorial.mf_explicit"
)

程式碼大約需要 10 分鐘才能完成,完成後「Explorer」窗格就會顯示 mf_explicit 模型。

取得訓練統計資料

您也可以選擇在Google Cloud 主控台中查看模型的訓練統計資料。

機器學習演算法會使用不同的參數建立許多模型疊代,然後選取能將損失降到最低的模型版本。這項程序稱為經驗風險最小化。模型的訓練統計資料可讓您查看與模型每個疊代相關的損失。

如要查看模型的訓練統計資料,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往 BigQuery

  2. 在「Explorer」窗格中,依序展開專案、bqml_tutorial 資料集,然後展開「Models」資料夾。

  3. 按一下 mf_explicit 模型,然後點選「Training」分頁標籤

  4. 在「View as」部分,按一下「Table」。結果應如下所示:

    +-----------+--------------------+--------------------+
    | Iteration | Training Data Loss | Duration (seconds) |
    +-----------+--------------------+--------------------+
    |  11       | 0.3943             | 42.59              |
    +-----------+--------------------+--------------------+
    |  10       | 0.3979             | 27.37              |
    +-----------+--------------------+--------------------+
    |   9       | 0.4038             | 40.79              |
    +-----------+--------------------+--------------------+
    |  ...      | ...                | ...                |
    +-----------+--------------------+--------------------+
    

    「Training Data Loss」資料欄代表在訓練模型後計算的損失指標。由於這是矩陣因式分解模型,因此這個欄會顯示均方誤差

您也可以使用 ML.TRAINING_INFO 函式查看模型訓練統計資料。

評估模型

比較模型傳回的預測電影評分,以及訓練資料中的實際使用者電影評分,藉此評估模型效能。

SQL

使用 ML.EVALUATE 函式評估模型:

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往 BigQuery

  2. 在查詢編輯器中貼上以下查詢,然後點選「執行」

    SELECT
    *
    FROM
    ML.EVALUATE(
      MODEL `bqml_tutorial.mf_explicit`,
      (
        SELECT
          user_id,
          item_id,
          rating
        FROM
          `bqml_tutorial.ratings`
      ));

    結果應如下所示:

    +---------------------+---------------------+------------------------+-----------------------+--------------------+--------------------+
    | mean_absolute_error | mean_squared_error  | mean_squared_log_error | median_absolute_error |      r2_score      | explained_variance |
    +---------------------+---------------------+------------------------+-----------------------+--------------------+--------------------+
    | 0.48494444327829156 | 0.39433706592870565 |   0.025437895793637522 |   0.39017059802629905 | 0.6840033369412044 | 0.6840033369412264 |
    +---------------------+---------------------+------------------------+-----------------------+--------------------+--------------------+
    

    評估結果中有個重要的指標,就是 R2 分數。R2 分數是種統計量具,用來確認線性迴歸的預測結果是否趨近於實際資料。0 值代表模型無法解釋平均值周圍之回應資料的變化。1 值表示模型能夠解釋所有平均值周圍之回應資料的變化。

    如要進一步瞭解 ML.EVALUATE 函式輸出內容,請參閱「矩陣因數分解模型」。

您也可以不提供輸入資料,直接呼叫 ML.EVALUATE。系統會使用訓練期間計算的評估指標。

BigQuery DataFrames

在嘗試這個範例之前,請先參閱 BigQuery 快速入門:使用 BigQuery DataFrames,按照 BigQuery DataFrames 設定說明進行操作。詳情請參閱 BigQuery DataFrames 參考資料說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定 ADC」。

呼叫 model.score() 來評估模型。

# Evaluate the model using the score() function
model.score(bq_df)
# Output:
# mean_absolute_error	mean_squared_error	mean_squared_log_error	median_absolute_error	r2_score	explained_variance
# 0.485403	                0.395052	        0.025515	            0.390573	        0.68343	        0.68343

取得使用者與項目組合的部分預測評分

取得五位使用者對每部電影的預測評分。

SQL

使用 ML.RECOMMEND 函式取得預測評分:

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往 BigQuery

  2. 在查詢編輯器中貼上以下查詢,然後點選「執行」

    SELECT
    *
    FROM
    ML.RECOMMEND(
      MODEL `bqml_tutorial.mf_explicit`,
      (
        SELECT
          user_id
        FROM
          `bqml_tutorial.ratings`
        LIMIT 5
      ));

    結果應如下所示:

    +--------------------+---------+---------+
    | predicted_rating   | user_id | item_id |
    +--------------------+---------+---------+
    | 4.2125303962491873 | 4       | 3169    |
    +--------------------+---------+---------+
    | 4.8068920531981263 | 4       | 3739    |
    +--------------------+---------+---------+
    | 3.8742203494732403 | 4       | 3574    |
    +--------------------+---------+---------+
    | ...                | ...     | ...     |
    +--------------------+---------+---------+
    

BigQuery DataFrames

在嘗試這個範例之前,請先參閱 BigQuery 快速入門:使用 BigQuery DataFrames,按照 BigQuery DataFrames 設定說明進行操作。詳情請參閱 BigQuery DataFrames 參考資料說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定 ADC」。

呼叫 model.predict() 即可取得預測評分。

# Use predict() to get the predicted rating for each movie for 5 users
subset = bq_df[["user_id"]].head(5)
predicted = model.predict(subset)
print(predicted)
# Output:
#   predicted_rating	user_id	 item_id	rating
# 0	    4.206146	     4354	  968	     4.0
# 1	    4.853099	     3622	  3521	     5.0
# 2	    2.679067	     5543	  920	     2.0
# 3	    4.323458	     445	  3175	     5.0
# 4	    3.476911	     5535	  235	     4.0

生成建議

使用預測評分,為每位使用者產生前五部推薦電影。

SQL

請按照下列步驟產生推薦內容:

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往 BigQuery

  2. 將預測的評分寫入資料表。在查詢編輯器中貼上以下查詢,然後按一下「Run」

    CREATE OR REPLACE TABLE `bqml_tutorial.recommend`
    AS
    SELECT
    *
    FROM
    ML.RECOMMEND(MODEL `bqml_tutorial.mf_explicit`);
  3. 將預測的評分與電影資訊彙整,並為每位使用者選取前五個結果。在查詢編輯器中貼上以下查詢,然後按一下「Run」

  SELECT
    user_id,
    ARRAY_AGG(STRUCT(movie_title, genre, predicted_rating) ORDER BY predicted_rating DESC LIMIT 5)
  FROM
    (
      SELECT
        user_id,
        item_id,
        predicted_rating,
        movie_title,
        genre
      FROM
        `bqml_tutorial.recommend`
      JOIN
        `bqml_tutorial.movies`
        ON
          item_id = movie_id
    )
  GROUP BY
    user_id;

結果應如下所示:

  +---------+-------------------------------------+------------------------+--------------------+
  | user_id | f0_movie_title                      | f0_genre               | predicted_rating   |
  +---------+-------------------------------------+------------------------+--------------------+
  | 4597    | Song of Freedom (1936)              | Drama                  | 6.8495752907364009 |
  |         | I Went Down (1997)                  | Action/Comedy/Crime    | 6.7203235758772877 |
  |         | Men With Guns (1997)                | Action/Drama           | 6.399407352232001  |
  |         | Kid, The (1921)                     | Action                 | 6.1952890198126731 |
  |         | Hype! (1996)                        | Documentary            | 6.1895766097451475 |
  +---------+-------------------------------------+------------------------+--------------------+
  | 5349    | Fandango (1985)                     | Comedy                 | 9.944574012151549  |
  |         | Breakfast of Champions (1999)       | Comedy                 | 9.55661860430112   |
  |         | Funny Bones (1995)                  | Comedy                 | 9.52778917835076   |
  |         | Paradise Road (1997)                | Drama/War              | 9.1643621767929133 |
  |         | Surviving Picasso (1996)            | Drama                  | 8.807353289233772  |
  +---------+-------------------------------------+------------------------+--------------------+
  | ...     | ...                                 | ...                    | ...                |
  +---------+-------------------------------------+------------------------+--------------------+
  

BigQuery DataFrames

在嘗試這個範例之前,請先參閱 BigQuery 快速入門:使用 BigQuery DataFrames,按照 BigQuery DataFrames 設定說明進行操作。詳情請參閱 BigQuery DataFrames 參考資料說明文件

如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定 ADC」。

呼叫 model.predict() 即可取得預測評分。

# import bigframes.bigquery as bbq

# Load movies
movies = bpd.read_gbq("bqml_tutorial.movies")

# Merge the movies df with the previously created predicted df
merged_df = bpd.merge(predicted, movies, left_on="item_id", right_on="movie_id")

# Separate users and predicted data, setting the index to 'movie_id'
users = merged_df[["user_id", "movie_id"]].set_index("movie_id")

# Take the predicted data and sort it in descending order by 'predicted_rating', setting the index to 'movie_id'
sort_data = (
    merged_df[["movie_title", "genre", "predicted_rating", "movie_id"]]
    .sort_values(by="predicted_rating", ascending=False)
    .set_index("movie_id")
)

# re-merge the separated dfs by index
merged_user = sort_data.join(users, how="outer")

# group the users and set the user_id as the index
merged_user.groupby("user_id").head(5).set_index("user_id").sort_index()
print(merged_user)
# Output:
# 	            movie_title	                genre	        predicted_rating
# user_id
#   1	    Saving Private Ryan (1998)	Action|Drama|War	    5.19326
#   1	        Fargo (1996)	       Crime|Drama|Thriller	    4.996954
#   1	    Driving Miss Daisy (1989)	    Drama	            4.983671
#   1	        Ben-Hur (1959)	       Action|Adventure|Drama	4.877622
#   1	     Schindler's List (1993)	   Drama|War	        4.802336
#   2	    Saving Private Ryan (1998)	Action|Drama|War	    5.19326
#   2	        Braveheart (1995)	    Action|Drama|War	    5.174145
#   2	        Gladiator (2000)	      Action|Drama	        5.066372
#   2	        On Golden Pond (1981)	     Drama	            5.01198
#   2	    Driving Miss Daisy (1989)	     Drama	            4.983671

清除所用資源

如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。

  • 您可以刪除建立的專案。
  • 或者您可以保留專案並刪除資料集。

刪除資料集

刪除專案將移除專案中所有的資料集與資料表。若您希望重新使用專案,您可以刪除本教學課程中所建立的資料集。

  1. 如有需要,請在 Google Cloud 控制台開啟 BigQuery 頁面。

    前往「BigQuery」頁面

  2. 在導覽窗格中,按一下您建立的 bqml_tutorial 資料集。

  3. 按一下視窗右側的「Delete dataset」。這個動作將會刪除資料集、資料表,以及所有資料。

  4. 在「Delete dataset」對話方塊中,輸入資料集的名稱 (bqml_tutorial),然後按一下「Delete」來確認刪除指令。

刪除專案

如要刪除專案,請進行以下操作:

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

後續步驟