tsai
tsai copied to clipboard
Training LSTM for time series data
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