MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere,...

39
CADEC 2019.01.24 & 2019.01.30 | CALLISTAENTERPRISE.SE MACHINE LEARNING IN JAVA DAVID STRÖM

Transcript of MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere,...

Page 1: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

CADEC 2019.01.24 & 2019.01.30 | CALLISTAENTERPRISE.SE

MACHINE LEARNING IN JAVA

DAVID STRÖM

Page 2: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

TODAY'S QUESTIONS

• What is it? • Why use machine learning, and why

use Java? • How do we do it?

?

Page 3: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

WHAT?

Page 4: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

WHAT

DEEP LEARNING MACHINE LEARNINGARTIFICIAL INTELLIGENCE

Page 5: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

WHY?

Page 6: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

WHY

• Some things are almost impossible to solve without, e.g. - Image recognition

• While other things get (a lot) better - Natural language processing - Recommendations - Robotic process automation - Anomaly detection

• Patterns are everywhere - Ask what your data can tell you?

Page 7: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

WHY - NOW

Page 8: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

CHALLENGE: NEW SKILLS

DATA MODELING

PYTHON

TENSOR FLOWSCI-KIT

R

BIG DATA

STATISTICAL ANALYSIS SQL

JAVAC#

SYSTEM DESIGN

TDD

DDDDATA SCIENCE SOFTWARE ENGINEERING

Page 9: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

DEEPLEARNING4J

• Open source • Includes various tools for ML - ND4J - DataVec - Arbiter - Some visualization tools

• Import Keras models • Supports dataprocessing on CUDA*

enabled GPUs (Nvidia)

*CUDA: COMPUTE UNIFIED DEVICE ARCHITECTURE

Page 10: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

WHY JAVA

• Python is by far more common machine learning language, but... - Java is versatile with huge

ecosystem of tools - Great number of systems are built

in Java - Great number of software

engineers use Java primarily

Page 11: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW?

Page 12: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW BUILD - DEEPLEARNING4J

dependencies { compile("org.deeplearning4j:deeplearning4j-core:1.0.0-beta3") compile("org.nd4j:nd4j-native-platform:1.0.0-beta3") }

Alternatively:

dependencies { compile("org.deeplearning4j:deeplearning4j-core:1.0.0-beta3") compile("org.nd4j:nd4j-cuda-9.2:1.0.0-beta3") }

Page 13: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

THE PROCESS

FINAL HYPOTHESIS

BUSINESS TARGET

% AQUIRE RAW DATA

PRE PROCESSSELECT MODEL

VALIDATE RESULT

TRAIN

FINAL HYPOTHESIS

TRIM OR CHANGE MODEL

IMPLEMENT

Page 14: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

THE PROCESS

FINAL HYPOTHESIS

BUSINESS TARGET

% AQUIRE RAW DATA

PRE PROCESSSELECT MODEL

VALIDATE RESULT

TRAIN

FINAL HYPOTHESIS

TRIM OR CHANGE MODEL

IMPLEMENT

Page 15: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

BUSINESS TARGET

} "A"

Page 16: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

DATA

Page 17: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW - INPUT DATA

SOUND

VIDEODOCUMENTS/TEXT

IMAGES

DATABASE/TABULAR DATA

Page 18: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW - INPUT PREPROCESSING

INPUTS

}233, 123, 34.. .

233, 123, 34.. .

212, 12, 43.. .

123, 243, 221. . .

2, 87, 123.. .

233, 123, 34.. .

233, 123, 34.. .

Page 19: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW - INPUT PREPROCESSING

// ... FileSplit fileSplit = new FileSplit(directory, {".png"}); ParentPathLabelGenerator labelMaker = new ParentPathLabelGenerator(); ImageRecordReader recordReader = new ImageRecordReader(28,28,1, labelMaker); recordReader.initialize(fileSplit); // ...

Page 20: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

MODEL

Page 21: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

MODEL - ARTIFICIAL NEURAL NETWORK ARCHITECTURE

• Three common types: - MLP (Multi layer perceptron) - CNN (Convolutional neural network) - RNN (Recurrent neural network)

• Hybrid networks: use layers or subnets of different types

Page 22: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

MODEL CHARACTERISTICS

