|
20 | 20 | package org.neo4j.gds.ml.core; |
21 | 21 |
|
22 | 22 | import org.jetbrains.annotations.TestOnly; |
23 | | -import org.neo4j.gds.ml.core.functions.PassthroughVariable; |
| 23 | +import org.neo4j.gds.ml.core.functions.SingleParentVariable; |
24 | 24 | import org.neo4j.gds.ml.core.tensor.Tensor; |
25 | 25 | import org.neo4j.gds.ml.core.tensor.TensorFactory; |
26 | 26 |
|
@@ -71,7 +71,7 @@ public void backward(Variable<?> function) { |
71 | 71 |
|
72 | 72 | gradients.clear(); |
73 | 73 | Queue<BackPropTask> executionQueue = new LinkedBlockingQueue<>(); |
74 | | - PassthroughVariable<?> dummy = new PassthroughVariable<>(function); |
| 74 | + var dummy = new PassthroughVariable<>(function); |
75 | 75 | executionQueue.add(new BackPropTask(function, dummy)); |
76 | 76 | Map<Variable<?>, AtomicInteger> upstreamCounters = new HashMap<>(); |
77 | 77 | initUpstream(dummy, upstreamCounters); |
@@ -167,4 +167,25 @@ static class BackPropTask { |
167 | 167 | } |
168 | 168 | } |
169 | 169 |
|
| 170 | + private static class PassthroughVariable<T extends Tensor<T>> extends SingleParentVariable<T, T> { |
| 171 | + |
| 172 | + public PassthroughVariable(Variable<T> parent) { |
| 173 | + super(parent, parent.dimensions()); |
| 174 | + |
| 175 | + if (parent instanceof PassthroughVariable) { |
| 176 | + throw new IllegalArgumentException("Redundant use of PassthroughVariables. Chaining does not make sense."); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public T apply(ComputationContext ctx) { |
| 182 | + return ctx.data(parent); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public T gradientForParent(ComputationContext ctx) { |
| 187 | + // initialize gradient computation with `1` |
| 188 | + return ctx.data(parent).map(v -> 1); |
| 189 | + } |
| 190 | + } |
170 | 191 | } |
0 commit comments