private static long[] calculatePermutedShape(DoubleVertex inputVertex, int... rearrange) { long[] shape = inputVertex.getShape(); long[] permutedShape = new long[shape.length]; for (int i = 0; i < shape.length; i++) { permutedShape[i] = shape[rearrange[i]]; } return permutedShape; }
@Override public MultiplicationVertex times(DoubleVertex that) { return multiply(that); }
@Override public DivisionVertex div(DoubleVertex that) { return divideBy(that); }
public static void main(String[] args) { //%%SNIPPET_START%% Overview DoubleVertex x = new UniformVertex(1, 2); DoubleVertex y = x.times(2); DoubleVertex observedY = new UniformVertex(new long[]{1, 2}, y, y.plus(0.5)); observedY.observe(new double[]{4.0, 4.49}); //%%SNIPPET_END%% Overview } }
@Test(expected = SingularMatrixException.class) public void differentiationFailsWhenMatrixIsSingular() { final long[] shape = new long[]{2, 2}; final DoubleVertex input = new UniformVertex(shape, 0, 10); input.setValue(DoubleTensor.create(new double[]{0, 0, 0, 0}, shape)); final DoubleVertex output = input.matrixDeterminant(); Differentiator.reverseModeAutoDiff(output, input); }
public DoubleVertexSamples generateGaussianSamples(double mu, double sigma, int nSamples) { DoubleVertex gaussian = new GaussianVertex(mu, sigma); List<DoubleTensor> samples = new ArrayList<>(nSamples); for (int i = 0; i < nSamples; i++) { samples.add(gaussian.sample()); } return new DoubleVertexSamples(samples); }
public static PartialsOf reverseModeAutoDiff(Vertex ofVertex, Set<DoubleVertex> wrt) { if (ofVertex.isObserved()) { return new PartialsOf(ofVertex, Collections.emptyMap()); } else { return reverseModeAutoDiff(ofVertex, Differentiable.withRespectToSelf(ofVertex.getShape()), wrt); } }
@Override public void observeValues(DoubleTensor input, OUTPUT output) { xVertex.setValue(input); yObservationVertex.observe(output); }
@Override public BooleanTensor sampleWithShape(long[] shape, KeanuRandom random) { return Bernoulli.withParameters(probTrue.getValue()).sample(shape, random); } }
default PartialDerivative forwardModeAutoDifferentiation(Map<Vertex, PartialDerivative> derivativeOfParentsWithRespectToInput) { if (((Vertex) this).isObserved()) { return PartialDerivative.EMPTY; } else { return withRespectToSelf(((Vertex) this).getShape()); } }
/** * A vertex that performs a user defined operation on a single input vertex * * @param inputVertex the input vertex */ public DoubleUnaryOpVertex(DoubleVertex inputVertex) { this(inputVertex.getShape(), inputVertex); }
@Override public MultiplicationVertex unaryMinus() { return multiply(-1.0); }
@Override public DivisionVertex div(double that) { return divideBy(that); }
/** * One to one constructor for mapping some shape of mu and sigma to * a matching shaped Smooth Uniform. * * @param xMin the xMin of the Smooth Uniform with either the same shape as specified for this vertex or a scalar * @param xMax the xMax of the Smooth Uniform with either the same shape as specified for this vertex or a scalar * @param edgeSharpness the edge sharpness of the Smooth Uniform */ public SmoothUniformVertex(DoubleVertex xMin, DoubleVertex xMax, double edgeSharpness) { this(checkHasOneNonLengthOneShapeOrAllLengthOne(xMin.getShape(), xMax.getShape()), xMin, xMax, edgeSharpness); }
@Override public MultiplicationVertex times(double that) { return multiply(that); }
/** * One to one constructor for mapping some shape of mu to * a matching shaped Poisson. * * @param mu mu with same shape as desired Poisson tensor or scalar */ @ExportVertexToPythonBindings public PoissonVertex(DoubleVertex mu) { this(mu.getShape(), mu); }
/** * One to one constructor for mapping some shape of probTrue to * a matching shaped Bernoulli. * * @param probTrue probTrue with same shape as desired Bernoulli tensor or scalar */ @ExportVertexToPythonBindings public BernoulliVertex(DoubleVertex probTrue) { this(probTrue.getShape(), probTrue); }
/** * Matches a vector of concentration values to a Dirichlet distribution * * @param concentration the concentration values of the dirichlet */ @ExportVertexToPythonBindings public DirichletVertex(DoubleVertex concentration) { this(concentration.getShape(), concentration); }