I'm pretty new in Java. I would like to initiate an array using Nd4j library INDArray x = new Nd4j.linspace(0, 1, 100);
, doing something like x = np.linspace(0, 1, 100)
in Python.
And I ran into an error
java: cannot find symbol
symbol: class linspace location: class
org.nd4j.linalg.factory.Nd4j
Although my Nd4j was well installed adding the following in pom.xml, and my IDE IntelliJ can detect ND4j class.
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-api</artifactId>
<version>1.0.0-M2</version>
</dependency>
Am I missing or misunderstood anything? Thanks for your kind help
CodePudding user response:
The problem is:
INDArray x = new Nd4j.linspace(0, 1, 100);
Because you use new
, Java tries to create a class called Nd4j.linspace
, which does not exist (as indicated by the "cannot find symbol, symbol: class linspace"). The class Nd4j
does have a method linspace
. To fix this, remove new
so you call the method:
INDArray x = Nd4j.linspace(0, 1, 100);
CodePudding user response:
Edit: Apologies. Thanks to the commenter down below. That error upon first glance looked like the standard error you get when the IDE only shows part of the error. That's very common when you are missing a backend.
In the context of a compilation error ensure you still have the backend. You don't need to depend on nd4j-api directly. It will be pulled in as a transitive dependency and wont' work without a backend anyways.
For the IDE, just make sure after declaring the correct dependencies (please see below answer for that) then refresh your maven context to ensure that the IDE has the correct information. Also ensure that you give the IDE time to download the correct dependencies.
Note also with the IDE it may still be out of sync so you may also test it independently on command line. When you do this you can just run:
mvn clean install -DskipTests
If your project compiles, then your IDE is the problem. That may mean you need to restart it or invalidate your IDE's cache.
On your correct dependencies:
You need more than just api. You need to pick a backend. A backend allows you to either use cpu or gpu depending on your use case.
Note that nd4j-api is just the layer for which you access lower level functionality provided by a specific implementation(eg: a backend)
Most of the time this is going to be nd4j-native-platform. This will allow you to use a cpu backend.
In concrete terms this will be:
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native-platform</artifactId>
<version>1.0.0-M2</version>
</dependency>
Note that when you specify nd4j-native-platform it will provide everything you need but will require a bit of time to download.
This artifact pre declares a set of dependencies for every operating system that nd4j supports. When running maven, you may prevent this by adding a JVM property:
mvn -Djavacpp.platform="$YOUR_PLATFORM" clean package
You should be able to also pre specify this in your pom's properties like:
<properties>
<javacpp.platform>$YOUR_PLATFORM</javacpp.platform>
</properties>
$YOUR_PLATFORM can usually be one of: linux-x86_64, windows-x86_64, macosx-x86_64 These are maven classifiers. You can see the comprehensive list for a particular version here: https://repo1.maven.org/maven2/org/nd4j/nd4j-native/1.0.0-M2/
Note that 1.0.0-M2 is the current version but may change so always specify the newest version if you want a version for a specific platform.