Introducing Reactive Machine Learning

81
Introducing Reactive Machine Learning Jeff Smith @jeffksmithjr

Transcript of Introducing Reactive Machine Learning

Page 1: Introducing Reactive Machine Learning

Introducing Reactive Machine Learning

Jeff Smith @jeffksmithjr

Page 2: Introducing Reactive Machine Learning

x.ai is a personal assistant who schedules meetings for you

Page 3: Introducing Reactive Machine Learning

You

Page 4: Introducing Reactive Machine Learning

nom nom, the data dogScala & Python Spark & Akka Couchbase Machine Learning

Page 5: Introducing Reactive Machine Learning

Reactive + Machine Learning

Page 6: Introducing Reactive Machine Learning

Machine Learning Systems

Page 7: Introducing Reactive Machine Learning

Traits of Reactive Systems

Page 8: Introducing Reactive Machine Learning

Responsive

Page 9: Introducing Reactive Machine Learning

Resilient

Page 10: Introducing Reactive Machine Learning

Elastic

Page 11: Introducing Reactive Machine Learning

Message-Driven

Page 12: Introducing Reactive Machine Learning

Reactive Strategies

Page 13: Introducing Reactive Machine Learning

Reactive Machine Learning

Page 14: Introducing Reactive Machine Learning

Reactive Machine Learning

Page 15: Introducing Reactive Machine Learning

Collecting Data

Page 16: Introducing Reactive Machine Learning

Machine Learning Systems

Page 17: Introducing Reactive Machine Learning
Page 18: Introducing Reactive Machine Learning
Page 19: Introducing Reactive Machine Learning

Uncertainty Interval

27 33

Page 20: Introducing Reactive Machine Learning

Immutable Factscase class PreyReading(sensorId: Int, locationId: Int, timestamp: Long, animalsLowerBound: Double, animalsUpperBound: Double, percentZebras: Double)

implicit val preyReadingFormatter = Json.format[PreyReading]

Page 21: Introducing Reactive Machine Learning

Distributed Data Storage

Page 22: Introducing Reactive Machine Learning

Partition Tolerance

Page 23: Introducing Reactive Machine Learning

Distributed Data Storage

Page 24: Introducing Reactive Machine Learning

Immutable Factsval reading = PreyReading(36, 12, currentTimeMillis(), 12.0, 18.0, 0.60)

val setDoc = bucket.set[PreyReading](readingId(reading), reading)

Page 25: Introducing Reactive Machine Learning

Scaling with Distributed Databases

Page 26: Introducing Reactive Machine Learning

Almost Immutable Data Model{ "sensor_id": 123, "readings": [ { "sensor_id": 456, "timestamp": 1457403598289, "lower_bound": 12.0, "upper_bound": 18.0, "percent_zebras": 0.60 } ] }

Page 27: Introducing Reactive Machine Learning

Hot Key

Page 28: Introducing Reactive Machine Learning

Truly Immutable Data Model{ "sensor_id_reading_id": 123456, "timestamp": 1457403598289, "lower_bound": 12.0, "upper_bound": 18.0, "percent_zebras": 0.60 }

Page 29: Introducing Reactive Machine Learning

Generating Features

Page 30: Introducing Reactive Machine Learning

Machine Learning Systems

Page 31: Introducing Reactive Machine Learning

Feature Generation

Raw Data FeaturesFeature Generation Pipeline

Page 32: Introducing Reactive Machine Learning

Microblogging Data

Page 33: Introducing Reactive Machine Learning

Pipeline Failure

Raw Data FeaturesFeature Generation Pipeline

Raw Data FeaturesFeature Generation Pipeline

Page 34: Introducing Reactive Machine Learning

Supervising Feature Generation

Raw Data FeaturesFeature Generation Pipeline

Supervision

Page 35: Introducing Reactive Machine Learning

Original Features

object SquawkLength extends FeatureType[Int]

object Super extends LabelType[Boolean]

val originalFeatures: Set[FeatureType] = Set(SquawkLength)val label = Super

Page 36: Introducing Reactive Machine Learning

Basic Features

object PastSquawks extends FeatureType[Int]

val basicFeatures = originalFeatures + PastSquawks

Page 37: Introducing Reactive Machine Learning

More Features

object MobileSquawker extends FeatureType[Boolean]

val moreFeatures = basicFeatures + MobileSquawker

Page 38: Introducing Reactive Machine Learning

Feature Collections

case class FeatureCollection(id: Int, createdAt: DateTime, features: Set[_ <: FeatureType[_]], label: LabelType[_])

Page 39: Introducing Reactive Machine Learning

Feature Collectionsval earlierCollection = FeatureCollection(101, earlier, basicFeatures, label)

val latestCollection = FeatureCollection(202, now, moreFeatures, label)

val featureCollections = sc.parallelize( Seq(earlierCollection, latestCollection))

Page 40: Introducing Reactive Machine Learning

Fallback Collections

val FallbackCollection = FeatureCollection(404, beginningOfTime, originalFeatures, label)

Page 41: Introducing Reactive Machine Learning

Fallback Collectionsdef validCollection(collections: RDD[FeatureCollection], invalidFeatures: Set[FeatureType[_]]) = { val validCollections = collections.filter( fc => !fc.features .exists(invalidFeatures.contains)) .sortBy(collection => collection.id) if (validCollections.count() > 0) { validCollections.first() } else FallbackCollection}

Page 42: Introducing Reactive Machine Learning

Learning Models

Page 43: Introducing Reactive Machine Learning

Machine Learning Systems

Page 44: Introducing Reactive Machine Learning

Learning Models

Features ModelModel Learning Pipeline

Page 45: Introducing Reactive Machine Learning

Models of Love

Page 46: Introducing Reactive Machine Learning

Traits of Spark

Page 47: Introducing Reactive Machine Learning

Reactive Strategies in Spark

Page 48: Introducing Reactive Machine Learning

Data Preparationval labelIndexer = new StringIndexer() .setInputCol("label") .setOutputCol("indexedLabel") .fit(instances)

val featureIndexer = new VectorIndexer() .setInputCol("features") .setOutputCol("indexedFeatures") .fit(instances)

val Array(trainingData, testingData) = instances.randomSplit( Array(0.8, 0.2))

Page 49: Introducing Reactive Machine Learning

Learning a Modelval decisionTree = new DecisionTreeClassifier() .setLabelCol("indexedLabel") .setFeaturesCol("indexedFeatures")

val labelConverter = new IndexToString() .setInputCol("prediction") .setOutputCol("predictedLabel") .setLabels(labelIndexer.labels)

val pipeline = new Pipeline() .setStages(Array(labelIndexer, featureIndexer, decisionTree, labelConverter))

Page 50: Introducing Reactive Machine Learning

Evolving Modeling Strategiesval randomForest = new RandomForestClassifier() .setLabelCol("indexedLabel") .setFeaturesCol("indexedFeatures")

val revisedPipeline = new Pipeline() .setStages(Array(labelIndexer, featureIndexer, randomForest, labelConverter))

Page 51: Introducing Reactive Machine Learning

Deep Models of Artistic Style

Page 52: Introducing Reactive Machine Learning

Refactoring Command Line Tools> python neural-art-tf.py -m vgg -mp ./vgg -c ./images/bear.jpg -s ./images/style.jpg -w 800

def produce_art(content_image_path, style_image_path, model_path, model_type, width, alpha, beta, num_iters):

Page 53: Introducing Reactive Machine Learning

Exposing a Serviceclass NeuralServer(object): def generate(self, content_image_path, style_image_path, model_path, model_type, width, alpha, beta, num_iters): produce_art(content_image_path, style_image_path, model_path, model_type, width, alpha, beta, num_iters) return True

daemon = Pyro4.Daemon() ns = Pyro4.locateNS() uri = daemon.register(NeuralServer) ns.register("neuralserver", uri) daemon.requestLoop()

