I'm trying to run this very simple Deeplearning4j example :
package application.test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration.Builder;
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.util.ModelSerializer;
import org.nd4j.jita.conf.Configuration;
import org.nd4j.jita.conf.Configuration.ExecutionModel;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.api.buffer.DataType;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.dataset.api.iterator.BaseDatasetIterator;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.dataset.api.iterator.fetcher.DataSetFetcher;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.learning.config.Adam;
import org.nd4j.linalg.lossfunctions.LossFunctions.LossFunction;
import org.nd4j.linalg.factory.RandomFactory;
public class Main {
public static void main(String[] args) {
int numInputs = 3;
int numOutputs = 2;
int numHiddenNodes = 4;
String modelFilePath = "C:\\Users\\Léon\\Desktop\\model.zip";
MultiLayerNetwork model = loadOrCreateModel(modelFilePath, numInputs, numOutputs, numHiddenNodes);
// Supposons que vous ayez vos données d'entrée et de sortie ici
double[][] inputData = {{0.1, 0.2, 0.3}, {0.4, 0.5, 0.6}}; // Exemple de données
double[][] outputData = {{1, 0}, {0, 1}}; // Étiquettes pour la classification
int batchSize = 1; // Taille du lot pour l'entraînement
int numEpochs = 10; // Nombre d'époques pour l'entraînement
DataSetIterator trainingData = prepareTrainingData(inputData, outputData, batchSize);
// Entraîner le modèle seulement s'il n'a pas été chargé
File file = new File(modelFilePath);
if (!file.exists()) {
trainNetwork(model, trainingData, numEpochs);
saveModel(model, modelFilePath);
}
// Ici, vous pouvez utiliser le modèle pour faire des prédictions ou des évaluations
}
private static MultiLayerNetwork loadOrCreateModel(String modelFilePath, int numInputs, int numOutputs, int numHiddenNodes) {
File file = new File(modelFilePath);
if (file.exists()) {
try {
return ModelSerializer.restoreMultiLayerNetwork(file);
} catch (IOException e) {
e.printStackTrace();
}
}
return createModel(numInputs, numOutputs, numHiddenNodes);
}
private static MultiLayerNetwork createModel(int numInputs, int numOutputs, int numHiddenNodes) {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.updater(new Adam())
.weightInit(WeightInit.XAVIER)
.list().layer(0, new DenseLayer.Builder().nIn(numInputs).nOut(numHiddenNodes).activation(Activation.RELU).build())
.layer(1, new OutputLayer.Builder(LossFunction.NEGATIVELOGLIKELIHOOD).activation(Activation.SOFTMAX).build())
.build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
return model;
}
private static DataSetIterator prepareTrainingData(double[][] inputData, double[][] outputData, int batchSize) {
INDArray inputNDArray = Nd4j.create(inputData);
INDArray outputNDArray = Nd4j.create(outputData);
DataSet dataSet = new DataSet(inputNDArray, outputNDArray);
List<DataSet> listDataSet = new ArrayList<>();
listDataSet.add(dataSet);
return new BaseDatasetIterator(batchSize, listDataSet.size(), (DataSetFetcher) listDataSet.iterator());
}
private static void trainNetwork(MultiLayerNetwork model, DataSetIterator trainingData, int numEpochs) {
for (int i = 0; i < numEpochs; i++) {
model.fit(trainingData);
}
}
private static void saveModel(MultiLayerNetwork model, String filePath) {
File file = new File(filePath);
try {
ModelSerializer.writeModel(model, file, true);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The comments are in French, but that doesn't matter. When I try to run this program, I get this nice error:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "org.nd4j.linalg.factory.RandomFactory.getRandom()" because "org.nd4j.linalg.factory.Nd4j.randomFactory" is null
I don't know where it's coming from, I first thought it was a missing nd4j dependency but I haven't been able to find one that solves the problem. I've also tried reducing the version of nd4j (I'm currently using M2.1) without success. Does anyone know how to solve this problem?
My dependencies :
I don't know where it's coming from, I first thought it was a missing nd4j dependency but I haven't been able to find one that solves the problem. I've also tried reducing the version of nd4j (I'm currently using M2.1) without success. Does anyone know how to solve this problem?
For your particular issue, I reverted to an earlier version of nd4j-api, in my case I reverted to M1.1, and the NPE is no longer there. However, I was just getting a ton of other issues with classes not being found. I used my gradle build to just use the M1.1 version of everything and I am no longer getting any errors.