/** * Create a discrete real-valued distribution using the given random number generator * and probability mass function enumeration. * * @param rng random number generator. * @param singletons array of random variable values. * @param probabilities array of probabilities. * @throws DimensionMismatchException if * {@code singletons.length != probabilities.length} * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedRealDistribution(final RandomGenerator rng, final double[] singletons, final double[] probabilities) throws DimensionMismatchException, NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { super(rng); innerDistribution = new EnumeratedDistribution<Double>( rng, createDistribution(singletons, probabilities)); }
/** * Create a discrete distribution using the given random number generator * and probability mass function definition. * * @param rng random number generator. * @param singletons array of random variable values. * @param probabilities array of probabilities. * @throws DimensionMismatchException if * {@code singletons.length != probabilities.length} * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedIntegerDistribution(final RandomGenerator rng, final int[] singletons, final double[] probabilities) throws DimensionMismatchException, NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { super(rng); innerDistribution = new EnumeratedDistribution<Integer>( rng, createDistribution(singletons, probabilities)); }
/** * Create a discrete real-valued distribution from the input data. Values are assigned * mass based on their frequency. * * @param rng random number generator used for sampling * @param data input dataset * @since 3.6 */ public EnumeratedRealDistribution(final RandomGenerator rng, final double[] data) { super(rng); final Map<Double, Integer> dataMap = new HashMap<Double, Integer>(); for (double value : data) { Integer count = dataMap.get(value); if (count == null) { count = 0; } dataMap.put(value, ++count); } final int massPoints = dataMap.size(); final double denom = data.length; final double[] values = new double[massPoints]; final double[] probabilities = new double[massPoints]; int index = 0; for (Entry<Double, Integer> entry : dataMap.entrySet()) { values[index] = entry.getKey(); probabilities[index] = entry.getValue().intValue() / denom; index++; } innerDistribution = new EnumeratedDistribution<Double>(rng, createDistribution(values, probabilities)); }
/** * Create a discrete integer-valued distribution from the input data. Values are assigned * mass based on their frequency. * * @param rng random number generator used for sampling * @param data input dataset * @since 3.6 */ public EnumeratedIntegerDistribution(final RandomGenerator rng, final int[] data) { super(rng); final Map<Integer, Integer> dataMap = new HashMap<Integer, Integer>(); for (int value : data) { Integer count = dataMap.get(value); if (count == null) { count = 0; } dataMap.put(value, ++count); } final int massPoints = dataMap.size(); final double denom = data.length; final int[] values = new int[massPoints]; final double[] probabilities = new double[massPoints]; int index = 0; for (Entry<Integer, Integer> entry : dataMap.entrySet()) { values[index] = entry.getKey(); probabilities[index] = entry.getValue().intValue() / denom; index++; } innerDistribution = new EnumeratedDistribution<Integer>(rng, createDistribution(values, probabilities)); }
private DiscreteDistribution(List<DistributionEntry<T>> distribution) { enumeratedDistribution = new EnumeratedDistribution<>(distribution.stream() .map(DistributionEntry::toPair) .collect(Guavate.toImmutableList())); }
/** * Constructs discrete weighted value with specified <code>values</code> and <code>weights</code>. * * @param weightedValuePairs List of values with their corresponding weights. */ public WeightedValue(List<WeightedValuePair<T>> weightedValuePairs) { if (weightedValuePairs == null || weightedValuePairs.isEmpty()) { throw new IllegalArgumentException("List of weighted values cannot be null nor empty."); } this.enumeratedDistribution = new EnumeratedDistribution<>(mapToPairList(weightedValuePairs)); this.values = weightedValuePairs.stream().map(x -> x.getValue()).collect(Collectors.toList()); }
/** * Constructs discrete weighted value with specified <code>values</code> and <code>weights</code>. * * @param weightedValuePairs List of values with their corresponding weights. */ public WeightedValue(List<WeightedValuePair<T>> weightedValuePairs) { if (weightedValuePairs == null || weightedValuePairs.isEmpty()) { throw new IllegalArgumentException("List of weighted values cannot be null nor empty."); } this.enumeratedDistribution = new EnumeratedDistribution<>(mapToPairList(weightedValuePairs)); this.values = weightedValuePairs.stream().map(x -> x.getValue()).collect(Collectors.toList()); }
/** * Create a discrete real-valued distribution using the given random number generator * and probability mass function enumeration. * * @param rng random number generator. * @param singletons array of random variable values. * @param probabilities array of probabilities. * @throws DimensionMismatchException if * {@code singletons.length != probabilities.length} * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedRealDistribution(final RandomGenerator rng, final double[] singletons, final double[] probabilities) throws DimensionMismatchException, NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { super(rng); innerDistribution = new EnumeratedDistribution<Double>( rng, createDistribution(singletons, probabilities)); }
/** * Create a discrete distribution using the given random number generator * and probability mass function definition. * * @param rng random number generator. * @param singletons array of random variable values. * @param probabilities array of probabilities. * @throws DimensionMismatchException if * {@code singletons.length != probabilities.length} * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedIntegerDistribution(final RandomGenerator rng, final int[] singletons, final double[] probabilities) throws DimensionMismatchException, NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { super(rng); innerDistribution = new EnumeratedDistribution<Integer>( rng, createDistribution(singletons, probabilities)); }
/** * Create a discrete real-valued distribution using the given random number generator * and probability mass function enumeration. * * @param rng random number generator. * @param singletons array of random variable values. * @param probabilities array of probabilities. * @throws DimensionMismatchException if * {@code singletons.length != probabilities.length} * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedRealDistribution(final RandomGenerator rng, final double[] singletons, final double[] probabilities) throws DimensionMismatchException, NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { super(rng); innerDistribution = new EnumeratedDistribution<Double>( rng, createDistribution(singletons, probabilities)); }
/** * Create a discrete distribution using the given random number generator * and probability mass function definition. * * @param rng random number generator. * @param singletons array of random variable values. * @param probabilities array of probabilities. * @throws DimensionMismatchException if * {@code singletons.length != probabilities.length} * @throws NotPositiveException if any of the probabilities are negative. * @throws NotFiniteNumberException if any of the probabilities are infinite. * @throws NotANumberException if any of the probabilities are NaN. * @throws MathArithmeticException all of the probabilities are 0. */ public EnumeratedIntegerDistribution(final RandomGenerator rng, final int[] singletons, final double[] probabilities) throws DimensionMismatchException, NotPositiveException, MathArithmeticException, NotFiniteNumberException, NotANumberException { super(rng); innerDistribution = new EnumeratedDistribution<Integer>( rng, createDistribution(singletons, probabilities)); }
/** * Create a discrete real-valued distribution from the input data. Values are assigned * mass based on their frequency. * * @param rng random number generator used for sampling * @param data input dataset * @since 3.6 */ public EnumeratedRealDistribution(final RandomGenerator rng, final double[] data) { super(rng); final Map<Double, Integer> dataMap = new HashMap<Double, Integer>(); for (double value : data) { Integer count = dataMap.get(value); if (count == null) { count = 0; } dataMap.put(value, ++count); } final int massPoints = dataMap.size(); final double denom = data.length; final double[] values = new double[massPoints]; final double[] probabilities = new double[massPoints]; int index = 0; for (Entry<Double, Integer> entry : dataMap.entrySet()) { values[index] = entry.getKey(); probabilities[index] = entry.getValue().intValue() / denom; index++; } innerDistribution = new EnumeratedDistribution<Double>(rng, createDistribution(values, probabilities)); }
/** * Create a discrete integer-valued distribution from the input data. Values are assigned * mass based on their frequency. * * @param rng random number generator used for sampling * @param data input dataset * @since 3.6 */ public EnumeratedIntegerDistribution(final RandomGenerator rng, final int[] data) { super(rng); final Map<Integer, Integer> dataMap = new HashMap<Integer, Integer>(); for (int value : data) { Integer count = dataMap.get(value); if (count == null) { count = 0; } dataMap.put(value, ++count); } final int massPoints = dataMap.size(); final double denom = data.length; final int[] values = new int[massPoints]; final double[] probabilities = new double[massPoints]; int index = 0; for (Entry<Integer, Integer> entry : dataMap.entrySet()) { values[index] = entry.getKey(); probabilities[index] = entry.getValue().intValue() / denom; index++; } innerDistribution = new EnumeratedDistribution<Integer>(rng, createDistribution(values, probabilities)); }
/** * Create a discrete integer-valued distribution from the input data. Values are assigned * mass based on their frequency. * * @param rng random number generator used for sampling * @param data input dataset * @since 3.6 */ public EnumeratedIntegerDistribution(final RandomGenerator rng, final int[] data) { super(rng); final Map<Integer, Integer> dataMap = new HashMap<Integer, Integer>(); for (int value : data) { Integer count = dataMap.get(value); if (count == null) { count = 0; } dataMap.put(value, ++count); } final int massPoints = dataMap.size(); final double denom = data.length; final int[] values = new int[massPoints]; final double[] probabilities = new double[massPoints]; int index = 0; for (Entry<Integer, Integer> entry : dataMap.entrySet()) { values[index] = entry.getKey(); probabilities[index] = entry.getValue().intValue() / denom; index++; } innerDistribution = new EnumeratedDistribution<Integer>(rng, createDistribution(values, probabilities)); }
/** * Create a discrete real-valued distribution from the input data. Values are assigned * mass based on their frequency. * * @param rng random number generator used for sampling * @param data input dataset * @since 3.6 */ public EnumeratedRealDistribution(final RandomGenerator rng, final double[] data) { super(rng); final Map<Double, Integer> dataMap = new HashMap<Double, Integer>(); for (double value : data) { Integer count = dataMap.get(value); if (count == null) { count = 0; } dataMap.put(value, ++count); } final int massPoints = dataMap.size(); final double denom = data.length; final double[] values = new double[massPoints]; final double[] probabilities = new double[massPoints]; int index = 0; for (Entry<Double, Integer> entry : dataMap.entrySet()) { values[index] = entry.getKey(); probabilities[index] = entry.getValue().intValue() / denom; index++; } innerDistribution = new EnumeratedDistribution<Double>(rng, createDistribution(values, probabilities)); }
/** * sample a sequence of integers from the model, using current (hPrev) memory state, seedIx is seed letter for first time step */ public String sample(int seedIx) { INDArray x = Nd4j.zeros(vocabSize, 1); x.putScalar(seedIx, 1); int sampleSize = 144; INDArray ixes = Nd4j.create(sampleSize); INDArray h = hPrev.dup(); for (int t = 0; t < sampleSize; t++) { h = Transforms.tanh(wxh.mmul(x).add(whh.mmul(h)).add(bh)); INDArray y = (why.mmul(h)).add(by); INDArray pm = Nd4j.getExecutioner().execAndReturn(new OldSoftMax(y)).ravel(); List<Pair<Integer, Double>> d = new LinkedList<>(); for (int pi = 0; pi < vocabSize; pi++) { d.add(new Pair<>(pi, pm.getDouble(0, pi))); } try { EnumeratedDistribution<Integer> distribution = new EnumeratedDistribution<>(d); int ix = distribution.sample(); x = Nd4j.zeros(vocabSize, 1); x.putScalar(ix, 1); ixes.putScalar(t, ix); } catch (Exception e) { } } return getSampleString(ixes); }
EnumeratedDistribution<Integer> distribution = new EnumeratedDistribution<>(d);