i have historical data of crypto i have following code to train a lstm model on my data…
- User
i have historical data of crypto i have following code to train a lstm model on my data check and see if it has any problems:
%%
import numpy as np import pandas as pd from tensorflow.keras.models import Sequential from tensorflow.keras.layers import LSTM, Dense from sklearn.preprocessing import MinMaxScaler import os
%%
def load_and_process_data(file_path, target_col, scaler, sequence_length, batch_size = 32): # Load data data = pd.read_csv(file_path)
# Split features and labels X = data.drop([ 'Date', 'Symbol', 'y_High_1d', 'y_Low_1d', 'y_Priority_1d', 'y_High_2d', 'y_Low_2d', 'y_Priority_2d', 'y_High_3d', 'y_Low_3d', 'y_Priority_3d', 'y_High_5d', 'y_Low_5d', 'y_Priority_5d'], axis=1).values y_high_1d = data['y_High_1d'].values y_low_1d = data['y_Low_1d'].values y_priority_1d = data['y_Priority_1d'].values y_high_2d = data['y_High_2d'].values y_low_2d = data['y_Low_2d'].values y_priority_2d = data['y_Priority_2d'].values y_high_3d = data['y_High_3d'].values y_low_3d = data['y_Low_3d'].values y_priority_3d = data['y_Priority_3d'].values y_high_5d = data['y_High_5d'].values y_low_5d = data['y_Low_5d'].values y_priority_5d = data['y_Priority_5d'].values # Optionally, apply preprocessing here (e.g., Scaler transform) X = scaler.fit_transform(X) X = scaler.transform(X) # # Create sequences # X_seq, y_seq = [], [] # for i in range(len(X) - sequence_length): # X_seq.append(X[i:i+sequence_length]) # y_seq.append(y[i+sequence_length]) # return np.array(X_seq), np.array(y_seq) # Splitting data into batches for i in range(0, len(data), batch_size): end = i + batch_size X_batch = X[i:end] y_high_batch_1d = y_high_1d[i:end] y_low_batch_1d = y_low_1d[i:end] y_priority_batch_1d = y_priority_1d[i:end] y_high_batch_2d = y_high_2d[i:end] y_low_batch_2d = y_low_2d[i:end] y_priority_batch_2d = y_priority_2d[i:end] y_high_batch_3d = y_high_3d[i:end] y_low_batch_3d = y_low_3d[i:end] y_priority_batch_3d = y_priority_3d[i:end] y_high_batch_5d = y_high_5d[i:end] y_low_batch_5d = y_low_5d[i:end] y_priority_batch_5d = y_priority_5d[i:end] yield X_batch, [y_high_batch_1d, y_low_batch_1d,y_priority_batch_1d, y_high_batch_2d, y_low_batch_2d,y_priority_batch_2d, y_high_batch_3d, y_low_batch_3d, y_priority_batch_3d, y_high_batch_5d, y_low_batch_5d, y_priority_batch_5d]%%
scaler = MinMaxScaler(feature_range=(0, 1))
Normally, you'd fit the scaler on your dataset. You'll need to adapt this to your scenario.
For demonstration, let's assume all features are within 0 and 100.
%%
def train_model(files, batch_size, epochs): target_cols =[ 'y_High_1d', 'y_Low_1d', 'y_Priority_1d', 'y_High_2d', 'y_Low_2d', 'y_Priority_2d', 'y_High_3d', 'y_Low_3d', 'y_Priority_3d', 'y_High_5d', 'y_Low_5d', 'y_Priority_5d']
for epoch in range(epochs): data_generator = load_and_process_data(file_path, target_cols, scaler, sequence_length=30, batch_size = batch_size) for X_batch, y_batch in data_generator: # Here’s how you iterate over the generator model.train_on_batch(X_batch, y_batch) - Assistant
- User
how do i correct Following up from the previous point, your data generator yields batches but does not consider the sequence creation for LSTM inputs. This is incorrect as LSTM requires input data in the form [samples, time steps, features]. You must integrate the sequence creation logic into or before the generator’s batch splitting logic.
- Assistant
- User
but in your provided code you generated y_batch just for y_High_1d i have 11 others too
- Assistant