在神经网络中将训练数据转换为数组

时间:2017-02-01 20:32:00

标签: neural-network deeplearning4j

我正在尝试使用deeplearning4j中的时间预测模型来进行文本处理,它不会将单词,句子,字符串作为输入要素并产生时间作为输出。但是在将输入数据建模到输出时,我很难转换这些值以及如何告诉网络这些输入值这些是相应的输出值。

我还应该减少x1和y的维度,而不是x1-x4吗?

training-data.csv具有以下具有100个值的列。 x1,x2,x3,x4(输入)y(输出)

我尝试使用可以捕获变量输入的SequenceRecorder和Iterator。 下面是我的代码

public static void main(String[] args) throws Exception
{
    // Initlizing parametres
    final Logger log = LoggerFactory.getLogger(MainExpert.class);
    final int seed =123;
    final int numInput = 4;
    final int numOutput = 1;
    final int numHidden = 20;
    final double learningRate = 0.015;
    final int batchSize =30;
    final int nEpochs =30;
    //final int inputFeatures =4;

    //Constructing Training data

    final File baseFolder =new File("/home/aj/my/samples/corpus");
    final File testFolder = new File("/home/aj/my/samples/corpus/train_data_0.csv");
    SequenceRecordReader trainReader = new CSVSequenceRecordReader(0,",");
    trainReader.initialize(new NumberedFileInputSplit(baseFolder.getAbsolutePath() + "/train_data_%d.csv",0,0));
    DataSetIterator trainIterator = new SequenceRecordReaderDataSetIterator(trainReader,batchSize,-1,4,true);


    SequenceRecordReader testReader = new CSVSequenceRecordReader(0,",");
    testReader.initialize(new NumberedFileInputSplit(baseFolder.getAbsolutePath() + "/test_data_%d.csv",0,0));
    DataSetIterator testIterator = new SequenceRecordReaderDataSetIterator(testReader,batchSize,-1,4,true);

    DataSet trainData = trainIterator.next();
    System.out.println(trainData);
    DataSet testData = testIterator.next();

    NormalizerMinMaxScaler normalizer = new NormalizerMinMaxScaler(0, 1);
    normalizer.fitLabel(true);
    normalizer.fit(trainData);              
    normalizer.transform(trainData);
    normalizer.transform(testData);

    //Configuring Network
    log.info("Building Model");
    MultiLayerConfiguration config = new NeuralNetConfiguration.Builder()
            .seed(seed)
            .iterations(1)
            .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
            .learningRate(learningRate)
            .updater(Updater.NESTEROVS).momentum(0.9)
            .list()
            .layer(0, new DenseLayer.Builder()
                    .nIn(numInput)
                    .nOut(numHidden)
                    .weightInit(WeightInit.XAVIER).
                    activation(Activation.RELU)
                    .build())
            .layer(1, new DenseLayer.Builder()
                    .nIn(numHidden)
                    .nOut(numHidden)
                    .weightInit(WeightInit.XAVIER)
                    .activation(Activation.RELU)
                    .build())
            .layer(2, new OutputLayer.Builder(LossFunction.MSE)
                    .nIn(numHidden)
                    .nOut(numOutput)
                    .weightInit(WeightInit.XAVIER)
                    .activation(Activation.IDENTITY)
                    .build())
            .pretrain(false).backprop(true).build();

    //Initializing network
    log.info("initlizing model");
    MultiLayerNetwork model = new MultiLayerNetwork(config);
    model.init();
    model.setListeners(new ScoreIterationListener(1));

    log.info("Training Model");
    for(int i=0;i<nEpochs;i++)
    {
        model.fit(trainData);
    }
    //Evaluation
    RegressionEvaluation reval=new RegressionEvaluation(1);

        while(testIterator.hasNext())
        {
    INDArray feat =testData.getFeatureMatrix();
    INDArray labels =testData.getLabels();
    INDArray prediction =model.output(feat);
    reval.eval(labels, prediction);
}
    System.out.println(reval.stats());
}

}

我的数据有四个输入值和一个输出值。 但我得到一个例外  org.deeplearning4j.exception.DL4JInvalidInputException: Input that is not a matrix; expected matrix (rank 2), got rank 3 array with shape [1, 4, 107]

1 个答案:

答案 0 :(得分:0)

我们在这里有一个端到端的csv分类器示例:https://github.com/deeplearning4j/dl4j-examples/blob/master/dl4j-examples/src/main/java/org/deeplearning4j/examples/recurrent/seqClassification/UCISequenceClassificationExample.java  一个rnn可以处理多变量输入。事实上,我鼓励它。只有一个输入功能对你没什么用。 我认为没有必要将其降低到x1和y。