use of org.nd4j.linalg.api.ops.random.impl.GaussianDistribution in project nd4j by deeplearning4j.
the class NormalDistribution method sample.
@Override
public INDArray sample(INDArray ret) {
if (random.getStatePointer() != null) {
if (means != null) {
return Nd4j.getExecutioner().exec(new GaussianDistribution(ret, means, standardDeviation), random);
} else {
return Nd4j.getExecutioner().exec(new GaussianDistribution(ret, mean, standardDeviation), random);
}
} else {
// For consistent values irrespective of c vs. fortran ordering
Iterator<int[]> idxIter = new NdIndexIterator(ret.shape());
int len = ret.length();
if (means != null) {
for (int i = 0; i < len; i++) {
int[] idx = idxIter.next();
ret.putScalar(idx, standardDeviation * random.nextGaussian() + means.getDouble(idx));
}
} else {
for (int i = 0; i < len; i++) {
ret.putScalar(idxIter.next(), standardDeviation * random.nextGaussian() + mean);
}
}
return ret;
}
}
use of org.nd4j.linalg.api.ops.random.impl.GaussianDistribution in project nd4j by deeplearning4j.
the class NativeRandom method nextGaussian.
@Override
public INDArray nextGaussian(char order, int[] shape) {
INDArray array = Nd4j.createUninitialized(shape, order);
GaussianDistribution op = new GaussianDistribution(array, 0.0, 1.0);
Nd4j.getExecutioner().exec(op, this);
return array;
}
use of org.nd4j.linalg.api.ops.random.impl.GaussianDistribution in project nd4j by deeplearning4j.
the class OrthogonalDistribution method sample.
@Override
public INDArray sample(int[] shape) {
int numRows = 1;
for (int i = 0; i < shape.length - 1; i++) numRows *= shape[i];
int numCols = shape[shape.length - 1];
val flatShape = new int[] { numRows, numCols };
val flatRng = Nd4j.getExecutioner().exec(new GaussianDistribution(Nd4j.createUninitialized(flatShape, Nd4j.order()), 0.0, 1.0), random);
int m = flatRng.rows();
int n = flatRng.columns();
val s = Nd4j.create(m < n ? m : n);
val u = m < n ? Nd4j.create(m, n) : Nd4j.create(m, m);
val v = Nd4j.create(n, n, 'f');
Nd4j.getBlasWrapper().lapack().gesvd(flatRng, s, u, v);
if (gains == null) {
if (u.rows() == numRows && u.columns() == numCols) {
return v.get(NDArrayIndex.interval(0, numRows), NDArrayIndex.interval(0, numCols)).mul(gain).reshape(shape);
} else {
return u.get(NDArrayIndex.interval(0, numRows), NDArrayIndex.interval(0, numCols)).mul(gain).reshape(shape);
}
} else {
throw new UnsupportedOperationException();
}
}
use of org.nd4j.linalg.api.ops.random.impl.GaussianDistribution in project nd4j by deeplearning4j.
the class RandomProjection method gaussianRandomMatrix.
/**
* Generate a dense Gaussian random matrix.
*
* The n' components of the random matrix are drawn from
* N(0, 1.0 / n').
*
* @param shape
* @param rng
* @return
*/
private INDArray gaussianRandomMatrix(int[] shape, Random rng) {
Nd4j.checkShapeValues(shape);
INDArray res = Nd4j.create(shape);
GaussianDistribution op1 = new GaussianDistribution(res, 0.0, 1.0 / Math.sqrt(shape[0]));
Nd4j.getExecutioner().exec(op1, rng);
return res;
}
Aggregations