a practical guide to machine learning on gcp

53
GCPではじめる かんたん機械学習 A Practical Guide to Machine Learning on GCP GDG DevFest Tokyo 2017

Upload: hayato-yoshikawa

Post on 21-Jan-2018

1.375 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: A practical guide to machine learning on GCP

GCPではじめるかんたん機械学習

A Practical Guide to Machine Learning on GCP

GDG DevFestTokyo 2017

Page 2: A practical guide to machine learning on GCP

Hi there!

● 吉川隼人 @hayatoy● APAC向けに色々するR&D● 参加コミュニティ

○ TFUG, GCPUG, GDG● 最近

GCPと機械学習に関する本を

執筆してます。

Page 3: A practical guide to machine learning on GCP

Agenda

● 機械学習(ML)ってなんだっけ?

● GCPのML系API

● ML API ✖ GAE ✖ Firebase

● Datalab ✖ BigQuery

● GAE ✖ ML Engine (TensorFlow)

Page 4: A practical guide to machine learning on GCP

機械学習ってなんだっけ?

Page 5: A practical guide to machine learning on GCP

機械学習とは

Page 6: A practical guide to machine learning on GCP

ただの関数です

Page 7: A practical guide to machine learning on GCP

y=f(x)

f(x)x y

Page 8: A practical guide to machine learning on GCP

機械学習でやりたいことって?

入力 x0 入力 x1 出力 y

180.1 75.2 ❌

160.4 70.3 ⭕

155.3 83.8 ⭕

177.2 68.6 ❌

170.2 62.5 ??

Page 9: A practical guide to machine learning on GCP

機械学習でやりたいことって?

入力 x0 入力 x1 出力 y

180.1 75.2 ❌

160.4 70.3 ⭕

155.3 83.8 ⭕

177.2 68.6 ❌

170.2 62.5 ??

既知の入力と出力

Page 10: A practical guide to machine learning on GCP

機械学習でやりたいことって?

入力 x0 入力 x1 出力 y

180.1 75.2 ❌

160.4 70.3 ⭕

155.3 83.8 ⭕

177.2 68.6 ❌

170.2 62.5 ??

既知の入力と出力

出力が未知

Page 11: A practical guide to machine learning on GCP

機械学習でやりたいことって?

入力 x0 入力 x1 出力 y

180.1 75.2 ❌

160.4 70.3 ⭕

155.3 83.8 ⭕

177.2 68.6 ❌

170.2 62.5 ??

既知の入力と出力

入力があったとき、未知の出力を知りたい(推論)出力が未知

Page 12: A practical guide to machine learning on GCP

関数に当てはめると・・

入力 x0 入力 x1 出力 y

180.1 75.2 ❌

160.4 70.3 ⭕

155.3 83.8 ⭕

177.2 68.6 ❌

170.2 62.5 ??

出力が未知の入力

f(x) ⭕ or ❌推論

Page 13: A practical guide to machine learning on GCP

関数に当てはめると・・

入力 x0 入力 x1 出力 y

180.1 75.2 ❌

160.4 70.3 ⭕

155.3 83.8 ⭕

177.2 68.6 ❌

170.2 62.5 ??

既知の入力と出力

出力が未知の入力

f(x) ⭕ or ❌

学習

推論

Page 14: A practical guide to machine learning on GCP

機械学習は何のパラメータを変えているの?身長

体重

メタボの例

Page 15: A practical guide to machine learning on GCP

機械学習は何のパラメータを変えているの?身長

体重

メタボの例

この辺に境界線がありそう・・

既知のデータから境界線(のパラメータ)を求める

Page 16: A practical guide to machine learning on GCP

機械学習 vs ルールベースアルゴリズム

ルールベース BMI計算して、しきい値で分岐した方が早いよね。

機械学習 既知データから自動で判別式を算出

今回の例なら、ルールベースが正解。

だけど入力が2種類だから2次元でプロットできたけど、1000種類あったら?

現実のデータはそんな簡単にルールがわからない!

Page 17: A practical guide to machine learning on GCP

ニューラルネットワーク

境界線を引くのは同じ。層やニューロンの数を増やすと境界線がどんどん複雑になっていく。

過学習

Page 18: A practical guide to machine learning on GCP

精度の高い結果を得るには

● 学習するデータ数

● モデルの設計

● 演算能力

結構大変!

Page 19: A practical guide to machine learning on GCP

GCP!

Page 20: A practical guide to machine learning on GCP

GCPと機械学習

Machine Learning API学習済みモデルで誰でも簡単に高精度な結果を

得ることができる

Serverless Machine Learning自分でモデルを作るときも簡単に学習と推論ができる

Page 21: A practical guide to machine learning on GCP

環境の使い分け

Cloud Machine Learning

Cloud Vision API

Cloud Speech API

Cloud Natural Language API

Cloud Translation

API

Cloud Video Intelligence

API

学習が不要 学習が必要

モデル作成不要 モデル作成必要

ML系API サーバーレスML

Page 22: A practical guide to machine learning on GCP

サーバーレスML

Cloud Machine Learning

Cloud Vision API

Cloud Speech API

Cloud Natural Language API

Cloud Translation

API

Cloud Video Intelligence

API

学習が不要 学習が必要

モデル作成不要 モデル作成必要

ML系API

どれか1つではなく使い分け&連携するのが

クール!

Page 23: A practical guide to machine learning on GCP

GCPのML系API

Page 24: A practical guide to machine learning on GCP

Cloud Vision API

