【问题标题】:Keras dimension LSTM net 3 dim expectedKeras 维度 LSTM net 3 dim 预期
【发布时间】:2019-12-12 18:57:09
【问题描述】:

我对 LSTM 顺序网络的输入尺寸和形状有疑问。我正在寻找正确的方法来重塑和适应这个 input_merged (?, 1, 2400, 60) 到 LSTM 输入已成功连接,但不接受来自 LSTM 网络输入的新维度。

计划

inp1 = Input(features_set3.shape)
inp2 = Input(features_set4.shape)
print("  shapeINP1 ")
print(inp1.shape)
print("  shapeINP2 ")
print(inp2.shape)

input_merged = Concatenate(axis=2)([inp1, inp2])

print(input_merged.shape)
print("  OK  ")

lstm = LSTM(units=50, return_sequences=True, input_shape=input_merged.shape)(input_merged)

model = Sequential()  

model.add(LSTM)  

尺寸错误和输入形状的日志

b'你好,TensorFlow!' 42 使用 TensorFlow 后端。

功能集 (1200, 60)

features_set3 (1, 1200, 60) DataConversionWarning:输入 dtype int64 的数据已由 MinMaxScaler 转换为 float64。 warnings.warn(msg, DataConversionWarning)

features_set2

(1200, 60)

features_set4

(1, 1200, 60)

形状INP1

(?, 1, 1200, 60)

shapeINP2

(?, 1, 1200, 60)

(?, 1, 2400, 60)

好的 回溯(最近一次通话最后): prog10-t12.py",第 84 行,在模块中 lstm = LSTM(units=50, return_sequences=True, input_shape=input_merged.shape)(input_merged)

文件“C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\recurrent.py”,第 532 行,调用中 返回超级(RNN,自我)。调用(输入,kwargs) 文件“base_layer.py”,第 414 行,调用中 self.assert_input_compatibility(输入) 文件“C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py”,第 311 行,在 assert_input_compatibility str(K.ndim(x)))

ValueError: Input 0 is in compatible with layer lstm_1: expected ndim=3, found ndim=4

【问题讨论】:

    标签: python input keras lstm dimension


    【解决方案1】:

    你应该先重塑你的输入张量:

    inp1 = keras.layers.Reshape((1200, 60))(inp1)
    inp2 = keras.layers.Reshape((1200, 60))(inp2)
    

    input_merged = keras.layers.Reshape((2400, 60))(input_merged)
    

    这会将input_merged 的形状更改为(None, 2400, 60),因此LSTM 层可以接受

    【讨论】:

    • 我收到关于 inp2 的 Reshape 命令的输入错误 > inp2 = keras.layers.Reshape(1200, 60)(inp2) > TypeError: __init__() 接受 2 个位置参数,但给出了 3 个
    • > features_set > (1200, 60) > features_set3 > (1, 1200, 60) > DataConversionWarning:输入 dtype int64 的数据已被 MinMaxScaler 转换为 float64。 > warnings.warn(msg, DataConversionWarning) > features_set2 > (1200, 60) > features_set4 > (1, 1200, 60) > shapeINP1 > (?, 1, 1200, 60) > shapeINP2 > (?, 1, 1200, 60 ) > shapeINP2-Reshape > Traceback (最近一次调用最后一次): > prog10-t13.py", line 82, in module > inp2 = keras.layers.Reshape(1200, 60)(inp2) > TypeError: __init__() 需要2 个位置参数,但给出了 3 个
    • 对不起,我忘了括号,试试keras.layers.Reshape((1200, 60))(inp2)
    • 嗨,这个解决方案导致与之前解决的串联不对齐,也许 inp2 重塑还需要串联更改 ValueError: A Concatenate layer requires input with matching shapes except the concat axis.得到输入形状:[(None, 1, 1200, 60), (None, 1200, 60)]
    • 是的,我误解了提供的输出。您可以同时重塑 inp1inp2 或仅重塑 input_merged
    猜你喜欢
    • 1970-01-01
    • 2018-12-04
    • 1970-01-01
    • 1970-01-01
    • 2017-08-31
    • 1970-01-01
    • 2022-01-20
    • 2023-03-05
    相关资源
    最近更新 更多