AWSハンズオン実践 〜Amazon Personalize〜

# はじめに

AWS公式のハンズオンシリーズの中から、[Amazon Personalizeのハンズオン](https://pages.awscloud.com/event_JAPAN_Hands-on-Amazon-Personalize-Forecast-2019.html?trk=aws_introduction_page)を実施しました。

本記事は自身のハンズオン学習メモとして投稿します。
# 目次
- [Amazon Personalizeとは](#Amazon Personalizeとは)
- [ハンズオン本編](#ハンズオン本編)
- [おわりに](#おわりに)

# Amazon Personalizeとは

ユーザー向けにパーソナライズしたレコメンデーションを簡単に追加できる機械学習サービス

- 主な機能  
  - ユーザー向けにPersonalizeされたレコメンデーションリストの提供
  - 「この商品を購入した方は...」といった特定のアイテムに対する類似アイテムリストの提供
  - 特定のアイテムリストをユーザーの嗜好に合わせて並び替えて提供

※パーソナライズ：多数の商品の中から、ユーザーの好みに合う商品を提示・推薦する技術

![personalize.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/82cda149-7675-5ecc-4407-e1317571c11c.png)

(https://pages.awscloud.com/event_JAPAN_Hands-on-Amazon-Personalize-Forecast-2019.html?trk=aws_introduction_page より引用)

これまでだとSageMaker等を利用し実装する必要があったが、マネージドで提供されるように。上記の図の1~10の内、必要なものだけをいじれば良い。

# ハンズオン本編

### データの準備  
9700 本の映画に対する 600 ユーザーの評価履歴にもとづいて、映画のリコメンデーションを⾏う  

- 学習データとなる評価履歴のデータを以下のURLからダウンロード・解凍  
(http://files.grouplens.org/datasets/movielens/ml-latest-small.zip)
- Excelでratings の列を削除する
- ヘッダを Personalize にあわせてUSER_ID,ITEM_ID,TIMESTAMPにする

![csv.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/f814ada9-5f0c-8020-d4e4-f7d53b79edbe.png)


- S3 に CSV をアップロード
- Personalize からバケットアクセスを許可(バケットポリシーを設定)


### データセットのインポート  
- Personalize のコンソールに移動、Dataset groupsを選択
- データセットグループを作成  
  - Create dataset group を選択してデータセットグループを作成
  - Interactions (必須), Users (オプション), Items (オプション) の3種のデータセットをデータセットグループに登録可能
  - このハンズオンでは、必須の Interactions のみ利⽤

![personalize2.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/23e891f2-6cee-5b4c-79d6-1b2038ee2410.png)

- データセットグループの設定
- Interaction データの設定
- schemaの作成(schema definition はこのまま。先程の手順でCSV をこれにあわせたので)

![personalize3.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/84b961d7-5e98-1717-95b2-66a246110575.png)

- データセットのインポート

![personalize4.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/41d14cc0-6177-309e-58e3-9f780c73f2c9.png)

- インポートの完了を待つ

### ソリューションを学習する
データインポートが終わると、ソリューション学習を start できる

![personalize5.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/ed21c37f-d03c-4746-15b9-88f7542424b2.png)

- ソリューション学習の設定をする

以下内容で設定。よく分からなくてもAutoMLで自動で選択してくれる機能も存在する。

![personalize6.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/0b49264a-9f70-41ed-36ff-f593c24a79da.png)

- 学習完了まで待つ

### キャンペーンを作成してリコメンデーションを⾏う
ソリューションの学習が終わるとキャンペーンを作成できる

![personalize7.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/2b45e272-31b4-abbf-7364-1c7fdc7af574.png)

- キャンペーンの作成

![personalize8.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/bd03d0c6-64eb-4136-e7bb-a971f2ca5bc4.png)

- キャンペーンからリコメンデーションを⾏う

キャンペーンをテストする画⾯が追加されているので、User ID を⼊⼒ (例えば1)しGet recommendationsを実⾏

![personalize9.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/634a0808-343a-ff43-2f42-56854f08b36e.png)

- 結果の確認

⼊⼒した User ID に推薦すべき Item ID のリストが出⼒される

![personalize10.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/d6359df3-5573-ce4c-79bb-fd8b1c6fcd6c.png)

- バッチ推論の開始 (S3への出⼒)

左のメニューから Batch inference jobs を選択

- バッチ推論ジョブの⼊⼒データ準備  
※ローカルでの作業  
  - リコメンデーション結果を得たいユーザのリストを json で作成
  - テキストエディタで以下のファイルを input.json として保存

```input.json
{"userId": "1"}
{"userId": "2"}
{"userId": "3"}
{"userId": "4"}
{"userId": "5"}
```

- ⼊⼒データをS3にアップロード
  - Personalize ⽤に作成したバケットに、input.json をアップロード

- バッチ推論ジョブの設定・実⾏

![personalize11.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/646352/42f1f72d-9ae6-ded6-9aa0-75a144967869.png)

- バッチ推論の結果を確認

Personalize ⽤のバケットに、出⼒結果 input.json.out を確認できるので確認
以下、ユーザIDごとの推薦商品のリストのファイル

```input.json.out
{"input":{"userId":"1"},"output":{"recommendedItems":["588","1073","1968","46578","1197","59315","356","1265","72998","1374","6539","73017","1097","91529","4878","2997","48774","4995","8533","4886","1356","50872","6942","112852","69844"],"scores":[0.0026756,0.0022548,0.0019869,0.0018073,0.0016906,0.001682,0.0016662,0.0016417,0.0016315,0.0015765,0.0015592,0.0015089,0.0015012,0.0014931,0.0014897,0.0014827,0.0014743,0.0014448,0.0014383,0.0014326,0.0014306,0.0014279,0.0014209,0.001413,0.0013909]},"error":null}
{"input":{"userId":"2"},"output":{"recommendedItems":["588","1197","648","1968","4995","1374","1073","59315","3471","4034","5989","81845","44191","6539","1580","2858","73017","69844","4896","1097","1376","1302","318","5349","356"],"scores":[0.0024747,0.0022842,0.0022726,0.0021697,0.0020913,0.0020476,0.002029,0.0019977,0.0019669,0.0019239,0.0019168,0.0018482,0.0018251,0.0018158,0.0017876,0.0017792,0.0017721,0.0017635,0.0017453,0.0017322,0.0017264,0.0017161,0.0017042,0.0016955,0.0016942]},"error":null}
{"input":{"userId":"3"},"output":{"recommendedItems":["588","91529","1968","318","59315","1073","8533","4995","69844","54259","1097","1374","81845","72998","4878","1197","3897","1265","73017","44191","112852","6539","1721","1580","96079"],"scores":[0.002948,0.0021137,0.0019071,0.0018712,0.0018015,0.0017114,0.0017009,0.0016365,0.0015842,0.0015761,0.0015647,0.001548,0.0015428,0.0015425,0.0015243,0.0015083,0.0015011,0.001495,0.0014892,0.0014781,0.0014725,0.0014623,0.0014612,0.001458,0.0014228]},"error":null}
{"input":{"userId":"4"},"output":{"recommendedItems":["8533","54259","69844","81845","73017","96079","81834","59315","55820","81591","72998","46578","6942","35836","56174","91500","119145","76093","1968","3911","48774","54001","8464","89745","61323"],"scores":[0.0023268,0.0021241,0.0020129,0.0019905,0.0019835,0.0019179,0.0018532,0.0017818,0.0017373,0.0017123,0.0016674,0.0016176,0.0015873,0.0015584,0.0015571,0.0015561,0.0015319,0.0015008,0.0014757,0.001424,0.0014225,0.0014181,0.0013818,0.0013691,0.0013487]},"error":null}
{"input":{"userId":"5"},"output":{"recommendedItems":["539","1073","2","587","367","1580","648","500","2628","1265","356","1197","317","594","377","141","432","350","1356","357","39","597","6","25","435"],"scores":[0.0084718,0.0060944,0.0057842,0.0055388,0.0047355,0.0047316,0.0046304,0.0045282,0.0044598,0.0043248,0.004305,0.0041455,0.0040596,0.0040247,0.0039599,0.0038525,0.0038448,0.0038191,0.0038077,0.0037269,0.0037107,0.0037047,0.003603,0.0035624,0.0034237]},"error":null}
{"input":"","output":{"recommendedItems":[],"scores":[]},"error":"User error: Input JSON is malformed."}
```

### 作成した AWS リソースの削除

粛々と削除。特筆すべき事はなし。


#おわりに

今回はAmazon Personalizeを利用したハンズオンを実施した。

実際に触ってみると、想像以上に簡単に各種サービスを使う事ができた。
機械学習の専門知識がなくとも簡単に導入する事が出来るのは大きな特徴である。

今回のようにAWSの各種AIサービスに触れていく事で、「何が出来るか」について一通り把握しておくのは大事だと思うので、続けていきたい。