LabelDetection

LandmarkDetection OCR

LogoDetection

FaceDetection

Explicit ContentDetection

Page 25: A practical guide to machine learning on GCP

ラベル検出

Page 26: A practical guide to machine learning on GCP

ラベル検出

from google.cloud import vision

client = vision.Client()

image = client.image(filename='seagull.jpg')

labels = image.detect_labels()

Page 27: A practical guide to machine learning on GCP

Cloud Natural Language API

Page 28: A practical guide to machine learning on GCP

感情分析

Page 29: A practical guide to machine learning on GCP

感情分析

from google.cloud import language

client = language.Client()

text = u"GCPが凄すぎて感動の渦"

document = client.document_from_text(text)

sentiment = document.analyze_sentiment()

Page 30: A practical guide to machine learning on GCP

Demo

Page 31: A practical guide to machine learning on GCP

感情分析でゲーム

ルールの説明

● AチームとBチームに別れます

● それぞれポジティブっぽい文章を

投稿します

● よりポジティブな文章で埋めた

チームが勝ち!

スクリーン

Aチーム Bチーム

Page 32: A practical guide to machine learning on GCP

Had fun?https://github.com/hayatoy/nl-bubble

Page 33: A practical guide to machine learning on GCP

ML API x GAEx Firebase

Page 34: A practical guide to machine learning on GCP

Web App

デモアプリのアーキテクチャ

Front-end Web AppApp Engine

FirebaseRealtime DB

Cloud NaturalLanguage API

Front-end Web AppApp Engine

Insert rowsvia REST API

Get sentiment analysis result

入力 結果表示

Page 35: A practical guide to machine learning on GCP

GAEからFirebase DBへインサート

url = 'https://[project].firebaseio.com/bubbles.json'

payload = json.dumps({'message': message,

'team': team,

'score': score,

'time':{'.sv':'timestamp'}})

urlfetch.fetch(url=url,

method=urlfetch.POST,

payload=payload)

Page 36: A practical guide to machine learning on GCP

JavascriptでFirebase DBの変更をリッスンする

var database = firebase.database();

var bubblesRef = firebase.database().ref('bubbles');

bubblesRef.on('child_added', function(snapshot) {

// do something..

});

Page 37: A practical guide to machine learning on GCP

Firebase簡単。他システムとの連携はGAEやCloud Functionsを使うといいかも。

Page 38: A practical guide to machine learning on GCP

Datalab x BigQuery

Page 39: A practical guide to machine learning on GCP

Cloud Datalab?

● Jupyter Notebookのクラウド版

● ブラウザ上でインタラクティブに

PythonコードやBigQueryのクエリが走る

Page 40: A practical guide to machine learning on GCP

Demo

Page 41: A practical guide to machine learning on GCP

Web App

デモアプリのアーキテクチャ

Front-end Web AppApp Engine

FirebaseRealtime DB

Cloud NaturalLanguage API

Front-end Web AppApp Engine

Insert rowsvia REST API

Get sentiment analysis result

BigQuery

CloudDatalab

Insert rowsvia REST API

Page 42: A practical guide to machine learning on GCP

Datalab上でBigQueryクエリ実行

%bq querySELECT team, SUM(score) as total_scoreFROM bubbles.devfest2017GROUP BY 1ORDER BY 2 DESC

セルの先頭にこれを書くだけ。

あとは普通にクエリを書ける。

Page 43: A practical guide to machine learning on GCP

クエリ結果をチャートにプロット

%bq query --name xxxSELECT ...

クエリに名前を付ける

%chart bars --data xxx{key: value..}

これだけ。

チャートオプション

チャートはGoogle Chartsを利用大体何でもできる

Page 44: A practical guide to machine learning on GCP

PandasからBigQueryクエリ実行

import pandas as pd

query = "SELECT * FROM ..."

df = pd.read_gbq(project_id='project', query=query)

そのままDataFrame形式で扱えるので便利!Pandas v0.20.0からread_gbqは別パッケージになったので注意

pip install pandas-gbq でインストールしてね。

Page 45: A practical guide to machine learning on GCP

GAE x ML Engine

(TensorFlow)

Page 46: A practical guide to machine learning on GCP

Cloud Machine Learning Engine?

● TensorFlowのフルマネージドサービス

● 学習・推論のジョブが流せる

● APIで他のシステムと連携ができる

Page 47: A practical guide to machine learning on GCP

Demo

Page 48: A practical guide to machine learning on GCP

リアルGCPUG

こんなモデルを作った。

Page 49: A practical guide to machine learning on GCP

pix2pix

Variableが200MBあって重い

Page 50: A practical guide to machine learning on GCP

重いモデルをどうやって使う?

● モバイルアプリに積むのは辛い

● GAEではTensorFlow動かない

● 自分でServeするのは面倒…

そうだML Engine!

Page 51: A practical guide to machine learning on GCP

GAEからOnline Prediction APIを使う

credentials = GoogleCredentials.get_application_default()ml = discovery.build("ml", "v1", credentials=credentials)

input_instance = dict(input=input_data, key="0")request_body = {"instances": [input_instance]}job_req = ml.projects().predict(name=[model], body=request_body)response = job_req.execute()

Page 52: A practical guide to machine learning on GCP

ML Engineの詳しい使い方

https://qiita.com/hayatoy

GCPと機械学習をもっと学びたい!

来月いい本が出るよ!

Page 53: A practical guide to machine learning on GCP

Thank you!

GDG DevFestTokyo 2017