Search in sources :

Example 16 with NormalizerStandardize

use of org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize in project deeplearning4j by deeplearning4j.

the class RBMTests method testIrisRectifiedHidden.

@Test
public void testIrisRectifiedHidden() {
    IrisDataFetcher fetcher = new IrisDataFetcher();
    fetcher.fetch(150);
    DataNormalization norm = new NormalizerStandardize();
    DataSet d = fetcher.next();
    norm.fit(d);
    norm.transform(d);
    INDArray params = Nd4j.create(1, 4 * 3 + 4 + 3);
    RBM rbm = getRBMLayer(4, 3, HiddenUnit.RECTIFIED, VisibleUnit.LINEAR, params, true, false, 1, LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD);
    rbm.fit(d.getFeatureMatrix());
}
Also used : DataNormalization(org.nd4j.linalg.dataset.api.preprocessor.DataNormalization) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataSet(org.nd4j.linalg.dataset.DataSet) IrisDataFetcher(org.deeplearning4j.datasets.fetchers.IrisDataFetcher) NormalizerStandardize(org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize) Test(org.junit.Test)

Example 17 with NormalizerStandardize

use of org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize in project deeplearning4j by deeplearning4j.

the class RBMTests method testIrisGaussianHidden.

@Test
public void testIrisGaussianHidden() {
    IrisDataFetcher fetcher = new IrisDataFetcher();
    fetcher.fetch(150);
    DataNormalization norm = new NormalizerStandardize();
    DataSet d = fetcher.next();
    norm.fit(d);
    norm.transform(d);
    INDArray params = Nd4j.create(1, 4 * 3 + 4 + 3);
    RBM rbm = getRBMLayer(4, 3, HiddenUnit.GAUSSIAN, VisibleUnit.GAUSSIAN, params, true, false, 1, LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD);
    rbm.fit(d.getFeatureMatrix());
}
Also used : DataNormalization(org.nd4j.linalg.dataset.api.preprocessor.DataNormalization) INDArray(org.nd4j.linalg.api.ndarray.INDArray) DataSet(org.nd4j.linalg.dataset.DataSet) IrisDataFetcher(org.deeplearning4j.datasets.fetchers.IrisDataFetcher) NormalizerStandardize(org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize) Test(org.junit.Test)

Example 18 with NormalizerStandardize

use of org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize in project nd4j by deeplearning4j.

the class NormalizerStandardizeTest method testBruteForce.

@Test
public void testBruteForce() {
    /* This test creates a dataset where feature values are multiples of consecutive natural numbers
           The obtained values are compared to the theoretical mean and std dev
         */
    // 0.01% of correct value
    double tolerancePerc = 0.01;
    int nSamples = 5120;
    int x = 1, y = 2, z = 3;
    INDArray featureX = Nd4j.linspace(1, nSamples, nSamples).reshape(nSamples, 1).mul(x);
    INDArray featureY = featureX.mul(y);
    INDArray featureZ = featureX.mul(z);
    INDArray featureSet = Nd4j.concat(1, featureX, featureY, featureZ);
    INDArray labelSet = Nd4j.zeros(nSamples, 1);
    DataSet sampleDataSet = new DataSet(featureSet, labelSet);
    double meanNaturalNums = (nSamples + 1) / 2.0;
    INDArray theoreticalMean = Nd4j.create(new double[] { meanNaturalNums * x, meanNaturalNums * y, meanNaturalNums * z });
    double stdNaturalNums = Math.sqrt((nSamples * nSamples - 1) / 12.0);
    INDArray theoreticalStd = Nd4j.create(new double[] { stdNaturalNums * x, stdNaturalNums * y, stdNaturalNums * z });
    NormalizerStandardize myNormalizer = new NormalizerStandardize();
    myNormalizer.fit(sampleDataSet);
    INDArray meanDelta = Transforms.abs(theoreticalMean.sub(myNormalizer.getMean()));
    INDArray meanDeltaPerc = meanDelta.div(theoreticalMean).mul(100);
    double maxMeanDeltaPerc = meanDeltaPerc.max(1).getDouble(0, 0);
    assertTrue(maxMeanDeltaPerc < tolerancePerc);
    INDArray stdDelta = Transforms.abs(theoreticalStd.sub(myNormalizer.getStd()));
    INDArray stdDeltaPerc = stdDelta.div(theoreticalStd).mul(100);
    double maxStdDeltaPerc = stdDeltaPerc.max(1).getDouble(0, 0);
    assertTrue(maxStdDeltaPerc < tolerancePerc);
    // SAME TEST WITH THE ITERATOR
    int bSize = 10;
    // 0.1% of correct value
    tolerancePerc = 0.1;
    DataSetIterator sampleIter = new TestDataSetIterator(sampleDataSet, bSize);
    myNormalizer.fit(sampleIter);
    meanDelta = Transforms.abs(theoreticalMean.sub(myNormalizer.getMean()));
    meanDeltaPerc = meanDelta.div(theoreticalMean).mul(100);
    maxMeanDeltaPerc = meanDeltaPerc.max(1).getDouble(0, 0);
    assertTrue(maxMeanDeltaPerc < tolerancePerc);
    stdDelta = Transforms.abs(theoreticalStd.sub(myNormalizer.getStd()));
    stdDeltaPerc = stdDelta.div(theoreticalStd).mul(100);
    maxStdDeltaPerc = stdDeltaPerc.max(1).getDouble(0, 0);
    assertTrue(maxStdDeltaPerc < tolerancePerc);
}
Also used : TestDataSetIterator(org.nd4j.linalg.dataset.api.iterator.TestDataSetIterator) INDArray(org.nd4j.linalg.api.ndarray.INDArray) NormalizerStandardize(org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize) TestDataSetIterator(org.nd4j.linalg.dataset.api.iterator.TestDataSetIterator) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 19 with NormalizerStandardize

