protected void cascadeValues(Map<VariableReference, ?> inputs) { List<Vertex> updatedVertices = new ArrayList<>(); for (Map.Entry<VariableReference, ?> input : inputs.entrySet()) { Vertex updatingVertex = vertexLookup.get(input.getKey()); if (updatingVertex == null) { throw new IllegalArgumentException("Cannot cascade update for input: " + input.getKey()); } updatingVertex.setValue(input.getValue()); updatedVertices.add(updatingVertex); } VertexValuePropagation.cascadeUpdate(updatedVertices); }
/** * This sets the value in this vertex and tells each child vertex about * the new change. This causes a cascading change of values if any of the * children vertices are non-probabilistic vertices (e.g. mathematical operations). * * @param value The new value at this vertex */ public void setAndCascade(T value) { setValue(value); VertexValuePropagation.cascadeUpdate(this); }
private void setOrObserveValue(Vertex vertex, Tensor valueTensor, boolean isObserved) { if (isObserved) { vertex.observe(valueTensor); } else { vertex.setValue(valueTensor); } }
@Override public void onProposalRejected(Proposal proposal) { for (Map.Entry<Vertex, Object> entry : fromValues.entrySet()) { Object oldValue = entry.getValue(); Vertex vertex = entry.getKey(); vertex.setValue(oldValue); } VertexValuePropagation.cascadeUpdate(fromValues.keySet()); } }
private static <T> void setValueFromSample(Vertex<T> vertex, KeanuRandom random) { vertex.setValue(vertex.sample(random)); }
public void setValue(double value) { super.setValue(DoubleTensor.scalar(value)); }
public void setValue(double[] values) { super.setValue(DoubleTensor.create(values)); }
public void setValue(int[] values) { super.setValue(IntegerTensor.create(values)); }
public void setValue(boolean value) { super.setValue(BooleanTensor.scalar(value)); }
public void setValue(boolean[] values) { super.setValue(BooleanTensor.create(values)); }
public void setValue(int value) { super.setValue(IntegerTensor.scalar(value)); }
public void setState(NetworkState state) { for (VariableReference reference : state.getVariableReferences()) { this.vertices.stream() .filter(v -> v.getId() == reference) .forEach(v -> v.setValue(state.get(reference))); } }
public static <T> void feedValue(LogProbGraph logProbGraph, Vertex<T> input, T value) { Vertex<T> placeholderVertex = logProbGraph.getPlaceholder(input); placeholderVertex.setValue(value); }
@Test public void itRestoresTheValueOfAnUnobservedVertex() { DoubleTensor originalValue = DoubleTensor.create(1., 2., 3.); DoubleTensor otherValue = DoubleTensor.create(4., 5., 6.); Vertex vertex = new GaussianVertex(1., 0.); vertex.setValue(originalValue); NetworkSnapshot snapshot = NetworkSnapshot.create(ImmutableSet.of(vertex)); vertex.setValue(otherValue); assertThat(vertex.getValue(), equalTo(otherValue)); assertThat(vertex.isObserved(), is(false)); snapshot.apply(); assertThat(vertex.getValue(), equalTo(originalValue)); assertThat(vertex.isObserved(), is(false)); }
@Test public void itRestoresTheObservedStatusOfAnObservedVertex() { DoubleTensor originalValue = DoubleTensor.create(1., 2., 3.); DoubleTensor otherValue = DoubleTensor.create(4., 5., 6.); Vertex vertex = new GaussianVertex(1., 0.); vertex.observe(originalValue); assertThat(vertex.getValue(), equalTo(originalValue)); assertThat(vertex.isObserved(), is(true)); NetworkSnapshot snapshot = NetworkSnapshot.create(ImmutableSet.of(vertex)); vertex.unobserve(); assertThat(vertex.getValue(), equalTo(originalValue)); assertThat(vertex.isObserved(), is(false)); vertex.setValue(otherValue); assertThat(vertex.getValue(), equalTo(otherValue)); assertThat(vertex.isObserved(), is(false)); snapshot.apply(); assertThat(vertex.getValue(), equalTo(originalValue)); assertThat(vertex.isObserved(), is(true)); } }