简体   繁体   English

创建 DL4J java 应用程序

[英]Creating a DL4J java application

How can I use DL4J in my own Java project using maven ?如何在我自己的Java项目中使用maven使用DL4J

For this project I am going to need to shade dependencies inside of my new projects jar however doing this causes my jar file to be almost 500MB when its normally like 4MB it also causes my jar to crash (this is before I even do anything with the shaded dependencies) so I assume I am doing something wrong.对于这个项目,我将需要在我的新项目 jar 中隐藏依赖关系,但是这样做会导致我的 jar 文件几乎达到500MB ,而它通常是4MB ,它还会导致我的 jar 崩溃(这是在我对阴影依赖项)所以我假设我做错了什么。

Here is what I added to my pom :这是我添加到我的pom中的内容:

<properties>
    <java.version>1.8</java.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <dl4j-master.version>1.0.0-M2</dl4j-master.version>
    <logback.version>1.2.3</logback.version>
    <maven-shade-plugin.version>2.4.3</maven-shade-plugin.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.deeplearning4j</groupId>
        <artifactId>deeplearning4j-core</artifactId>
        <version>${dl4j-master.version}</version>
    </dependency>

    <dependency>
        <groupId>org.nd4j</groupId>
        <artifactId>nd4j-native</artifactId>
        <version>${dl4j-master.version}</version>
    </dependency>
 </dependencies>

Then to resolve the maven errors I went into my project structure and added the following libraries.然后为了解决maven错误,我进入了我的项目结构并添加了以下库。 I then clicked add to project which created a libs folder and then downloaded a ton of files.然后我单击添加到项目,它创建了一个 libs 文件夹,然后下载了大量文件。 This did resolve all errors.这确实解决了所有错误。

org.apache.cassandra:cassandra-all:1.1.42
org.deeplearning4j:deeplearning4j-core:1.0.0-alpha2

Looking in my libs folder I know I can delete the android stuff but I assume I need to keep all of the windows , linux , and macos jars so that I can run my jar on multiple operating systems however there is a bunch more here that I am not sure about.查看我的 libs 文件夹,我知道我可以删除 android 的东西,但我假设我需要保留所有windowslinuxmacos jars 以便我可以在多个操作系统上运行我的 jar 但是这里还有很多我我不确定。

My objective with DL4J is to train a model to recognize a pattern and return an array of size 2 with one of the 9 permutations of 0, 1, and -1 for example:我使用DL4J的目标是训练一个 model 来识别一个模式并返回一个大小为 2 的数组,其中包含 0、1 和 -1 的 9 种排列之一,例如:

new int[]{0, 1}, new int[] {1, 0}, new int[] {1, -1}

To train the model I can supply it with as much data as it needs (please let me know an estimated amount).为了训练 model,我可以为其提供所需的数据(请告知估计数量)。 This data will be of high quality meaning that it will contain a few integers and booleans along with being assign one of the 9 array permutations (all of this data will be accurate).该数据将是高质量的,这意味着它将包含一些整数和布尔值以及分配 9 个数组排列之一(所有这些数据都是准确的)。 From this I am hoping to be able to train the model to output all array permutations that a given set of data will satisfy ordered from most to least likely.由此我希望能够训练 model 到 output 给定数据集将满足的所有数组排列,从最可能到最不可能排序。 Also how fast would a trained model be able to perform these calculations?另外,训练有素的 model 执行这些计算的速度有多快? Anyway I would greatly appreciate any insight into the required dependencies / structure needed to achieve my desired outcome.无论如何,我将非常感谢任何对实现我想要的结果所需的依赖项/结构的洞察。

Here are images of my libs folder in case you are curious.这是我的 libs 文件夹的图像,以防您好奇。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