use of org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize in project nd4j by deeplearning4j.

the class NormalizerStandardizeTest method testConstant.

@Test
public void testConstant() {
    // 10% of correct value
    double tolerancePerc = 10.0;
    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);
}
Also used : INDArray(org.nd4j.linalg.api.ndarray.INDArray) NormalizerStandardize(org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Example 20 with NormalizerStandardize

use of org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize in project nd4j by deeplearning4j.

the class NormalizerStandardizeTest method testTransform.

@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);
    // within 0.1%
    double tolerancePerc = 0.10;
    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);
    // 99% of the time it should be within this many SEMs
    assertTrue(sampleMeanSEM < 2.6);
    // within 1% - std dev value
    tolerancePerc = 1;
    sampleStd = myNormalizer.getStd();
    sampleStdDelta = Transforms.abs(sampleStd.sub(normData.theoreticalStd));
    assertTrue(sampleStdDelta.div(normData.theoreticalStd).max(1).mul(100).getDouble(0, 0) < tolerancePerc);
    // within 1%
    tolerancePerc = 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);
    }
}
Also used : TestDataSetIterator(org.nd4j.linalg.dataset.api.iterator.TestDataSetIterator) INDArray(org.nd4j.linalg.api.ndarray.INDArray) NormalizerStandardize(org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize) TestDataSetIterator(org.nd4j.linalg.dataset.api.iterator.TestDataSetIterator) DataSetIterator(org.nd4j.linalg.dataset.api.iterator.DataSetIterator) Test(org.junit.Test) BaseNd4jTest(org.nd4j.linalg.BaseNd4jTest)

Aggregations

NormalizerStandardize (org.nd4j.linalg.dataset.api.preprocessor.NormalizerStandardize)23 INDArray (org.nd4j.linalg.api.ndarray.INDArray)20 Test (org.junit.Test)19 DataSetIterator (org.nd4j.linalg.dataset.api.iterator.DataSetIterator)13 BaseNd4jTest (org.nd4j.linalg.BaseNd4jTest)12 TestDataSetIterator (org.nd4j.linalg.dataset.api.iterator.TestDataSetIterator)9 MultiLayerConfiguration (org.deeplearning4j.nn.conf.MultiLayerConfiguration)6 MultiLayerNetwork (org.deeplearning4j.nn.multilayer.MultiLayerNetwork)6 NormalizerMinMaxScaler (org.nd4j.linalg.dataset.api.preprocessor.NormalizerMinMaxScaler)6 NeuralNetConfiguration (org.deeplearning4j.nn.conf.NeuralNetConfiguration)5 DataSet (org.nd4j.linalg.dataset.DataSet)5 DataNormalization (org.nd4j.linalg.dataset.api.preprocessor.DataNormalization)5 IrisDataSetIterator (org.deeplearning4j.datasets.iterator.impl.IrisDataSetIterator)4 DenseLayer (org.deeplearning4j.nn.conf.layers.DenseLayer)4 OutputLayer (org.deeplearning4j.nn.conf.layers.OutputLayer)3 DataSet (org.nd4j.linalg.dataset.api.DataSet)3 ArrayList (java.util.ArrayList)2 RecordReader (org.datavec.api.records.reader.RecordReader)2 CSVRecordReader (org.datavec.api.records.reader.impl.csv.CSVRecordReader)2 FileSplit (org.datavec.api.split.FileSplit)2