对 LSTM 形状的质疑

我看到很多人对 LSTM 有同样的问题,所以我想让这个问题介绍一个通用的例子,然后介绍我自己的。


预期的输入形状由(样本、时间步长、特征)组成。这是我第一次陷入困境,因为许多示例仅提供两个输入,如下所示:


model.add(LSTM(32, input_shape=(TIMESTEPS, FEATURES), activation='relu', return_sequences = True))

如果我是对的,当省略第三个参数时,您只是没有指定样本数。


因此,假设我有以下结构作为输入:


import numpy as np    

np.zeros((BATCHES, TIMESTEPS, FEATURES))

用数字来表示我们可以得到:


np.zeros((2, 3, 5))


[[[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 input_shape=(480, 16),并且model.predict()正在接受使用 创建后填充形状 (1, 480, 16) 的输入batch = np.zeros((90, 480, 16))。形状为 (1, 480, 16) 的单个小批量输入的预测等于,model.predict(batch[[i]])但我期望返回一个长度等于 480 的 1D 数组,相反,我收到的是:[[0. 1. ... 0. 0.]]< 480。


该数组的值目前并不重要,但他的形状应该预测每个时间步长的值。


我的问题出在哪里?提前致谢


更新: 我的案例的整个模型声明是:


model = Sequential()

model.add(LSTM(32, input_shape=(480, 16), activation='relu', return_sequences = True)))

model.add(Dense(16))

model.compile(loss='mse', optimizer=Adam(lr=0.1))

return model

输入类似于以下声明:


batch = np.zeros((90, 480, 16)) # this is filled after

input_to_predict = batch[[i]] # where i is in the range > 0  and < 90

model.predict(input_to_predict)


MMMHUHU
浏览 67回答 1
1回答

慕婉清6462132

上面的示例返回 shape (1, 480, 16)。当您设置 时return_sequences=True,Keras 将返回时间步维度(您的“中间”维度),因此如果您的输入有 480 个时间步,它将输出 480 个时间步。最后一个维度将是最后一层中的单元数。import numpy as npfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import *model = Sequential()model.add(LSTM(32, input_shape=(480, 16), return_sequences = True))model.add(Dense(16))model.compile(loss='mse', optimizer='adam')batch = np.zeros((90, 480, 16))input_to_predict = batch[[0]]model.predict(input_to_predict).shapearray([[[0., 0., 0., ..., 0., 0., 0.],&nbsp; &nbsp; &nbsp; &nbsp; [0., 0., 0., ..., 0., 0., 0.],&nbsp; &nbsp; &nbsp; &nbsp; [0., 0., 0., ..., 0., 0., 0.],&nbsp; &nbsp; &nbsp; &nbsp; ...,&nbsp; &nbsp; &nbsp; &nbsp; [0., 0., 0., ..., 0., 0., 0.],&nbsp; &nbsp; &nbsp; &nbsp; [0., 0., 0., ..., 0., 0., 0.],&nbsp; &nbsp; &nbsp; &nbsp; [0., 0., 0., ..., 0., 0., 0.]]], dtype=float32)(1, 480, 16)如果设置return_sequences=False,它不会返回时间步长维度:import numpy as npfrom tensorflow.keras.models import Sequentialfrom tensorflow.keras.layers import *model = Sequential()model.add(LSTM(32, input_shape=(480, 16), return_sequences = False))model.add(Dense(16))model.compile(loss='mse', optimizer='adam')batch = np.zeros((90, 480, 16))input_to_predict = batch[[0]]model.predict(input_to_predict).shape(1, 16)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python