From a76066b7c53e3e5e6f3120f100b6137e459fab4d Mon Sep 17 00:00:00 2001 From: Kishar Date: Tue, 17 Oct 2023 13:03:22 +0530 Subject: [PATCH] Multilayerperceptron example added with seaborndataset --- .../gradle/wrapper/gradle-wrapper.properties | 5 ++ .../MultilayerPerceptronSeabornExample.java | 50 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 example/gradle/wrapper/gradle-wrapper.properties create mode 100644 example/src/main/java/de/example/nn/MultilayerPerceptronSeabornExample.java diff --git a/example/gradle/wrapper/gradle-wrapper.properties b/example/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..edc0bb1 --- /dev/null +++ b/example/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,5 @@ +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.5-bin.zip +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists diff --git a/example/src/main/java/de/example/nn/MultilayerPerceptronSeabornExample.java b/example/src/main/java/de/example/nn/MultilayerPerceptronSeabornExample.java new file mode 100644 index 0000000..ca43b49 --- /dev/null +++ b/example/src/main/java/de/example/nn/MultilayerPerceptronSeabornExample.java @@ -0,0 +1,50 @@ +package de.example.nn; + + +import de.edux.api.Classifier; +import de.edux.functions.activation.ActivationFunction; +import de.edux.functions.initialization.Initialization; +import de.edux.functions.loss.LossFunction; +import de.edux.ml.knn.KnnClassifier; +import de.edux.ml.nn.config.NetworkConfiguration; +import de.edux.ml.nn.network.MultilayerPerceptron; +import de.edux.ml.nn.network.api.Dataset; +import de.example.data.seaborn.Penguin; +import de.example.data.seaborn.SeabornDataProcessor; +import de.example.data.seaborn.SeabornProvider; + +import java.io.File; +import java.util.List; + +/** + * Knn - K nearest neighbors + * Dataset: Seaborn Penguins + */ +public class MultilayerPerceptronExample2 { + private static final boolean SHUFFLE = true; + private static final boolean NORMALIZE = true; + private static final boolean FILTER_INCOMPLETE_RECORDS = true; + private static final double TRAIN_TEST_SPLIT_RATIO = 0.75; + private static final File CSV_FILE = new File("example" + File.separator + "datasets" + File.separator + "seaborn-penguins" + File.separator + "penguins.csv"); + + public static void main(String[] args) { + var seabornDataProcessor = new SeabornDataProcessor(); + List data = seabornDataProcessor.loadDataSetFromCSV(CSV_FILE, ',', SHUFFLE, NORMALIZE, FILTER_INCOMPLETE_RECORDS); + + Dataset dataset = seabornDataProcessor.split(data, TRAIN_TEST_SPLIT_RATIO); + var seabornProvider = new SeabornProvider(data, dataset.trainData(), dataset.testData()); + seabornProvider.printStatistics(); + double[][] features = seabornProvider.getTrainFeatures(); + double[][] labels = seabornProvider.getTrainLabels(); + + double[][] testFeatures = seabornProvider.getTestFeatures(); + double[][] testLabels = seabornProvider.getTestLabels(); + + NetworkConfiguration networkConfiguration = new NetworkConfiguration(seabornProvider.getTrainFeatures()[0].length, List.of(32, 6), 3, 0.01, 1000, ActivationFunction.LEAKY_RELU, ActivationFunction.SOFTMAX, LossFunction.CATEGORICAL_CROSS_ENTROPY, Initialization.XAVIER, Initialization.XAVIER); + + MultilayerPerceptron multilayerPerceptron = new MultilayerPerceptron(networkConfiguration, testFeatures, testLabels); + multilayerPerceptron.train(seabornProvider.getTrainFeatures(), labels); + multilayerPerceptron.evaluate(testFeatures, testLabels); + } + +}