Page 54: Introducing Reactive Machine Learning

Encoding Model Types

object ModelType extends Enumeration { type ModelType = Value val VGG = Value("VGG") val I2V = Value("I2V") }

Page 55: Introducing Reactive Machine Learning

Encoding Valid Configurationcase class JobConfiguration(contentPath: String, stylePath: String, modelPath: String, modelType: ModelType, width: Integer = 800, alpha: java.lang.Double = 1.0, beta: java.lang.Double = 200.0, iterations: Integer = 5000)

Page 56: Introducing Reactive Machine Learning

Finding the Serviceval ns = NameServerProxy.locateNS(null) val remoteServer = new PyroProxy(ns.lookup("neuralserver"))

Page 57: Introducing Reactive Machine Learning

Calling the Servicedef callServer(remoteServer: PyroProxy, jobConfiguration: JobConfiguration) = { Future.firstCompletedOf( List( timedOut, Future { remoteServer.call("generate", jobConfiguration.contentPath, jobConfiguration.stylePath, jobConfiguration.modelPath, jobConfiguration.modelType.toString, jobConfiguration.width, jobConfiguration.alpha, jobConfiguration.beta, jobConfiguration.iterations).asInstanceOf[Boolean] }))}

Page 58: Introducing Reactive Machine Learning

Profiles with Style

Page 59: Introducing Reactive Machine Learning

Hybrid Model learning

Features ModelModel Learning Pipeline

Page 60: Introducing Reactive Machine Learning

Publishing Models

Page 61: Introducing Reactive Machine Learning

Machine Learning Systems

Page 62: Introducing Reactive Machine Learning

Publishing Models

Model Predictive ServicePublishing Process

Page 63: Introducing Reactive Machine Learning

Building Lineages

val rawData: RawData val featureSet: Set[FeatureType] val model: ClassificationModel val modelMetrics: BinaryClassificationMetrics

Page 64: Introducing Reactive Machine Learning

Predicting

Page 65: Introducing Reactive Machine Learning

Machine Learning Systems

Page 66: Introducing Reactive Machine Learning

Predicting

Client Predictive ServiceRequest> <Prediction

Page 67: Introducing Reactive Machine Learning

Models as Servicesdef predict(model: Features => Prediction) (features: Features): Future[Either[String, Prediction]] = { ??? }

Page 68: Introducing Reactive Machine Learning

Models as Servicesval routes = { pathPrefix("model") { (post & entity(as[PredictionRequest])) { context => complete { predict(model: Features => Prediction)(extractFeatures(context)) .map[ToResponseMarshallable] { case Right(prediction) => PredictionSummary(prediction) case Left(errorMessage) => BadRequest -> errorMessage } } } } }

Page 69: Introducing Reactive Machine Learning

Clojure Functions

Page 70: Introducing Reactive Machine Learning

Clojure Predictive Service

Page 71: Introducing Reactive Machine Learning

Predicting

Client Predictive ServiceRequest

Page 72: Introducing Reactive Machine Learning

Summary

Page 73: Introducing Reactive Machine Learning

Machine Learning Systems

Page 74: Introducing Reactive Machine Learning

Traits of Reactive Systems

Page 75: Introducing Reactive Machine Learning

Reactive Strategies

Page 76: Introducing Reactive Machine Learning

Reactive Machine Learning

Page 77: Introducing Reactive Machine Learning

For Later

Page 78: Introducing Reactive Machine Learning

manning.com reactivemachinelearning.com medium.com/data-engineering

M A N N I N G

Jeff SmithUse the code reactnymu for 39% off!

Page 79: Introducing Reactive Machine Learning

x.ai @xdotai [email protected] New York, New York

We’re hiring!

Page 80: Introducing Reactive Machine Learning

Thank You!

Page 81: Introducing Reactive Machine Learning

Code: reactnymu

Jeff Smith @jeffksmithjr reactivemachinelearning.com

M A N N I N G

Jeff Smith