使用 JMH 对我的神经网络进行基准测试,但我如何混合我的 Maven 依赖项?

Benchmarking my neural network with JMH, but how do I mix my maven dependencies?

我按照本指南 (http://tutorials.jenkov.com/java-performance/jmh.html) 并使用 class MyBenchmark 打开了一个新项目,如下所示:

package com.jenkov;

import org.openjdk.jmh.annotations.Benchmark;
import org.datavec.api.records.reader.RecordReader;
import org.datavec.api.records.reader.impl.csv.CSVRecordReader;
import org.datavec.api.split.FileSplit;
import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.weights.WeightInit;
import org.deeplearning4j.optimize.listeners.PerformanceListener;
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
import org.deeplearning4j.examples.utils.DownloaderUtility;
import org.nd4j.evaluation.classification.Evaluation;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.dataset.SplitTestAndTrain;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization;
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize;
import org.nd4j.linalg.learning.config.Sgd;
import org.nd4j.linalg.lossfunctions.LossFunctions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.File;

public class MyBenchmark {

    @Benchmark
    public void testMethod() {


            Logger log = LoggerFactory.getLogger(MyBenchmark.class);


                //First: get the dataset using the record reader. CSVRecordReader handles loading/parsing
                int numLinesToSkip = 0;
                char delimiter = ',';
                RecordReader recordReader = new CSVRecordReader(numLinesToSkip,delimiter);
                recordReader.initialize(new FileSplit(new File(DownloaderUtility.IRISDATA.Download(),"iris.txt")));

                //Second: the RecordReaderDataSetIterator handles conversion to DataSet objects, ready for use in neural network
                int labelIndex = 4;     //5 values in each row of the iris.txt CSV: 4 input features followed by an integer label (class) index. Labels are the 5th value (index 4) in each row
                int numClasses = 3;     //3 classes (types of iris flowers) in the iris data set. Classes have integer values 0, 1 or 2
                int batchSize = 150;    //Iris data set: 150 examples total. We are loading all of them into one DataSet (not recommended for large data sets)

                DataSetIterator iterator = new RecordReaderDataSetIterator(recordReader,batchSize,labelIndex,numClasses);
                DataSet allData = iterator.next();
                allData.shuffle();
                SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.65);  //Use 65% of data for training

                DataSet trainingData = testAndTrain.getTrain();
                DataSet testData = testAndTrain.getTest();

                //We need to normalize our data. We'll use NormalizeStandardize (which gives us mean 0, unit variance):
                DataNormalization normalizer = new NormalizerStandardize();
                normalizer.fit(trainingData);           //Collect the statistics (mean/stdev) from the training data. This does not modify the input data
                normalizer.transform(trainingData);     //Apply normalization to the training data
                normalizer.transform(testData);         //Apply normalization to the test data. This is using statistics calculated from the *training* set


                final int numInputs = 4;
                int outputNum = 3;
                long seed = 6;


                log.info("Build model....");
                MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                    .seed(seed)
                    .activation(Activation.TANH)
                    .weightInit(WeightInit.XAVIER)
                    .updater(new Sgd(0.1))
                    .l2(1e-4)
                    .list()
                    .layer(new DenseLayer.Builder().nIn(numInputs).nOut(3)
                        .build())
                    .layer(new DenseLayer.Builder().nIn(3).nOut(3)
                        .build())
                    .layer( new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                        .activation(Activation.SOFTMAX) //Override the global TANH activation with softmax for this layer
                        .nIn(3).nOut(outputNum).build())
                    .build();

                //run the model
                MultiLayerNetwork model = new MultiLayerNetwork(conf);
                model.init();
                //record score once every 100 iterations
                model.setListeners(new ScoreIterationListener(100));
                model.setListeners(new PerformanceListener(100));
                for(int i=0; i<1000; i++ ) {
                    model.fit(trainingData);
                }

                //evaluate the model on the test set
                Evaluation eval = new Evaluation(3);
                INDArray output = model.output(testData.getFeatures());
                eval.eval(testData.getLabels(), output);
                log.info(eval.stats());

            }

        }

