addVector function
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
Implementation
// VECTOR (1D) OPERATIONS
////////////////////////////////////////////////////////////////////////////////
// Assuming your Tensor, Node, and type aliases are defined.
Tensor<Vector> addVector(Tensor<Vector> a, Tensor<Vector> b) {
int N = a.value.length;
Vector outValue = [];
for (int i = 0; i < N; i++) {
outValue.add(a.value[i] + b.value[i]);
}
Tensor<Vector> out = Tensor<Vector>(outValue);
out.creator = Node(
[a, b],
() {
for (int i = 0; i < a.value.length; i++) {
a.grad[i] += out.grad[i];
b.grad[i] += out.grad[i];
}
},
opName: 'add_vector', // <-- Renamed for clarity
cost: N,
);
return out;
}