rgrenz/rdflime-util

'LimeRdfExplainer' object has no attribute 'w2v_dump'

Closed this issue · 2 comments

Getting error:

'LimeRdfExplainer' object has no attribute 'w2v_dump' when following the example from https://github.com/rgrenz/rdflime-util/blob/main/notebooks/3_RDFLIME.ipynb

To facilitate things, I've reduced the movies sample to just 400. After completing the embeddings and classifier part, when running the following piece:


#reload(lime.lime_rdf)
from lime.lime_rdf import LimeRdfExplainer

# TODO ! Check if entities in correct order

explainer = LimeRdfExplainer(
    transformer=transformer, 
    entities=entities,
    class_names=clf.classes_,
    kernel=None,
    kernel_width=25,
    verbose=False,
    feature_selection="auto"
)

# Attention: Index value, not row number!
explained_idx = 1
explained_entity_row = dataset.loc[explained_idx]
explained_entity_uri = explained_entity_row['DBpedia_URI16']
embedding = transformer._embeddings[transformer._entities.index(explained_entity_uri)]
prediction = clf.predict_proba([embedding])

print("Explaining", explained_entity_uri)
print("Original prediction:", prediction, " / ".join(clf.classes_))
print("True class:", explained_entity_row['label'])

data, probabilities, distances, explanation = explainer.explain_instance(
    entity=explained_entity_uri, 
    classifier_fn=clf.predict_proba,
    num_features=10,
    num_samples=50,
    allow_triple_addition=False,
    allow_triple_subtraction=True,
    max_changed_triples=5,
    change_count_fixed=True,
    use_w2v_freeze=True,
    center_correction=False,
    single_run=False,
    train_with_all=False,
    distance_metric="cosine",
    model_regressor=None,
    short_uris=False,
    labels=(0,1)
)

Getting:

Explaining http://dbpedia.org/resource/Ratatouille_(film)
Original prediction: [[0.48323835 0.51676165]] bad / good
True class: good
Preparing perturbed walks: 100%|██████████| 50/50 [00:00<00:00, 3542.79it/s]
Average remaining walks per artificial entity (from 484): 5.6
Training perturbed W2V embeddings:   0%|          | 0/50 [00:00<?, ?it/s]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[<ipython-input-73-71d3944b2a8a>](https://localhost:8080/#) in <module>
     41     model_regressor=None,
     42     short_uris=False,
---> 43     labels=(0,1)
     44 )

2 frames
[/usr/local/lib/python3.7/dist-packages/lime/lime_rdf.py](https://localhost:8080/#) in get_perturbed_embedding(self, entity, perturbed_entity_walks, entity_suffix)
    294 
    295         # Clone w2v instance
--> 296         w2v = pickle.loads(self.w2v_dump)
    297         # w2v.wv = pickle.loads(self.wv_dump)
    298         wv = w2v.wv

AttributeError: 'LimeRdfExplainer' object has no attribute 'w2v_dump'

First time I run the code for a different entity, I get:

Explaining http://dbpedia.org/resource/Jolene_(film)
Original prediction: [[0.48890381 0.51109619]] bad / good
True class: bad
Preparing perturbed walks: 100%|██████████| 50/50 [00:00<00:00, 3078.66it/s]
Average remaining walks per artificial entity (from 484): 5.3
Training perturbed W2V embeddings:   0%|          | 0/50 [00:00<?, ?it/s]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[<ipython-input-24-88f133c7605a>](https://localhost:8080/#) in <module>
     42     model_regressor=None,
     43     short_uris=False,
---> 44     labels=(0,1)
     45 )

2 frames
[/usr/local/lib/python3.7/dist-packages/lime/lime_rdf.py](https://localhost:8080/#) in get_perturbed_embedding(self, entity, perturbed_entity_walks, entity_suffix)
    286 
    287             if self.use_w2v_freeze:
--> 288                 freeze_vector = np.zeros(len(wv), dtype=np.float32)
    289                 freeze_vector[-1] = 1
    290                 w2v.wv.vectors_lockf = freeze_vector

TypeError: object of type 'Word2VecKeyedVectors' has no len()

Finally, I was able to overcome the error by configuring:

    use_w2v_freeze=False,

Working now, though the explanations have some encoding issues that make them difficult to interpret:

explanation.as_list(label=1)

[(('http://dbpedia.org/resource/Ratatouille_(film)',
   'http://dbpedia.org/ontology/musicComposer',
   "b'\\x14\\xd6\\xc3\\xab\\x95\\x02\\xca\\x13'"),
  1.4000199261142489e-06),
 (("b'\\x99U\\xe7\\x01\\xa0\\xdb\\xd2\\x7f'",
   'http://www.w3.org/1999/02/22-rdf-syntax-ns#type',
   "b'\\xff\\xea\\xf0\\xd3fL1!'"),
  -1.3621930950623729e-06),
 (("b'\\xb0\\xf7\\x7f\\x8c\\xd5\\x94\\x8d\\xca'",
   'http://www.w3.org/2004/02/skos/core#prefLabel',
   "b'\\x8a\\x8e\\xf0}\\x12H\\x8e\\xe5'"),
  1.026473208977316e-06),
...