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.14k stars 643 forks source link

Classification - how does sliding window derive the class for a window? #809

Closed E-Penguin closed 1 year ago

E-Penguin commented 1 year ago

I have a "long" dataset consisting of n features (columns) by m rows (time steps) (n >1, this is multivariate). For class labels I have added an extra column containing the label (0 or 1), so now the final column is the class label, per time step. I have put this into a SlidingWindow, per the tutorial with get_x and get_y set accordingly. For get_x I just use the first column, the target column if this were a regression problem.

The code runs well enough but I'm not sure what to interpret the output. It appears that everything is predicted class=0 and 'true' is given as class=0, however in the test split there are definitely rows (time steps) where the class=1. Is this doing some sort of average per window? How is tsai allocating a label to a given window?

image

    # Args to SlidingWindow
    window_length = 128 # 2x batch size
    stride = window_length # no overlap
    horizon = None
    get_x = 0
    get_y = data.shape[1]-1
    seq_first = True
    random_start = 0

    # Apply sliding window
    X,y = SlidingWindow(window_length, get_x = get_x, get_y=get_y)(data)

    # Get splits
    splits = get_splits(y, valid_size=0.2, stratify=True, shuffle=False, show_plot=True)
    tfms = [None, [Categorize()]]

    try:
      dsets = TSDatasets(X, y, tfms=tfms, splits=splits, inplace=True)
      dls = TSDataLoaders.from_dsets(dsets.train, dsets.valid, bs=[64, 128], batch_tfms=[TSStandardize()], num_workers=0)

      model = InceptionTime(dls.vars, dls.c)
      learn = Learner(dls, model, metrics=accuracy)
      learn.save(f'stage0_{chan_id}')

      learn.load(f'stage0_{chan_id}')
      #learn.lr_find(show_plot=True)

      learn.fit_one_cycle(25, lr_max=1e-3)
      learn.save(f'stage1_{chan_id}')

      learn.recorder.plot_metrics()

      learn.show_results()

      learn.show_probas()

      interp = ClassificationInterpretation.from_learner(learn)
      interp.plot_confusion_matrix()
    except:
        print("oh no")
vrodriguezf commented 1 year ago

If I remember correctly, you can pass an argument y_func to the sliding window to decide how you set the y value for each window

oguiza commented 1 year ago

If I remember correctly, you can pass an argument y_func to the sliding window to decide how you set the y value for each window Thanks @vrodriguezf. You are totally right.

Closed due to lack of response.