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.19k stars 649 forks source link

RuntimeError: Can only calculate the mean of floating types. Got Long instead. #292

Closed maheshs11 closed 2 years ago

maheshs11 commented 2 years ago

RuntimeError: Can only calculate the mean of floating types. Got Long instead. image dls = TSDataLoaders.from_dsets(dsets.train, dsets.valid, bs=64, batch_tfms=TSStandardize(by_var=True))

if batch_tfms=TSStandardize(by_var=True) is removed image RuntimeError: expected scalar type Long but found Float

oguiza commented 2 years ago

Hi @maheshs11, Based on what you've shared, it looks like X dtype is int. I'm not sure what y dtype is (looks like y too). TST only accepts float data as X. However y can be of type int (for classification tasks). So you should convert X to float before creating the dataloader.

maheshs11 commented 2 years ago

@oguiza thanks for the reply! can u share the usage of predict function? learn.predict( format? )

maheshs11 commented 2 years ago

Labeled test data

test_ds = dls.dataset.add_test(X, y)# In this case I'll use X and y, but this would be your test data test_dl = dls.new(test_ds) next(iter(test_dl))

got this code ,format for X, y and its usage? i gave train data only it predicted for 64 sample .

vrodriguezf commented 2 years ago

Have a look at learn.get_X_preds to do inference

maheshs11 commented 2 years ago

Have a look at learn.get_X_preds to do inference @oguiza image

gives a tensor list and prediction , what is this tensor list ?

as soon as my data goes into this code X, y = SlidingWindow(window_length, stride=stride, horizon=horizon)(df) my single coloumn converts to (123,1,5)

it would be simpler to understand output if there would be a parameter csv = true . which does k = X.reshape(X[0],x[2],order='c') X is converted array 3d to 2d k = pd.DataFrame(k) k['y_act'] = y y actual k['y_pred'] = y_pred y is predicted one k.to_csv('train.csv',index = False)

oguiza commented 2 years ago

@maheshs11,

I've updated tsai's documentation. It's hopefully more clear now:

Once you've read it please let me know if you have any further questions before closing this issue.

maheshs11 commented 2 years ago

@oguiza image

my categories are (1,2,3) tfms = [None, [Categorize()]] this changes to (0,1,2 ) is this necessary ? if removed i will get target out of bound error. image in this the label is (0,1,2) it is predicting only (1,2) is there anything like mv_learn ? for multiple class univariate? or my model error? model = TST(dls.vars, dls.c, dls.len, fc_dropout=0.9) learn = Learner(dls, model, loss_func=LabelSmoothingCrossEntropyFlat(), metrics=[accuracy], cbs=ShowGraphCallback2())

oguiza commented 2 years ago

Could you please use this function and share the output?

my_setup()

and do the same with this:

check_data(X, y=None, splits=None, show_plot=True)

Otherwise I don't know what type of data and versions you are using, so I can't help you.

maheshs11 commented 2 years ago

image

oguiza commented 2 years ago

You are not using y or splits?

maheshs11 commented 2 years ago

image i am using.

oguiza commented 2 years ago

I've tested some code similar to yours and the issue seems to be related to the LabelSmoothingCrossEntropyFlat loss. I get an error when I use it and export the model, but it works fine if I use the default loss (CrossEntropyLossFlat). LabelSmoothingCrossEntropyFlat comes from fastai. You may want to follow up with them directly. This issue doesn't seem to be related to tsai.

Here's the error I get (which is different from yours):

---------------------------------------------------------------------------
PicklingError                             Traceback (most recent call last)
<ipython-input-52-0e453cf9a470> in <module>
----> 1 learn.export("clf.pkl")

~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/fastai/learner.py in export(self, fname, pickle_module, pickle_protocol)
    373         #To avoid the warning that come from PyTorch about model not being checked
    374         warnings.simplefilter("ignore")
--> 375         torch.save(self, self.path/fname, pickle_module=pickle_module, pickle_protocol=pickle_protocol)
    376     self.create_opt()
    377     if state is not None: self.opt.load_state_dict(state)

~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/torch/serialization.py in save(obj, f, pickle_module, pickle_protocol, _use_new_zipfile_serialization)
    377         if _use_new_zipfile_serialization:
    378             with _open_zipfile_writer(opened_file) as opened_zipfile:
--> 379                 _save(obj, opened_zipfile, pickle_module, pickle_protocol)
    380                 return
    381         _legacy_save(obj, opened_file, pickle_module, pickle_protocol)

~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/torch/serialization.py in _save(obj, zip_file, pickle_module, pickle_protocol)
    482     pickler = pickle_module.Pickler(data_buf, protocol=pickle_protocol)
    483     pickler.persistent_id = persistent_id
--> 484     pickler.dump(obj)
    485     data_value = data_buf.getvalue()
    486     zip_file.write_record('data.pkl', data_value, len(data_value))

PicklingError: Can't pickle typing.Union[bool, NoneType]: it's not the same object as typing.Union

Try with a different loss function.

maheshs11 commented 2 years ago

@oguiza no still have the issue!

maheshs11 commented 2 years ago

I've tested some code similar to yours and the issue seems to be related to the LabelSmoothingCrossEntropyFlat loss. I get an error when I use it and export the model, but it works fine if I use the default loss (CrossEntropyLossFlat). LabelSmoothingCrossEntropyFlat comes from fastai. You may want to follow up with them directly. This issue doesn't seem to be related to tsai.

Here's the error I get (which is different from yours):

---------------------------------------------------------------------------
PicklingError                             Traceback (most recent call last)
<ipython-input-52-0e453cf9a470> in <module>
----> 1 learn.export("clf.pkl")

