apple-0.3.0.0: test/examples/trainMnist.apple
λtrainLabels.λtrainImages.
{
x ⟜ ♭`{3∘[2,3,4]} (trainImages :: Arr (60000 × 28 × 28 × 1) float);
targets ⟜ (λn.[?x=n,.1::float,.0]'irange 0 9 1)'trainLabels;
l1init ← 𝔯 0 1 :: M ₇₈₄,₁₂₈ float;
l2init ← 𝔯 0 1; -- 128×10
train ←
λl1.λl2.
{
softmax ← λxs.
{ m ⟜ (⋉)/* _1 xs; a ⟜ [e:(x-m)]`{0} xs
; sum ← [(+)/x]
; n ⟜ sum`{1} (a::M float)
; ⍉([(%x)'y]`{0,1} n a)
};
dsoftmax ← λxs. [x*(1-x)]`{0} (softmax xs);
dsigmoid ← ((λx.⸎x⟜ℯ(_x);x%(1+x)^2)`{0});
-- fw
xl1p ⟜ x%.l1;
xSigmoid ← [1%(1+ℯ(_x))]`{0} xl1p;
xl2p ⟜ xSigmoid%.l2;
out ← softmax xl2p;
-- bw
error ⟜ (*)`{0,0} ({n⟜ℝ(𝓉out); [2*x%n]`{0} ((-)`{0,0} out targets)}) (dsoftmax xl2p);
ul2 ← (⍉xSigmoid)%.error;
ul1 ← (⍉x)%.((*)`{0,0} (⍉(l2%.(⍉error))) (dsigmoid xl1p));
((+)`{0,0} l1 ul1, (+)`{0,0} l2 ul2)
};
[train (x->1) (x->2)]^:2 (l1init,l2init)
}