From 4d15d7548789cb6c7e49060be87db981904c3d50 Mon Sep 17 00:00:00 2001 From: Panadestein Date: Sun, 5 Jan 2025 21:35:32 +0100 Subject: [PATCH] feat: fixed BP. --- src/bqn/nn.bqn | 14 +++++++------- src/nn.org | 25 +++++++++---------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/bqn/nn.bqn b/src/bqn/nn.bqn index b0ac329..6b15b50 100644 --- a/src/bqn/nn.bqn +++ b/src/bqn/nn.bqn @@ -4,19 +4,19 @@ M ← +˝∘×⎉1‿∞ Minn ← {rt‿ly𝕊dat: A‿DA ← ⟨1÷1+⋆∘-, ⊢×1⊸-⟩ - BP ← {ts‿we𝕊𝕩: - do ← <(-⟜ts×DA)⊢´𝕩 - 𝕩×⌜¨do∾do{d𝕊w‿z: z DA⊸×d M˜⍉w}`⋈¨´1⊸↓¨we⋈⌽𝕩 + BP ← {fs‿ts‿we𝕊𝕩: + do ← <(-⟜ts×DA)⊑hx‿tx ← 1(↑⋈↓)⌽𝕩 + (fs<⊸∾tx)×⌜˜¨⌽do∾do{d𝕊w‿z: z DA⊸×d M˜⍉w}`⋈¨´tx⋈˜1↓we } FP ← {z𝕊b‿we: we‿z⋄A¨b+we M z}`⟜(⋈¨´) E ⇐ FP{⊢´𝕩𝔽 dat{𝕨𝕊bi‿we: fs‿ts ← 𝕨(↑⋈↓)˜⊑ly - bi⋈we-rt× ts‿we BP fs<⊸FP𝕩}˝˜(U⚇1-⟜1∘≠⋈·<∘⌽˘2⊸↕)ly} + bi⋈we-rt× fs‿ts‿we BP fs<⊸FP𝕩}˝˜(U⚇1-⟜1∘≠⋈·<∘⌽˘2⊸↕)ly} } -n‿ri‿rf‿dr ← 100‿2.8‿4‿0.1 +n‿ri‿rf‿dr ← 10‿2.8‿4‿0.1 ≠td ← ∾{𝕩∾˘2↕𝕩(⊣×1⊸-×⊢)⍟(↕n)•rand.Range 0}¨↕∘⌈⌾((ri+dr×⊢)⁼)rf -lm ← 0.01‿⟨2, 10, 1⟩ Minn td -lm.E 2.85 +lm ← 0.01‿⟨2, 5, 1⟩ Minn td +≡lm.E 2.85 )r Setplot "scatter" ⋄ •Out¨ Plot´ m {ns←≠⊑𝕨 ⋄ (>𝕨)⋈˜∘‿ns⥊ns/𝕩} r diff --git a/src/nn.org b/src/nn.org index 9cea315..cd75d70 100644 --- a/src/nn.org +++ b/src/nn.org @@ -57,13 +57,13 @@ The original C implementation has 175 lines excluding the optimization loop. The #+begin_src bqn :tangle ./bqn/nn.bqn Minn ← {rt‿ly𝕊dat: A‿DA ← ⟨1÷1+⋆∘-, ⊢×1⊸-⟩ - BP ← {ts‿we𝕊𝕩: - do ← <(-⟜ts×DA)⊢´𝕩 - 𝕩×⌜¨do∾do{d𝕊w‿z: z DA⊸×d M˜⍉w}`⋈¨´1⊸↓¨we⋈⌽𝕩 + BP ← {fs‿ts‿we𝕊𝕩: + do ← <(-⟜ts×DA)⊑hx‿tx ← 1(↑⋈↓)⌽𝕩 + (fs<⊸∾tx)×⌜˜¨⌽do∾do{d𝕊w‿z: z DA⊸×d M˜⍉w}`⋈¨´tx⋈˜1↓we } FP ← {z𝕊b‿we: we‿z⋄A¨b+we M z}`⟜(⋈¨´) E ⇐ FP{⊢´𝕩𝔽 dat{𝕨𝕊bi‿we: fs‿ts ← 𝕨(↑⋈↓)˜⊑ly - bi⋈we-rt× ts‿we BP fs<⊸FP𝕩}˝˜(U⚇1-⟜1∘≠⋈·<∘⌽˘2⊸↕)ly} + bi⋈we-rt× fs‿ts‿we BP fs<⊸FP𝕩}˝˜(U⚇1-⟜1∘≠⋈·<∘⌽˘2⊸↕)ly} } #+end_src @@ -122,29 +122,22 @@ Moreover, it is not so trivial to approximate: the recurrence lacks a [[https:// the context of neural networks[fn:5]. First let's generate some reference data: #+begin_src bqn :tangle ./bqn/nn.bqn - n‿ri‿rf‿dr ← 100‿2.8‿4‿0.1 + n‿ri‿rf‿dr ← 10‿2.8‿4‿0.1 ≠td ← ∾{𝕩∾˘2↕𝕩(⊣×1⊸-×⊢)⍟(↕n)•rand.Range 0}¨↕∘⌈⌾((ri+dr×⊢)⁼)rf #+end_src #+RESULTS: -: 1287 +: 117 Then we can train =Minn=, and generate some approximated data with the perceptron: #+begin_src bqn :tangle ./bqn/nn.bqn - lm ← 0.01‿⟨2, 10, 1⟩ Minn td - lm.E 2.85 + lm ← 0.01‿⟨2, 5, 1⟩ Minn td + ≡lm.E 2.85 #+end_src #+RESULTS: -: ⟨ ⟨ 10 1 ⟩ ⟨ 1 10 ⟩ ⟩ -: Error: Mapping: Expected equal shape prefix (10‿2 ≡ ≢𝕨, ⟨2⟩ ≡ ≢𝕩) -: at bi⋈we-rt× ts‿we BP fs<⊸FP𝕩}˝˜(U⚇1-⟜1∘≠⋈·<∘⌽˘2⊸↕)ly} -: ^ -: at E ⇐ FP{⊢´𝕩𝔽 dat{𝕨𝕊bi‿we: fs‿ts ← 𝕨(↑⋈↓)˜⊑ly -: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -: at lm.E 2.85 -: ^^^^ +: 118 Let’s see if we’ve gotten the numbers right after learning. But then again, what is a number that a man may know it[fn:6]...