B4J Question Neural network error

claudiob4

Member
Licensed User
Longtime User
Hi all
I'm testing a neural network in Java. Unfortunately the compilation gives me a class not found error and I can't solve the problem. Can anyone help me?


Error:

Waiting for debugger to connect...
Program started.
Error occurred on line: 43 (Main)
java.lang.ClassNotFoundException: org.deeplearning4j$nn$conf$NeuralNetConfiguration$Builder
at anywheresoftware.b4j.object.JavaObject.getCorrectClassName(JavaObject.java:289)
at anywheresoftware.b4j.object.JavaObject.InitializeNewInstance(JavaObject.java:84)
at b4j.example.main._initializeneuralnetconfiguration(main.java:122)
at b4j.example.main._appstart(main.java:79)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
at b4j.example.main.start(main.java:38)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)



trial model:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

#AdditionalJar: deeplearning4j-core-1.0.0-M2.1.jar
#AdditionalJar: nd4j-native-platform-1.0.0-M2.1.jar
#AdditionalJar: slf4j-api-2.1.0-alpha1.jar
#AdditionalJar: slf4j-simple-2.1.0-alpha1.jar

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.Show

    ' Costruzione della configurazione della rete neurale
    Dim nnConf As JavaObject
    nnConf = InitializeNeuralNetConfiguration
    If nnConf.IsInitialized Then
        Log("Neural network configuration initialized successfully")
    Else
        Log("Failed to initialize neural network configuration")
    End If

    ' Creazione del modello
    Dim model As JavaObject
    model.InitializeNewInstance("org.deeplearning4j.nn.multilayer.MultiLayerNetwork", Array(nnConf))
    model.RunMethod("init", Null)

    ' Stampa del sommario del modello
    Log(model.RunMethod("summary", Null))

    ' Esempio di addestramento con dati fittizi
    TrainModel(model)
End Sub

Sub InitializeNeuralNetConfiguration As JavaObject
    Dim nnConf As JavaObject
    nnConf.InitializeNewInstance("org.deeplearning4j.nn.conf.NeuralNetConfiguration$Builder", Null)
    nnConf = nnConf.RunMethod("seed", Array(12345)) ' Seed per la randomizzazione dei pesi
    nnConf = nnConf.RunMethod("updater", Array(CreateUpdater("adam"))) ' Ottimizzatore Adam
    nnConf = nnConf.RunMethod("list", Null)
    nnConf = nnConf.RunMethod("layer", Array(0, CreateDenseLayer(8, 26, "relu"))) ' Primo layer
    nnConf = nnConf.RunMethod("layer", Array(1, CreateDenseLayer(26, 26, "relu"))) ' Secondo layer
    nnConf = nnConf.RunMethod("layer", Array(2, CreateDenseLayer(26, 26, "relu"))) ' Terzo layer
    nnConf = nnConf.RunMethod("layer", Array(3, CreateDenseLayer(26, 8, "relu"))) ' Quarto layer
    nnConf = nnConf.RunMethod("layer", Array(4, CreateOutputLayer(8, 8, "softmax"))) ' Output layer
    nnConf = nnConf.RunMethod("build", Null)
    Return nnConf
End Sub

Sub CreateDenseLayer(numInput As Int, numOutput As Int, activation As String) As JavaObject
    Dim denseLayerBuilder As JavaObject
    denseLayerBuilder.InitializeNewInstance("org.deeplearning4j.nn.conf.layers.DenseLayer$Builder", Null)
    denseLayerBuilder = denseLayerBuilder.RunMethod("nIn", Array(numInput))
    denseLayerBuilder = denseLayerBuilder.RunMethod("nOut", Array(numOutput))
    denseLayerBuilder = denseLayerBuilder.RunMethod("activation", Array(activation))
    denseLayerBuilder = denseLayerBuilder.RunMethod("weightInit", Array("xavier"))
    Return denseLayerBuilder.RunMethod("build", Null)
End Sub

