Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Training LSTM for time series data #869

Open
AliNaqvi110 opened this issue Dec 21, 2023 · 0 comments
Open

Training LSTM for time series data #869

AliNaqvi110 opened this issue Dec 21, 2023 · 0 comments

Comments

@AliNaqvi110
Copy link

Hi I want to predict the High price for stock price, using other multivariate features,
I'm using the following code snippet to do so
import pandas as pd
import torch
from tsai.models.RNN import RNN
from tsai.data.core import TSDataset, TSDataLoaders
from tsai.utils import accuracy

Load your DataFrame (assuming a time index)

data = pd.read_csv("your_data.csv", index_col="date_time") # Adjust file path

Separate features and target

features = ["Open", "Low", "Close", "Volume"] # Adjust feature names
target_col = "High"

# Reshape data for time series (assuming observations are columns)

data = data.transpose()

Split into training and validation sets

train_df, valid_df = train_test_split(data, test_size=0.2, random_state=42, shuffle=False)
train_df = train_df.transpose()
valid_df = valid_df.transpose()

print(train_df.shape)
print(valid_df.shape)

Create TSDataset objects directly from DataFrames

Create TSDataset objects from NumPy arrays

train_ds = TSDataset(train_df.to_numpy()) # Convert to NumPy array
valid_ds = TSDataset(valid_df.to_numpy()) # Convert to NumPy array
print('yes')
print(train_df.to_numpy().shape)
print(valid_df.to_numpy().shape)

Define the model

model = RNN(
c_in=len(features),
c_out=1,
hidden_size=100,
n_layers=2,
bidirectional=True,
rnn_dropout=0.5,
fc_dropout=0.5
)

Create TSDataLoaders

bs = 16
dls = TSDataLoaders.from_dsets(train_ds, valid_ds, bs=bs, num_workers=0, shuffle=False)

Select MSE loss function

loss_func = nn.MSELoss()

Create the Learner

learn = Learner(dls, model, loss_func=loss_func, metrics=accuracy)

Train the model

learn.fit_one_cycle(1, 3e-3) # Adjust epochs and learning rate

but I'm getting this error again again, even bard can't recognize this error
"IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)",

Can you let me know what mistake I'm making or what I'm missing to fix the error, I need this badly to be affixed. Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant