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

How to use lime with 3 dim time series data and lstm #741

Open
CynthiaLong opened this issue Apr 1, 2024 · 0 comments
Open

How to use lime with 3 dim time series data and lstm #741

CynthiaLong opened this issue Apr 1, 2024 · 0 comments

Comments

@CynthiaLong
Copy link

CynthiaLong commented Apr 1, 2024

Hi,

I am trying to use lime on my lstm model which is a classifier based on 3 dimensional numerical time series data (num_samples, num_time_length (varying depending on sample), num_features). Is there any existing method that can fits my model?

I modified code based on this tutorial. It seems that it only accepts string or bytes-like object.

code:

save_dir = './lstm_240324.pt'
model = torch.load(save_dir)
from lime.lime_text import LimeTextExplainer
class_names=['negative','positive']
explainer= LimeTextExplainer(class_names=class_names)
def predict_proba(arr):
    pred = model(arr.to(torch.float32))
    # print(pred.detach().numpy().shape)
    return pred.detach().numpy()
predict_proba(test_sentences[0]).shape

explainer.explain_instance(test_sentences[0].detach().numpy(), predict_proba).show_in_notebook(text=True)

error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[17], line 1
----> 1 explainer.explain_instance(test_sentences[:1],predict_proba).show_in_notebook(text=True)

File E:\Anaconda\envs\lime_env\Lib\site-packages\lime\lime_text.py:409, in LimeTextExplainer.explain_instance(self, text_instance, classifier_fn, labels, top_labels, num_features, num_samples, distance_metric, model_regressor)
    368 def explain_instance(self,
    369                      text_instance,
    370                      classifier_fn,
   (...)
    375                      distance_metric='cosine',
    376                      model_regressor=None):
    377     """Generates explanations for a prediction.
    378 
    379     First, we generate neighborhood data by randomly hiding features from
   (...)
    403         explanations.
    404     """
    406     indexed_string = (IndexedCharacters(
    407         text_instance, bow=self.bow, mask_string=self.mask_string)
    408                       if self.char_level else
--> 409                       IndexedString(text_instance, bow=self.bow,
    410                                     split_expression=self.split_expression,
    411                                     mask_string=self.mask_string))
    412     domain_mapper = TextDomainMapper(indexed_string)
    413     data, yss, distances = self.__data_labels_distances(
    414         indexed_string, classifier_fn, num_samples,
    415         distance_metric=distance_metric)

File E:\Anaconda\envs\lime_env\Lib\site-packages\lime\lime_text.py:114, in IndexedString.__init__(self, raw_string, split_expression, bow, mask_string)
    110 else:
    111     # with the split_expression as a non-capturing group (?:), we don't need to filter out
    112     # the separator character from the split results.
    113     splitter = re.compile(r'(%s)|$' % split_expression)
--> 114     self.as_list = [s for s in splitter.split(self.raw) if s]
    115     non_word = splitter.match
    117 self.as_np = np.array(self.as_list)

TypeError: expected string or bytes-like object, got 'list'

Any suggestions are welcome. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant