为了账号安全,请及时绑定邮箱和手机立即绑定

对 LSTM 形状的质疑

对 LSTM 形状的质疑

MMMHUHU 2023-07-11 10:44:43
我看到很多人对 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 afterinput_to_predict = batch[[i]] # where i is in the range > 0  and < 90model.predict(input_to_predict)
查看完整描述

1 回答

?
慕婉清6462132

TA贡献1804条经验 获得超2个赞

上面的示例返回 shape (1, 480, 16)。当您设置 时return_sequences=True,Keras 将返回时间步维度(您的“中间”维度),因此如果您的输入有 480 个时间步,它将输出 480 个时间步。最后一个维度将是最后一层中的单元数。


import numpy as np

from tensorflow.keras.models import Sequential

from 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).shape

array([[[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.]]], dtype=float32)

(1, 480, 16)

如果设置return_sequences=False,它不会返回时间步长维度:


import numpy as np

from tensorflow.keras.models import Sequential

from 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)


查看完整回答
反对 回复 2023-07-11
  • 1 回答
  • 0 关注
  • 65 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信