Maschinelles Lernen in der JVM
Erik Rohkohl HTWK Leipzig/itemis AG
Outline
• Perceptron
• Multi-Layer-Perceptron (MLP) a.k.a. künstliches neuronales Netz
• Daten-Extraktion und Aufbereitung
• Model mit Deeplearning4J
• Training und Testen
https://github.com/erohkohl 2JUG Saxony Camp 2018
Perceptron
3https://github.com/erohkohlJUG Saxony Camp 2018
XOR-Problem
4https://github.com/erohkohlJUG Saxony Camp 2018
XOR-Problem
5
f(x, y) (x, y, z)
https://github.com/erohkohlJUG Saxony Camp 2018
TensorFlow Playground
6https://github.com/erohkohlJUG Saxony Camp 2018
Problemstellung
7https://github.com/erohkohlJUG Saxony Camp 2018
Merkmalsvektor
8https://github.com/erohkohlJUG Saxony Camp 2018
Vereinfachter Merkmalsvektor
9https://github.com/erohkohlJUG Saxony Camp 2018
One-hot Encoding
10https://github.com/erohkohlJUG Saxony Camp 2018
Input Vektor
11https://github.com/erohkohlJUG Saxony Camp 2018
12https://github.com/erohkohlJUG Saxony Camp 2018
One vs. All
13https://github.com/erohkohlJUG Saxony Camp 2018
Daten-Extraktion
14
HTML zeigen
https://github.com/erohkohlJUG Saxony Camp 2018
Daten-Extraktion
15
Document doc = Jsoup.parse(html); String[] s = doc.getElementsByClass("meals").eachText().get(0).split(","); Meal fastPlate = null; for (int i = 0; i < s.length; i++) { if (s[i].trim().equals(MenuType.FastPlate.getName())) { fastPlate = Meal.getType(s[i + 19].toString().trim()); break; } }
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(„yyyy-MM-dd"); String dateStr = p.getFileName().toString().subSequence(0, 10); LocalDate date = LocalDate.parse(dateStr, formatter);
return new Menu(fastPlate, date);
https://github.com/erohkohlJUG Saxony Camp 2018
Trainings Daten
16
INDArray input = Nd4j.zeros(trainNbr, inputSize); // row x col INDArray labels = Nd4j.zeros(trainNbr, targetSize);
for (int row = 0; row < trainNbr; row++) { int[][] menuVec = menusVec[row]; for (int col = 0; col < inputSize; col++) { input.putScalar(new int[]{row, col}, menuVec[0][col]); } for (int col = 0; col < targetSize; col++) { labels.putScalar(new int[]{row, col}, menuVec[1][col]); } }
DataSet ds = new DataSet(input, labels);
https://github.com/erohkohlJUG Saxony Camp 2018
MLP-Konfiguration
17
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder() .iterations(20000) .learningRate(0.01) .seed(123) .optimizationAlgo(OptimizationAlgorithm.LINE_GRADIENT_DESCENT) .biasInit(0) .list() .layer(0, new DenseLayer.Builder() .nIn(11) .nOut(32) .activation(Activation.RELU) .build())
. . . .layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.SQUARED_LOSS) .nIn(32) .nOut(3) .activation(Activation.SOFTMAX) .build()) .build();
https://github.com/erohkohlJUG Saxony Camp 2018
Training
18
MultiLayerNetwork net = new MultiLayerNetwork(conf); net.init(); net.fit(ds);
https://github.com/erohkohlJUG Saxony Camp 2018
Evaluation
19
INDArray output = net.output(ds.getFeatureMatrix());
Evaluation eval = new Evaluation(targetSize); eval.eval(ds.getLabels(), output); System.out.println(eval.stats());
https://github.com/erohkohlJUG Saxony Camp 2018
Testen
20
DataSet testDs = new DataSet(input, labels);
List<String> names = new ArrayList<>(); names.add(0, "Huehnchen"); names.add(1, "Schwein"); names.add(2, "Rind"); testDs.setLabelNames(names);
MultiLayerNetwork net = model.getNet(); List<String> prediction = net.predict(testDs);
https://github.com/erohkohlJUG Saxony Camp 2018
21https://github.com/erohkohlJUG Saxony Camp 2018
Ausblick
22
Rekurrente neuronale Netze LSTM Netze
https://github.com/erohkohlJUG Saxony Camp 2018
Quellen
23
• YouTube: Artificial Intelligence Courses, 7. Multiclass Classification One vs All • Deeplearning4J Documentation, A Beginner’s Guide to Multilayer Perceptron • Studentenwerk Leipzig, Mensa am Park Speiseplan • xkcd, AI Research
https://github.com/erohkohlJUG Saxony Camp 2018
24
Deep learning in Java's virtual machine https://github.com/erohkohl/ml-jvm
https://github.com/erohkohlJUG Saxony Camp 2018
Tagging Stack-Overflow questions with deep neural networks https://github.com/erohkohl/question-tagging