-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathambiguity.clj
249 lines (193 loc) · 6.69 KB
/
ambiguity.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
(ns ambiguity
(:require
[bennischwerdtner.hd.binary-sparse-segmented :as hd]
[tech.v3.tensor :as dtt]
[tech.v3.datatype.functional :as f]))
;; ------------------------
;; Ambiguity primitives *** WIP ***
;; ----------------------
;;
;; The Cortex is an information mixing machine - V. Braitenberg
;;
;;
;; Update:
;; --------
;; This preceeds src/bennischwerdtner/hd/data.clj
;;
;; - 'without' can be replaced with set difference
;; - mix only works well when you work with small amont of seed vectors. (thinnig quickly loses signal strenght)
;; - you probably want to use the set functions generally instead.
;; - vanishingly, roughly, mostly, impossibly, nothing, everything, non-sense make sense I think.
;; I wonder if there is a kind of "set language" that includes them.
;; - vanishingly is conceptually the same thing as the intersection with a (relatively large) subspace of hyperspace.
;; - moslty and roughly are definend in terms of it
;;
;; - would be interesting to program in superposition, utilizing ambiguity
;;
(defn non-sense
"Returns a fresh random hypervector.
Aliases: [[hd/->hv]], [[hd/->seed]]
"
[]
(hd/->hv))
;; I think there is something deep about the concept that
;; non-sense and gensym are the same operation
;; P. Kanerva: "Randomness is the path of least assumption".
;; G. Buzsáki: "Nothing is new for the brain." (pre allocated, random trajectories are there for pluggin).
;; D. Deutsch: "The idea comes first." (Ideas must be random at the bottom).
(def create non-sense)
(defn mix
"Mix `a`, `b`, ... `arg` hypervectors, representing the superposition.
This thins the output, if you want to mix and keep dense just use
[[hd/bundle]] or [[f/+]].
alias: [[possibly]].
"
([a b & args] (hd/thin (apply f/+ a b args)))
([a b] (hd/thin (hd/bundle a b))))
(def possibly mix)
;; Not really sure about this yet.
;; Using both vectors to find a third that is dissimilar to both `is` bind.
;; And then I permute to distinguish it from bind.
(defn neither "Quoted bind." [a b]
(hd/permute (hd/bind a b)))
(defn vanishingly
"Returns a weaker hypervector version of `a`.
`amount-of-a`: If 1, this is identity, if 0 this returns a zero vector.
See [[mostly]], [[nothing]], [[roughly]].
"
([a] (vanishingly a 0.5))
([a amount-of-a]
(hd/drop a (- 1 amount-of-a))))
(comment
(let [a (create)] (hd/similarity a (vanishingly a)))
(for [n (range 5)]
(let [a (create)
amount (rand)
c (vanishingly a amount)]
[:amount amount :similarity (hd/similarity a c)]))
'([:amount 0.03376090805508081 :similarity 0.05]
[:amount 0.27461511894016855 :similarity 0.25]
[:amount 0.27046438370634807 :similarity 0.3]
[:amount 0.592055396689516 :similarity 0.55]
[:amount 0.6953485647339649 :similarity 0.7]))
(defn mostly
"Returns a hypervector that is mostly `a` and a little bit `b`.
`amount-of-b`:
If 1, this is equalent to [[mix]], and returns a vector
that is equally similar to both `a` and `b` (~0.5 each).
I.e. that is in the middle between the 2.
If 0, this retuns `a`.
If 0 < `amount-of-b` < 1, this returns a point between `a` and `b`
that is closer to `a` than to `b`.
See [[roughly]].
"
([a b] (mostly a b 0.4))
([a b amount-of-b] (mix a (vanishingly b amount-of-b))))
(defn roughly
"Returns a hypervector similar to `a`, but with random noise mixed in.
Think 'merely roughly `a` to a certain degree'.
`amount-of-noise`: If 0, this is identity. If 1, this returns a vector with 0.5 similarity to `a`.
See [[mostly]], [[vanishingly]], [[non-sense]], [[create]].
"
([a] (roughly a 0.2))
([a amount-of-noise]
(mostly a (non-sense) amount-of-noise)))
(comment
(let [a (create)]
[(hd/similarity a (roughly a 0.2))
(hd/similarity a (roughly a 1))])
[0.9 0.5])
(comment
(for [n (range 5)]
(let [a (create)
b (create)
c (mostly a b)]
[(hd/similarity a c) (hd/similarity b c)]))
'([0.85 0.15] [0.8 0.2] [0.8 0.2] [0.85 0.15] [0.85 0.15]))
;; alias 'absolutely-not'?
(def impossibly
"Returns a hypervector representing the 'removal' of the arguments.
Note that this returns a hypervector with negative bits.
This is different from representing the absence of the arguments.
[[nothing]] is the commplete absence.
[[without]] is the absence of a part in a whole.
[[hd/similarity]].
"
(comp dtt/->tensor f/- f/+))
(comment
(let [a (create)
b (create)
c (hd/bind a b)
c2 (hd/bind a (impossibly b))]
[(f/sum c2) (hd/similarity b (hd/unbind c2 a))
(= (impossibly b) (hd/unbind c2 a))
(hd/similarity (mix a (impossibly b)) a)
(hd/similarity (mix a (impossibly b)) b)])
[-20.0 0.0 true 1.0 0.0])
(defn without
"Returns a (potentially super sparse) version of `a` with all of `b` removed.
Super sparse means sparser than per [[hd/maximally-sparse?]].
See [[impossibly]].
"
[a b]
(mix a (impossibly b)))
(comment
(let [a (create)
b (create)
c (mix a b)]
[(hd/similarity a c) (hd/similarity b c)
(hd/similarity a (without c b))
(hd/similarity b (without c b))])
[0.45 0.55 0.45 0.0])
(defn nothing
"Returns the empty hypervector.
'Nothing' is similar to no other hypervector, not even itself.
(because similarity is overlap of non 0 bits here).
It is still [[=]] to iteslf.
(= (nothing) (nothing))
true
(hd/similarity (nothing) (nothing))
0.0
(hd/similarity (nothing) (create))
0.0
See [[hd/similarity]], [[non-sense]], [[everything]]
"
[]
(hd/->empty))
(comment
(= (nothing) (nothing))
true
(hd/similarity (nothing) (nothing))
0.0
(hd/similarity (nothing) (create))
0.0)
;; ... 'everything' would also make sense
;; If used to represent a permutation wiring, perhaps this should be equal to a fully connected neural net layer
(defn everything
"Returns a maximally dense hypervector that is similar all hypervectors.
Except if they are super sparse, like 'nothing'.
This would also activate every single address location of a sparse distributed memory,
if used as address word.
(let [a (non-sense)
b (non-sense)
c (neither a b)]
[(hd/similarity (neither a b) (everything))
(hd/similarity b (everything))
(hd/similarity c (everything))
;; not even everything is similar to nothing
(hd/similarity (nothing) (everything))])
[1.0 1.0 1.0 0.0]
See [[hd/similarity]], [[nothing]], [[non-sense]].
"
[]
(hd/->ones))
(comment
(let [a (non-sense)
b (non-sense)
c (neither a b)]
[(hd/similarity (neither a b) (everything))
(hd/similarity b (everything))
(hd/similarity c (everything))
;; not even everything is similar to nothing
(hd/similarity (nothing) (everything))])
[1.0 1.0 1.0 0.0])