machine learning mit java und h2o · 5/7/2018  · intro h2o machine learning platform 9. h 2 o...

34
MACHINE LEARNING MIT JAVA UND H2O Dr. Jonathan Boidol, Stephan Schiffner 05.07.2018, Java Forum, Stuttgart

Upload: others

Post on 20-May-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

MACHINE LEARNING MIT JAVA UND H2O

Dr. Jonathan Boidol, Stephan Schiffner05.07.2018, Java Forum, Stuttgart

Page 2: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Outline

Introduction H2O

Demo Use Case: Klassifikationvon Sentiment in Tweets

2

ML Pipeline Modellbildung

ML Pipeline Anwendung

Mehr Machine Learning mit H2O

Page 3: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Machine Learning im Einsatz

3

Page 4: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Machine Learning in a Nutshell

4

Gelabelte Trainingsdaten

Machine Learning Algo.

Classifier

Neue (ungelabelte)

Daten

Vorhersagen

Oder wenigstens: supervised learning…

Page 5: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Machine Learning im Einsatz

Produktiv-Systeme

5

Data Science

Page 6: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Tweet KlassifikationSentimentvorhersage mit WordEmbeddings

6

Page 7: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Ziel

7

Live-Vorhersage von Wetter-Gefühlen aus

Tweets:

▪ Textvorverarbeitung von Tweets

▪ Training eines Word2Vec Modell

▪ Training eines 4-Klassen Klassifikators

anhand der Word2Vec-Vektoren

▪ Streaming und Klassifizierung neuer

Tweets

Page 8: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Weather Tweets

Trainingsdaten▪ Datensatz von Tweets▪ 1000 gelabelte Beispiele▪ 4 Klassen einschl. „nicht

relevant“

8

Negative

“Uqhhh it's sooooo hot outside ! #Texasweather”

Neutral / author is just sharing information“Weather Alert: Flood Warning issued May 22 at 6:32PM MDT expiring May 23 at 9:32AM MDT by NWS Glasgow {link}... {link}”

Positive“Friday evening. Weather? Beautiful. Last man standing at the office. Bitter? Nah. It only makes payday that much sweeter. Stay hungry.”

Not related to weather condition“Community Blood Center Media Blood drive Tues Noon-6 at Westridge Mall by Penneys lower level - Blood to help Joplin st ...”

Page 9: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Intro H2OMachine Learning Platform

9

Page 10: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

H2O Fakten

▪ Open source▪ In-memory, verteilte, skalierbare ML platform▪ Erstellen von ML Modellen und einfacher Export zur

Produktivanwendung der Modelle▪ Hocheffiziente Versionen bekannter Algorithmen▪ APIs: ▪ H2O4GPU, Sparkling Water, Steam▪ Leader im 2018 Gartner Magic Quadrant für Data Science and

Machine Learning Platforms

10

Page 11: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Überblick H2O

Source: https://www.h2o.ai/h2o/ 11

Page 12: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

H2O-Cluster

• H2O Cluster in Java (auch lokal)

• Funktionsaufrufe werden in REST-Calls übersetzt

• Interfaces in R/Python/Java/Scala/GUI

12Source: https://www.h2o.ai/h2o/

Page 13: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Use-Case ArchitekturImplementiert mit R, H2O, Spark Streaming & Java

13

Page 14: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Architektur

14

Trainingsdaten Modelltraining

VorhersageTwitter

Modell

Streaming

Modell

Page 15: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Trainings-Pipeline

16

Page 16: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Teil I: R-Studio

17

Page 17: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Datenimport

library(h2o)

h2o_context <- h2o.init(nthreads=-1, max_mem_size = "12G")

# aus Filesystem:

twitter_data_raw <- h2o.importFile("weather-agg-DFE.csv", header = T)

# Aufbereitung und Formatierung

...

w2v.model <- h2o.word2vec(words, epochs = 100)

twitter.vecs <- h2o.transform(w2v.model, tokenize(twitter_data_df$tweet))

twitter_data_embedded <- h2o.cbind(twitter_data_df[, c("confidence", "emotion")],

twitter.vecs)

18

Page 18: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Datenimport

library(h2o)

h2o_context <- h2o.init(nthreads=-1, max_mem_size = "12G")

# aus Hadoop:

twitter_data_hdfs <- "hdfs://node-1:/user/data/twitter/weather-agg-DFE.csv"

twitter_data_raw <- h2o.importFile(twitter_data_hdfs, header = T)

# Aufbereitung und Formatierung

...

w2v.model <- h2o.word2vec(words, epochs = 100)

twitter.vecs <- h2o.transform(w2v.model, tokenize(twitter_data_df$tweet))

