本文整理汇总了Java中org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize类的典型用法代码示例。如果您正苦于以下问题:Java NormalizerStandardize类的具体用法?Java NormalizerStandardize怎么用?Java NormalizerStandardize使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
NormalizerStandardize类属于org.nd4j.linalg.dataset.api.preprocessor包,在下文中一共展示了NormalizerStandardize类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: createDataSource
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
private void createDataSource() throws IOException, InterruptedException {
//First: get the dataset using the record reader. CSVRecordReader handles loading/parsing
int numLinesToSkip = 0;
String delimiter = ",";
RecordReader recordReader = new CSVRecordReader(numLinesToSkip, delimiter);
recordReader.initialize(new InputStreamInputSplit(dataFile));
//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
DataSetIterator iterator = new RecordReaderDataSetIterator(recordReader, batchSize, labelIndex, numClasses);
DataSet allData = iterator.next();
allData.shuffle();
SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.80); //Use 80% of data for training
trainingData = testAndTrain.getTrain();
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
}
开发者ID:mccorby,项目名称:FederatedAndroidTrainer,代码行数:27,代码来源:IrisFileDataSource.java
示例2: createDataSource
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
private void createDataSource() throws IOException, InterruptedException {
//First: get the dataset using the record reader. CSVRecordReader handles loading/parsing
int numLinesToSkip = 0;
String delimiter = ",";
RecordReader recordReader = new CSVRecordReader(numLinesToSkip, delimiter);
recordReader.initialize(new InputStreamInputSplit(dataFile));
//Second: the RecordReaderDataSetIterator handles conversion to DataSet objects, ready for use in neural network
int labelIndex = 11;
DataSetIterator iterator = new RecordReaderDataSetIterator(recordReader, batchSize, labelIndex, labelIndex, true);
DataSet allData = iterator.next();
SplitTestAndTrain testAndTrain = allData.splitTestAndTrain(0.80); //Use 80% of data for training
trainingData = testAndTrain.getTrain();
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
}
开发者ID:mccorby,项目名称:FederatedAndroidTrainer,代码行数:25,代码来源:DiabetesFileDataSource.java
示例3: testBruteForce4d
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Test
public void testBruteForce4d() {
Construct4dDataSet imageDataSet = new Construct4dDataSet(10, 5, 10, 15);
NormalizerStandardize myNormalizer = new NormalizerStandardize();
myNormalizer.fit(imageDataSet.sampleDataSet);
assertEquals(imageDataSet.expectedMean, myNormalizer.getMean());
float aat = Transforms.abs(myNormalizer.getStd().div(imageDataSet.expectedStd).sub(1)).maxNumber().floatValue();
float abt = myNormalizer.getStd().maxNumber().floatValue();
float act = imageDataSet.expectedStd.maxNumber().floatValue();
System.out.println("ValA: " + aat);
System.out.println("ValB: " + abt);
System.out.println("ValC: " + act);
assertTrue(aat < 0.05);
NormalizerMinMaxScaler myMinMaxScaler = new NormalizerMinMaxScaler();
myMinMaxScaler.fit(imageDataSet.sampleDataSet);
assertEquals(imageDataSet.expectedMin, myMinMaxScaler.getMin());
assertEquals(imageDataSet.expectedMax, myMinMaxScaler.getMax());
DataSet copyDataSet = imageDataSet.sampleDataSet.copy();
myNormalizer.transform(copyDataSet);
}
开发者ID:deeplearning4j,项目名称:nd4j,代码行数:25,代码来源:PreProcessor3D4DTest.java
示例4: testRevert
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Test
public void testRevert() {
double tolerancePerc = 0.01; // 0.01% of correct value
int nSamples = 500;
int nFeatures = 3;
INDArray featureSet = Nd4j.randn(nSamples, nFeatures);
INDArray labelSet = Nd4j.zeros(nSamples, 1);
DataSet sampleDataSet = new DataSet(featureSet, labelSet);
NormalizerStandardize myNormalizer = new NormalizerStandardize();
myNormalizer.fit(sampleDataSet);
DataSet transformed = sampleDataSet.copy();
myNormalizer.transform(transformed);
//System.out.println(transformed.getFeatures());
myNormalizer.revert(transformed);
//System.out.println(transformed.getFeatures());
INDArray delta = Transforms.abs(transformed.getFeatures().sub(sampleDataSet.getFeatures()))
.div(sampleDataSet.getFeatures());
double maxdeltaPerc = delta.max(0, 1).mul(100).getDouble(0, 0);
assertTrue(maxdeltaPerc < tolerancePerc);
}
开发者ID:deeplearning4j,项目名称:nd4j,代码行数:23,代码来源:NormalizerStandardizeTest.java
示例5: testRestoreUnsavedNormalizerFromInputStream
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Test
public void testRestoreUnsavedNormalizerFromInputStream() throws Exception {
DataSet dataSet = trivialDataSet();
NormalizerStandardize norm = new NormalizerStandardize();
norm.fit(dataSet);
ComputationGraph cg = simpleComputationGraph();
cg.init();
File tempFile = File.createTempFile("tsfs", "fdfsdf");
tempFile.deleteOnExit();
ModelSerializer.writeModel(cg, tempFile, true);
FileInputStream fis = new FileInputStream(tempFile);
NormalizerStandardize restored = ModelSerializer.restoreNormalizerFromInputStream(fis);
assertEquals(null, restored);
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:21,代码来源:ModelSerializerTest.java
示例6: testMeanStdZeros
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Test
public void testMeanStdZeros() {
List<List<Writable>> data = new ArrayList<>();
Schema.Builder builder = new Schema.Builder();
int numColumns = 6;
for (int i = 0; i < numColumns; i++)
builder.addColumnDouble(String.valueOf(i));
for (int i = 0; i < 5; i++) {
List<Writable> record = new ArrayList<>(numColumns);
data.add(record);
for (int j = 0; j < numColumns; j++) {
record.add(new DoubleWritable(1.0));
}
}
INDArray arr = RecordConverter.toMatrix(data);
Schema schema = builder.build();
JavaRDD<List<Writable>> rdd = sc.parallelize(data);
DataRowsFacade dataFrame = DataFrames.toDataFrame(schema, rdd);
//assert equivalent to the ndarray pre processing
NormalizerStandardize standardScaler = new NormalizerStandardize();
standardScaler.fit(new DataSet(arr.dup(), arr.dup()));
INDArray standardScalered = arr.dup();
standardScaler.transform(new DataSet(standardScalered, standardScalered));
DataNormalization zeroToOne = new NormalizerMinMaxScaler();
zeroToOne.fit(new DataSet(arr.dup(), arr.dup()));
INDArray zeroToOnes = arr.dup();
zeroToOne.transform(new DataSet(zeroToOnes, zeroToOnes));
List<Row> rows = Normalization.stdDevMeanColumns(dataFrame, dataFrame.get().columns());
INDArray assertion = DataFrames.toMatrix(rows);
//compare standard deviation
assertTrue(standardScaler.getStd().equalsWithEps(assertion.getRow(0), 1e-1));
//compare mean
assertTrue(standardScaler.getMean().equalsWithEps(assertion.getRow(1), 1e-1));
}
开发者ID:deeplearning4j,项目名称:DataVec,代码行数:41,代码来源:NormalizationTests.java
示例7: irisCsv
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
static DataIterator<NormalizerStandardize> irisCsv(String name) {
CSVRecordReader recordReader = new CSVRecordReader(0, ",");
try {
recordReader.initialize(new FileSplit(new File(name)));
} catch (Exception e) {
e.printStackTrace();
}
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 = 50; //Iris data set: 150 examples total.
RecordReaderDataSetIterator iterator = new RecordReaderDataSetIterator(
recordReader,
batchSize,
labelIndex,
numClasses
);
NormalizerStandardize normalizer = new NormalizerStandardize();
while (iterator.hasNext()) {
normalizer.fit(iterator.next());
}
iterator.reset();
iterator.setPreProcessor(normalizer);
return new DataIterator<>(iterator, normalizer);
}
开发者ID:wmeddie,项目名称:dl4j-trainer-archetype,代码行数:31,代码来源:DataIterator.java
示例8: main
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
public static void main(String... args) throws Exception {
Options options = new Options();
options.addOption("i", "input", true, "The file with test data.");
options.addOption("m", "model", true, "Name of trained model file.");
CommandLine cmd = new BasicParser().parse(options, args);
String input = cmd.getOptionValue("i");
String modelName = cmd.getOptionValue("m");
if (cmd.hasOption("i") && cmd.hasOption("m")) {
MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(modelName);
DataIterator<NormalizerStandardize> it = DataIterator.irisCsv(input);
RecordReaderDataSetIterator testData = it.getIterator();
NormalizerStandardize normalizer = it.getNormalizer();
normalizer.load(
new File(modelName + ".norm1"),
new File(modelName + ".norm2"),
new File(modelName + ".norm3"),
new File(modelName + ".norm4")
);
Evaluation eval = new Evaluation(3);
while (testData.hasNext()) {
DataSet ds = testData.next();
INDArray output = model.output(ds.getFeatureMatrix());
eval.eval(ds.getLabels(), output);
}
log.info(eval.stats());
} else {
log.error("Invalid arguments.");
new HelpFormatter().printHelp("Evaluate", options);
}
}
开发者ID:wmeddie,项目名称:dl4j-trainer-archetype,代码行数:38,代码来源:Evaluate.java
示例9: testBruteForce3d
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Test
public void testBruteForce3d() {
NormalizerStandardize myNormalizer = new NormalizerStandardize();
NormalizerMinMaxScaler myMinMaxScaler = new NormalizerMinMaxScaler();
int timeSteps = 15;
int samples = 100;
//multiplier for the features
INDArray featureScaleA = Nd4j.create(new double[] {1, -2, 3}).reshape(3, 1);
INDArray featureScaleB = Nd4j.create(new double[] {2, 2, 3}).reshape(3, 1);
Construct3dDataSet caseA = new Construct3dDataSet(featureScaleA, timeSteps, samples, 1);
Construct3dDataSet caseB = new Construct3dDataSet(featureScaleB, timeSteps, samples, 1);
myNormalizer.fit(caseA.sampleDataSet);
assertEquals(caseA.expectedMean, myNormalizer.getMean());
assertTrue(Transforms.abs(myNormalizer.getStd().div(caseA.expectedStd).sub(1)).maxNumber().floatValue() < 0.01);
myMinMaxScaler.fit(caseB.sampleDataSet);
assertEquals(caseB.expectedMin, myMinMaxScaler.getMin());
assertEquals(caseB.expectedMax, myMinMaxScaler.getMax());
//Same Test with an Iterator, values should be close for std, exact for everything else
DataSetIterator sampleIterA = new TestDataSetIterator(caseA.sampleDataSet, 5);
DataSetIterator sampleIterB = new TestDataSetIterator(caseB.sampleDataSet, 5);
myNormalizer.fit(sampleIterA);
assertEquals(myNormalizer.getMean(), caseA.expectedMean);
assertTrue(Transforms.abs(myNormalizer.getStd().div(caseA.expectedStd).sub(1)).maxNumber().floatValue() < 0.01);
myMinMaxScaler.fit(sampleIterB);
assertEquals(myMinMaxScaler.getMin(), caseB.expectedMin);
assertEquals(myMinMaxScaler.getMax(), caseB.expectedMax);
}
开发者ID:deeplearning4j,项目名称:nd4j,代码行数:37,代码来源:PreProcessor3D4DTest.java
示例10: testDifferentBatchSizes
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Test
public void testDifferentBatchSizes() {
// Create 6x1 matrix of the numbers 1 through 6
INDArray values = Nd4j.linspace(1, 6, 6).transpose();
DataSet dataSet = new DataSet(values, values);
// Test fitting a DataSet
NormalizerStandardize norm1 = new NormalizerStandardize();
norm1.fit(dataSet);
assertEquals(3.5f, norm1.getMean().getFloat(0), 1e-6);
assertEquals(1.70783f, norm1.getStd().getFloat(0), 1e-4);
// Test fitting an iterator with equal batch sizes
DataSetIterator testIter1 = new TestDataSetIterator(dataSet, 3); // Will yield 2 batches of 3 rows
NormalizerStandardize norm2 = new NormalizerStandardize();
norm2.fit(testIter1);
assertEquals(3.5f, norm2.getMean().getFloat(0), 1e-6);
assertEquals(1.70783f, norm2.getStd().getFloat(0), 1e-4);
// Test fitting an iterator with varying batch sizes
DataSetIterator testIter2 = new TestDataSetIterator(dataSet, 4); // Will yield batch of 4 and batch of 2 rows
NormalizerStandardize norm3 = new NormalizerStandardize();
norm3.fit(testIter2);
assertEquals(3.5f, norm3.getMean().getFloat(0), 1e-6);
assertEquals(1.70783f, norm3.getStd().getFloat(0), 1e-4);
// Test fitting an iterator with batches of single rows
DataSetIterator testIter3 = new TestDataSetIterator(dataSet, 1); // Will yield 6 batches of 1 row
NormalizerStandardize norm4 = new NormalizerStandardize();
norm4.fit(testIter3);
assertEquals(3.5f, norm4.getMean().getFloat(0), 1e-6);
assertEquals(1.70783f, norm4.getStd().getFloat(0), 1e-4);
}
开发者ID:deeplearning4j,项目名称:nd4j,代码行数:34,代码来源:NormalizerStandardizeTest.java
示例11: testUnderOverflow
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Test
public void testUnderOverflow() {
// This dataset will be basically constant with a small std deviation
// And the constant is large. Checking if algorithm can handle
double tolerancePerc = 1; //Within 1 %
double toleranceAbs = 0.0005;
int nSamples = 1000;
int bSize = 10;
int x = -1000000, y = 1000000;
double z = 1000000;
INDArray featureX = Nd4j.rand(nSamples, 1).mul(1).add(x);
INDArray featureY = Nd4j.rand(nSamples, 1).mul(2).add(y);
INDArray featureZ = Nd4j.rand(nSamples, 1).mul(3).add(z);
INDArray featureSet = Nd4j.concat(1, featureX, featureY, featureZ);
INDArray labelSet = Nd4j.zeros(nSamples, 1);
DataSet sampleDataSet = new DataSet(featureSet, labelSet);
DataSetIterator sampleIter = new TestDataSetIterator(sampleDataSet, bSize);
INDArray theoreticalMean = Nd4j.create(new double[] {x, y, z});
NormalizerStandardize myNormalizer = new NormalizerStandardize();
myNormalizer.fit(sampleIter);
INDArray meanDelta = Transforms.abs(theoreticalMean.sub(myNormalizer.getMean()));
INDArray meanDeltaPerc = meanDelta.mul(100).div(theoreticalMean);
assertTrue(meanDeltaPerc.max(1).getDouble(0, 0) < tolerancePerc);
//this just has to not barf
//myNormalizer.transform(sampleIter);
myNormalizer.transform(sampleDataSet);
}
开发者ID:deeplearning4j,项目名称:nd4j,代码行数:33,代码来源:NormalizerStandardizeTest.java
示例12: testConstant
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Test
public void testConstant() {
double tolerancePerc = 10.0; // 10% of correct value
int nSamples = 500;
int nFeatures = 3;
int constant = 100;
INDArray featureSet = Nd4j.zeros(nSamples, nFeatures).add(constant);
INDArray labelSet = Nd4j.zeros(nSamples, 1);
DataSet sampleDataSet = new DataSet(featureSet, labelSet);
NormalizerStandardize myNormalizer = new NormalizerStandardize();
myNormalizer.fit(sampleDataSet);
//Checking if we gets nans
assertFalse(Double.isNaN(myNormalizer.getStd().getDouble(0)));
myNormalizer.transform(sampleDataSet);
//Checking if we gets nans, because std dev is zero
assertFalse(Double.isNaN(sampleDataSet.getFeatures().min(0, 1).getDouble(0)));
//Checking to see if transformed values are close enough to zero
assertEquals(Transforms.abs(sampleDataSet.getFeatures()).max(0, 1).getDouble(0, 0), 0,
constant * tolerancePerc / 100.0);
myNormalizer.revert(sampleDataSet);
//Checking if we gets nans, because std dev is zero
assertFalse(Double.isNaN(sampleDataSet.getFeatures().min(0, 1).getDouble(0)));
assertEquals(Transforms.abs(sampleDataSet.getFeatures().sub(featureSet)).min(0, 1).getDouble(0), 0,
constant * tolerancePerc / 100.0);
}
开发者ID:deeplearning4j,项目名称:nd4j,代码行数:31,代码来源:NormalizerStandardizeTest.java
示例13: randomData
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Before
public void randomData() {
Nd4j.getRandom().setSeed(12345);
batchSize = 13;
batchCount = 20;
lastBatch = batchSize / 2;
INDArray origFeatures = Nd4j.rand(batchCount * batchSize + lastBatch, 10);
INDArray origLabels = Nd4j.rand(batchCount * batchSize + lastBatch, 3);
data = new DataSet(origFeatures, origLabels);
stdScaler = new NormalizerStandardize();
minMaxScaler = new NormalizerMinMaxScaler();
}
开发者ID:deeplearning4j,项目名称:nd4j,代码行数:13,代码来源:NormalizerTests.java
示例14: write
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Override
public void write(@NonNull NormalizerStandardize normalizer, @NonNull OutputStream stream) throws IOException {
try (DataOutputStream dos = new DataOutputStream(stream)) {
dos.writeBoolean(normalizer.isFitLabel());
Nd4j.write(normalizer.getMean(), dos);
Nd4j.write(normalizer.getStd(), dos);
if (normalizer.isFitLabel()) {
Nd4j.write(normalizer.getLabelMean(), dos);
Nd4j.write(normalizer.getLabelStd(), dos);
}
dos.flush();
}
}
开发者ID:deeplearning4j,项目名称:nd4j,代码行数:16,代码来源:StandardizeSerializerStrategy.java
示例15: restore
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Override
public NormalizerStandardize restore(@NonNull InputStream stream) throws IOException {
DataInputStream dis = new DataInputStream(stream);
boolean fitLabels = dis.readBoolean();
NormalizerStandardize result = new NormalizerStandardize(Nd4j.read(dis), Nd4j.read(dis));
result.fitLabel(fitLabels);
if (fitLabels) {
result.setLabelStats(Nd4j.read(dis), Nd4j.read(dis));
}
return result;
}
开发者ID:deeplearning4j,项目名称:nd4j,代码行数:15,代码来源:StandardizeSerializerStrategy.java
示例16: normalize
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Override
public void normalize() {
//FeatureUtil.normalizeMatrix(getFeatures());
NormalizerStandardize inClassPreProcessor = new NormalizerStandardize();
inClassPreProcessor.fit(this);
inClassPreProcessor.transform(this);
}
开发者ID:deeplearning4j,项目名称:nd4j,代码行数:8,代码来源:DataSet.java
示例17: testRocMultiToHtml
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
@Test
public void testRocMultiToHtml() throws Exception {
DataSetIterator iter = new IrisDataSetIterator(150, 150);
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().weightInit(WeightInit.XAVIER).list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(4).activation(Activation.TANH).build()).layer(1,
new OutputLayer.Builder().nIn(4).nOut(3).activation(Activation.SOFTMAX)
.lossFunction(LossFunctions.LossFunction.MCXENT).build())
.build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
NormalizerStandardize ns = new NormalizerStandardize();
DataSet ds = iter.next();
ns.fit(ds);
ns.transform(ds);
for (int i = 0; i < 30; i++) {
net.fit(ds);
}
for (int numSteps : new int[] {20, 0}) {
ROCMultiClass roc = new ROCMultiClass(numSteps);
iter.reset();
INDArray f = ds.getFeatures();
INDArray l = ds.getLabels();
INDArray out = net.output(f);
roc.eval(l, out);
String str = EvaluationTools.rocChartToHtml(roc, Arrays.asList("setosa", "versicolor", "virginica"));
System.out.println(str);
}
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:36,代码来源:EvaluationToolsTests.java
示例18: MyPreProcessor
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
public MyPreProcessor (NormalizerStandardize normalizer)
{
mean = normalizer.getMean().getDouble(0);
std = normalizer.getStd().getDouble(0);
logger.info(String.format("Pixel pre-processor mean:%.2f std:%.2f", mean, std));
}
开发者ID:Audiveris,项目名称:omr-dataset-tools,代码行数:7,代码来源:Training.java
示例19: process
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
/**
* Process the available pairs (image + annotations) to extract the corresponding
* symbols features, later used to train the classifier.
*
* @throws IOException in case of IO problem
*/
public void process ()
throws IOException
{
if (Main.cli.arguments.isEmpty()) {
logger.warn("No input specified for features. Exiting.");
return;
}
try {
features = getPrintWriter(FEATURES_PATH); // Output features file
journal = getPrintWriter(JOURNAL_PATH); // Output journal file
sheets = getPrintWriter(SHEETS_PATH); // Output sheets file
// Header comment line for each CSV file
final int numPixels = CONTEXT_WIDTH * CONTEXT_HEIGHT;
features.println("# " + numPixels + " pixels, shapeId");
journal.println("# row, sheetId, symbolId, interline, x, y, w, h, shapeId");
sheets.println("# sheetId, sheetPath");
// Scan the provided inputs (which can be simple files or folders)
for (Path path : Main.cli.arguments) {
if (!Files.exists(path)) {
logger.warn("Could not find {}", path);
continue;
}
if (Files.isDirectory(path)) {
// Process folder recusrsively
processFolder(path);
} else {
// We look for "foo.xml" Annotations files
final String fileName = path.getFileName().toString();
if (fileName.endsWith(INFO_EXT)) {
processFile(path);
}
}
}
features.flush();
features.close();
journal.flush();
journal.close();
sheets.flush();
sheets.close();
// Store dim stats per shape
storeDims();
// pixels.store(PIXELS_PATH);
DistributionStats stats = pixels.build();
NormalizerStandardize normalizer = new NormalizerStandardize(
stats.getMean(),
stats.getStd());
NormalizerSerializer.getDefault().write(normalizer, PIXELS_PATH.toFile());
} catch (Throwable ex) {
logger.warn("Error loading data", ex);
}
}
开发者ID:Audiveris,项目名称:omr-dataset-tools,代码行数:68,代码来源:Features.java
示例20: main
import org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
//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("src/main/resources/DL4J_Resources/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;
int iterations = 1000;
long seed = 6;
log.info("Build model....");
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.seed(seed)
.iterations(iterations)
.activation(Activation.TANH)
.weightInit(WeightInit.XAVIER)
.learningRate(0.1)
.regularization(true).l2(1e-4)
.list()
.layer(0, new DenseLayer.Builder().nIn(numInputs).nOut(3)
.build())
.layer(1, new DenseLayer.Builder().nIn(3).nOut(3)
.build())
.layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD)
.activation(Activation.SOFTMAX)
.nIn(3).nOut(outputNum).build())
.backprop(true).pretrain(false)
.build();
//run the model
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
model.setListeners(new ScoreIterationListener(100));
model.fit(trainingData);
//evaluate the model on the test set
Evaluation eval = new Evaluation(3);
INDArray output = model.output(testData.getFeatureMatrix());
eval.eval(testData.getLabels(), output);
log.info(eval.stats());
//Save the model
File locationToSave = new File("src/main/resources/generatedModels/DL4J/DL4J_Iris_Model.zip"); //Where to save the network. Note: the file is in .zip format - can be opened externally
boolean saveUpdater = true; //Updater: i.e., the state for Momentum, RMSProp, Adagrad etc. Save this if you want to train your network more in the future
ModelSerializer.writeModel(model, locationToSave, saveUpdater);
//Load the model
MultiLayerNetwork restored = ModelSerializer.restoreMultiLayerNetwork(locationToSave);
System.out.println("Saved and loaded parameters are equal: " + model.params().equals(restored.params()));
System.out.println("Saved and loaded configurations are equal: " + model.getLayerWiseConfigurations().equals(restored.getLayerWiseConfigurations()));
}
开发者ID:kaiwaehner,项目名称:kafka-streams-machine-learning-examples,代码行数:78,代码来源:DeepLearning4J_CSV_Model.java
注:本文中的org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论