CJC Source (struct-based forward pass)
struct Layer {
weights: Tensor,
bias: Tensor
}
fn forward(layer: Layer,
input: Tensor) -> Tensor {
matmul(input, layer.weights)
|> add_bias(layer.bias)
}
// Build layers from weight tensors
let layer1 = Layer {
weights: w1, bias: b1
};
let layer2 = Layer {
weights: w2, bias: b2
};
// Forward pass
let h = forward(layer1, x);
let h_relu = relu_1x4(h);
let out = forward(layer2, h_relu);
// Verify both approaches match
assert_eq(output.get([0,0]),
out.get([0,0]));
assert_eq(output.get([0,1]),
out.get([0,1]));
Live Output — $ cjc run demo3_pipeline.cjc
=== 2-Layer Neural Network Forward Pass ===
Input x:
Tensor(shape=[1, 3], data=[1, 2, 3])
Hidden pre-activation:
Tensor(shape=[1, 4],
data=[0.1, 0.0, 0.75, 1.0])
Hidden post-ReLU:
Tensor(shape=[1, 4],
data=[0.1, 0.0, 0.75, 1.0])
Network output:
Tensor(shape=[1, 2],
data=[0.17, 0.045])
Output shape: [1, 2]
All shape assertions passed!
Target:
Tensor(shape=[1, 2], data=[1, 0])
Output - Target:
Tensor(shape=[1, 2], data=[-0.83, 0.045])
Struct-based forward pass output:
Tensor(shape=[1, 2],
data=[0.17, 0.045])
Struct-based result matches manual!
=== Demo 3 complete ===