本文整理汇总了Java中org.nd4j.linalg.ops.transforms.Transforms.abs方法的典型用法代码示例。如果您正苦于以下问题:Java Transforms.abs方法的具体用法?Java Transforms.abs怎么用?Java Transforms.abs使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.nd4j.linalg.ops.transforms.Transforms
的用法示例。
在下文中一共展示了Transforms.abs方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: applyUpdater
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* Calculate the update based on the given gradient
*
* @param gradient the gradient to get the update for
* @param iteration
* @return the gradient
*/
@Override
public void applyUpdater(INDArray gradient, int iteration, int epoch) {
if (m == null || u == null)
throw new IllegalStateException("Updater has not been initialized with view state");
//m = B_1 * m + (1-B_1)*grad
m.muli(config.getBeta1()).addi(gradient.mul(1 - config.getBeta1()));
//u = max(B_2 * u, |grad|)
u.muli(config.getBeta2());
Transforms.abs(gradient, false); //In-place should be OK here, original gradient values aren't used again later
Nd4j.getExecutioner().exec(new OldMax(u, gradient, u, u.length()));
double beta1t = FastMath.pow(config.getBeta1(), iteration + 1);
double learningRate = config.getLearningRate(iteration, epoch);
double alphat = learningRate / (1.0 - beta1t);
if (Double.isNaN(alphat) || Double.isInfinite(alphat) || alphat == 0.0) {
alphat = config.getEpsilon();
}
u.addi(1e-32); // prevent NaNs in params
gradient.assign(m).muli(alphat).divi(u);
}
示例2: testUnderOverflow
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的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);
}
示例3: testIMinIAMin
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testIMinIAMin() {
INDArray arr = Nd4j.create(new double[] {-0.24, -0.26, -0.07, -0.01});
INDArray abs = Transforms.abs(arr);
IAMin iaMin = new IAMin(abs);
IMin iMin = new IMin(arr.dup());
double imin = Nd4j.getExecutioner().execAndReturn(iMin).getFinalResult();
double iamin = Nd4j.getExecutioner().execAndReturn(iaMin).getFinalResult();
System.out.println("IMin: " + imin);
System.out.println("IAMin: " + iamin);
assertEquals(3, iamin, 1e-12);
assertEquals(1, imin, 1e-12);
}
示例4: map
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public NDArrayWritable map(Writable w) {
NDArrayWritable n = (NDArrayWritable) w;
INDArray i = n.get();
if (i == null) {
return n;
}
NDArrayWritable o;
switch (mathFunction) {
case ABS:
o = new NDArrayWritable(Transforms.abs(i, DUP));
break;
case ACOS:
o = new NDArrayWritable(Transforms.acos(i, DUP));
break;
case ASIN:
o = new NDArrayWritable(Transforms.asin(i, DUP));
break;
case ATAN:
o = new NDArrayWritable(Transforms.atan(i, DUP));
break;
case CEIL:
o = new NDArrayWritable(Transforms.ceil(i, DUP));
break;
case COS:
o = new NDArrayWritable(Transforms.cos(i, DUP));
break;
case COSH:
//No cosh operation in ND4J
throw new UnsupportedOperationException("sinh operation not yet supported for NDArray columns");
case EXP:
o = new NDArrayWritable(Transforms.exp(i, DUP));
break;
case FLOOR:
o = new NDArrayWritable(Transforms.floor(i, DUP));
break;
case LOG:
o = new NDArrayWritable(Transforms.log(i, DUP));
break;
case LOG10:
o = new NDArrayWritable(Transforms.log(i, 10.0, DUP));
break;
case SIGNUM:
o = new NDArrayWritable(Transforms.sign(i, DUP));
break;
case SIN:
o = new NDArrayWritable(Transforms.sin(i, DUP));
break;
case SINH:
//No sinh op in ND4J
throw new UnsupportedOperationException("sinh operation not yet supported for NDArray columns");
case SQRT:
o = new NDArrayWritable(Transforms.sqrt(i, DUP));
break;
case TAN:
//No tan op in ND4J yet - but tan(x) = sin(x)/cos(x)
INDArray sinx = Transforms.sin(i, true);
INDArray cosx = Transforms.cos(i, true);
o = new NDArrayWritable(sinx.divi(cosx));
break;
case TANH:
o = new NDArrayWritable(Transforms.tanh(i, DUP));
break;
default:
throw new RuntimeException("Unknown function: " + mathFunction);
}
//To avoid threading issues...
Nd4j.getExecutioner().commit();
return o;
}
示例5: runOp
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public void runOp() {
Transforms.abs(arr);
// ops.execTransform(0,arr.data().asNioFloat(),arr.shapeInfo(),arr.data().asNioFloat(),arr.shapeInfo(),null,arr.length(),indexes,indexes);
//ops.execTransform(0,arr.data().asNioFloat(),1,arr.data().asNioFloat(),1,null,arr.length());
}
示例6: testBruteForce
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@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
*/
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 = featureSet.dup().getColumns(new int[] {0});
DataSet sampleDataSet = new DataSet(featureSet, labelSet);
double meanNaturalNums = (nSamples + 1) / 2.0;
INDArray theoreticalMean =
Nd4j.create(new double[] {meanNaturalNums * x, meanNaturalNums * y, meanNaturalNums * z});
INDArray theoreticallabelMean = theoreticalMean.dup().getColumns(new int[] {0});
double stdNaturalNums = Math.sqrt((nSamples * nSamples - 1) / 12.0);
INDArray theoreticalStd =
Nd4j.create(new double[] {stdNaturalNums * x, stdNaturalNums * y, stdNaturalNums * z});
INDArray theoreticallabelStd = theoreticalStd.dup().getColumns(new int[] {0});
NormalizerStandardize myNormalizer = new NormalizerStandardize();
myNormalizer.fitLabel(true);
myNormalizer.fit(sampleDataSet);
INDArray meanDelta = Transforms.abs(theoreticalMean.sub(myNormalizer.getMean()));
INDArray labelDelta = Transforms.abs(theoreticallabelMean.sub(myNormalizer.getLabelMean()));
INDArray meanDeltaPerc = meanDelta.div(theoreticalMean).mul(100);
INDArray labelDeltaPerc = labelDelta.div(theoreticallabelMean).mul(100);
double maxMeanDeltaPerc = meanDeltaPerc.max(1).getDouble(0, 0);
assertTrue(maxMeanDeltaPerc < tolerancePerc);
assertTrue(labelDeltaPerc.max(1).getDouble(0, 0) < tolerancePerc);
INDArray stdDelta = Transforms.abs(theoreticalStd.sub(myNormalizer.getStd()));
INDArray stdDeltaPerc = stdDelta.div(theoreticalStd).mul(100);
INDArray stdlabelDeltaPerc =
Transforms.abs(theoreticallabelStd.sub(myNormalizer.getLabelStd())).div(theoreticallabelStd);
double maxStdDeltaPerc = stdDeltaPerc.max(1).mul(100).getDouble(0, 0);
double maxlabelStdDeltaPerc = stdlabelDeltaPerc.max(1).getDouble(0, 0);
assertTrue(maxStdDeltaPerc < tolerancePerc);
assertTrue(maxlabelStdDeltaPerc < tolerancePerc);
// SAME TEST WITH THE ITERATOR
int bSize = 10;
tolerancePerc = 0.1; // 1% of correct value
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(theoreticalMean.sub(myNormalizer.getMean()));
stdDeltaPerc = stdDelta.div(theoreticalStd).mul(100);
maxStdDeltaPerc = stdDeltaPerc.max(1).getDouble(0, 0);
assertTrue(maxStdDeltaPerc < tolerancePerc);
}
示例7: testTransform
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的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);
}
}
示例8: testBruteForce
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的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++;
}
}
示例9: testBruteForce
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@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
*/
double tolerancePerc = 0.01; // 0.01% of correct value
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;
tolerancePerc = 0.1; // 0.1% of correct value
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);
}
示例10: testTransform
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的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);
}
}
示例11: noOp
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public INDArray noOp() {
return Transforms.abs(x());
}