PlaceholderVertex#getValue or #sample throws an IllegalStateException when no value has been set to it yet.
@@ -34,7 +34,7 @@ public class LogProbGraph {
return (Vertex<T>) inputs.get(input);
}
- static public class DoublePlaceholderVertex extends DoubleVertex implements PlaceholderVertex, NonProbabilistic<DoubleTensor>, Differentiable, NonSaveableVertex {
+ static public class DoublePlaceholderVertex extends DoubleVertex implements PlaceholderVertex<DoubleTensor>, NonProbabilistic<DoubleTensor>, Differentiable, NonSaveableVertex {
public DoublePlaceholderVertex(long[] initialShape) {
super(initialShape);
@@ -42,15 +42,21 @@ public class LogProbGraph {
@Override
public DoubleTensor calculate() {
- return this.getValue();
+ return getPlaceholderVertexValue(this);
}
@Override
public DoubleTensor sample(KeanuRandom random) {
- return this.getValue();
+ return getPlaceholderVertexValue(this);
}
}
- private interface PlaceholderVertex {
+ private interface PlaceholderVertex<T> {
+ default T getPlaceholderVertexValue(Vertex<T> vertex) {
+ if (!vertex.hasValue()) {
+ throw new IllegalStateException("Cannot calculate a PlaceholderVertex with no value.");
+ }
+ return vertex.getValue();
+ }
}
}
