From 1ae3bde58f3bddbe4e360630b61cf94c76d2726a Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Thu, 20 Oct 2016 21:32:26 +0200 Subject: [PATCH 1/3] Fix deep learning example code --- examples/deep_learning_keras.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/examples/deep_learning_keras.py b/examples/deep_learning_keras.py index 857d3e020..43655c0db 100644 --- a/examples/deep_learning_keras.py +++ b/examples/deep_learning_keras.py @@ -7,6 +7,7 @@ import cytoolz import numpy from keras.models import Sequential, model_from_json from keras.layers import LSTM, Dense, Embedding, Dropout, Bidirectional +from keras.layers import TimeDistributed from keras.optimizers import Adam import cPickle as pickle @@ -48,10 +49,16 @@ class SentimentAnalyser(object): def get_features(docs, max_length): - Xs = numpy.zeros((len(list(docs)), max_length), dtype='int32') + docs = list(docs) + Xs = numpy.zeros((len(docs), max_length), dtype='int32') for i, doc in enumerate(docs): - for j, token in enumerate(doc[:max_length]): - Xs[i, j] = token.rank if token.has_vector else 0 + j = 0 + for token in doc: + if token.has_vector and not token.is_punct and not token.is_space: + Xs[i, j] = token.rank + 1 + j += 1 + if j >= max_length: + break return Xs @@ -75,9 +82,12 @@ def compile_lstm(embeddings, shape, settings): embeddings.shape[1], input_length=shape['max_length'], trainable=False, - weights=[embeddings] + weights=[embeddings], + mask_zero=True ) ) + model.add(TimeDistributed(Dense(shape['nr_hidden'] * 2))) + model.add(Dropout(settings['dropout'])) model.add(Bidirectional(LSTM(shape['nr_hidden']))) model.add(Dropout(settings['dropout'])) model.add(Dense(shape['nr_class'], activation='sigmoid')) @@ -87,11 +97,11 @@ def compile_lstm(embeddings, shape, settings): def get_embeddings(vocab): - max_rank = max(lex.rank for lex in vocab if lex.has_vector) + max_rank = max(lex.rank+1 for lex in vocab if lex.has_vector) vectors = numpy.ndarray((max_rank+1, vocab.vectors_length), dtype='float32') for lex in vocab: if lex.has_vector: - vectors[lex.rank] = lex.vector + vectors[lex.rank + 1] = lex.vector return vectors From e80944276fbcbf46cad966c337cb270644d272be Mon Sep 17 00:00:00 2001 From: Matthew Honnibal Date: Thu, 20 Oct 2016 21:58:56 +0200 Subject: [PATCH 2/3] Fix Span.vector_norm --- spacy/tokens/span.pyx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spacy/tokens/span.pyx b/spacy/tokens/span.pyx index fa081c90a..2cac2d947 100644 --- a/spacy/tokens/span.pyx +++ b/spacy/tokens/span.pyx @@ -133,8 +133,8 @@ cdef class Span: property vector_norm: def __get__(self): - if 'vector_norm' in self.doc.getters_for_spans: - return self.doc.getters_for_spans['vector'](self) + if 'vector_norm' in self.doc.user_span_hooks: + return self.doc.user_span_hooks['vector'](self) cdef float value if self._vector_norm is None: self._vector_norm = 1e-20 From 452b766d827b4265cddc4e123cc449a835d59f60 Mon Sep 17 00:00:00 2001 From: Adam Ever Hadani Date: Thu, 20 Oct 2016 14:57:18 -0700 Subject: [PATCH 3/3] added ujson dependency to setup.py --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 75c29f20e..f9e95830a 100644 --- a/setup.py +++ b/setup.py @@ -202,7 +202,8 @@ def setup_package(): 'six', 'cloudpickle', 'pathlib', - 'sputnik>=0.9.2,<0.10.0'], + 'sputnik>=0.9.2,<0.10.0', + 'ujson>=1.35'], classifiers=[ 'Development Status :: 5 - Production/Stable', 'Environment :: Console',