Sub CreateOutputLayer(numInput As Int, numOutput As Int, activation As String) As JavaObject
    Dim outputLayerBuilder As JavaObject
    outputLayerBuilder.InitializeNewInstance("org.deeplearning4j.nn.conf.layers.OutputLayer$Builder", Null)
    outputLayerBuilder = outputLayerBuilder.RunMethod("nIn", Array(numInput))
    outputLayerBuilder = outputLayerBuilder.RunMethod("nOut", Array(numOutput))
    outputLayerBuilder = outputLayerBuilder.RunMethod("activation", Array(activation))
    outputLayerBuilder = outputLayerBuilder.RunMethod("weightInit", Array("xavier"))
    outputLayerBuilder = outputLayerBuilder.RunMethod("lossFunction", Array("negativeloglikelihood"))
    Return outputLayerBuilder.RunMethod("build", Null)
End Sub

Sub CreateUpdater(updaterType As String) As JavaObject
    Dim updater As JavaObject
    updater.InitializeStatic("org.nd4j.linalg.learning.config." & updaterType.SubString2(0,1).ToUpperCase & updaterType.SubString(1).ToLowerCase)
    Return updater
End Sub

Sub TrainModel(model As JavaObject)
    ' Esempio di dati fittizi per l'addestramento
    Dim features(,) As Double = Array(Array(1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), Array(0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0))
    Dim labels(,) As Double = Array(Array(1.0, 0.0), Array(0.0, 1.0))
    
    ' Creazione delle matrici di input e output
    Dim Nd4j As JavaObject
    Nd4j.InitializeStatic("org.nd4j.linalg.factory.Nd4j")
    Dim input As JavaObject = Nd4j.RunMethod("create", Array(features))
    Dim output As JavaObject = Nd4j.RunMethod("create", Array(labels))
    
    ' Addestramento del modello
    For i = 1 To 1000
        model.RunMethod("fit", Array(input, output))
    Next
    
    ' Predizione con il modello addestrato
    Dim prediction As JavaObject = model.RunMethod("output", Array(input))
    Log("Predictions: " & prediction)
End Sub

Sub MainForm_CloseRequest (EventData As Event)
    MainForm.Close
End Sub
 

Daestrum

Expert
Licensed User
Longtime User
think you are missing some additional jars

I can get past your class not found - but hit a problem later

these are the jars I am using in #AdditionalJar

B4X:
    #AdditionalJar: deeplearning4j-core-1.0.0-M2.1.jar
    #AdditionalJar: nd4j-native-platform-1.0.0-M2.jar
    #AdditionalJar: deeplearning4j-nn-1.0.0-M2.1.jar
    #AdditionalJar: nd4j-api-1.0.0-M2.1.jar
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
The list got a bit bgger so far - just chasing more and more errors
B4X:
    #AdditionalJar: deeplearning4j-core-1.0.0-M2.1.jar
    #AdditionalJar: deeplearning4j-nn-1.0.0-M2.1.jar
    #AdditionalJar: nd4j-api-1.0.0-M2.1.jar
    #additionaljar: javacpp-1.5.10.jar
    #AdditionalJar: slf4j-simple-2.0.13.jar
    #AdditionalJar: slf4j-api-2.0.13.jar
    #AdditionalJar: nd4j-native-platform-1.0.0-M2.1.jar
    #AdditionalJar: nd4j-common-1.0.0-M2.1.jar

now getting "Caused by: java.lang.RuntimeException: org.nd4j.linalg.factory.Nd4jBackend$NoAvailableBackendException: Please ensure that you have an nd4j backend on your classpath.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
This is my current list
B4X:
    #AdditionalJar: deeplearning4j-core-1.0.0-M2.1.jar
    #AdditionalJar: deeplearning4j-nn-1.0.0-M2.1.jar
    #AdditionalJar: nd4j-api-1.0.0-M2.1.jar
    #additionaljar: javacpp-1.5.10.jar
    #AdditionalJar: slf4j-simple-2.0.13.jar
    #AdditionalJar: slf4j-api-2.0.13.jar
    #AdditionalJar: nd4j-native-platform-1.0.0-M2.1.jar
    #AdditionalJar: nd4j-common-1.0.0-M2.1.jar
    #AdditionalJar: nd4j-x86-0.4-rc3.8.jar
    #AdditionalJar: nd4j-native-api-1.0.0-M2.1.jar
    #AdditionalJar: jblas-1.2.5.jar
    #AdditionalJar: deeplearning4j-datavec-iterators-1.0.0-M2.1.jar
    #AdditionalJar: commons-io-2.16.1.jar
    #AdditionalJar: openblas-platform-0.3.26-1.5.10.jar

