当前位置: 首页 > 知识库问答 >
问题:

Deeplearning4J-如何使用保存的模型?

雷锋
2023-03-14

我将Deeplearning4j(Ver.1.0.0-M1.1)用于构建神经网络。

我以Deeplearning4j中的IrisClassifier为例。

//First: get the dataset using the record reader. CSVRecordReader handles loading/parsing
int numLinesToSkip = 0;
char delimiter = ',';
RecordReader recordReader = new CSVRecordReader(numLinesToSkip,delimiter);
recordReader.initialize(new FileSplit(new File(DownloaderUtility.IRISDATA.Download(),"iris.txt")));

//Second: the RecordReaderDataSetIterator handles conversion to DataSet objects, ready for use in neural network
int labelIndex = 4;     //5 values in each row of the iris.txt CSV: 4 input features followed by an integer label (class) index. Labels are the 5th value (index 4) in each row
int numClasses = 3;     //3 classes (types of iris flowers) in the iris data set. Classes have integer values 0, 1 or 2
int batchSize = 150;    //Iris data set: 150 examples total. We are loading all of them into one DataSet (not recommended for large data sets)

DataSetIterator iterator = new RecordReaderDataSetIterator(recordReader,batchSize,labelIndex,numClasses);
DataSet allData = iterator.next();
allData.shuffle();
SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.65);  //Use 65% of data for training

DataSet trainingData = testAndTrain.getTrain();
DataSet testData = testAndTrain.getTest();

//We need to normalize our data. We'll use NormalizeStandardize (which gives us mean 0, unit variance):
DataNormalization normalizer = new NormalizerStandardize();
normalizer.fit(trainingData);           //Collect the statistics (mean/stdev) from the training data. This does not modify the input data
normalizer.transform(trainingData);     //Apply normalization to the training data
normalizer.transform(testData);         //Apply normalization to the test data. This is using statistics calculated from the *training* set

final int numInputs = 4;
int outputNum = 3;
long seed = 6;

log.info("Build model....");
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
    .seed(seed)
    .activation(Activation.TANH)
    .weightInit(WeightInit.XAVIER)
    .updater(new Sgd(0.1))
    .l2(1e-4)
    .list()
    .layer(new DenseLayer.Builder().nIn(numInputs).nOut(3)
        .build())
    .layer(new DenseLayer.Builder().nIn(3).nOut(3)
        .build())
    .layer( new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
        .activation(Activation.SOFTMAX) //Override the global TANH activation with softmax for this layer
        .nIn(3).nOut(outputNum).build())
    .build();

//run the model
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
//record score once every 100 iterations
model.setListeners(new ScoreIterationListener(100));

for(int i=0; i<1000; i++ ) {
    model.fit(trainingData);
}

//evaluate the model on the test set
Evaluation eval = new Evaluation(3);
INDArray output = model.output(testData.getFeatures());

eval.eval(testData.getLabels(), output);
log.info(eval.stats());
5.1,3.5,1.4,0.2,0
...
7.0,3.2,4.7,1.4,1
...
6.3,3.3,6.0,2.5,2
// Save the Model
File locationToSave = new File("C:/Projects/deeplearning4j/trained_iris_model.zip");
ModelSerializer.writeModel(model, locationToSave, false);

// Open the model
File locationToLoad = new File("C:/Projects/deeplearning4j/trained_iris_model.zip");
MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(locationToLoad);
5.1,3.5,1.4,0.2
...
7.0,3.2,4.7,1.4
...
6.3,3.3,6.0,2.5

int numLinesToSkip = 0;
char delimiter = ',';
CSVRecordReader recordReader = new CSVRecordReader(numLinesToSkip, delimiter);  //skip no lines at the top - i.e. no header
recordReader.initialize(new FileSplit(new File("C:/Projects/deeplearning4j/iris-to-predict.txt")));

我怎么能得到预测?

萨克斯!

共有1个答案

颜楚青
2023-03-14

所以,添加这段代码解决了我的问题:

int batchSize = 150;
DataSetIterator iterator = new RecordReaderDataSetIterator(recordReader, batchSize);
DataSet allData = iterator.next();

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

INDArray output = model.output(allData.getFeatures());

// Output
System.out.println(output);
 类似资料:
  • 我使用deeplearning4j对我的数据的第一部分训练了一个模型并保存了它。 现在,我想在下一部分数据上训练这个保存的模型。

  • 基本问题是试图使用自定义数据模型来创建用于deeplearning4j网络的DataSetIterator。 我试图使用的数据模型是一个java类,它包含一系列双打,这些双打是从特定股票的报价中创建的,例如时间戳、打开、关闭、高、低、卷、技术指标1、技术指标2等。我查询了一个internet源示例(也是来自同一站点的其他几个指标),它提供了json字符串,我将这些字符串转换为我的数据模型,以便于访

  • 我有训练DNN网络的代码。我不想每次都训练这个网络,因为它占用了太多的时间。如何保存模型? 运行此函数后,我得到一个,我想保存它。

  • 历元3...验证精度=0.933 历元4...验证精度=0.958 历元5...验证精度=0.965

  • 问题内容: 我正在尝试对代码进行编程,以便如果用户按下“夜间按钮”,则背景将变为黑色,并且如果用户关闭应用程序,则背景将保持黑色。(白天模式也是如此。) 请注意:我已经对按钮进行了编码,当他们按下按钮时,所有场景都会切换到该模式。 这是我的代码,需要保存背景色:(两个if语句都需要它) 我的日夜颜色: 问题答案: Swift 4.2或更高版本 请注意,这只会将RGBA CGFloat值另存为属性列

  • 在TensorFlow中训练模型后: 如何保存已训练的模型? 以后如何还原此保存的模型?