-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyper_if.clj
98 lines (87 loc) · 2.76 KB
/
hyper_if.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(ns hyper-if
(:require
[bennischwerdtner.hd.binary-sparse-segmented :as
hd]
[tech.v3.datatype :as dtype]
[tech.v3.tensor :as dtt]
[tech.v3.parallel.for :as pfor]
[tech.v3.datatype.argops :as dtype-argops]
[tech.v3.datatype.functional :as f]))
(let [lut (atom {})]
;; "encountering a symbol"
;; since symbol and value are interchangeable in hdc (Kanerva 2009), why not simply call it `prototype`
;;
(defn ->prototype
[sym]
(or (@lut sym)
(let [v (hd/->hv) _ (swap! lut assoc sym v)]
v)))
(defn cleanup-lookup-verbose
([query-v] (cleanup-lookup-verbose query-v 0.1))
([query-v threshold]
(->> (map (fn [[k v]]
{:k k
:similarity (hd/similarity v query-v)
:v v})
@lut)
(filter (comp #(<= threshold %) :similarity))
(sort-by :similarity (fn [a b] (compare b a))))))
(defn cleanup-lookup-value
[query-v]
(some->> (cleanup-lookup-verbose query-v)
first
:k)))
;; Idea 1:
;;
;; A hyper if
;; In high dimensional computing, the outcome of a calculation could represent
;; the combination of all 'possible' outcomes
;;
;; Interesting here to note is that 'what is possible?' is defined by the threshold, too
;;
;; We can imagine dynamically lowering and increasing the threshold
;; (would model something like 'fast' and 'slow' thinking perhaps)
;;
(defn condition->branches [condition]
;; everything above threshold comes out of the thing
(map :k (cleanup-lookup-verbose condition)))
(defmacro hyper-if
[condition consequence alternative]
`(let [condition# ~condition
branches# (condition->branches condition#)]
(hd/thin
(apply
hd/bundle
(for [branch# branches#]
;; using clojure truthiness of your values now
(if branch# ~consequence ~alternative))))))
(def both-true-and-false
(hd/thin
(hd/bundle
(->prototype true)
(->prototype false))))
(defn coin
[]
(hyper-if both-true-and-false
(->prototype :heads)
(->prototype :tails)))
;; all the bookeeping can go away ofc
(map :k (cleanup-lookup-verbose (coin)))
'(:heads :tails)
;; => (:heads :tails)
(comment
(map :k
(cleanup-lookup-verbose
(hyper-if (hd/bundle (->prototype true)
(->prototype false))
(->prototype :heads)
(->prototype :tails))))
(:heads :tails)
(cleanup-lookup-value (hyper-if (->prototype false)
(->prototype :heads)
(->prototype :tails)))
:tails
(hyper-if (hd/bundle (->prototype true)
(->prototype false))
(->prototype :heads)
(->prototype :tails)))