1

“我是神经网络和 DL4j 的新手,我想用 CSV 训练神经网络并构建线性回归。如何解决这些错误“无法解析方法'.iterations 和 getFeatureMatrix()'”?


“以前我试图这样做,但在‘种子’中有另一个错误”。

import org.datavec.api.records.reader.RecordReader;
import org.datavec.api.records.reader.impl.csv.CSVRecordReader;
import org.datavec.api.split.FileSplit;
import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator;
import org.deeplearning4j.nn.api.OptimizationAlgorithm;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.Updater;
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.optimize.listeners.ScoreIterationListener;
import org.nd4j.evaluation.classification.Evaluation;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.api.DataSet;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.lossfunctions.LossFunctions;
import java.io.File;






public class Data {
    public static void main(String[] args) throws Exception {

参数:

        int seed = 3000;
        int batchSize = 200;
        double learningRate = 0.001;
        int nEpochs = 150;
        int numInputs = 2;
        int numOutputs = 2;
        int numHiddenNodes = 100;

加载数据:

        //load data train
        RecordReader rr = new CSVRecordReader();
        rr.initialize(new FileSplit(new File("train.csv")));
        DataSetIterator trainIter = new RecordReaderDataSetIterator(rr, batchSize, 0, 2);

        //load test data

        RecordReader rrTest = new CSVRecordReader();
        rr.initialize(new FileSplit(new File("test.csv")));


        DataSetIterator testIter = new RecordReaderDataSetIterator(rrTest, batchSize, 0, 2);

网络配置:

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                .seed(seed)
                .iterations(1000)
                .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                .learningRate(learningRate)
                .updater(Updater.NESTEROVS).momentum(0.9)
                .list()
                .layer(0, new DenseLayer.Builder()
                        .nIn(numInputs)
                        .nOut(numHiddenNodes)
                        .weightInit(WeightInit.XAVIER)
                        .activation(Activation.fromString("relu"))
                        .build())
                .layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
                        .weightInit(WeightInit.XAVIER)
                        .activation(Activation.fromString("softmax"))
                        .weightInit(WeightInit.XAVIER)
                        .nIn(numHiddenNodes)
                        .nOut(numOutputs)
                        .build()
                )
                .pretrain(false).backprop(true).build();

        MultiLayerNetwork model = new MultiLayerNetwork(conf);
        model.init();
        model.setListeners(new ScoreIterationListener((15)));
        for (int n = 0; n < nEpochs; n++) {
            model.fit((trainIter));
            System.out.println(("--------------eval model"));
            Evaluation eval = new Evaluation(numOutputs);
            while (testIter.hasNext()) {
                DataSet t = testIter.next();
                INDArray features = getFeatureMatrix();
                INDArray lables = t.getLabels();
                INDArray predicted = model.output(features, false);
                eval.eval(lables, predicted);
            }
            System.out.println(eval.stats());
        }
    }
}

日志

建造

4

1 回答 1

1

首先你应该考虑使用更多的类(比如一个用于神经网络的定义,一个用于训练过程等,......)。只是一个最佳实践的东西。

我不知道您使用的是哪个版本的 DL4J,但我们可以注意到getFeatureMatrix() 已被删除。另一件事是,应该在 DataSet 对象上调用此函数,而不是像您似乎所做的那样“静态地”调用此函数。(你应该这样做t.getFeatureMatrix())。

神经网络创建的迭代()函数几乎相同;自某些 DL4J 版本以来,此功能已被删除。您可以在此线程上获得有关此功能的更多信息。现在你必须找到一个替代方法来设置迭代次数,你可以看看这个线程。希望它能回答你的问题!

于 2019-08-12T09:34:47.760 回答