I am trying to predict stock prices using an LSTM model. However, because of the noisy data, I need a custom loss with a penalizing constant. I don't know how to get the data from previous timestep tho (the argument 'prev' in the code given)

I'm doing this by using the condition: Lower constant if (true-prev)(pred-prev)>0 else higher constant

def custom_loss(true,pred,prev):
    beta=0.5
    condition = (true-prev)(pred-prev) 
    if condition > 0:
        return K.mean(beta*K.square(true-pred))
    else:
        return K.mean((2-beta)*K.square(true-pred))

#model
model = Sequential()
model.add(LSTM(units=50, return_sequences = True, input_shape=(features_set.shape[1], X.shape[1])))  
model.add(Dropout(0.2)) 

model.add(LSTM(units=50, return_sequences = True))
model.add(Dropout(0.2)) 

model.add(LSTM(units=50, return_sequences = True))
model.add(Dropout(0.2)) 

model.add(LSTM(units=50))
model.add(Dropout(0.2)) 

model.add(Dense(units = X.shape[1]))  

opt = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, decay=0.01)

model.compile(optimizer = opt, loss = custom_loss, metrics = ['accuracy']) 


model.fit(features_set, labels, epochs = 100, batch_size = 32, verbose=1)

How do I get the value of the previous timestep since the custom loss is only dependent on the true and the predicted value?

Related posts

Recent Viewed