~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/fastai/learner.py in export(self, fname, pickle_module, pickle_protocol)
    373         #To avoid the warning that come from PyTorch about model not being checked
    374         warnings.simplefilter("ignore")
--> 375         torch.save(self, self.path/fname, pickle_module=pickle_module, pickle_protocol=pickle_protocol)
    376     self.create_opt()
    377     if state is not None: self.opt.load_state_dict(state)

~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/torch/serialization.py in save(obj, f, pickle_module, pickle_protocol, _use_new_zipfile_serialization)
    377         if _use_new_zipfile_serialization:
    378             with _open_zipfile_writer(opened_file) as opened_zipfile:
--> 379                 _save(obj, opened_zipfile, pickle_module, pickle_protocol)
    380                 return
    381         _legacy_save(obj, opened_file, pickle_module, pickle_protocol)

~/opt/anaconda3/envs/py36/lib/python3.6/site-packages/torch/serialization.py in _save(obj, zip_file, pickle_module, pickle_protocol)
    482     pickler = pickle_module.Pickler(data_buf, protocol=pickle_protocol)
    483     pickler.persistent_id = persistent_id
--> 484     pickler.dump(obj)
    485     data_value = data_buf.getvalue()
    486     zip_file.write_record('data.pkl', data_value, len(data_value))

PicklingError: Can't pickle typing.Union[bool, NoneType]: it's not the same object as typing.Union

Try with a different loss function.

have sent you invite to repo could u please look into it?

oguiza commented 2 years ago

@maheshs11 , If you are still interested, please, post a code that reproduces the issue. I have been unable to reproduce it. So I can't help.

maheshs11 commented 2 years ago

@maheshs11 , If you are still interested, please, post a code that reproduces the issue. I have been unable to reproduce it. So I can't help.

shared the code with you @oguiza https://github.com/maheshs11/timeseries/invitations

oguiza commented 2 years ago

@maheshs11 , Thanks for the invite to your rep, but if you want to share some code, please do it here or in a gist.

maheshs11 commented 2 years ago

@maheshs11 , Thanks for the invite to your rep, but if you want to share some code, please do it here or in a gist. https://gist.github.com/maheshs11/f6272d710da1217c8ee61d05e6bca157

oguiza commented 2 years ago

I've reviewed your code again. I'm not sure exactly what you are trying to achieve. It seems you have a time series with 1 feature (X). You take 5 steps (window size). And try to predict the next step (y with horizon 1 which is an int !!). It's weird.

I'm unable to understand what you are trying to achieve.

This code works but I?m not sure if it's helpful:

import pandas as pd

window_length = 5
stride = None
horizon=1
data = np.random.rand(200)
df = pd.DataFrame(data, columns=["var"])
print(df)
X, y = SlidingWindow(window_length, stride=stride, horizon=horizon)(df)
X = X.astype('float')
splits = get_splits(y, valid_size=.2, stratify=True, random_state=23, shuffle=False)
print(splits)

tfms = [None, [TSCategorize()]]
dsets = TSDatasets(X, y, tfms=tfms, splits=splits)
dls = TSDataLoaders.from_dsets(dsets.train, dsets.valid, bs=50)
model = TST(dls.vars, dls.c, dls.len, fc_dropout=0.9)
learn = Learner(dls, model, loss_func=CrossEntropyLossFlat(),
                metrics=[accuracy], cbs=ShowGraphCallback2())
learn.fit_one_cycle(1, 1e-4) # changed from 250 to 1 for testing
learn.export("models/clf.pkl") # make sure you set the path to a folder that already exists

from tsai.inference import load_learner
learn = load_learner("models/clf.pkl")
probas, target, preds = learn.get_X_preds(X[splits[0]], y[splits[0]])

probas, target, preds

This is the 2nd time I demonstrate a code that works. I have not been able to replicate your issue . I will consider this issue closed. Sorry.

maheshs11 commented 2 years ago

I've reviewed your code again. I'm not sure exactly what you are trying to achieve. It seems you have a time series with 1 feature (X). You take 5 steps (window size). And try to predict the next step (y with horizon 1 which is an int !!). It's weird.

I'm unable to understand what you are trying to achieve.

This code works but I?m not sure if it's helpful:

import pandas as pd

window_length = 5
stride = None
horizon=1
data = np.random.rand(200)
df = pd.DataFrame(data, columns=["var"])
print(df)
X, y = SlidingWindow(window_length, stride=stride, horizon=horizon)(df)
X = X.astype('float')
splits = get_splits(y, valid_size=.2, stratify=True, random_state=23, shuffle=False)
print(splits)

tfms = [None, [TSCategorize()]]
dsets = TSDatasets(X, y, tfms=tfms, splits=splits)
dls = TSDataLoaders.from_dsets(dsets.train, dsets.valid, bs=50)
model = TST(dls.vars, dls.c, dls.len, fc_dropout=0.9)
learn = Learner(dls, model, loss_func=CrossEntropyLossFlat(),
                metrics=[accuracy], cbs=ShowGraphCallback2())
learn.fit_one_cycle(1, 1e-4) # changed from 250 to 1 for testing
learn.export("models/clf.pkl") # make sure you set the path to a folder that already exists

from tsai.inference import load_learner
learn = load_learner("models/clf.pkl")
probas, target, preds = learn.get_X_preds(X[splits[0]], y[splits[0]])

probas, target, preds

This is the 2nd time I demonstrate a code that works. I have not been able to replicate your issue . I will consider this issue closed. Sorry.

(x) is the feature which is int and one feature but model expects float value and shape (variables, dimensions, channels) so converted it accordingly, i am trying to achieve given any set of x feature it should predict/classify the (X+1) th feature. @oguiza thanks for reply!