timeseriesAI / tsai

Time series Timeseries Deep Learning Machine Learning Python Pytorch fastai | State-of-the-art Deep Learning library for Time Series and Sequences in Pytorch / fastai
https://timeseriesai.github.io/tsai/
Apache License 2.0
5.21k stars 651 forks source link

XCM on multivariate regression #549

Closed Rabea007 closed 1 year ago

Rabea007 commented 2 years ago

Hi ,

I managed to use show_gradcam func on classification model and it worked well. But when I use it with regression models (through [TSRegression()] tfms):

tfms = [None, [TSRegression()]]
batch_tfms = TSStandardize(by_var=True)
dls = get_ts_dls(X, y, splits=splits, tfms=tfms, batch_tfms=batch_tfms)
xb, yb = dls.one_batch()
bs, c_in, seq_len = xb.shape
model = XCM(c_in, c_out, seq_len)  # c_out=1 for regression
learn = Learner(dls, model, metrics=mape)

(1) when yb passed as scalar: model.show_gradcam(xb[0], yb[0]) I get this error:

Traceback (most recent call last):
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-23-85cc0921a09c>", line 2, in <module>
    model.show_gradcam(xb[0], yb[0])
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/tsai/models/XCMPlus.py", line 51, in show_gradcam
    att_maps = get_attribution_map(self, [self.backbone.conv2dblock, self.backbone.conv1dblock], x, y=y, detach=detach, cpu=cpu, apply_relu=apply_relu)
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/tsai/models/explainability.py", line 42, in get_attribution_map
    A_k, w_ck = get_acts_and_grads(model, modules, x, y, detach=detach, cpu=cpu)
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/tsai/models/explainability.py", line 24, in get_acts_and_grads
    if preds.shape[0] == 1: preds[0, y].backward()
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/fastai/torch_core.py", line 341, in __torch_function__
    res = super().__torch_function__(func, types, args=args, kwargs=kwargs)
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/torch/_tensor.py", line 1051, in __torch_function__
    ret = func(*args, **kwargs)
IndexError: tensors used as indices must be long, byte or bool tensors
where `yb[0]= tensor(62.1246)` 
  or `yb[0].reshape(1,-1) = tensor([[62.1246]])`

(2) when yb passed as vector: model.show_gradcam(xb, yb)

I get this error:
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3441, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-32-00fd15dae914>", line 2, in <module>
    model.show_gradcam(xb, yb)
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/tsai/models/XCMPlus.py", line 51, in show_gradcam
    att_maps = get_attribution_map(self, [self.backbone.conv2dblock, self.backbone.conv1dblock], x, y=y, detach=detach, cpu=cpu, apply_relu=apply_relu)
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/tsai/models/explainability.py", line 42, in get_attribution_map
    A_k, w_ck = get_acts_and_grads(model, modules, x, y, detach=detach, cpu=cpu)
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/tsai/models/explainability.py", line 27, in get_acts_and_grads
    torch_slice_by_dim(preds, y).mean().backward()
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/tsai/utils.py", line 679, in torch_slice_by_dim
    return torch.gather(t, dim, index, **kwargs)
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/fastai/torch_core.py", line 341, in __torch_function__
    res = super().__torch_function__(func, types, args=args, kwargs=kwargs)
  File "/Users/rabea/.pyenv/versions/vanti-ml/lib/python3.8/site-packages/torch/_tensor.py", line 1051, in __torch_function__
    ret = func(*args, **kwargs)
RuntimeError: index 62 is out of bounds for dimension 1 with size 1
 same error, when I try to reshape yb as `model.show_gradcam(xb, yb.reshape(-1,1))`
where 
```
yb = tensor([61.8183, 63.1218, 61.2199, 60.8769, 60.8578, 61.3410, 62.9058, 58.9887,
        61.5752, 62.7453, 63.6413, 62.7808, 61.6009, 60.4122, 61.5103, 60.3366,
        63.7720, 63.0993, 62.7974, 61.9339, 62.3776, 63.3748, 64.4119, 59.9032,
        66.1629, 63.6486, 62.7214, 61.3141, 62.0019, 62.2747, 63.2539, 62.0974,
        63.5082, 64.6145, 63.0425, 61.4340, 62.2446, 59.7469, 58.5598, 62.2977,
        63.7953, 60.6709, 60.9382, 62.3146, 65.7199, 61.3860, 61.1460, 63.7503,
        65.8243, 64.8419, 62.3534, 62.0956, 58.5733, 60.5605, 63.3492, 65.0612,
        61.0339, 63.1580, 59.9684, 61.4563, 66.8889, 62.0398, 61.7930, 62.6272])
```

Do you know why this error is happening especially in the regression case and not in classification ? Is there any possible way to fix it ?

Rabea007 commented 2 years ago

anybody ?

oguiza commented 1 year ago

Hi @Rabea007 , Apologies for the very late reply. The issue is that XCM model was originally designed for classification tasks. It's gradcam module specifically shows how read datapoint impacts the probability of a sample on a particular target class. This is not possible with regression tasks.

As an alternative, you might find learn.feature_importance or learn.step_importance useful. Both support classification, regression and forecasting tasks. (docs)

oguiza commented 1 year ago

Closed due to lack of response.