The error now is it cant find a backend
 
Upvote 0

claudiob4

Member
Licensed User
Longtime User
The error is now the following:

Waiting for debugger to connect...
Program started.
Error occurred on line: 59 (Main)
java.lang.RuntimeException: Method: seed not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:130)
at b4j.example.main._initializeneuralnetconfiguration(main.java:125)
at b4j.example.main._appstart(main.java:79)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
at b4j.example.main.start(main.java:38)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
Seed has to be a long - I just defined it as
B4X:
Dim s As Long = 12345
...
    nnConf.RunMethod("seed", Array(s))
...
 
Upvote 0

claudiob4

Member
Licensed User
Longtime User
Thanks for your interest. I made the change and now the error is:


Waiting for debugger to connect...
Program started.
[JavaFX Application Thread] WARN org.nd4j.linalg.factory.Nd4jBackend - failed to process available backends
java.util.ServiceConfigurationError: org.nd4j.linalg.factory.Nd4jBackend: org.nd4j.linalg.cpu.CpuBackend Unable to get public no-arg constructor
at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:581)
at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:672)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1232)
at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1264)
at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1299)
at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1384)
at org.nd4j.linalg.factory.Nd4jBackend.load(Nd4jBackend.java:156)
at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:5062)
at org.nd4j.linalg.factory.Nd4j.<clinit>(Nd4j.java:284)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration$Builder.seed(NeuralNetConfiguration.java:655)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4j.example.main._initializeneuralnetconfiguration(main.java:129)
at b4j.example.main._appstart(main.java:79)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
at b4j.example.main.start(main.java:38)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NoClassDefFoundError: org/nd4j/linalg/io/Resource
at java.base/java.lang.Class.getDeclaredConstructors0(Native Method)
at java.base/java.lang.Class.privateGetDeclaredConstructors(Class.java:3138)
at java.base/java.lang.Class.getConstructor0(Class.java:3343)
at java.base/java.lang.Class.getConstructor(Class.java:2152)
at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:659)
at java.base/java.util.ServiceLoader$1.run(ServiceLoader.java:656)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.util.ServiceLoader.getConstructor(ServiceLoader.java:667)
... 39 more
Caused by: java.lang.ClassNotFoundException: org.nd4j.linalg.io.Resource
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 47 more
Error occurred on line: 60 (Main)
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:132)
at b4j.example.main._initializeneuralnetconfiguration(main.java:129)
at b4j.example.main._appstart(main.java:79)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:237)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:109)
at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:98)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:96)
at b4j.example.main.start(main.java:38)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.NullPointerException
at org.nd4j.linalg.factory.Nd4j.getRandom(Nd4j.java:507)
at org.deeplearning4j.nn.conf.NeuralNetConfiguration$Builder.seed(NeuralNetConfiguration.java:655)
... 31 more
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
That's exactly where I am too.

I also transferred it into a non-ui app, just to see if it helped.

I think it may be worthwhile trying the java code in a #If Java block to see if it works , then when it's working, translate to B4X step by step.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Unable to get public no-arg constructor
The Constructor must be called with Parameters. There is no public no-arg constructor.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
If you comment out the seed line - you get a bit further.

in InitializeNeuralNetConfiguration
1, "relu" should be "RELU"
2, "softmax" should be "SOFTMAX"

Check anywhere you have lowercase parameter as if the are enums they should probably be uppercase.
 
Upvote 0
Top