Reputation: 107
I am writing code for Gensim Doc2Vec model in Python 3
This is the snippet I am running:
model1.docvecs.doctag_syn0norm = (model1.docvecs.doctag_syn0 / sqrt((model1.docvecs.doctag_syn0 ** 2).sum(-1))[..., newaxis]).astype(REAL)[d_indices]
I am getting the following error:
AttributeError: can t set attribute
The right hand value is getting calculated when I tested separately but is not getting assigned.
I feel that it is a setter issue as I can't set the value to what I want for that class attribute.
Let me know if there's any work-around as I don't want to change gensim's source code.
Clone my repo and run get_labels.py in model run folder following Readme instructions to reproduce the error.
It's in line 90 of cand_gen.py
The full error stack trace is:
Extracting candidate labels
models loaded
Data Gathered
cand_generation.py:71: DeprecationWarning: Call to deprecated `syn0` (Attribute will be removed in 4.0.0, use self.vectors instead).
model1.wv.syn0norm = (model1.wv.syn0 / sqrt((model1.wv.syn0 ** 2).sum(-1))[..., newaxis]).astype(REAL)
cand_generation.py:71: DeprecationWarning: Call to deprecated `syn0norm` (Attribute will be removed in 4.0.0, use self.vectors_norm instead).
model1.wv.syn0norm = (model1.wv.syn0 / sqrt((model1.wv.syn0 ** 2).sum(-1))[..., newaxis]).astype(REAL)
cand_generation.py:89: DeprecationWarning: Call to deprecated `doctag_syn0` (Attribute will be removed in 4.0.0, use docvecs.vectors_docs instead).
model1.docvecs.doctag_syn0norm = (model1.docvecs.doctag_syn0 / sqrt((model1.docvecs.doctag_syn0 ** 2).sum(-1))[..., newaxis]).astype(REAL)[d_indices]
Traceback (most recent call last):
File "cand_generation.py", line 89, in <module>
model1.docvecs.doctag_syn0norm = (model1.docvecs.doctag_syn0 / sqrt((model1.docvecs.doctag_syn0 ** 2).sum(-1))[..., newaxis]).astype(REAL)[d_indices]
AttributeError: can t set attribute
Executing Unsupervised model
Traceback (most recent call last):
File "unsupervised_labels.py", line 33, in <module>
test_chunk_size = len(label_list[0])
IndexError: list index out of range
Executing Supervised Model
page Rank models loaded
Traceback (most recent call last):
File "supervised_labels.py", line 49, in <module>
test_chunk_size = len(label_list[0])
IndexError: list index out of range
Upvotes: 0
Views: 539
Reputation: 54153
First & generally, it's not necessarily likely a 4-year-old repository that hasn't been updated regularly would continue to work with continuously-updated versions of Gensim - which has had about 15 releases, moving from version 0.13.3 to 3.8.3, since that repository was last updated.
With such an old example, you might want to search fresher examples of what you want to do, or if you absolutely need to use that base, reach out to its original author for advice.
More specifically: your code is trying to assign into an obsolete property of the model, no longer used by the latest Gensim code. But, to maintain backward compatibility with code that read the old value, the latest code includes a property accessor only - no setter. Trying to set a value for which there is no settor method creates the error you're seeing.
Why isn't there a settor? The _norm
internal arrays have always been calculated internally by Gensim as needed (chiefly via the .init_sims()
method, before doing bulk similarity operations). It wasn't foreseen that anyone would want to assign into it – and indeed, it's not wise to do so. It wasn't wise when that code was written, either - such code should trigger Gensim's own calculation.
The absolute smallest-change to the code you're using that might work would be to both assign-into, and then later access, some other property name. That could be any property name of your choosing. (It is only the overlap with a name that used to be significant to Gensim that's causing the error.) This would retain the unwise external calculation/modification, and if Gensim's own calculation is later triggered through more normal use, it'd result in double-memory usage. But if hewing close to what the original code does, this is the minimal change. (There might be other errors, for similar reasons related to the code being outdated, after this one is overcome.)
A slightly-more compatible approach would be to fix the code you're using to trigger, and reuse, Gensim's own normed-vallues calculation instead. This would likely involve:
model.docvecs.init_sims()
instead of assigning into model1.docvecs.doctag_syn0norm
.doctag_syn0norm
to instead use the new name for the same internal property – as highlighted in the DeprecationWarning
messages in your output – which is .vectors_norm
.Good luck!
Upvotes: 1