2

I am trying to run CnnSentenceClassification from deeplearning4j example. I moved this file to my Gradle project. When I run the class from the eclipse it works fine. However when I run it from ./gradlew run I get following error:

Exception in thread "main" java.lang.ExceptionInInitializerError
at 
main.CnnSentenceClassification.main(CnnSentenceClassification.java:75)
Caused by: java.lang.RuntimeException: 
org.nd4j.linalg.factory.Nd4jBackend$NoAvailableBackendException: 
Please ensure that you have an nd4j backend on your classpath. Please 
see: http://nd4j.org/getstarted.html
at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:6089)
at org.nd4j.linalg.factory.Nd4j.<clinit>(Nd4j.java:201)
... 1 more
Caused by: 
org.nd4j.linalg.factory.Nd4jBackend$NoAvailableBackendException: 
Please ensure that you have an nd4j backend on your classpath. Please 
see: http://nd4j.org/getstarted.html
at org.nd4j.linalg.factory.Nd4jBackend.load(Nd4jBackend.java:258)
at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:6086)
... 2 more

I checked and nd4j-api-0.9.1.jar is in my classpath. This is my build.gradle:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'

repositories {
    jcenter()
}

mainClassName="main.CnnSentenceClassification"

dependencies {
    compile group: 'org.deeplearning4j', name: 'deeplearning4j-core', version: '0.9.1'
    compile group: 'org.deeplearning4j', name: 'deeplearning4j-nlp', version: '0.9.1'       

    testCompile group: 'org.nd4j', name: 'nd4j-native-platform', version: '0.9.1'
    compile group: 'org.nd4j', name: 'nd4j-api', version: '0.9.1'

    compile "org.slf4j:slf4j-simple:1.7.25"
    compile "org.slf4j:slf4j-api:1.7.25"
}

2 Answers2

1

I was having the same problem. You need an ND4J backend, which means updating your dependency tree.

For Maven builds, add the following dependency to your project's pom:

<dependency>
    <groupId>org.nd4j</groupId>
    <artifactId>nd4j-native</artifactId>
    <version>0.9.1</version>
</dependency>

For Gradle builds, just add the following line in your dependencies:

compile "org.nd4j:nd4j-native:0.9.1"

This native backend uses the CPU for computations. There is another dependency for a CUDA-enabled graphics card.

I found this link helpful: DL4J Performance Debugging

zmx
  • 1,051
  • 1
  • 10
  • 12
  • As he states in his question he is using Gradle so not hugely helpful providing a Maven link. – thomasters Jun 18 '20 at 08:53
  • try this: compile "org.nd4j:nd4j-native:0.9.1" – zmx Jun 18 '20 at 11:00
  • or just access the maven repository for that (you have both gradle and maven there, among other build systems): https://mvnrepository.com/artifact/org.nd4j/nd4j-native/0.9.1 – zmx Jun 18 '20 at 11:05
0

Edit: new link is here. https://deeplearning4j.konduit.ai/config/backends

Old content below: You have test scope on the nd4j backend. An nd4j backend is NEVER optional. https://nd4j.org/backend.html

The error is right in the message. We even give you a link with an explanation right in the stack trace.

Adam Gibson
  • 3,055
  • 1
  • 10
  • 12
  • Thank you so much for your answer. I know that ND4j is not optional. But the point is why it works inside eclipse? I have not install ND4j separately. – Farshad Bakhshandegan Moghadda Oct 26 '17 at 15:46
  • I told you already. It's because you have the nd4j dependency (NOT API nd4j-NATIVE caps for emphasis) You are misusing nd4j. An nd4j backend should not be a test dependency. – Adam Gibson Oct 27 '17 at 01:47
  • Thank you so much. It is fixed. – Farshad Bakhshandegan Moghadda Oct 27 '17 at 07:33
  • 2
    The link is broken. It would be great to explain instead of posting a link. Thanks – F___ThisToxicCommunityHere May 06 '19 at 02:19
  • 3
    It would be even greater to get a little information about this "backend" idea without having to study the entire concept of nd4j starting with "NDArrays: How Are They Stored in Memory?" I just want to use deeplearning4j to train some models, not to study nd4j for days. (And I don't get a backend, even after adding nd4j-native and so on) – F___ThisToxicCommunityHere May 06 '19 at 02:26
  • I'm not sure what else we can do for you. There's a stack trace that prints out exactly where to go when we don't find one. A direct link on what the problem is and how to solve it is about as easy as it gets. If you have any feedback we can take action on file an issue, happy to accommodate. – Adam Gibson May 11 '19 at 13:00
  • "wE evEn giVe you a LiNk riGhT iN tHe sTack tRace." the link literally 404s. – Krusty the Clown Jan 14 '23 at 01:55
  • Hi, this is a link from 3 years ago if you haven't noticed :) I updated the link. – Adam Gibson Jan 14 '23 at 21:31