我正在尝试使用LSTM模型预测股票价格。但是,由于数据嘈杂,我需要具有惩罚常数的自定义损失。我不知道如何从先前的时间步tho中获得数据(给定代码中的参数“ prev”)
我正在使用以下条件进行此操作: 较低的常量,如果(true-prev)(pred-prev)> 0,则较高的常量
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)
由于自定义损失仅取决于真实值和预测值,我如何获取上一个时间步长的值?