Refine search
public static List<Pair<INDArray, String>> get3dPermutedWithShape(long seed, long... shape) { Nd4j.getRandom().setSeed(seed); long[] createdShape = {shape[1], shape[2], shape[0]}; int lencreatedShape = ArrayUtil.prod(createdShape); INDArray arr = Nd4j.linspace(1, lencreatedShape, lencreatedShape).reshape(createdShape); INDArray permuted = arr.permute(2, 0, 1); return Collections.singletonList(new Pair<>(permuted, "get3dPermutedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
@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 { Iterator<long[]> idxIter = new NdIndexIterator(ret.shape()); //For consistent values irrespective of c vs. fortran ordering long len = ret.length(); if (means != null) { for (int i = 0; i < len; i++) { long[] 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; } } }
/** * Create a random ndarray with the given shape and order * * @param shape the shape of the ndarray * @return the random ndarray with the specified shape */ @Override public INDArray rand(char order, int[] shape) { return Nd4j.getRandom().nextDouble(order, shape); }
public static long[] broadcastToShape(long[] inputShapeWithOnes, long seed) { Nd4j.getRandom().setSeed(seed); val shape = new long[inputShapeWithOnes.length]; for (int i = 0; i < shape.length; i++) { if (inputShapeWithOnes[i] == 1) { shape[i] = Nd4j.getRandom().nextInt(9) + 1; } else shape[i] = inputShapeWithOnes[i]; } return shape; }
/** * This method returns new onject implementing Random interface, initialized with seed value * * @param seed seed for this rng object * @return object implementing Random interface */ public Random getNewRandomInstance(long seed) { try { Random t = (Random) randomClass.newInstance(); if (t.getStatePointer() != null) { // TODO: attach this thing to deallocator // if it's stateless random - we just don't care then } t.setSeed(seed); return t; } catch (Exception e) { throw new RuntimeException(e); } }
@Override public INDArray sample(int[] shape) { if (random.getStatePointer() != null) { if (means != null) { return Nd4j.getExecutioner().exec(new GaussianDistribution( Nd4j.createUninitialized(shape, Nd4j.order()), means, standardDeviation), random); } else { return Nd4j.getExecutioner().exec(new GaussianDistribution( Nd4j.createUninitialized(shape, Nd4j.order()), mean, standardDeviation), random); } } else { INDArray ret = Nd4j.createUninitialized(shape, Nd4j.order()); Iterator<int[]> idxIter = new NdIndexIterator(shape); //For consistent values irrespective of c vs. fortran ordering 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; } } }
@Override public INDArray sample(INDArray ret) { if (random.getStatePointer() != null) { if (p != null) { return Nd4j.getExecutioner() .exec(new org.nd4j.linalg.api.ops.random.impl.BinomialDistributionEx( ret, numberOfTrials, p), random); } else { return Nd4j.getExecutioner() .exec(new org.nd4j.linalg.api.ops.random.impl.BinomialDistributionEx( ret, numberOfTrials, probabilityOfSuccess), random); Iterator<long[]> idxIter = new NdIndexIterator(ret.shape()); //For consistent values irrespective of c vs. fortran ordering long len = ret.length(); if (p != null) { for (int i = 0; i < len; i++) { org.apache.commons.math3.distribution.BinomialDistribution binomialDistribution = new org.apache.commons.math3.distribution.BinomialDistribution( (RandomGenerator) Nd4j.getRandom(), numberOfTrials, p.getDouble(idx)); ret.putScalar(idx, binomialDistribution.sample());
switch (values){ case RANDOM_UNIFORM: return Nd4j.rand(Nd4j.createUninitialized(shape,order)); case RANDOM_NORMAL: return Nd4j.randn(Nd4j.createUninitialized(shape,order)); case ONE_HOT: Random r = new Random(Nd4j.getRandom().nextLong()); INDArray out = Nd4j.create(shape,order); if(shape.length == 1){ out.putScalar(r.nextInt((int) shape[0]), 1.0); } else if(shape.length == 2){ for( int i=0; i<shape[0]; i++ ){ out.putScalar(i, r.nextInt((int) shape[1]), 1.0); out.putScalar(i, r.nextInt((int) shape[1]), j, 1.0); return Nd4j.createUninitialized(shape,order).assign(1.0); case BINARY: return Nd4j.getExecutioner().exec(new BernoulliDistribution(Nd4j.createUninitialized(shape, order), 0.5)); case INTEGER_0_10: return Transforms.floor(Nd4j.rand(shape).muli(10), false);
@Override public INDArray sample(INDArray ret) { if (random.getStatePointer() != null) { return Nd4j.getExecutioner().exec(new org.nd4j.linalg.api.ops.random.impl.UniformDistribution( ret, lower, upper), random); } else { val idxIter = new NdIndexIterator(ret.shape()); //For consistent values irrespective of c vs. fortran ordering long len = ret.length(); for (int i = 0; i < len; i++) { ret.putScalar(idxIter.next(), sample()); } return ret; } } }
/** * Create an ndarray * of * @param seed * @param rank * @param numShapes * @return */ public static int[][] getRandomBroadCastShape(long seed, int rank, int numShapes) { Nd4j.getRandom().setSeed(seed); INDArray coinFlip = Nd4j.getDistributions().createBinomial(1, 0.5).sample(new int[] {numShapes, rank}); int[][] ret = new int[(int) coinFlip.rows()][(int) coinFlip.columns()]; for (int i = 0; i < coinFlip.rows(); i++) { for (int j = 0; j < coinFlip.columns(); j++) { int set = coinFlip.getInt(i, j); if (set > 0) ret[i][j] = set; else { //anything from 0 to 9 ret[i][j] = Nd4j.getRandom().nextInt(9) + 1; } } } return ret; }
/** * Create a random ndarray with the given shape using given seed * * @param shape the shape of the ndarray * @param seed the seed to use * @return the random ndarray with the specified shape */ public static INDArray rand(int[] shape, long seed) { INDArray ret = createUninitialized(shape, Nd4j.order());//;INSTANCE.rand(shape, seed); logCreationIfNecessary(ret); Nd4j.getRandom().setSeed(seed); return getExecutioner().exec(new UniformDistribution(ret), Nd4j.getRandom()); }
/** * Sample a dataset * * @param numSamples the number of samples to getFromOrigin * @param rng the rng to use * @param withReplacement whether to allow duplicates (only tracked by example row number) * @return the sample dataset */ @Override public DataSet sample(int numSamples, org.nd4j.linalg.api.rng.Random rng, boolean withReplacement) { INDArray examples = Nd4j.create(numSamples, getFeatures().columns()); INDArray outcomes = Nd4j.create(numSamples, numOutcomes()); Set<Integer> added = new HashSet<>(); for (int i = 0; i < numSamples; i++) { int picked = rng.nextInt(numExamples()); if (!withReplacement) while (added.contains(picked)) picked = rng.nextInt(numExamples()); examples.putRow(i, get(picked).getFeatures()); outcomes.putRow(i, get(picked).getLabels()); } return new DataSet(examples, outcomes); }
@Override public INDArray sample(int[] shape) { if (random.getStatePointer() != null) { return Nd4j.getExecutioner().exec(new org.nd4j.linalg.api.ops.random.impl.UniformDistribution( Nd4j.createUninitialized(shape, Nd4j.order()), lower, upper), random); } else { INDArray ret = Nd4j.createUninitialized(shape, Nd4j.order()); Iterator<int[]> idxIter = new NdIndexIterator(shape); //For consistent values irrespective of c vs. fortran ordering int len = ret.length(); for (int i = 0; i < len; i++) { ret.putScalar(idxIter.next(), sample()); } return ret; } } }
final LayerWorkspaceMgr workspaceMgr) { boolean is2dInput = input.rank() < 3; //Edge case of T=1, may have shape [m,nIn], equiv. to [m,nIn,1] val timeSeriesLength = (is2dInput ? 1 : input.size(2)); val hiddenLayerSize = recurrentWeights.size(0); val miniBatchSize = input.size(0); val inputLayerSize = input.size(1); INDArray finalMemCellState = Nd4j.createUninitialized( new long[] {/*numLayers * (bidirectional ? 2 : 1),*/ miniBatchSize, hiddenLayerSize}, 'c'); INDArray finalStepActivations = Nd4j.createUninitialized( new long[] {/*numLayers * (bidirectional ? 2 : 1),*/ miniBatchSize, hiddenLayerSize}, 'c'); toReturn.prevMemCell = prevMemCell; Nd4j.getExecutioner().commit(); Nd4j.getRandom().getSeed()));
/** * Apply dropout to the given input * and return the drop out mask used * @param input the input to do drop out on * @param dropout the drop out probability */ public static void applyDropout(INDArray input, double dropout) { if (Nd4j.getRandom().getStatePointer() != null) { Nd4j.getExecutioner().exec(new DropOutInverted(input, dropout)); } else { Nd4j.getExecutioner().exec(new LegacyDropOutInverted(input, dropout)); } }
/** * Random normal using the specified seed * * @param shape the shape of the ndarray * @return */ public static INDArray randn(int[] shape, long seed) { Nd4j.getRandom().setSeed(seed); Nd4j.getExecutioner().exec(new GaussianDistribution(Nd4j.createUninitialized(shape, order())), Nd4j.getRandom()); return randn(shape, Nd4j.getRandom()); }
public static List<Pair<INDArray, String>> get6dReshapedWithShape(int seed, int... shape) { Nd4j.getRandom().setSeed(seed); int[] shape3d = {shape[0] * shape[2], shape[4] * shape[5], shape[1] * shape[3]}; INDArray array3d = Nd4j.rand(shape3d); INDArray array6d = array3d.reshape(ArrayUtil.toLongArray(shape)); return Collections.singletonList(new Pair<>(array6d, "get6dReshapedWithShape(" + seed + "," + Arrays.toString(shape) + ").get(0)")); }
/** * Fill the given ndarray with random numbers drawn from a normal distribution * * @param target target array * @return the given target array */ public static INDArray randn(INDArray target, long seed) { Nd4j.getRandom().setSeed(seed); return getExecutioner().exec(new GaussianDistribution(target), Nd4j.getRandom()); }
int[] inShape = adaptForTensorDescr(ArrayUtil.toInts(input.shape())); int[] inStride = adaptForTensorDescr(ArrayUtil.toInts(input.stride())); checkCudnn(cudnnSetTensorNdDescriptor(cudnnContext.xTensorDesc, dataType, inShape.length, inShape, inStride)); int[] outShape = adaptForTensorDescr(ArrayUtil.toInts(resultArray.shape())); int[] outStride = adaptForTensorDescr(ArrayUtil.toInts(resultArray.stride())); checkCudnn(cudnnSetTensorNdDescriptor(cudnnContext.yTensorDesc, dataType, outShape.length, outShape, outStride)); long seed = Nd4j.getRandom().nextLong(); lastInitializedP = p; checkCudnn(cudnnSetDropoutDescriptor(cudnnContext.dropoutDesc, cudnnContext, p, rngStates, rngStates.capacity(), seed));