gpt4 book ai didi

java - 使用 RecordReader 从文件读取 CSV 数据时出错

转载 作者:行者123 更新时间:2023-12-01 17:48:16 25 4
gpt4 key购买 nike

我想使用 RecodrReaderDataSetIterator 加载训练数据集表单文件,但出现错误 java.lang.ExceptionInInitializerError尝试了一下,但它没有告诉我任何信息。

这是主要逻辑以及发生错误的位置:

public void trainNN() throws IOException, InterruptedException {

String setName = "trainingDataCSV";

createDataSet(TRAINING_SET_SIZE, setName);

DataSet allData;
RecordReader recordReader = new CSVRecordReader(0, ',');
recordReader.initialize(new FileSplit(new File(setName))); // error here

int numPixels = IMAGE_SIZE * IMAGE_SIZE;
DataSetIterator iterator = new RecordReaderDataSetIterator(recordReader, TRAINING_SET_SIZE, numPixels, numPixels + 4);
allData = iterator.next();


DataNormalization normalizer = new NormalizerStandardize();
normalizer.fit(allData);
normalizer.transform(allData);

SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.65);
DataSet trainingData = testAndTrain.getTrain();
DataSet testData = testAndTrain.getTest();


MultiLayerConfiguration configuration = new NeuralNetConfiguration.Builder()
.activation(Activation.TANH)
.weightInit(WeightInit.XAVIER)
// .learningRate(0.1)
.l2(0.0001)
.list()
.layer(0, new DenseLayer.Builder().nIn(numPixels).nOut(50).build())
.layer(1, new DenseLayer.Builder().nIn(50).nOut(50).build())
.layer(2, new OutputLayer.Builder(
LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.activation(Activation.SOFTMAX)
.nIn(50).nOut(4).build())
.build();

MultiLayerNetwork neuralNetwork = new MultiLayerNetwork(configuration);

INDArray output = neuralNetwork.output(testData.getFeatures());
Evaluation eval = new Evaluation(3);
eval.eval(testData.getLabels(), output);

System.out.println(eval.stats());
}

完整的错误堆栈跟踪

java.lang.ExceptionInInitializerError
at org.datavec.api.util.ndarray.RecordConverter.toMinibatchArray(RecordConverter.java:198)
at org.deeplearning4j.datasets.datavec.RecordReaderMultiDataSetIterator.next(RecordReaderMultiDataSetIterator.java:160)
at org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator.next(RecordReaderDataSetIterator.java:377)
at org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator.next(RecordReaderDataSetIterator.java:452)
at org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator.next(RecordReaderDataSetIterator.java:85)
at App.trainNN(App.java:116)
at App.setup(App.java:59)
at processing.core.PApplet.handleDraw(PApplet.java:2412)
at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1557)
at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)
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:5131)
at org.nd4j.linalg.factory.Nd4j.<clinit>(Nd4j.java:226)
... 10 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:218)
at org.nd4j.linalg.factory.Nd4j.initContext(Nd4j.java:5128)
... 11 more

以下是创建数据文件的方法,以防万一。
它包含 400 个值,其中包含以分号分隔的 1.0 或 0.0,以及图像上形状的位置和尺寸的 4 个附加值

    public void createDataSet(int amount, String name) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(name));
writer.write("");


for (int i = 0; i < amount; i++) {
Image image = new Image(IMAGE_SIZE, MIN_OBJECT_SIZE, MAX_OBJECT_SIZE, OBJECTS_AMOUNT);
int positionX = image.getPosition()[0];
int positionY = image.getPosition()[1];
int w = image.getW();
int h = image.getH();

for (int pixel = 0; pixel < image.getPixelValues().length; pixel++) {
writer.append(image.getPixelValues()[pixel] + ",");
}

writer.append(positionX + ",");
writer.append(positionY + ",");
writer.append(w + ",");
writer.append(String.valueOf(h) + '\n');

}

writer.close();

}

最佳答案

异常的重要部分是:

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

并且您可能会在初始化 ND4J 时遇到相同的错误(例如 Nd4j.zeros(1))。

确保您的依赖项中有正确的 ND4J 后端,如文档中所示:https://deeplearning4j.konduit.ai/config/maven#add-a-backend

关于java - 使用 RecordReader 从文件读取 CSV 数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60829917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com