forked from jrosebr1/keras-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
404 lines (351 loc) · 37.4 KB
/
index.html
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<!DOCTYPE html>
<html lang="en">
<head>
<title>The Keras Blog</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="http://blog.keras.io/theme/css/main.css" type="text/css" />
<link rel="stylesheet" href="http://blog.keras.io/theme/css/pygment.css" type="text/css" />
<link href="https://fonts.googleapis.com/css?family=Lato:400,700|Source+Sans+Pro:400,700|Inconsolata:400,700" rel="stylesheet" type="text/css">
<link href="http://blog.keras.io/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="The Keras Blog ATOM Feed" />
<!--[if IE]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->
<!--[if lte IE 7]>
<link rel="stylesheet" type="text/css" media="all" href="http://blog.keras.io/css/ie.css"/>
<script src="http://blog.keras.io/js/IE8.js" type="text/javascript"></script><![endif]-->
<!--[if lt IE 7]>
<link rel="stylesheet" type="text/css" media="all" href="http://blog.keras.io/css/ie6.css"/><![endif]-->
</head>
<body id="index" class="home">
<header id="banner" class="body">
<h1>
<a href="http://blog.keras.io/index.html">The Keras Blog </a>
</h1>
<p id="side">
<a href="https://github.com/fchollet/keras">Keras</a> is a Deep Learning library for Python, that is simple, modular, and extensible.
</p>
<nav><ul>
<li><a href="http://blog.keras.io/">Archives</a></li>
<li >
<a href="https://github.com/fchollet/keras">Github</a>
</li>
<li >
<a href="http://keras.io/">Documentation</a>
</li>
<li >
<a href="https://groups.google.com/forum/#!forum/keras-users">Google Group</a>
</li>
</ul></nav>
</header><!-- /#banner -->
<section id="content" class="body">
<aside id="featured"><article>
<h1 class="entry-title"><a href="http://blog.keras.io/using-pre-trained-word-embeddings-in-a-keras-model.html">Using pre-trained word embeddings in a Keras model</a></h1>
<footer class="post-info">
<abbr class="published" title="2016-07-16T00:00:00+02:00">
Sat 16 July 2016
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://twitter.com/fchollet">Francois Chollet</a>
</address>
<p>In <a href="http://blog.keras.io/category/tutorials.html">Tutorials</a>. </p>
</p></footer><!-- /.post-info --><!-- /.post-info -->
<p>In this tutorial, we will walk you through the process of solving a text classification problem using pre-trained word embeddings and a convolutional neural network.</p>
<p>The full code for this tutorial is <a href="https://github.com/fchollet/keras/blob/master/examples/pretrained_word_embeddings.py">available on Github</a>.</p>
<hr />
<h2>What are word embeddings?</h2>
<p>"Word embeddings" are a family of natural language processing techniques aiming at mapping semantic meaning into a geometric space. This is done by associating a numeric vector to every word in a dictionary, such that the distance (e.g. L2 distance or more commonly cosine distance) between any two vectors would capture part of the semantic relationship between the two associated words. The geometric space formed by these vectors is called an <em>embedding space</em>.</p>
<p>For instance, "coconut" and "polar bear" are words that are semantically quite different, so a reasonable embedding space would represent them as vectors that would be very far apart. But "kitchen" and "dinner" are related words, so they should be embedded close to each other.</p>
<p>Ideally, in a good embeddings space, the "path" (a vector) to go from "kitchen" and "dinner" would capture precisely the semantic relationship between these two concepts. In this case the relationship is "where x occurs", so you would expect the vector <code>kitchen - dinner</code> (difference of the two embedding vectors, i.e. path to go from dinner to kitchen) to capture this "where x occurs" relationship. Basically, we should have the vectorial identity: <code>dinner + (where x occurs) = kitchen</code> (at least approximately). If that's indeed the case, then we can use such a relationship vector to answer questions. For instance, starting from a new vector, e.g. "work", and applying this relationship vector, we should get sometime meaningful, e.g. <code>work + (where x occurs) = office</code>, answering "where does work occur?".</p>
<p>Word embeddings are computed by applying dimensionality reduction techniques to datasets of co-occurence statistics between words in a corpus of text. This can be done via neural networks (the "word2vec" technique), or via matrix factorization.</p>
<hr />
<h2>GloVe word embeddings</h2>
<p>We will be using GloVe embeddings, which you can read about <a href="http://nlp.stanford.edu/projects/glove/">here</a>. GloVe stands for "Global Vectors for Word Representation". It's a somewhat popular embedding technique based on factorizing a matrix of word co-occurence statistics.</p>
<p>Specifically, we will use the 100-dimensional GloVe embeddings of 400k words computed on a 2014 dump of English Wikipedia. You can download them <a href="http://nlp.stanford.edu/data/glove.6B.zip">here</a> (warning: following this link will start a 822MB download).</p>
<hr />
<h2>20 Newsgroup dataset</h2>
<p>The task we will try to solve will be to classify posts coming from 20 different newsgroup, into their original 20 categories --the infamous "20 Newsgroup dataset". You can read about the dataset and download the raw text data <a href="http://www.cs.cmu.edu/afs/cs.cmu.edu/project/theo-20/www/data/news20.html">here</a>.</p>
<p>Categories are fairly semantically distinct and thus will have quite different words associated with them. Here are a few sample categories:</p>
<div class="highlight"><pre>comp.sys.ibm.pc.hardware
comp.graphics
comp.os.ms-windows.misc
comp.sys.mac.hardware
comp.windows.x
rec.autos
rec.motorcycles
rec.sport.baseball
rec.sport.hockey
</pre></div>
<hr />
<h2>Approach</h2>
<p>Here's how we will solve the classification problem:</p>
<ul>
<li>convert all text samples in the dataset into sequences of word indices. A "word index" would simply be an integer ID for the word. We will only consider the top 20,000 most commonly occuring words in the dataset, and we will truncate the sequences to a maximum length of 1000 words.</li>
<li>prepare an "embedding matrix" which will contain at index <code>i</code> the embedding vector for the word of index <code>i</code> in our word index.</li>
<li>load this embedding matrix into a Keras <code>Embedding</code> layer, set to be frozen (its weights, the embedding vectors, will not be updated during training).</li>
<li>build on top of it a 1D convolutional neural network, ending in a softmax output over our 20 categories.</li>
</ul>
<hr />
<h2>Preparing the text data</h2>
<p>First, we will simply iterate over the folders in which our text samples are stored, and format them into a list of samples. We will also prepare at the same time a list of class indices matching the samples:</p>
<div class="highlight"><pre><span class="n">texts</span> <span class="o">=</span> <span class="p">[]</span> <span class="c"># list of text samples</span>
<span class="n">labels_index</span> <span class="o">=</span> <span class="p">{}</span> <span class="c"># dictionary mapping label name to numeric id</span>
<span class="n">labels</span> <span class="o">=</span> <span class="p">[]</span> <span class="c"># list of label ids</span>
<span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">TEXT_DATA_DIR</span><span class="p">)):</span>
<span class="n">path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">TEXT_DATA_DIR</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
<span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
<span class="n">label_id</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">labels_index</span><span class="p">)</span>
<span class="n">labels_index</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">label_id</span>
<span class="k">for</span> <span class="n">fname</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">listdir</span><span class="p">(</span><span class="n">path</span><span class="p">)):</span>
<span class="k">if</span> <span class="n">fname</span><span class="o">.</span><span class="n">isdigit</span><span class="p">():</span>
<span class="n">fpath</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">fname</span><span class="p">)</span>
<span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">fpath</span><span class="p">)</span>
<span class="n">texts</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">())</span>
<span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="n">labels</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">label_id</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Found </span><span class="si">%s</span><span class="s"> texts.'</span> <span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="n">texts</span><span class="p">))</span>
</pre></div>
<p>Then we can format our text samples and labels into tensors that can be fed into a neural network. To do this, we will rely on Keras utilities <code>keras.preprocessing.text.Tokenizer</code> and <code>keras.preprocessing.sequence.pad_sequences</code>.</p>
<div class="highlight"><pre><span class="kn">from</span> <span class="nn">keras.preprocessing.text</span> <span class="kn">import</span> <span class="n">Tokenizer</span>
<span class="kn">from</span> <span class="nn">keras.preprocessing.sequence</span> <span class="kn">import</span> <span class="n">pad_sequences</span>
<span class="n">tokenizer</span> <span class="o">=</span> <span class="n">Tokenizer</span><span class="p">(</span><span class="n">nb_words</span><span class="o">=</span><span class="n">MAX_NB_WORDS</span><span class="p">)</span>
<span class="n">tokenizer</span><span class="o">.</span><span class="n">fit_on_texts</span><span class="p">(</span><span class="n">texts</span><span class="p">)</span>
<span class="n">sequences</span> <span class="o">=</span> <span class="n">tokenizer</span><span class="o">.</span><span class="n">texts_to_sequences</span><span class="p">(</span><span class="n">texts</span><span class="p">)</span>
<span class="n">word_index</span> <span class="o">=</span> <span class="n">tokenizer</span><span class="o">.</span><span class="n">word_index</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Found </span><span class="si">%s</span><span class="s"> unique tokens.'</span> <span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="n">word_index</span><span class="p">))</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">pad_sequences</span><span class="p">(</span><span class="n">sequences</span><span class="p">,</span> <span class="n">maxlen</span><span class="o">=</span><span class="n">MAX_SEQUENCE_LENGTH</span><span class="p">)</span>
<span class="n">labels</span> <span class="o">=</span> <span class="n">to_categorical</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">labels</span><span class="p">))</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Shape of data tensor:'</span><span class="p">,</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Shape of label tensor:'</span><span class="p">,</span> <span class="n">labels</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
<span class="c"># split the data into a training set and a validation set</span>
<span class="n">indices</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">indices</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="n">indices</span><span class="p">]</span>
<span class="n">labels</span> <span class="o">=</span> <span class="n">labels</span><span class="p">[</span><span class="n">indices</span><span class="p">]</span>
<span class="n">nb_validation_samples</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">VALIDATION_SPLIT</span> <span class="o">*</span> <span class="n">data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
<span class="n">x_train</span> <span class="o">=</span> <span class="n">data</span><span class="p">[:</span><span class="o">-</span><span class="n">nb_validation_samples</span><span class="p">]</span>
<span class="n">y_train</span> <span class="o">=</span> <span class="n">labels</span><span class="p">[:</span><span class="o">-</span><span class="n">nb_validation_samples</span><span class="p">]</span>
<span class="n">x_val</span> <span class="o">=</span> <span class="n">data</span><span class="p">[</span><span class="o">-</span><span class="n">nb_validation_samples</span><span class="p">:]</span>
<span class="n">y_val</span> <span class="o">=</span> <span class="n">labels</span><span class="p">[</span><span class="o">-</span><span class="n">nb_validation_samples</span><span class="p">:]</span>
</pre></div>
<hr />
<h2>Preparing the Embedding layer</h2>
<p>Next, we compute an index mapping words to known embeddings, by parsing the data dump of pre-trained embeddings:</p>
<div class="highlight"><pre><span class="n">embeddings_index</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">GLOVE_DIR</span><span class="p">,</span> <span class="s">'glove.6B.100d.txt'</span><span class="p">))</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
<span class="n">values</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
<span class="n">word</span> <span class="o">=</span> <span class="n">values</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="n">coefs</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">values</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="n">dtype</span><span class="o">=</span><span class="s">'float32'</span><span class="p">)</span>
<span class="n">embeddings_index</span><span class="p">[</span><span class="n">word</span><span class="p">]</span> <span class="o">=</span> <span class="n">coefs</span>
<span class="n">f</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="k">print</span><span class="p">(</span><span class="s">'Found </span><span class="si">%s</span><span class="s"> word vectors.'</span> <span class="o">%</span> <span class="nb">len</span><span class="p">(</span><span class="n">embeddings_index</span><span class="p">))</span>
</pre></div>
<p>At this point we can leverage our <code>embedding_index</code> dictionary and our <code>word_index</code> to compute our embedding matrix:</p>
<div class="highlight"><pre><span class="n">embedding_matrix</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="n">word_index</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span> <span class="n">EMBEDDING_DIM</span><span class="p">))</span>
<span class="k">for</span> <span class="n">word</span><span class="p">,</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">word_index</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="n">embedding_vector</span> <span class="o">=</span> <span class="n">embeddings_index</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">word</span><span class="p">)</span>
<span class="k">if</span> <span class="n">embedding_vector</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
<span class="c"># words not found in embedding index will be all-zeros.</span>
<span class="n">embedding_matrix</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">embedding_vector</span>
</pre></div>
<p>We load this embedding matrix into an <code>Embedding</code> layer. Note that we set <code>trainable=False</code> to prevent the weights from being updated during training.</p>
<div class="highlight"><pre><span class="kn">from</span> <span class="nn">keras.layers</span> <span class="kn">import</span> <span class="n">Embedding</span>
<span class="n">embedding_layer</span> <span class="o">=</span> <span class="n">Embedding</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">word_index</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span>
<span class="n">EMBEDDING_DIM</span><span class="p">,</span>
<span class="n">weights</span><span class="o">=</span><span class="p">[</span><span class="n">embedding_matrix</span><span class="p">],</span>
<span class="n">input_length</span><span class="o">=</span><span class="n">MAX_SEQUENCE_LENGTH</span><span class="p">,</span>
<span class="n">trainable</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
</pre></div>
<p>An <code>Embedding</code> layer should be fed sequences of integers, i.e. a 2D input of shape <code>(samples, indices)</code>. These input sequences should be padded so that they all have the same length in a batch of input data (although an <code>Embedding</code> layer is capable of processing sequence of heterogenous length, if you don't pass an explicit <code>input_length</code> argument to the layer).</p>
<p>All that the <code>Embedding</code> layer does is to map the integer inputs to the vectors found at the corresponding index in the embedding matrix, i.e. the sequence <code>[1, 2]</code> would be converted to <code>[embeddings[1], embeddings[2]]</code>. This means that the output of the <code>Embedding</code> layer will be a 3D tensor of shape <code>(samples, sequence_length, embedding_dim)</code>.</p>
<hr />
<h2>Training a 1D convnet</h2>
<p>Finally we can then build a small 1D convnet to solve our classification problem:</p>
<div class="highlight"><pre><span class="n">sequence_input</span> <span class="o">=</span> <span class="n">Input</span><span class="p">(</span><span class="n">shape</span><span class="o">=</span><span class="p">(</span><span class="n">MAX_SEQUENCE_LENGTH</span><span class="p">,),</span> <span class="n">dtype</span><span class="o">=</span><span class="s">'int32'</span><span class="p">)</span>
<span class="n">embedded_sequences</span> <span class="o">=</span> <span class="n">embedding_layer</span><span class="p">(</span><span class="n">sequence_input</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">Conv1D</span><span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s">'relu'</span><span class="p">)(</span><span class="n">embedded_sequences</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">MaxPooling1D</span><span class="p">(</span><span class="mi">5</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">Conv1D</span><span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s">'relu'</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">MaxPooling1D</span><span class="p">(</span><span class="mi">5</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">Conv1D</span><span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s">'relu'</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">MaxPooling1D</span><span class="p">(</span><span class="mi">35</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span> <span class="c"># global max pooling</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">Flatten</span><span class="p">()(</span><span class="n">x</span><span class="p">)</span>
<span class="n">x</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="mi">128</span><span class="p">,</span> <span class="n">activation</span><span class="o">=</span><span class="s">'relu'</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>
<span class="n">preds</span> <span class="o">=</span> <span class="n">Dense</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">labels_index</span><span class="p">),</span> <span class="n">activation</span><span class="o">=</span><span class="s">'softmax'</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>
<span class="n">model</span> <span class="o">=</span> <span class="n">Model</span><span class="p">(</span><span class="n">sequence_input</span><span class="p">,</span> <span class="n">preds</span><span class="p">)</span>
<span class="n">model</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">loss</span><span class="o">=</span><span class="s">'categorical_crossentropy'</span><span class="p">,</span>
<span class="n">optimizer</span><span class="o">=</span><span class="s">'rmsprop'</span><span class="p">,</span>
<span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="s">'acc'</span><span class="p">])</span>
<span class="c"># happy learning!</span>
<span class="n">model</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="n">x_train</span><span class="p">,</span> <span class="n">y_train</span><span class="p">,</span> <span class="n">validation_data</span><span class="o">=</span><span class="p">(</span><span class="n">x_val</span><span class="p">,</span> <span class="n">y_val</span><span class="p">),</span>
<span class="n">nb_epoch</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="mi">128</span><span class="p">)</span>
</pre></div>
<p>This model reaches <strong>95% classification accuracy</strong> on the validation set after only 2 epochs. You could probably get to an even higher accuracy by training longer with some regularization mechanism (such as dropout) or by fine-tuning the <code>Embedding</code> layer.</p>
<p>We can also test how well we would have performed by not using pre-trained word embeddings, but instead initializing our <code>Embedding</code> layer from scratch and learning its weights during training. We just need to replace our <code>Embedding</code> layer with the following:</p>
<div class="highlight"><pre><span class="n">embedding_layer</span> <span class="o">=</span> <span class="n">Embedding</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">word_index</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">,</span>
<span class="n">EMBEDDING_DIM</span><span class="p">,</span>
<span class="n">input_length</span><span class="o">=</span><span class="n">MAX_SEQUENCE_LENGTH</span><span class="p">)</span>
</pre></div>
<p>After 2 epochs, this approach only gets us to <strong>90% validation accuracy</strong>, less than what the previous model could reach in just one epoch. Our pre-trained embeddings were definitely buying us something. In general, using pre-trained embeddings is relevant for natural processing tasks were little training data is available (functionally the embeddings act as an injection of outside information which might prove useful for your model).</p>
</article></aside><!-- /#featured -->
<h1>Other articles</h1>
<hr />
<ol id="posts-list" class="hfeed">
<li><article class="hentry">
<header>
<h1><a href="http://blog.keras.io/on-the-importance-of-democratizing-artificial-intelligence.html" rel="bookmark" title="Permalink to On the importance of democratizing Artificial Intelligence">On the importance of democratizing Artificial Intelligence</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2016-07-06T00:00:00+02:00">
Wed 06 July 2016
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://twitter.com/fchollet">Francois Chollet</a>
</address>
<p>In <a href="http://blog.keras.io/category/essays.html">Essays</a>. </p>
</p></footer><!-- /.post-info --><!-- /.post-info -->
<p>We all know about the incredible progress that deep learning has made in recent years. In just 5 years, we went from near-unusable speech recognition and image recognition, to near-human accuracy. We went from machines that couldn't beat a serious Go player, to beating a world champion. We went ...</p>
<a class="readmore" href="http://blog.keras.io/on-the-importance-of-democratizing-artificial-intelligence.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
<li><article class="hentry">
<header>
<h1><a href="http://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html" rel="bookmark" title="Permalink to Building powerful image classification models using very little data">Building powerful image classification models using very little data</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2016-06-05T00:00:00+02:00">
Sun 05 June 2016
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://twitter.com/fchollet">Francois Chollet</a>
</address>
<p>In <a href="http://blog.keras.io/category/tutorials.html">Tutorials</a>. </p>
</p></footer><!-- /.post-info --><!-- /.post-info -->
<p>In this tutorial, we will present a few simple yet effective methods that you can use to build a powerful image classifier, using only very few training examples --just a few hundred or thousand pictures from each class you want to be able to recognize. </p>
<p>We will go over the ...</p>
<a class="readmore" href="http://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
<li><article class="hentry">
<header>
<h1><a href="http://blog.keras.io/building-autoencoders-in-keras.html" rel="bookmark" title="Permalink to Building Autoencoders in Keras">Building Autoencoders in Keras</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2016-05-14T00:00:00+02:00">
Sat 14 May 2016
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://twitter.com/fchollet">Francois Chollet</a>
</address>
<p>In <a href="http://blog.keras.io/category/tutorials.html">Tutorials</a>. </p>
</p></footer><!-- /.post-info --><!-- /.post-info -->
<p>In this tutorial, we will answer some common questions about autoencoders, and we will cover code examples of the following models:</p>
<ul>
<li>a simple autoencoder based on a fully-connected layer</li>
<li>a sparse autoencoder</li>
<li>a deep fully-connected autoencoder</li>
<li>a deep convolutional autoencoder</li>
<li>an image denoising model</li>
<li>a sequence-to-sequence autoencoder</li>
<li>a variational autoencoder ...</li></ul>
<a class="readmore" href="http://blog.keras.io/building-autoencoders-in-keras.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
<li><article class="hentry">
<header>
<h1><a href="http://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html" rel="bookmark" title="Permalink to Keras as a simplified interface to TensorFlow: tutorial">Keras as a simplified interface to TensorFlow: tutorial</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2016-04-24T00:00:00+02:00">
Sun 24 April 2016
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://twitter.com/fchollet">Francois Chollet</a>
</address>
<p>In <a href="http://blog.keras.io/category/tutorials.html">Tutorials</a>. </p>
</p></footer><!-- /.post-info --><!-- /.post-info -->
<h1>A complete guide to using Keras as part of a TensorFlow workflow</h1>
<p>If TensorFlow is your primary framework, and you are looking for a simple & high-level model definition interface to make your life easier, this tutorial is for you.</p>
<p>Keras layers and models are fully compatible with pure-TensorFlow tensors, and ...</p>
<a class="readmore" href="http://blog.keras.io/keras-as-a-simplified-interface-to-tensorflow-tutorial.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
<li><article class="hentry">
<header>
<h1><a href="http://blog.keras.io/introducing-keras-10.html" rel="bookmark" title="Permalink to Introducing Keras 1.0">Introducing Keras 1.0</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2016-04-11T00:00:00+02:00">
Mon 11 April 2016
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://twitter.com/fchollet">Francois Chollet</a>
</address>
<p>In <a href="http://blog.keras.io/category/news.html">News</a>. </p>
</p></footer><!-- /.post-info --><!-- /.post-info -->
<p>Keras was initially released a year ago, late March 2015. It has made tremendous progress since, both on the development front, and as a community.</p>
<p>But continuous improvement isn't enough. A year of developing Keras, using Keras, and getting feedback from thousands of users has taught us a lot ...</p>
<a class="readmore" href="http://blog.keras.io/introducing-keras-10.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
<li><article class="hentry">
<header>
<h1><a href="http://blog.keras.io/how-convolutional-neural-networks-see-the-world.html" rel="bookmark" title="Permalink to How convolutional neural networks see the world">How convolutional neural networks see the world</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2016-01-30T00:00:00+01:00">
Sat 30 January 2016
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://twitter.com/fchollet">Francois Chollet</a>
</address>
<p>In <a href="http://blog.keras.io/category/demo.html">Demo</a>. </p>
</p></footer><!-- /.post-info --><!-- /.post-info -->
<h2>An exploration of convnet filters with Keras</h2>
<p>In this post, we take a look at what deep convolutional neural networks (convnets) really learn, and how they understand the images we feed them. We will use Keras to visualize inputs that maximize the activation of the filters in different layers of ...</p>
<a class="readmore" href="http://blog.keras.io/how-convolutional-neural-networks-see-the-world.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
<li><article class="hentry">
<header>
<h1><a href="http://blog.keras.io/keras-now-running-on-tensorflow.html" rel="bookmark" title="Permalink to Keras, now running on TensorFlow">Keras, now running on TensorFlow</a></h1>
</header>
<div class="entry-content">
<footer class="post-info">
<abbr class="published" title="2015-12-01T00:00:00+01:00">
Tue 01 December 2015
</abbr>
<address class="vcard author">
By <a class="url fn" href="https://twitter.com/fchollet">Francois Chollet</a>
</address>
<p>In <a href="http://blog.keras.io/category/news.html">News</a>. </p>
</p></footer><!-- /.post-info --><!-- /.post-info -->
<p>The purpose of Keras is to be a model-level framework, providing a set of "Lego blocks" for building Deep Learning models in a fast and straightforward way.
Among Deep Learning frameworks, Keras is resolutely high up on the ladder of abstraction.</p>
<p>As such, Keras does not handle itself low-level tensor ...</p>
<a class="readmore" href="http://blog.keras.io/keras-now-running-on-tensorflow.html">read more</a>
</div><!-- /.entry-content -->
</article></li>
</ol><!-- /#posts-list -->
</section><!-- /#content -->
<footer id="footer" class="body">
<address id="about" class="vcard body">
Powered by <a href="http://alexis.notmyidea.org/pelican/">pelican</a>, which takes great advantages of <a href="http://python.org">python</a>.
</address><!-- /#about -->
</footer><!-- /#footer -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-61785484-1");
pageTracker._trackPageview();
} catch(err) {}</script>
</body>
</html>