【发布时间】:2021-09-27 04:00:37
【问题描述】:
我知道 LSTMS 需要一个三维数据集才能按照这种格式运行,N_samples x TimeSteps x Variables。我想将我所有行的单个时间步长的数据重组为按小时计算的 Lag 时间步长。这个想法是,LSTM 将逐小时进行批量训练(从 310033 行 x 1 时间步 x 83 变量到 310033 行 x 60 时间步 x 83 变量)。
但是,我的模型的损失很奇怪(随着 epoch 增加训练损失),并且训练准确度从单个时间步下降到滞后时间步。这让我相信我做错了这个转变。这是重组数据的正确方法还是有更好的方法?
数据是 1 秒记录的时间序列数据,已经被预处理到 0-1 范围内,One-Hot 编码,清洗等...
Python 中的当前转换:
X_train, X_test, y_train, y_test = train_test_split(scaled, target, train_size=.7, shuffle = False)
#reshape input to be 3D [samples, timesteps, features]
#X_train = X_train.reshape((X_train.shape[0], 1, X_train.shape[1])) - Old method for 1 timestep
#X_test = X_test.reshape((X_test.shape[0], 1, X_test.shape[1])) - Old method for 1 timestep
#Generate Lag time Steps 3D framework for LSTM
#As required for LSTM networks, we must reshape the input data into N_samples x TimeSteps x Variables
hours = len(X_train)/3600
hours = math.floor(hours) #Most 60 min hours availible in subset of data
temp =[]
# Pull hours into the three dimensional feild
for hr in range(hours, len(X_train) + hours):
temp.append(scaled[hr - hours:hr, 0:scaled.shape[1]])
X_train = np.array(temp) #Export Train Features
hours = len(X_test)/3600
hours = math.floor(hours) #Most 60 min hours availible in subset of data
temp =[]
# Pull hours into the three dimensional feild
for hr in range(hours, len(X_test) + hours):
temp.append(scaled[hr - hours:hr, 0:scaled.shape[1]])
X_test = np.array(temp) #Export Test Features
转换后的数据形状:
模型注入:
model.add(LSTM(128, return_sequences=True,
input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dropout(0.15)) #15% drop out layer
#model.add(BatchNormalization())
#Layer 2
model.add(LSTM(128, return_sequences=False))
model.add(Dropout(0.15)) #15% drop out layer
#Layer 3 - return a single vector
model.add(Dense(32))
#Output of 2 because we have 2 classes
model.add(Dense(2, activation= 'sigmoid'))
# Define optimiser
opt = tf.keras.optimizers.Adam(learning_rate=1e-5, decay=1e-6)
# Compile model
model.compile(loss='sparse_categorical_crossentropy', # Mean Square Error Loss = 'mse'; Mean Absolute Error = 'mae'; sparse_categorical_crossentropy
optimizer=opt,
metrics=['accuracy'])
history = model.fit(X_train, y_train, epochs=epoch, batch_size=batch, validation_data=(X_test, y_test), verbose=2, shuffle=False)
关于如何提高性能或修复滞后时间步的任何意见?
【问题讨论】:
标签: tensorflow keras deep-learning lstm reshape