本文整理汇总了Java中org.nd4j.linalg.dataset.api.iterator.DataSetIterator.setPreProcessor方法的典型用法代码示例。如果您正苦于以下问题:Java DataSetIterator.setPreProcessor方法的具体用法?Java DataSetIterator.setPreProcessor怎么用?Java DataSetIterator.setPreProcessor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.nd4j.linalg.dataset.api.iterator.DataSetIterator
的用法示例。
在下文中一共展示了DataSetIterator.setPreProcessor方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDataSetIterator
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; //导入方法依赖的package包/类
/**
* This method returns the iterator. Scales all intensity values: it divides them by 255.
*
* @param data the dataset to use
* @param seed the seed for the random number generator
* @param batchSize the batch size to use
* @return the iterator
* @throws Exception
*/
@Override
public DataSetIterator getDataSetIterator(Instances data, int seed, int batchSize)
throws Exception {
batchSize = Math.min(data.numInstances(), batchSize);
validate(data);
ImageRecordReader reader = getImageRecordReader(data);
final int labelIndex = 1; // Use explicit label index position
final int numPossibleLabels = data.numClasses();
DataSetIterator tmpIter =
new RecordReaderDataSetIterator(reader, batchSize, labelIndex, numPossibleLabels);
DataNormalization scaler = new ImagePreProcessingScaler(0, 1);
scaler.fit(tmpIter);
tmpIter.setPreProcessor(scaler);
return tmpIter;
}
示例2: testItervsDataset
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; //导入方法依赖的package包/类
public float testItervsDataset(DataNormalization preProcessor) {
DataSet dataCopy = data.copy();
DataSetIterator dataIter = new TestDataSetIterator(dataCopy, batchSize);
preProcessor.fit(dataCopy);
preProcessor.transform(dataCopy);
INDArray transformA = dataCopy.getFeatures();
preProcessor.fit(dataIter);
dataIter.setPreProcessor(preProcessor);
DataSet next = dataIter.next();
INDArray transformB = next.getFeatures();
while (dataIter.hasNext()) {
next = dataIter.next();
INDArray transformb = next.getFeatures();
transformB = Nd4j.vstack(transformB, transformb);
}
return Transforms.abs(transformB.div(transformA).rsub(1)).maxNumber().floatValue();
}
示例3: testNormalizerPrefetchReset
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; //导入方法依赖的package包/类
@Test
public void testNormalizerPrefetchReset() throws Exception {
//Check NPE fix for: https://github.com/deeplearning4j/deeplearning4j/issues/4214
RecordReader csv = new CSVRecordReader();
csv.initialize(new FileSplit(new ClassPathResource("iris.txt").getTempFileFromArchive()));
int batchSize = 3;
DataSetIterator iter = new RecordReaderDataSetIterator(csv, batchSize, 4, 4, true);
DataNormalization normalizer = new NormalizerMinMaxScaler(0, 1);
normalizer.fit(iter);
iter.setPreProcessor(normalizer);
iter.inputColumns(); //Prefetch
iter.totalOutcomes();
iter.hasNext();
iter.reset();
iter.next();
}
示例4: testGradient2dSimple
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; //导入方法依赖的package包/类
@Test
public void testGradient2dSimple() {
DataNormalization scaler = new NormalizerMinMaxScaler();
DataSetIterator iter = new IrisDataSetIterator(150, 150);
scaler.fit(iter);
iter.setPreProcessor(scaler);
DataSet ds = iter.next();
INDArray input = ds.getFeatureMatrix();
INDArray labels = ds.getLabels();
MultiLayerConfiguration.Builder builder =
new NeuralNetConfiguration.Builder().updater(new NoOp())
.seed(12345L).weightInit(WeightInit.DISTRIBUTION)
.dist(new NormalDistribution(0, 1)).list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(3)
.activation(Activation.IDENTITY).build())
.layer(1, new BatchNormalization.Builder().nOut(3).build())
.layer(2, new ActivationLayer.Builder().activation(Activation.TANH).build())
.layer(3, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.activation(Activation.SOFTMAX).nIn(3).nOut(3).build())
.pretrain(false).backprop(true);
MultiLayerNetwork mln = new MultiLayerNetwork(builder.build());
mln.init();
if (PRINT_RESULTS) {
for (int j = 0; j < mln.getnLayers(); j++)
System.out.println("Layer " + j + " # params: " + mln.getLayer(j).numParams());
}
boolean gradOK = GradientCheckUtil.checkGradients(mln, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);
assertTrue(gradOK);
}
示例5: testGradient2dFixedGammaBeta
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; //导入方法依赖的package包/类
@Test
public void testGradient2dFixedGammaBeta() {
DataNormalization scaler = new NormalizerMinMaxScaler();
DataSetIterator iter = new IrisDataSetIterator(150, 150);
scaler.fit(iter);
iter.setPreProcessor(scaler);
DataSet ds = iter.next();
INDArray input = ds.getFeatureMatrix();
INDArray labels = ds.getLabels();
MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder().updater(new NoOp())
.seed(12345L).weightInit(WeightInit.DISTRIBUTION)
.dist(new NormalDistribution(0, 1)).list()
.layer(0, new DenseLayer.Builder().nIn(4).nOut(3).activation(Activation.IDENTITY).build())
.layer(1, new BatchNormalization.Builder().lockGammaBeta(true).gamma(2.0).beta(0.5).nOut(3)
.build())
.layer(2, new ActivationLayer.Builder().activation(Activation.TANH).build())
.layer(3, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.activation(Activation.SOFTMAX).nIn(3).nOut(3).build())
.pretrain(false).backprop(true);
MultiLayerNetwork mln = new MultiLayerNetwork(builder.build());
mln.init();
if (PRINT_RESULTS) {
for (int j = 0; j < mln.getnLayers(); j++)
System.out.println("Layer " + j + " # params: " + mln.getLayer(j).numParams());
}
boolean gradOK = GradientCheckUtil.checkGradients(mln, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);
assertTrue(gradOK);
}
示例6: testTransform
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; //导入方法依赖的package包/类
@Test
public void testTransform() {
/*Random dataset is generated such that
AX + B where X is from a normal distribution with mean 0 and std 1
The mean of above will be B and std A
Obtained mean and std dev are compared to theoretical
Transformed values should be the same as X with the same seed.
*/
long randSeed = 2227724;
int nFeatures = 2;
int nSamples = 6400;
int bsize = 8;
int a = 5;
int b = 100;
INDArray sampleMean, sampleStd, sampleMeanDelta, sampleStdDelta, delta, deltaPerc;
double maxDeltaPerc, sampleMeanSEM;
genRandomDataSet normData = new genRandomDataSet(nSamples, nFeatures, a, b, randSeed);
genRandomDataSet expectedData = new genRandomDataSet(nSamples, nFeatures, 1, 0, randSeed);
genRandomDataSet beforeTransformData = new genRandomDataSet(nSamples, nFeatures, a, b, randSeed);
NormalizerStandardize myNormalizer = new NormalizerStandardize();
myNormalizer.fitLabel(true);
DataSetIterator normIterator = normData.getIter(bsize);
DataSetIterator expectedIterator = expectedData.getIter(bsize);
DataSetIterator beforeTransformIterator = beforeTransformData.getIter(bsize);
myNormalizer.fit(normIterator);
double tolerancePerc = 0.5; //within 0.5%
sampleMean = myNormalizer.getMean();
sampleMeanDelta = Transforms.abs(sampleMean.sub(normData.theoreticalMean));
assertTrue(sampleMeanDelta.mul(100).div(normData.theoreticalMean).max(1).getDouble(0, 0) < tolerancePerc);
//sanity check to see if it's within the theoretical standard error of mean
sampleMeanSEM = sampleMeanDelta.div(normData.theoreticalSEM).max(1).getDouble(0, 0);
assertTrue(sampleMeanSEM < 2.6); //99% of the time it should be within this many SEMs
tolerancePerc = 5; //within 5%
sampleStd = myNormalizer.getStd();
sampleStdDelta = Transforms.abs(sampleStd.sub(normData.theoreticalStd));
assertTrue(sampleStdDelta.div(normData.theoreticalStd).max(1).mul(100).getDouble(0, 0) < tolerancePerc);
tolerancePerc = 1; //within 1%
normIterator.setPreProcessor(myNormalizer);
while (normIterator.hasNext()) {
INDArray before = beforeTransformIterator.next().getFeatures();
DataSet here = normIterator.next();
assertEquals(here.getFeatures(), here.getLabels()); //bootstrapping existing test on features
INDArray after = here.getFeatures();
INDArray expected = expectedIterator.next().getFeatures();
delta = Transforms.abs(after.sub(expected));
deltaPerc = delta.div(before.sub(expected));
deltaPerc.muli(100);
maxDeltaPerc = deltaPerc.max(0, 1).getDouble(0, 0);
//System.out.println("=== BEFORE ===");
//System.out.println(before);
//System.out.println("=== AFTER ===");
//System.out.println(after);
//System.out.println("=== SHOULD BE ===");
//System.out.println(expected);
assertTrue(maxDeltaPerc < tolerancePerc);
}
}
示例7: testBruteForce
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; //导入方法依赖的package包/类
@Test
public void testBruteForce() {
//X_std = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))
//X_scaled = X_std * (max - min) + min
// Dataset features are scaled consecutive natural numbers
int nSamples = 500;
int x = 4, y = 2, z = 3;
INDArray featureX = Nd4j.linspace(1, nSamples, nSamples).reshape(nSamples, 1);
INDArray featureY = featureX.mul(y);
INDArray featureZ = featureX.mul(z);
featureX.muli(x);
INDArray featureSet = Nd4j.concat(1, featureX, featureY, featureZ);
INDArray labelSet = Nd4j.zeros(nSamples, 1);
DataSet sampleDataSet = new DataSet(featureSet, labelSet);
//expected min and max
INDArray theoreticalMin = Nd4j.create(new double[] {x, y, z});
INDArray theoreticalMax = Nd4j.create(new double[] {nSamples * x, nSamples * y, nSamples * z});
INDArray theoreticalRange = theoreticalMax.sub(theoreticalMin);
NormalizerMinMaxScaler myNormalizer = new NormalizerMinMaxScaler();
myNormalizer.fit(sampleDataSet);
INDArray minDataSet = myNormalizer.getMin();
INDArray maxDataSet = myNormalizer.getMax();
INDArray minDiff = minDataSet.sub(theoreticalMin).max(1);
INDArray maxDiff = maxDataSet.sub(theoreticalMax).max(1);
assertEquals(minDiff.getDouble(0, 0), 0.0, 0.000000001);
assertEquals(maxDiff.max(1).getDouble(0, 0), 0.0, 0.000000001);
// SAME TEST WITH THE ITERATOR
int bSize = 1;
DataSetIterator sampleIter = new TestDataSetIterator(sampleDataSet, bSize);
myNormalizer.fit(sampleIter);
minDataSet = myNormalizer.getMin();
maxDataSet = myNormalizer.getMax();
assertEquals(minDataSet.sub(theoreticalMin).max(1).getDouble(0, 0), 0.0, 0.000000001);
assertEquals(maxDataSet.sub(theoreticalMax).max(1).getDouble(0, 0), 0.0, 0.000000001);
sampleIter.setPreProcessor(myNormalizer);
INDArray actual, expected, delta;
int i = 1;
while (sampleIter.hasNext()) {
expected = theoreticalMin.mul(i - 1).div(theoreticalRange);
actual = sampleIter.next().getFeatures();
delta = Transforms.abs(actual.sub(expected));
assertTrue(delta.max(1).getDouble(0, 0) < 0.0001);
i++;
}
}
示例8: testTransform
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; //导入方法依赖的package包/类
@Test
public void testTransform() {
/*Random dataset is generated such that
AX + B where X is from a normal distribution with mean 0 and std 1
The mean of above will be B and std A
Obtained mean and std dev are compared to theoretical
Transformed values should be the same as X with the same seed.
*/
long randSeed = 41732786;
int nFeatures = 2;
int nSamples = 6400;
int bsize = 8;
int a = 5;
int b = 100;
INDArray sampleMean, sampleStd, sampleMeanDelta, sampleStdDelta, delta, deltaPerc;
double maxDeltaPerc, sampleMeanSEM;
genRandomDataSet normData = new genRandomDataSet(nSamples, nFeatures, a, b, randSeed);
DataSet genRandExpected = normData.theoreticalTransform;
genRandomDataSet expectedData = new genRandomDataSet(nSamples, nFeatures, 1, 0, randSeed);
genRandomDataSet beforeTransformData = new genRandomDataSet(nSamples, nFeatures, a, b, randSeed);
NormalizerStandardize myNormalizer = new NormalizerStandardize();
DataSetIterator normIterator = normData.getIter(bsize);
DataSetIterator genRandExpectedIter = new TestDataSetIterator(genRandExpected, bsize);
DataSetIterator expectedIterator = expectedData.getIter(bsize);
DataSetIterator beforeTransformIterator = beforeTransformData.getIter(bsize);
myNormalizer.fit(normIterator);
double tolerancePerc = 0.10; //within 0.1%
sampleMean = myNormalizer.getMean();
sampleMeanDelta = Transforms.abs(sampleMean.sub(normData.theoreticalMean));
assertTrue(sampleMeanDelta.mul(100).div(normData.theoreticalMean).max(1).getDouble(0, 0) < tolerancePerc);
//sanity check to see if it's within the theoretical standard error of mean
sampleMeanSEM = sampleMeanDelta.div(normData.theoreticalSEM).max(1).getDouble(0, 0);
assertTrue(sampleMeanSEM < 2.6); //99% of the time it should be within this many SEMs
tolerancePerc = 1; //within 1% - std dev value
sampleStd = myNormalizer.getStd();
sampleStdDelta = Transforms.abs(sampleStd.sub(normData.theoreticalStd));
assertTrue(sampleStdDelta.div(normData.theoreticalStd).max(1).mul(100).getDouble(0, 0) < tolerancePerc);
tolerancePerc = 1; //within 1%
normIterator.setPreProcessor(myNormalizer);
while (normIterator.hasNext()) {
INDArray before = beforeTransformIterator.next().getFeatures();
INDArray origBefore = genRandExpectedIter.next().getFeatures();
INDArray after = normIterator.next().getFeatures();
INDArray expected = expectedIterator.next().getFeatures();
delta = Transforms.abs(after.sub(expected));
deltaPerc = delta.div(Transforms.abs(before.sub(expected)));
deltaPerc.muli(100);
maxDeltaPerc = deltaPerc.max(0, 1).getDouble(0, 0);
/*
System.out.println("=== BEFORE ===");
System.out.println(before);
System.out.println("=== ORIG BEFORE ===");
System.out.println(origBefore);
System.out.println("=== AFTER ===");
System.out.println(after);
System.out.println("=== SHOULD BE ===");
System.out.println(expected);
System.out.println("% diff, "+ maxDeltaPerc);
*/
assertTrue(maxDeltaPerc < tolerancePerc);
}
}