Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Keras dependency #2937

Merged
merged 3 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
273 changes: 0 additions & 273 deletions docs/notebooks/keras_wrapper.ipynb

This file was deleted.

41 changes: 1 addition & 40 deletions gensim/models/keyedvectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1588,55 +1588,16 @@ def intersect_word2vec_format(self, fname, lockf=0.0, binary=False, encoding='ut
self.vectors_lockf[self.get_index(word)] = lockf # lock-factor: 0.0=no changes
logger.info("merged %d vectors into %s matrix from %s", overlap_count, self.wv.vectors.shape, fname)

def get_keras_embedding(self, train_embeddings=False):
"""Get a Keras 'Embedding' layer with weights set as the Word2Vec model's learned word embeddings.

Parameters
----------
train_embeddings : bool
If False, the weights are frozen and stopped from being updated.
If True, the weights can/will be further trained/updated.

Returns
-------
`keras.layers.Embedding`
Embedding layer.

Raises
------
ImportError
If `Keras <https://pypi.org/project/Keras/>`_ not installed.

Warnings
--------
Current method work only if `Keras <https://pypi.org/project/Keras/>`_ installed.

"""
try:
from keras.layers import Embedding
except ImportError:
raise ImportError("Please install Keras to use this function")
weights = self.vectors

# set `trainable` as `False` to use the pretrained word embedding
# No extra mem usage here as `Embedding` layer doesn't create any new matrix for weights
layer = Embedding(
input_dim=weights.shape[0], output_dim=weights.shape[1],
weights=[weights], trainable=train_embeddings
)
return layer

def _upconvert_old_d2vkv(self):
"""Convert a deserialized older Doc2VecKeyedVectors instance to latest generic KeyedVectors"""

self.vocab = self.doctags
self._upconvert_old_vocab() # destroys 'vocab', fills 'key_to_index' & 'extras'
for k in self.key_to_index.keys():
old_offset = self.get_vecattr(k, 'offset')
true_index = old_offset + self.max_rawint + 1
self.key_to_index[k] = true_index
del self.expandos['offset'] # no longer needed
if(self.max_rawint > -1):
if self.max_rawint > -1:
self.index_to_key = list(range(0, self.max_rawint + 1)) + self.offset2doctag
else:
self.index_to_key = self.offset2doctag
Expand Down
Loading