如何在 Keras 中为 Bi-LSTM 准备好二维形状
How to get a 2D shape ready for a Bi-LSTM in Keras
我有一个 2D numpy 矩阵(来自 DataFrame),其中包含已经压缩的词向量(我使用了最大池化技术,正在尝试将 logres 与 bi-LSTM 方法进行比较),但我不是确定如何准备它以在 keras 模型中使用它。
我知道 Bi-LSTM 模型需要 3D 张量,并尝试使用谷歌搜索解决方案,但找不到有效的解决方案。
这是我现在拥有的:
# Set model parameters
epochs = 4
batch_size = 32
input_shape = (1, 10235, 3072)
# Create the model
model = Sequential()
model.add(Bidirectional(LSTM(64, return_sequences = True, input_shape = input_shape)))
model.add(Dropout(0.5))
model.add(Dense(1, activation = 'sigmoid'))
# Try using different optimizers and different optimizer configs
model.compile('adam', 'binary_crossentropy', metrics = ['accuracy'])
# Fit the training set over the model and correct on the validation set
model.fit(inputs['X_train'], inputs['y_train'],
batch_size = batch_size,
epochs = epochs,
validation_data = [inputs['X_validation'], inputs['y_validation']])
# Get score over the test set
return model.evaluate(inputs['X_test'], inputs['y_test'])
我目前遇到以下错误:
ValueError: Input 0 is incompatible with layer bidirectional_23: expected ndim=3, found ndim=2
我的训练数据 (inputs['X_train']
) 的形状是 (10235, 3072)
。
非常感谢!
我通过执行以下操作使其与回复的建议一起工作:
- 移除
return_sequence = True
;
- 对 X 集应用以下转换:
np.reshape(inputs[dataset], (inputs[dataset].shape[0], inputs[dataset].shape[1], 1))
- 将LSTM层的输入形状改为
(10235, 3072, 1)
,也就是X_train
的形状。
我有一个 2D numpy 矩阵(来自 DataFrame),其中包含已经压缩的词向量(我使用了最大池化技术,正在尝试将 logres 与 bi-LSTM 方法进行比较),但我不是确定如何准备它以在 keras 模型中使用它。
我知道 Bi-LSTM 模型需要 3D 张量,并尝试使用谷歌搜索解决方案,但找不到有效的解决方案。
这是我现在拥有的:
# Set model parameters
epochs = 4
batch_size = 32
input_shape = (1, 10235, 3072)
# Create the model
model = Sequential()
model.add(Bidirectional(LSTM(64, return_sequences = True, input_shape = input_shape)))
model.add(Dropout(0.5))
model.add(Dense(1, activation = 'sigmoid'))
# Try using different optimizers and different optimizer configs
model.compile('adam', 'binary_crossentropy', metrics = ['accuracy'])
# Fit the training set over the model and correct on the validation set
model.fit(inputs['X_train'], inputs['y_train'],
batch_size = batch_size,
epochs = epochs,
validation_data = [inputs['X_validation'], inputs['y_validation']])
# Get score over the test set
return model.evaluate(inputs['X_test'], inputs['y_test'])
我目前遇到以下错误:
ValueError: Input 0 is incompatible with layer bidirectional_23: expected ndim=3, found ndim=2
我的训练数据 (inputs['X_train']
) 的形状是 (10235, 3072)
。
非常感谢!
我通过执行以下操作使其与回复的建议一起工作:
- 移除
return_sequence = True
; - 对 X 集应用以下转换:
np.reshape(inputs[dataset], (inputs[dataset].shape[0], inputs[dataset].shape[1], 1))
- 将LSTM层的输入形状改为
(10235, 3072, 1)
,也就是X_train
的形状。