Page 23: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

DEEP LEARNING - MULTI LAYER PERCEPTRON

• General purpose architecture • Particularly useful for tabular data,

e.g. csv-files.

DATABASE/TABULAR DATA

Page 24: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

DEEP LEARNING - CONVOLUTIONAL NEURAL NETWORK

• Useful to make generalisations of the input (has/has not)

• Particularly useful for identifying patterns in images

• Not good for anomaly detection

IMAGES

Page 25: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

DEEP LEARNING - RECURRENT NEURAL NETWORK

• Complex, often difficult to train • LSTM (Long-short term memory)

successful exception • Useful for time-series such as sound

or text

SOUNDDOCUMENTS/TEXT

Page 26: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW TO BUILD?

Page 27: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW BUILD - CONVOLUTIONAL NEURAL NETWORK

• We will build a convolutional neural network using Deeplearning4J

Page 28: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

AINPUT (400X400) DENSE

LAYER 150

CONVOLUTION 5X5

SUBSAMPLING 100

CONVOLUTION 5X5

SUBSAMPLING 50

OUTPUT LAYER 26 (A-Z)

"A"

Page 29: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW BUILD - CONVOLUTIONAL NEURAL NETWORK

• We will build a convolutional neural network using Deeplearning4J • Use Deeplearning4J MultilayerConfiguration to build a MultiLayerNetwork

Page 30: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW TO BUILD - MULTILAYERCONFIGURATION

// ... MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .seed(132); .optimizationAlgo(STOCHASTIC_GRADIENT_DESCENT) .weightInit(XAVIER) .updater(new Nesterov(learningRate, momentum)) .list() .layer(0,new ConvolutionLayer.Builder(5,5) .nIn(1).nOut(100).activation(IDENTITY).build()) // ... More layers here .layer(5,new OutputLayer.Builder() .lossFunction(NEGATIVELOGLIKELIHOOD) .nOut(26).activation(SOFTMAX).build()) .backprop(true) .build(); // ...

Page 31: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW TO BUILD - CONVOLUTIONAL NEURAL NETWORK

• We will build a convolutional neural network using Deeplearning4J • Use Deeplearning4J MultilayerConfiguration to build a MultiLayerNetwork • Use Deeplearning4J EarlyStoppingTrainer to train and save the network

Page 32: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW TO BUILD - EARLYSTOPPINGTRAINER

private static EarlyStoppingTrainer trainer(DataSetIterator iter, MultiLayerNetwork model) { EarlyStoppingConfiguration conf = new EarlyStoppingConfiguration.Builder() .epochTerminationConditions(new MaxEpochsTerminationCondition(30)); .iterationTerminationConditions(new MaxTimeIterationTerminationCondition (2, HOURS)) .scoreCalculator(new ClassificationScoreCalculator(ACCURACY, iter)) .evaluateEveryNEpochs(1) .modelSaver(new LocalFileModelSaver(modelSaveDirectory) .build(); return new EarlyStoppingTrainer(conf, model, iter); }

Page 33: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java
Page 34: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java
Page 35: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

HOW TO USE?

Page 36: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

DEMO

Page 37: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

ALL THE ANSWERS

• What is it? - Self-adapting algorithms to identify patterns

• Why machine learning? - Patterns are everywhere, you probably have

patterns in your data, how can you use that? • Why use Java for machine learning? - Java has a great ecosystem of tools and

frameworks - Sheer force of numbers (systems and

developers) • How can we use Java for machine learning? - I think DL4J solves many of the problems

related to working with ML...

!

Page 38: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

SOME LINKS

• https://deeplearning4j.org/ • https://skymind.ai/wiki/ • https://archive.ics.uci.edu/ml/datasets.html • https://www.analyticsvidhya.com/blog/ • https://machinelearningmastery.com/blog/

Page 39: MACHINE LEARNING IN JAVA - Callista Enterprise€¢ Why machine learning? -Patterns are everywhere, you probably have patterns in your data, how can you use that? • Why use Java

THANK YOU!

ALL CODE USED IN DEMOS: HTTPS://GITHUB.COM/CALLISTAENTERPRISE/CADEC-2019-DEEPLEARNING4J.GIT