【问题标题】:Input shape for LSTM which has one hot encoded data具有一个热编码数据的 LSTM 的输入形状
【发布时间】:2019-06-20 11:25:19
【问题描述】:

示例数据集包含用户的位置点。

df.head()

   user           tslot         Location_point
0   0   2015-12-04 13:00:00     4356
1   0   2015-12-04 13:15:00     4356
2   0   2015-12-04 13:30:00     3659
3   0   2015-12-04 13:45:00     4356
4   0   2015-12-04 14:00:00     8563

df.shape 

(288,3)

由于位置点是分类值,因此它们是热编码的。

encoded = to_categorical(df['Location_point'])

编码值如下

[[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]

编码值的形状为 (288,8564)。

我试图塑造训练数据

X_trai = []
y_trai = []
for i in range(96, 288):
    X_trai.append(encoded[i-96:i])
    y_trai.append(encoded[i])
X_trai, y_trai = np.array(X_trai), np.array(y_trai)

X_trai = np.reshape(X_trai, (X_trai.shape[0], X_trai.shape[1], 1))

模型是

regressor = Sequential()

regressor.add(LSTM(units = 50, return_sequences = True, input_shape = (X_trai.shape[1], 1)))
regressor.add(Dropout(0.2))

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

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

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

regressor.add(Dense(units = 1))

regressor.compile(optimizer = 'adam', loss = 'mean_squared_error')

regressor.fit(X_trai, y_trai, epochs = 100, batch_size = 32)

这不是正确的模型。我是深度学习的新手。我试图查看一些示例,但无法理解一种热编码。如果有人能解释输入形状、输出形状和正确的模型,我将不胜感激。

The input is the sequence of the location points and the output is to predict
 the next location point for that user.

【问题讨论】:

  • 另外,能否请您发布错误。我认为它需要输入的 3 维形状。
  • 是的,但我不知道如何将我的数据转换为 3 维。你能根据我的数据建议我的意见吗,
  • 你能把错误贴出来吗。
  • ValueError:检查输入时出错:预期dense_3_input 有2 维,但得到了形状为(1、288、8654)的数组。错误与输入的形状有关。
  • 我认为凯文已经尝试回答了这个问题。

标签: python machine-learning keras deep-learning time-series


【解决方案1】:

输入形状取决于您的数据,如果您有一个包含 288 个时间步长和 8564 个特征的样本,那么您的输入形状将是 (batch_size=1, timesteps=288, n_features=8564),如果您有 288 个样本单个时间步长(batch_size=288, timesteps=1, n_features=8564)。

无论如何,这里有一个关于如何为 LSTM 模型准备数据的教程。 https://machinelearningmastery.com/reshape-input-data-long-short-term-memory-networks-keras/ https://machinelearningmastery.com/how-to-one-hot-encode-sequence-data-in-python/

LSTM 的输入形状如下:

形状为 (batch_size, timesteps, input_dim) 的 3D 张量,(可选)形状为 (batch_size, output_dim) 的 2D 张量。

Timesteps 将是您的时间序列序列长度,input_dim 是您拥有的特征数量,在这种情况下,因为它们是一个热编码的,所以它将是 8564。

输出形状将取决于模型的架构。

  • 第一层为您提供(batch_size、timesteps、units)的输出
  • 第二层:(batch_size, timesteps, units)
  • 第三层:(batch_size, units)
  • 最后一层:(batch_size, 1)

不过,您可以通过以下方式检查您的模型输入/输出形状:

regressor.input_shape & regressor.output_shape

最后,您为什么不将 Location_point 视为数字变量?

【讨论】:

  • 您好@kevin,抱歉回复晚了。我浏览了您上面提到的书,但找不到解决问题的方法。我尝试了所有输入形状但失败了。我不能将位置点视为数值。在预测值时,模型输出随机值,但位置点是特定的分类值,应使用一种热编码进行预测。你能详细写出输入形状吗?让我知道是否有任何事情。谢谢
猜你喜欢
  • 2018-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
相关资源
最近更新 更多