Skip to content

Commit

Permalink
feat: fixed BP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Panadestein committed Jan 5, 2025
1 parent 31cdba6 commit 4d15d75
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
14 changes: 7 additions & 7 deletions src/bqn/nn.bqn
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ M ← +˝∘×⎉1‿∞

Minn ← {rtly𝕊dat:
ADA ← ⟨1÷1+⋆-, ⊢×1-
BP ← {tswe𝕊𝕩:
do<(-ts×DA)´𝕩
𝕩×⌜¨dodo{d𝕊wz: z DA×d M˜w}`¨´1¨we⋈⌽𝕩
BP ← {fstswe𝕊𝕩:
do<(-ts×DA)hxtx1(↑⋈↓)𝕩
(fs<tx)×⌜˜¨dodo{d𝕊wz: z DA×d M˜w}`¨´tx˜1we
}
FP ← {z𝕊bwe: wezA¨b+we M z}`(¨´)
EFP{´𝕩𝔽 dat{𝕨𝕊biwe: fsts𝕨(↑⋈↓)˜ly
biwe-rt× tswe BP fs<FP𝕩}˝˜(U1-1≠⋈·<˘2)ly}
biwe-rt× fstswe BP fs<FP𝕩}˝˜(U1-1≠⋈·<˘2)ly}
}

nrirfdr1002.840.1
nrirfdr102.840.1
td{𝕩˘2𝕩(⊣×1-×⊢)(n)•rand.Range 0}¨((ri+dr×⊢))rf

lm0.01‿⟨2, 10, 1Minn td
lm.E 2.85
lm0.01‿⟨2, 5, 1Minn td
lm.E 2.85

)r Setplot "scatter"•Out¨ Plot´ m {ns≠⊑𝕨 ⋄ (>𝕨)˜nsns/𝕩} r
25 changes: 9 additions & 16 deletions src/nn.org
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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]...

Expand Down

0 comments on commit 4d15d75

Please sign in to comment.