fzi-forschungszentrum-informatik / TSInterpret

An Open-Source Library for the interpretability of time series classifiers

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Native Guide explainer - issue generating the CF graph

hugo-rddi opened this issue · comments

Resource / Tutorial for reference
https://fzi-forschungszentrum-informatik.github.io/TSInterpret/Notebooks/NunCF_torch/

Context :

  • X (2400 time series of 1488 values) - shape=(2400, 1488)

  • We reshaped X according to what is done in the tutorial to have X.shape=(2400, 1, 1488).

  • y (labels) - shape=(2400,1)

  • both of them split in train/validation sets

All went well, including the NativeGuide model declaration.

exp_model = NativeGuideCF(
    model,
    (X_train, y_train),
    backend='PYT', 
    mode='feat',
    method='NUN_CF'
)

However issue starts at this part :

cf_ts, cf_label = exp_model.explain(
    input_x.numpy(),
    np.argmax(model(input_x).detach(), axis=1)
)

where

  • input_x.shape=(1, 1, 1488) is the instance that we want to explain.

The code generates the following error :

    229 while prob_target > 0.5 and counter < max_iter:
    230     subarray_length += 1
--> 231     most_influencial_array = self._findSubarray(
    232         (training_weights), subarray_length
    233     )
    234     starting_point = np.where(training_weights == most_influencial_array[0])[0][
    235         0
    236     ]
    237     X_example = instance.copy().reshape(1, -1)
...
---> 43 result = getattr(asarray(obj), method)(*args, **kwds)
     44 if wrap:
     45     if not isinstance(result, mu.ndarray):

ValueError: attempt to get argmax of an empty sequence 

Only way to make this cell work is to replace np.argmax(...) by one of the label that is not argmax, which doesnt really makes sense imo.

Do you have an idea of what is happening ?
Thanks for your help

Hi @hugo-rddi,

thanks for bringing this to our attention.

There was actually a small error in the function _findSubarray, it should be fixed now.

Install the current version with
pip install https://github.com/fzi-forschungszentrum-informatik/TSInterpret/archive/refs/heads/main.zip

The fixes will be available via pypi end of next week with version 0.3.3.

Feel free to provide feedback and let me know if it works (also if you find other issues).

Unfortunately, the installation failed... Any idea why ?

An error occurred while installing confection==0.1.1 ; python_version >= '3.6' 
An error occurred while installing debugpy==1.6.7

I'll double check myself too just in case

Interesting, I will also recheck.

Unfortunately, so far I was not able to replicate the issue with python3.10.
I am also not really familiar with the package names (at least no direct dependency from TSInterpret point to those). They also do not appear if I call pip list after the pip install. Can you maybe add the complete log / error?

Okay let's forget this part as it might be an isolated issue on my side !


Something else came up at the same stage that the one initially presented in this issue.
As I said, X.shape=(2400, 1488). It has been split into train/ validation with :

  • X_train composed by 1920 TimeSeries/TS/sequence
  • X_val by 480 TimeSeries/TS/sequence

Usual processing steps done

train_x = X_train.reshape(-1,1,  X_train.shape[-2])
val_x = X_val.reshape(-1,1,  X_val.shape[-2])
train_y = enc1.transform(y_train.reshape(-1,1))
val_y = enc1.transform(y_val.reshape(-1,1))

model.eval()

exp_model = NativeGuideCF(
    model,
    (train_x, train_y),
    backend='PYT', 
    mode='feat',
    method='NUN_CF'
)
input_x = val_x[10].reshape(1,1,-1)
y_target = model(torch.from_numpy(input_x).float()).detach().numpy()

Then, executing this cell raised the following error :

cf_ts, cf_label = exp_model.explain(
    input_x,
    np.argmax(y_target, axis=1)
)
    142 knn = KNeighborsTimeSeries(n_neighbors=n_neighbors, metric=distance)
...
--> 144 dist, ind = knn.kneighbors(query.reshape(1, ts_length, 1), return_distance=True)
    145 x_train.reshape(-1, 1, ts_length)
    146 return dist[0], x_train[np.where(y != predicted_label)][ind[0]]

ValueError: cannot reshape array of size 480 into shape (1,1920,1)

There is an obvious issue with train and val shapes but Idk why... Is the XAI model supposed to be declared with the whole dataset?

Just for me to understand: Your training time series has 1920 time steps , i.e. -1,1920 and your testing 480 --> so different sequence lengths ?

If that's the case I might need to tweak the code a bit.

No, 1488 time steps.
1920 and 480 are the training / validation sizes.

The TS letters stand for Time-series, sorry for the confusion.
But in that case may be the initial reshape wasnt done correctly?

yes it seems like smth went wrong with the inital reshape. Maybe check both the shape of train_x and input_x after reshaping.
I think :
train_x = X_train.reshape(-1,1, X.shape[-1])
should solve it, if your variable is named X and the shape is as indicated above.

All good.

I think our sequences was in a different initial shape.
We changed for :

train_x = X_train.reshape(-2,1,  X_train.shape[-1])
val_x = X_val.reshape(-2,1,  X_val.shape[-1])

It's working and your update made the graph generates !
Thanks for your reactivity and expertise

Your welcome,.
I will close this issue.