(I am now shading it correctly so I don't have the libs folder) (我现在正确地对它进行着色,所以我没有 libs 文件夹)

However I still need to figure out how to use the pom.xml to not include all jars related to ios and android .但是我仍然需要弄清楚如何使用pom.xml不包括与ios和 android 相关的所有jars

Firstly just looking at your versions try to keep them up to date.首先,只查看您的版本,尽量让它们保持最新。 alpha2 is years old at this point. alpha2 在这一点上是岁。 Try to use M1.1 or M2.尝试使用 M1.1 或 M2。

Short answer: use -Djavacpp.platform=${YOUR_PLATFORM} that's just how things work for a java library that uses c++ code.简短回答:使用 -Djavacpp.platform=${YOUR_PLATFORM} 这就是使用 c++ 代码的 java 库的工作方式。

Long answer:长答案:

With the assumption you'll use a newer version I can give you some general advice: when building jars we build native dependencies for different operating systems.假设您将使用较新的版本,我可以给您一些一般性建议:在构建 jars 时,我们为不同的操作系统构建本机依赖项。 That means c/c++ code per OS.这意味着每个操作系统的 c/c++ 代码。 We do this for performance reasons.我们这样做是出于性能原因。 Most libraries with native code work like this.大多数具有本机代码的库都是这样工作的。

Java is not capable of fast math code that also runs on gpus. Java 不支持也在 GPU 上运行的快速数学代码。 It won't be for years to come.未来几年都不会。 Even most current efforts are not going to be fast enough for many ML workloads.对于许多 ML 工作负载,即使是当前的大多数努力也不够快。

Note that I know this is not normal in java but this is a common trade off for any java library with a native component.请注意,我知道这在 java 中是不正常的,但对于任何具有本机组件的 java 库来说,这是一个常见的权衡。 That includes something like.netty which is used in cassandra which you're also using there.这包括 cassandra 中使用的 .netty 之类的东西,您也在那里使用它。

The way we do this is via javacpp.我们这样做的方式是通过 javacpp。 Javacpp is what allows us to automatically generate bindings per platform to some underlying c++ code that powers the math routines we actually run when doing calculations. Javacpp 允许我们自动为每个平台生成绑定到一些底层 c++ 代码,这些代码为我们在计算时实际运行的数学例程提供支持。

When building an uber jar you can either just let the jar build with mvn clean package or you can specify -Djavacpp.platform= during the build.构建 uber jar 时,您可以只让 jar 使用 mvn clean package 构建,或者您可以在构建期间指定 -Djavacpp.platform=。

That will allow you to only include the dependencies you want.这将允许您只包含所需的依赖项。

Note that this has a trade off of only running on certain platforms if you do that.请注意,如果您这样做,这需要权衡仅在某些平台上运行。

You can't have a multi platform jar without also accepting the trade off of the larger size.如果不接受较大尺寸的权衡,就无法拥有多平台 jar。 This isn't a problem for most people.这对大多数人来说不是问题。

If you want to hand optimize a multi platform jar, you can also manually include the platforms you want by specifying the classifiers you want.如果你想手动优化一个多平台 jar,你也可以通过指定你想要的分类器来手动包含你想要的平台。 You should be ready to understand how to use maven classifiers if you're ready to do so.如果您准备好这样做,您应该准备好了解如何使用 maven 分类器。

Regarding performance: I'm not sure what you're expecting but it should be fast enough for whatever you're hoping for.关于性能:我不确定您的期望是什么,但它应该足够快以满足您的期望。 Performance is always going to be relative to the neural.net you build (neural.nets vary in size) and the size of the data you are dealing with.性能总是与您构建的 neural.net(neural.net 的大小不同)和您正在处理的数据的大小有关。

Use our project template for the rest: https://github.com/eclipse/deeplearning4j-examples/tree/master/mvn-project-template使用我们的项目模板 rest: https://github.com/eclipse/deeplearning4j-examples/tree/master/mvn-project-template

I'm not sure where you got your versions from or what tutorials you found but the project would also appreciate feedback if you can't find something.我不确定你从哪里得到你的版本或者你找到了什么教程,但如果你找不到任何东西,该项目也会感谢反馈。 Please feel free to file an issue at: https://github.com/eclipse/deeplearning4j/issues if you can't find something.如果您找不到任何东西,请随时在以下网址提出问题: https://github.com/eclipse/deeplearning4j/issues Thanks.谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM