【发布时间】:2019-01-14 03:46:51
【问题描述】:
我已经使用 Keras 构建了 LSTM 架构,但我不确定复制时间步长是否是处理可变序列长度的好方法。
我有一个具有多特征序列和不同时间步长的多维数据集。它是一个多元时间序列数据,包含多个用于训练 LSTM 的示例,Y 为 0 或 1。目前,我正在为每个序列复制最后的时间步长以确保 timesteps = 3。
如果有人能回答以下问题或疑虑,我将不胜感激:
1. 用零表示的特征值创建额外的时间步更合适吗?
2. 解决这个问题的正确方法是什么、填充序列和评估掩码。
3. 我也在 Y 变量中复制最后一个时间步以进行预测,并且 Y 中的值 1 仅出现在最后一个时间步。
# The input sequences are
trainX = np.array([
[
# Input features at timestep 1
[1, 2, 3],
# Input features at timestep 2
[5, 2, 3] #<------ duplicate this to ensure compliance
],
# Datapoint 2
[
# Features at timestep 1
[1, 8, 9],
# Features at timestep 2
[9, 8, 9],
# Features at timestep 3
[7, 6, 1]
]
])
# The desired model outputs is as follows:
trainY = np.array([
# Datapoint 1
[
# Target class at timestep 1
[0],
# Target class at timestep 2
[1] #<---------- duplicate this to ensure compliance
],
# Datapoint 2
[
# Target class at timestep 1
[0],
# Target class at timestep 2
[0]
# Target class at time step 3
[0]
]
])
timesteps = 3
model = Sequential()
model.add(LSTM(3, kernel_initializer ='uniform', return_sequences=True, batch_input_shape=(None, timesteps, trainX.shape[2]),
kernel_constraint=maxnorm(3), name='LSTM'))
model.add(Dropout(0.2))
model.add(LSTM(3, return_sequences=True, kernel_constraint=maxnorm(3), name='LSTM-2'))
model.add(Flatten(name='Flatten'))
model.add(Dense(timesteps, activation='sigmoid', name='Dense'))
model.compile(loss="mse", optimizer="sgd", metrics=["mse"])
model.fit(trainX, trainY, epochs=2000, batch_size=2)
predY = model.predict(testX)
【问题讨论】:
标签: keras padding lstm masking