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
4.92k stars 622 forks source link

Is it possible to do multitasking (or to predict a list)? #801

Closed GitGabbo closed 1 year ago

GitGabbo commented 1 year ago

I'd like to predict, starting from the same input, an array of outputs. I have the 3d numpy array X of shape (229, 18, 164) and I'd want to pass to the TSDatasets also a Y that has a shape of (229, 5), e.g. array([[4, 4, 5, 5, 5], [4, 4, 5, 5, 5], [4, 4, 5, 5, 5], ...]).

The question is, is it possible somehow? What tfms/tls should I use?

oguiza commented 1 year ago

Hi @GitGabbo, All you need to do to create an output with any dimension is to prepare a y with the desired dimension and choose any of the models ending with "Plus". tsai will prepare the model to automatically generate the required output shape. For example:

X = random_rand(229, 18, 164)
y = random_randint(0, 4, size=(229, 5)) # 4 classes
splits = TSSplitter(show_plot=False)(y)
print(X.shape, y.shape)
tfms = [None, TSClassification()]
batch_tfms = TSStandardize()
dls = get_ts_dls(X, y, splits=splits, tfms=tfms, batch_tfms=batch_tfms)
learn = ts_learner(dls, 'InceptionTimePlus', metrics=accuracy, cbs=[ShowGraph()])
xb, yb = dls.train.one_batch()
print(learn.model.to(xb.device)(xb).shape)
# torch.Size([64, 5, 4])

So you can train it using:

learn.fit_one_cycle(1, 1e-3)
GitGabbo commented 1 year ago

It was exactly what I was looking for. Thank you very much @oguiza for your quick response and for your fantastic work!

oguiza commented 1 year ago

Closed based on previous response.