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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
|
- ---
- title: FlyVec
- keywords: fastai
- sidebar: home_sidebar
- summary: "Sparse Binary Word Embeddings Inspired by the Fruit Fly Brain"
- description: "Sparse Binary Word Embeddings Inspired by the Fruit Fly Brain"
- nb_path: "nbs/index.ipynb"
- ---
- <!--
- #################################################
- ### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
- #################################################
- # file to edit: nbs/index.ipynb
- # command to build the docs after a change: nbdev_build_docs
- -->
- <div class="container" id="notebook-container">
-
- {% raw %}
-
- <div class="cell border-box-sizing code_cell rendered">
- </div>
- {% endraw %}
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <p>Code based on the ICLR 2021 paper <a href="https://arxiv.org/abs/2101.06887">Can a Fruit Fly Learn Word Embeddings?</a>.</p>
- <p>In this work we use a well-established neurobiological network motif from the mushroom body of the fruit fly brain to learn sparse binary word embeddings from raw unstructured text. This package allows the user to access pre-trained word embeddings and generate sparse binary hash codes for individual words.</p>
- <p>Interactive demos of the learned concepts available at <a href="http://flyvec.org">flyvec.org</a>.</p>
- </div>
- </div>
- </div>
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <h2 id="How-to-use">How to use<a class="anchor-link" href="#How-to-use"> </a></h2>
- </div>
- </div>
- </div>
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <h3 id="Install-from-Pip-(recommended)">Install from Pip (recommended)<a class="anchor-link" href="#Install-from-Pip-(recommended)"> </a></h3><p><code>pip install flyvec</code></p>
- </div>
- </div>
- </div>
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <h3 id="Installing-from-Source">Installing from Source<a class="anchor-link" href="#Installing-from-Source"> </a></h3><p>After cloning:</p>
- <pre><code>conda env create -f environment-dev.yml
- conda activate flyvec
- pip install -e .</code></pre>
- </div>
- </div>
- </div>
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <h3 id="Basic-Usage">Basic Usage<a class="anchor-link" href="#Basic-Usage"> </a></h3><p>An example below illustrates how one can access the binary word embedding for individual tokens for a default hash length <code>k=50</code>.</p>
- </div>
- </div>
- </div>
- {% raw %}
-
- <div class="cell border-box-sizing code_cell rendered">
- <div class="input">
- <div class="inner_cell">
- <div class="input_area">
- <div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
- <span class="kn">from</span> <span class="nn">flyvec</span> <span class="kn">import</span> <span class="n">FlyVec</span>
- <span class="n">model</span> <span class="o">=</span> <span class="n">FlyVec</span><span class="o">.</span><span class="n">load</span><span class="p">()</span>
- <span class="n">embed_info</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_sparse_embedding</span><span class="p">(</span><span class="s2">"market"</span><span class="p">);</span> <span class="n">embed_info</span>
- </pre></div>
- </div>
- </div>
- </div>
- <div class="output_wrapper">
- <div class="output">
- <div class="output_area">
- <div class="output_text output_subarea output_execute_result">
- <pre>{'token': 'market',
- 'id': 1180,
- 'embedding': array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
- 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1,
- 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
- 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0], dtype=int8)}</pre>
- </div>
- </div>
- </div>
- </div>
- </div>
- {% endraw %}
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <h3 id="Changing-the-Hash-Length">Changing the Hash Length<a class="anchor-link" href="#Changing-the-Hash-Length"> </a></h3><p>The user can obtain the FlyVec embeddings for any hash length using the following example.</p>
- </div>
- </div>
- </div>
- {% raw %}
-
- <div class="cell border-box-sizing code_cell rendered">
- <div class="input">
- <div class="inner_cell">
- <div class="input_area">
- <div class=" highlight hl-ipython3"><pre><span></span><span class="n">small_embed</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_sparse_embedding</span><span class="p">(</span><span class="s2">"market"</span><span class="p">,</span> <span class="mi">4</span><span class="p">);</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">small_embed</span><span class="p">[</span><span class="s1">'embedding'</span><span class="p">])</span>
- </pre></div>
- </div>
- </div>
- </div>
- <div class="output_wrapper">
- <div class="output">
- <div class="output_area">
- <div class="output_text output_subarea output_execute_result">
- <pre>4</pre>
- </div>
- </div>
- </div>
- </div>
- </div>
- {% endraw %}
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <h3 id="Handling-"unknown"-tokens">Handling "unknown" tokens<a class="anchor-link" href="#Handling-"unknown"-tokens"> </a></h3>
- </div>
- </div>
- </div>
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <p>FlyVec uses a simple, word-based tokenizer. The provided model uses a vocabulary with about 20,000 words, all lower-cased, with special tokens for numbers (<code><NUM></code>) and unknown words (<code><UNK></code>). Unknown tokens have the token id of <code>0</code>, which can be used to filter unknown tokens.</p>
- </div>
- </div>
- </div>
- {% raw %}
-
- <div class="cell border-box-sizing code_cell rendered">
- <div class="input">
- <div class="inner_cell">
- <div class="input_area">
- <div class=" highlight hl-ipython3"><pre><span></span><span class="n">unk_embed</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">get_sparse_embedding</span><span class="p">(</span><span class="s2">"DefNotAWord"</span><span class="p">)</span>
- <span class="k">if</span> <span class="n">unk_embed</span><span class="p">[</span><span class="s1">'id'</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
- <span class="nb">print</span><span class="p">(</span><span class="s2">"I AM THE UNKNOWN TOKEN DON'T USE ME FOR ANYTHING IMPORTANT"</span><span class="p">)</span>
- </pre></div>
- </div>
- </div>
- </div>
- <div class="output_wrapper">
- <div class="output">
- <div class="output_area">
- <div class="output_subarea output_stream output_stdout output_text">
- <pre>I AM THE UNKNOWN TOKEN DON'T USE ME FOR ANYTHING IMPORTANT
- </pre>
- </div>
- </div>
- </div>
- </div>
- </div>
- {% endraw %}
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <h3 id="Batch-generating-word-embeddings">Batch generating word embeddings<a class="anchor-link" href="#Batch-generating-word-embeddings"> </a></h3><p>Embeddings for individual words in a sentence can be obtained using this snippet.</p>
- </div>
- </div>
- </div>
- {% raw %}
-
- <div class="cell border-box-sizing code_cell rendered">
- <div class="input">
- <div class="inner_cell">
- <div class="input_area">
- <div class=" highlight hl-ipython3"><pre><span></span><span class="n">sentence</span> <span class="o">=</span> <span class="s2">"Supreme Court dismissed the criminal charges."</span>
- <span class="n">tokens</span> <span class="o">=</span> <span class="n">model</span><span class="o">.</span><span class="n">tokenize</span><span class="p">(</span><span class="n">sentence</span><span class="p">)</span>
- <span class="n">embedding_info</span> <span class="o">=</span> <span class="p">[</span><span class="n">model</span><span class="o">.</span><span class="n">get_sparse_embedding</span><span class="p">(</span><span class="n">t</span><span class="p">)</span> <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">tokens</span><span class="p">]</span>
- <span class="n">embeddings</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="n">e</span><span class="p">[</span><span class="s1">'embedding'</span><span class="p">]</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">embedding_info</span><span class="p">])</span>
- <span class="nb">print</span><span class="p">(</span><span class="s2">"TOKENS: "</span><span class="p">,</span> <span class="p">[</span><span class="n">e</span><span class="p">[</span><span class="s1">'token'</span><span class="p">]</span> <span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">embedding_info</span><span class="p">])</span>
- <span class="nb">print</span><span class="p">(</span><span class="s2">"EMBEDDINGS: "</span><span class="p">,</span> <span class="n">embeddings</span><span class="p">)</span>
- </pre></div>
- </div>
- </div>
- </div>
- <div class="output_wrapper">
- <div class="output">
- <div class="output_area">
- <div class="output_subarea output_stream output_stdout output_text">
- <pre>TOKENS: ['supreme', 'court', 'dismissed', 'the', 'criminal', 'charges']
- EMBEDDINGS: [[0 1 0 ... 0 0 0]
- [0 0 0 ... 0 0 0]
- [0 0 0 ... 0 1 0]
- [0 0 0 ... 0 0 0]
- [0 0 0 ... 0 1 0]
- [0 0 0 ... 0 1 0]]
- </pre>
- </div>
- </div>
- </div>
- </div>
- </div>
- {% endraw %}
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <h3 id="FlyVec-vocabulary">FlyVec vocabulary<a class="anchor-link" href="#FlyVec-vocabulary"> </a></h3><p>The vocabulary under the hood uses the gensim <code>Dictionary</code> and can be accessed by either IDs (<code>int</code>s) or Tokens (<code>str</code>s).</p>
- </div>
- </div>
- </div>
- {% raw %}
-
- <div class="cell border-box-sizing code_cell rendered">
- <div class="input">
- <div class="inner_cell">
- <div class="input_area">
- <div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">token_vocab</span><span class="p">[:</span><span class="mi">5</span><span class="p">])</span>
- <span class="c1"># The IDs that correspond to those tokens</span>
- <span class="nb">print</span><span class="p">(</span><span class="n">model</span><span class="o">.</span><span class="n">vocab</span><span class="p">[:</span><span class="mi">5</span><span class="p">])</span>
- <span class="c1"># The dictionary object itself</span>
- <span class="n">model</span><span class="o">.</span><span class="n">dictionary</span><span class="p">;</span>
- </pre></div>
- </div>
- </div>
- </div>
- <div class="output_wrapper">
- <div class="output">
- <div class="output_area">
- <div class="output_subarea output_stream output_stdout output_text">
- <pre>['properties', 'a', 'among', 'and', 'any']
- [2, 3, 4, 5, 6]
- </pre>
- </div>
- </div>
- </div>
- </div>
- </div>
- {% endraw %}
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <h1 id="Training">Training<a class="anchor-link" href="#Training"> </a></h1><p>Please note that the training code is included, though code for processing the inputs.</p>
- <p><strong>Prerequisites</strong></p>
- <p>You need a python environment with <code>numpy</code> installed, a system that supports CUDA, <code>nvcc</code>, and <code>g++</code>.</p>
- <p><strong>Building the Source Files</strong></p>
- <p><code>flyvec_compile</code></p>
- <p>(Or, if using from source, you can also run <code>make training</code>)</p>
- <p>Note that you will see some warnings. This is expected.</p>
- <p><strong>Training</strong></p>
- <p><code>flyvec_train path/to/encodings.npy path/to/offsets.npy -o save/checkpoints/in/this/directory</code></p>
- <p><strong>Description of Inputs</strong></p>
- <ul>
- <li><code>encodings.npy</code> -- An <code>np.int32</code> array representing the tokenized vocabulary-IDs of the input corpus, of shape <code>(N,)</code> where <code>N</code> is the number of tokens in the corpus</li>
- <li><code>offsets.npy</code> -- An <code>np.uint64</code> array of shape <code>(C,)</code> where <code>C</code> is the number of chunks in the corpus. Each each value represents the index that starts a new chunk within <code>encodings.npy</code>.
- (Chunks can be thought of as sentences or paragraphs within the corpus; boundaries over which the sliding window does not cross.)</li>
- </ul>
- <p><strong>Description of Outputs</strong></p>
- <ul>
- <li><code>model_X.npy</code> -- Stores checkpoints after every epoch within the specified output directory</li>
- </ul>
- <p>See <code>flyvec_train --help</code> for more options.</p>
- </div>
- </div>
- </div>
- <div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
- <div class="text_cell_render border-box-sizing rendered_html">
- <h1 id="Citation">Citation<a class="anchor-link" href="#Citation"> </a></h1><p>If you use this in your work, please cite:</p>
- <pre><code>@article{liang2021can,
- title={Can a Fruit Fly Learn Word Embeddings?},
- author={Liang, Yuchen and Ryali, Chaitanya K and Hoover, Benjamin and Grinberg, Leopold and Navlakha, Saket and Zaki, Mohammed J and Krotov, Dmitry},
- journal={arXiv preprint arXiv:2101.06887},
- year={2021}
- url={https://arxiv.org/abs/2101.06887}
- }</code></pre>
- </div>
- </div>
- </div>
- </div>
-
|