backward method
Starting point of gradient calculation. The network of Nodes and Tensors traverses in topological order over all Tensors affecting this Tensor adding a given gradient, calculated via their creator.
The value of startingGrad affects the first gradient given into the backwards process and is set to 1.0 by default.
Implementation
void backward({double startingGrad = 1.0}) {
for (int i = 0; i < grad.length; i = i + 1) {
grad[i] = startingGrad;
}
if (creator == null) return;
List<Node> topo = [];
Set<Node> visited = {};
void buildTopo(Node? node) {
if (node == null || visited.contains(node)) return;
visited.add(node);
for (int i = 0; i < node.inputs.length; i = i + 1) {
Tensor inputTensor = node.inputs[i];
if (inputTensor.creator != null) {
buildTopo(inputTensor.creator);
}
}
topo.add(node);
}
buildTopo(creator);
for (int i = topo.length - 1; i >= 0; i = i - 1) {
topo[i].backwardFn();
}
}