还有我的 pom.xml,其中包含 deeplearning4j-examples 依赖项:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.jenkov</groupId>
<artifactId>first-benchmark</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>JMH benchmark sample: Java</name>

<!--
   This is the demo/sample template build script for building Java benchmarks with JMH.
   Edit as needed.
-->

<dependencies>
    <dependency>
        <groupId>org.openjdk.jmh</groupId>
        <artifactId>jmh-core</artifactId>
        <version>${jmh.version}</version>
    </dependency>
    <dependency>
        <groupId>org.openjdk.jmh</groupId>
        <artifactId>jmh-generator-annprocess</artifactId>
        <version>${jmh.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.datavec</groupId>
        <artifactId>datavec-api</artifactId>
        <version>1.0.0-beta7</version>
    </dependency>
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-datasets</artifactId>
        <version>1.0.0-beta7</version>
    </dependency>
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-examples</artifactId>
        <version>0.0.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-nn</artifactId>
        <version>1.0.0-beta7</version>
    </dependency>
    <dependency>
        <groupId>com.jenkov</groupId>
        <artifactId>first-benchmark</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

    <!--
        JMH version to use with this project.
      -->
    <jmh.version>1.28</jmh.version>

    <!--
        Java source/target to use for compilation.
      -->
    <javac.target>1.8</javac.target>

    <!--
        Name of the benchmark Uber-JAR to generate.
      -->
    <uberjar.name>benchmarks</uberjar.name>
</properties>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.0</version>
            <configuration>
                <compilerVersion>${javac.target}</compilerVersion>
                <source>${javac.target}</source>
                <target>${javac.target}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <finalName>${uberjar.name}</finalName>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>org.openjdk.jmh.Main</mainClass>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                        </transformers>
                        <filters>
                            <filter>
                                <!--
                                    Shading signed JARs will fail without this.
                                    
                                -->
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <pluginManagement>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.5</version>
            </plugin>
            <plugin>
                <artifactId>maven-deploy-plugin</artifactId>
                <version>2.8.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-install-plugin</artifactId>
                <version>2.5.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
            </plugin>
            <plugin>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
            </plugin>
            <plugin>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.3</version>
            </plugin>
            <plugin>
                <artifactId>maven-source-plugin</artifactId>
                <version>2.2.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

现在据我所知,我必须将我的神经网络代码放在那里(这是一个 DL4J-Example-NN),运行 在终端中“mvn clean install” 运行 “java -jar target/benchmarks.jar” 在那里,但是 My.Benchmark.java 无法导入

import org.deeplearning4j.examples.utils.DownloaderUtility;

即使我声明了依赖关系

<dependency>
            <groupId>org.deeplearning4j</groupId>
            <artifactId>deeplearning4j-examples</artifactId>
            <version>0.0.3.1</version>
        </dependency>

为了它 我错过了什么?这个方法到底对不对?

非常感谢

您需要构建一个可执行 JAR。

参见例如How can I create an executable JAR with dependencies using Maven? 了解如何使用 Maven 执行此操作的信息。

您可以使用 maven 程序集或 maven shade 插件。

这可能有点棘手,我不知道你做了什么。但这是让它工作的方法。

  1. 制作一个简单的 hello world jmh 类型的项目并测试它是否有效。

  2. 下载 dl4j 示例 github 并测试一个示例以确保其有效。

  3. 既然两者都在工作,请进入 DL4J 示例并注意不同的包有不同的 pom 文件。此外还有一个用于整个 dl4j 父级的主 pom。您应该使用 DL4J 主示例 POM!不要尝试自己创建

  4. 我的建议是新建一个项目。使用 dl4j master 作为父 pom 文件,然后在项目目录中添加第二个 pom 文件。您可以在 dl4j 示例 github 中清楚地看到这一点,这似乎也是适合您情况的最佳实践。