【发布时间】: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