【问题标题】:tensorflow/keras lstm input shapetensorflow/keras lstm 输入形状
【发布时间】:2019-12-11 13:16:15
【问题描述】:

我正在尝试通过 Keras 的 tutorials 学习 keras 功能 API,当我尝试修改示例时,我似乎得到了形状不匹配的问题。教程代码和下面的唯一区别是我删除了嵌入层,因为我的是回归问题。

首先,我知道 LSTM 需要 3 个维度。在我的示例中,我有:

TRAIN_BATCH_SIZE=32
MODEL_INPUT_BATCH_SIZE=128

headline_data = np.random.uniform(low=1, high=9000, size=(MODEL_INPUT_BATCH_SIZE, 100)).astype(np.float32)
additional_data = np.random.uniform(low=1, high=9000, size=(MODEL_INPUT_BATCH_SIZE, 5)).astype(np.float32)
labels = np.random.randint(0, 1 + 1, size=(MODEL_INPUT_BATCH_SIZE, 1))

main_input = Input(shape=(100,), dtype='float32', name='main_input')

lstm_out = LSTM(32)(main_input)

auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)

auxiliary_input = Input(shape=(5,), name='aux_input')
x = keras.layers.concatenate([lstm_out, auxiliary_input])

# We stack a deep densely-connected network on top
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)

# And finally we add the main logistic regression layer
main_output = Dense(1, activation='sigmoid', name='main_output')(x)

# This defines a model with two inputs and two outputs:
model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])

model.compile(optimizer='rmsprop',
                loss={'main_output': 'binary_crossentropy', 'aux_output': 'binary_crossentropy'},
                loss_weights={'main_output': 1., 'aux_output': 0.2})

# And trained it via:
model.fit({'main_input': headline_data, 'aux_input': additional_data},
                {'main_output': labels, 'aux_output': labels},
                epochs=2, batch_size=TRAIN_BATCH_SIZE)

当我运行上述内容时,我得到:

ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=2

所以,我尝试像这样改变我的输入形状:

main_input = Input(shape=(100,1), dtype='float64', name='main_input')

当我运行它时,我得到:

ValueError: Error when checking input: expected main_input to have 3 dimensions, but got array with shape (128, 100)

我对错误的来源感到困惑和迷茫。非常感谢您对此提供一些指导。

编辑

我也试过设置:

headline_data = np.expand_dims(headline_data, axis=2)

然后使用,

main_input = Input(shape=headline_data.shape, dtype='float64', name='main_input')

然后,我得到:

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

看起来真的很奇怪!

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:
    ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=2
    

    您的问题在于数据的形状。

    headline_data = np.random.uniform(low=1, high=9000, size=(MODEL_INPUT_BATCH_SIZE, 100))
    headline_data.shape
    

    返回

    (128,100)
    

    但是这应该具有三个维度。

    如果不仔细检查,您可能需要执行以下操作:

    headline_data.reshape(128,1,100)
    

    看看这篇文章,它应该清楚一切。

    Link

    * 更新 *

    执行以下操作:

    headling_data = healdine_data.reshape(128,1,100)
    main_input = Input(shape=(1,100), dtype='float32', name='main_input')
    

    我测试了它并且它有效,所以如果它不适合你,请告诉我 =)

    ---- 完整代码:----

    import numpy as np
    
    from tensorflow import keras
    from tensorflow.keras import Model
    from tensorflow.keras.layers import Input, LSTM, Dense
    
    
    TRAIN_BATCH_SIZE=32
    MODEL_INPUT_BATCH_SIZE=128
    
    headline_data = np.random.uniform(low=1, high=9000, size=(MODEL_INPUT_BATCH_SIZE, 100)).astype(np.float32)
    headline_data.shape
    lstm_data = headline_data.reshape(MODEL_INPUT_BATCH_SIZE,1,100)
    additional_data = np.random.uniform(low=1, high=9000, size=(MODEL_INPUT_BATCH_SIZE, 5)).astype(np.float32)
    labels = np.random.randint(0, 1 + 1, size=(MODEL_INPUT_BATCH_SIZE, 1))
    
    main_input = Input(shape=(1,100), dtype='float32', name='main_input')
    
    lstm_out = LSTM(32)(main_input)
    
    auxiliary_output = Dense(1, activation='sigmoid', name='aux_output')(lstm_out)
    
    auxiliary_input = Input(shape=(5,), name='aux_input')
    x = keras.layers.concatenate([lstm_out, auxiliary_input])
    
    # We stack a deep densely-connected network on top
    x = Dense(64, activation='relu')(x)
    x = Dense(64, activation='relu')(x)
    x = Dense(64, activation='relu')(x)
    
    # And finally we add the main logistic regression layer
    main_output = Dense(1, activation='sigmoid', name='main_output')(x)
    
    # This defines a model with two inputs and two outputs:
    model = Model(inputs=[main_input, auxiliary_input], outputs=[main_output, auxiliary_output])
    
    model.compile(optimizer='rmsprop',
                    loss={'main_output': 'binary_crossentropy', 'aux_output': 'binary_crossentropy'},
                    loss_weights={'main_output': 1., 'aux_output': 0.2})
    
    # And trained it via:
    model.fit({'main_input': lstm_data, 'aux_input': additional_data},
                    {'main_output': labels, 'aux_output': labels},
                    epochs=1000, batch_size=TRAIN_BATCH_SIZE)
    

    【讨论】:

    • 我已经阅读了该帖子 100 次 :( - 也许我有点愚蠢,但是,当我将其重塑为 (128,1,100) 时,我得到 ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4,这很荒谬,因为之前它说应该是 3 而不是 2!我会再看一遍帖子!
    • 嗯,也许我的回答是错误的,我必须验证
    • 我不确定您的答案是否错误,因为您发布的链接也具有相同的输入形状。这很奇怪。 btw:以上代码来自keras教程https://keras.io/getting-started/functional-api-guide/#multi-input-and-multi-output-models,这里我只是简单的删除了embedding layer(因为我的是回归问题)。
    • 我已经尝试过您的更新(我尝试了lstm_shape = headline_data.reshape(MODEL_INPUT_BATCH_SIZE,1,100),然后是main_input = Input(shape=lstm_shape.shape, dtype='float32', name='main_input'),它仍然给我:ValueError: Error when checking input: expected main_input to have 3 dimensions, but got array with shape (128, 100).. 我不知道为什么?!
    • 我假设您在代码中进一步使用 lstm_shape ?还是?
    猜你喜欢
    • 2018-02-27
    • 2019-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2018-08-23
    • 2020-09-04
    相关资源
    最近更新 更多