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

AttributeError: module 'gensim.models.keyedvectors' has no attribute 'BaseKeyedVectors' #67

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions responsibly/we/bias.py
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ def _extract_neutral_words(self, specific_words):
extended_specific_words.add(word.upper())
extended_specific_words.add(word.title())

neutral_words = [word for word in self.model.vocab
neutral_words = [word for word in self.model.key_to_index
if word not in extended_specific_words]

return neutral_words
Expand Down Expand Up @@ -897,7 +897,7 @@ def learn_full_specific_words(self, seed_specific_words,
data = []
non_specific_example_count = 0

for word in self.model.vocab:
for word in self.model.key_to_index:
is_specific = word in seed_specific_words

if not is_specific:
Expand All @@ -923,7 +923,7 @@ def learn_full_specific_words(self, seed_specific_words,
clf.fit(X, y)

full_specific_words = []
for word in self.model.vocab:
for word in self.model.key_to_index:
vector = [normalize(self[word])]
if clf.predict(vector):
full_specific_words.append(word)
Expand Down
27 changes: 14 additions & 13 deletions responsibly/we/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
from sklearn.cluster import KMeans
from sklearn.manifold import TSNE
from sklearn.metrics import accuracy_score

from gensim.models import KeyedVectors

WORD_EMBEDDING_MODEL_TYPES = (gensim.models.keyedvectors.KeyedVectors,
gensim.models.keyedvectors.BaseKeyedVectors,
# gensim.models.keyedvectors.BaseKeyedVectors,
gensim.models.fasttext.FastText,
gensim.models.word2vec.Word2Vec,
gensim.models.base_any2vec.BaseWordEmbeddingsModel,) # pylint: disable=line-too-long

gensim.models.word2vec.Word2Vec,)
# gensim.models.base_any2vec.BaseWordEmbeddingsModel,) # pylint: disable=line-too-long

def round_to_extreme(value, digits=2):
place = 10**digits
Expand Down Expand Up @@ -85,9 +84,11 @@ def cosine_similarities_by_words(model, word, words):


def update_word_vector(model, word, new_vector):
model.vectors[model.vocab[word].index] = new_vector
if model.vectors_norm is not None:
model.vectors_norm[model.vocab[word].index] = normalize(new_vector)
model.vectors[model.key_to_index[word]] = new_vector
if model.get_vector is not None:
# model.vectors_norm[model.key_to_index[word]] = normalize(new_vector)
model.get_vector(word, norm=True) == normalize(new_vector)



def generate_one_word_forms(word):
Expand Down Expand Up @@ -166,7 +167,7 @@ def most_similar(model, positive=None, negative=None,
if negative is None:
negative = []

model.init_sims()
# model.init_sims()

if (isinstance(positive, string_types)
and not negative):
Expand Down Expand Up @@ -199,8 +200,8 @@ def most_similar(model, positive=None, negative=None,
mean.append(weight * word)
else:
mean.append(weight * model.word_vec(word, use_norm=True))
if word in model.vocab:
all_words.add(model.vocab[word].index)
if word in model.key_to_index:
all_words.add(model.key_to_index[word])

if not mean:
raise ValueError("Cannot compute similarity with no input.")
Expand All @@ -210,8 +211,8 @@ def most_similar(model, positive=None, negative=None,
if indexer is not None:
return indexer.most_similar(mean, topn)

limited = (model.vectors_norm if restrict_vocab is None
else model.vectors_norm[:restrict_vocab])
limited = (model.get_vector if restrict_vocab is None
else model.get_vector(restrict_vocab))
dists = limited @ mean

if topn is None:
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ def build_description():
"matplotlib >= 2.2, < 3",
"seaborn >= 0.9",
"scikit-learn >= 0.19",
"gensim >= 3.7, < 3.8",
"gensim >= 3.7, <= 4.0",
"tabulate >= 0.8",
"six >= 1.10",
"click >= 6.0",
"tqdm >= 4.24",
"mlxtend >= 0.13, < 0.17",
# "mlxtend >= 0.13, < 0.17",
"mlxtend >= 0.13, < 0.23.0",
],
)