add3D function
///////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////
Implementation
// 3D TENSOR OPERATIONS
////////////////////////////////////////////////////////////////////////////////
Tensor<Tensor3D> add3D(Tensor<Tensor3D> a, Tensor<Tensor3D> b) {
int depth = a.value.length;
int height = a.value[0].length;
int width = a.value[0][0].length;
Tensor3D outValue = [];
for (int d = 0; d < depth; d++) {
Matrix matrix = [];
for (int h = 0; h < height; h++) {
Vector row = [];
for (int w = 0; w < width; w++) {
row.add(a.value[d][h][w] + b.value[d][h][w]);
}
matrix.add(row);
}
outValue.add(matrix);
}
Tensor<Tensor3D> out = Tensor<Tensor3D>(outValue);
out.creator = Node(
[a, b],
() {
for (int d = 0; d < depth; d++) {
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
a.grad[d][h][w] += out.grad[d][h][w];
b.grad[d][h][w] += out.grad[d][h][w];
}
}
}
},
opName: 'add_3d', // <-- Renamed for clarity
cost: depth * height * width,
);
return out;
}