twitter_data_embedded <- h2o.cbind(twitter_data_df[, c("confidence", "emotion")],

twitter.vecs)

19

Page 19: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Entscheidungsbäume

20

Ja Nein

sun

too rain

pleasant

• Hierarchische Baum-Struktur

• Abzweigungen nach Kriterien

• Blatt des Baums -> Entscheidung

gefunden

• Varianten:

• Mehrere Bäume

• Spezialisierte Modelle in Blättern

• Kombination mit NN

Page 20: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Modellentwicklung

target <- "emotion"

emotion_model_gbm <- h2o.gbm(y = target,

x = feature_list,

training_frame = twitter_data_embedded_train,

ntrees = 100,

max_depth = 4,

learn_rate = 0.1 )

# Validierung, Parameteroptimierung, andere Modelle

...

21

Page 21: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Modellfiles

22

H2O + Spark Cluster Integration ist optional möglich• Entwicklung direkt in Spark-Umgebung• Sparkling Water• Auch hier Python/R/… Interfaces

H2O-Cluster/-Umgebung/… nicht mehr nötig:• Dependencies für Java-Einbindung• Serialisierte Modelle

vs.

Page 22: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

MOJO vs POJO

POJO:• Kompilierbare Base-Klassen der Modelle• Braucht h2o-genmodel.jar

MOJO:• Model ObJect, Optimized• Platzsparendes Binärformat• Erst für wenige Modelle verfügbar• Braucht h2o-genmodel.jar

23

public class MyPojo {

private String someProperty;

public String getSomeProperty() {

return someProperty;

}

public void setSomeProperty(String

someProperty) {

this.someProperty = someProperty;

}

}

Page 23: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Modellexport

# model_dependencies.jar enthält Abhängigkeiten für Java:

# Importfunktionen, Modelwrapper, etc..

modelfile <- h2o.download_mojo(emotion_model_gbm,

path = "/model/dir",

get_genmodel_jar = T,

genmodel_name = "model_dependencies.jar")

24

Page 24: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Anwendungs-Pipeline

25

Page 25: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Java & Spark

26

Page 26: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Spark/Spark-Streaming in a nutshell

27

Slavetasktasktasktasktask

Slavetasktasktasktasktask

Slavetasktasktasktasktask

Slavetasktasktasktasktask

MasterSpark

Context

Datenstream

Spark Streaming

Rece

iver Batches Results

Page 27: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Spark Streaming

// Erzeugen des spark streaming context

SparkConf sparkConf = new SparkConf().setMaster("local[4]").setAppName("StreamDemo");

JavaStreamingContext ssc = new JavaStreamingContext(sparkConf, Durations.seconds(2));

// stream processing definieren

JavaReceiverInputDStream<Status> tweetStream = TwitterUtils.createStream(ssc, filters);

...

// Start des stream-processing

ssc.start();

ssc.awaitTermination();

ssc.close();

28

Page 28: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Modell Einbinden// Mojos einlesen

import hex.genmodel.algos.word2vec.WordEmbeddingModel;

import hex.genmodel.MojoModel;

import hex.genmodel.easy.EasyPredictModelWrapper;

EasyPredictModelWrapper predictionModel = new EasyPredictModelWrapper(

MojoModel.load(predictionModelPath));

WordEmbeddingModel w2vModel = Word2VecMojoModel.load(w2vModelPath);

29

Page 29: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Scoring

//eigentliches scoring

float[] tweetEmbedding = new float[vecSize];

words.stream().map(word ->

w2vModel.transform0(word,

new float[vecSize])

).reduce(

tweetEmbedding, Utils::sumFloatarrays

);

MultinomialModelPrediction prediction = predictionModel.predictMultinomial(tweetEmbedding);

30

Page 30: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Live-DemoWenn das Wifi mitspielt…

31

Page 31: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Ausblick H2OWeitere Machine Learning Fähigkeiten

32

Page 32: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Ensemble Modelsens <- h2o.stackedEnsemble(

…)

H2O Machine Learning: More

Deep NNsm_deepl = h2o.deeplearning(

epochs = 50,

hidden = c(20, 20)

)

XGBoostsm_xgb = h2o.xgb(

…)

Optimizationparam_opt <- h2o.grid(

"model", hyper_params = …)

33

Page 33: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

H2O Flow

GUI für Programmierfaule

34

http://localhost:54321/flow/index.html#

Page 34: MACHINE LEARNING MIT JAVA UND H2O · 5/7/2018  · Intro H2O Machine Learning Platform 9. H 2 O Fakten Open source In-memory, verteilte, skalierbare ML platform Erstellen von ML Modellen

Thank you foryour attention!

Questions?

36