如何将张量重塑为 LSTM?

Hen*_* .H 5 python keras

我在 Keras 中编码时遇到一些问题。我需要使用两种类型的嵌入处理两个顺序输入,一种是字嵌入,另一种是 doc2vec 嵌入,两者都是dim=300. 然后我将把这两个向量连接成一个更长的向量,因为我想从中获得一些堆叠的特征。然而,这两个嵌入可能位于不同的空间中,因此我必须将这两个向量映射到同一个向量中nn.flatten()。然后我需要将 flatten 的输出向量输入到 LSTM 模型。但编译器抱怨说Input 0 is incompatible with lstm_1: expected ndim=3, found ndim=2,我根本没有设置过ndim=3,而且我不知道如何将向量重塑为具有正确形状的新输入。请帮忙解决这个问题。

n_hidden = 50

batch_size = 64

def classification_softmax(left, right):
''' Helper function for the similarity estimate of the LSTMs outputs'''
return K.abs(left - right)
embedding_layer = Embedding(len(embeddings), 300, weights=[embeddings], input_length=max_seq_length,
                            trainable=False)


embedding_cfg_layer =  Embedding(len(cfg_embedding_matrix), 300, weights=[cfg_embedding_matrix], input_length=1,
                            trainable=False)


#cfg_embedding_l=krs.layers.Flatten()(embedding_cfg_layer(cfg_left_input))
#cfg_embedding_r=krs.layers.Flatten()(embedding_cfg_layer(cfg_right_input))
#encoded_left = krs.layers.Concatenate(axis=1)([krs.layers.Flatten()(embedding_layer(left_input)),cfg_embedding_l])
#encoded_right = krs.layers.Concatenate(axis=1)([krs.layers.Flatten()(embedding_layer(right_input)), cfg_embedding_r])

encoded_left = encoded_left
encoded_right = encoded_right
# Since this is a siamese network, both sides share the same LSTM
shared_lstm = LSTM(n_hidden,return_sequences=True)

#encoded_left=krs.layers.Reshape((2,))(encoded_left)
#encoded_right=krs.layers.Reshape((2,))(encoded_right)
left_output = shared_lstm(encoded_left)
right_output = shared_lstm(encoded_right)
    cfg_embedding_l=embedding_cfg_layer(cfg_left_input)
cfg_embedding_r=embedding_cfg_layer(cfg_right_input)
encoded_left = krs.layers.Concatenate(axis=0)([(embedding_layer(left_input),cfg_embedding_l])
encoded_right = krs.layers.Concatenate(axis=0)(embedding_layer(right_input), cfg_embedding_r])
...   
dist = Lambda(lambda x: classification_softmax(x[0], x[1]))([left_output, right_output])
classify = Dense(5, activation=softMaxAxis1)(dist)
# Pack it all up into a model
malstm = Model([left_input, right_input,cfg_left_input,cfg_right_input], [classify])

optimizer = Adadelta(clipnorm=gradient_clipping_norm)

# malstm.compile(loss='mean_squared_error', optimizer='adam', metrics= 
['accuracy', f1, recall,precision])
malstm.compile(loss='categorical_crossentropy', optimizer='adam', metrics= 
[categorical_accuracy])#, f1, recall, precision])

# Start training
training_start_time = time()

malstm_trained = malstm.fit(
[X_train['left'], X_train['right'], X_train['cfg_A'], X_train['cfg_B']],
krs.utils.to_categorical(Y_train, 5),
batch_size=batch_size, nb_epoch=n_epoch,
#callbacks=[metrics],
validation_data=(
    [X_validation['left'], X_validation['right'], 
X_validation['cfg_A'],X_validation['cfg_B']],
    krs.utils.to_categorical(Y_validation, 5)))
Run Code Online (Sandbox Code Playgroud)

Pri*_*usa 0

我无法从示例代码中说出输入的确切形状,因此我无法为您提供确切形状的答案,但在这种情况下,您应该使用图层Reshape

我将实现你的形状的逻辑飞跃(batch_size, 300 * max_seq_length + 300),因为第一Embedding层输出一个(max_seq_length, 300)张量,第二层输出一个(1, 300)张量,然后你将它们展平并连接起来。

您想要将 2D 形状重塑(batch_size, 300 * max_seq_length + 300)为 3D 形状,例如(batch_size, 300 * max_seq_length + 300, 1)。添加Reshape具有目标形状的图层:

reshape = keras.layers.Reshape(300 * max_seq_length + 300, 1)

encoded_left = reshape(encoded_left)
encoded_right = reshape(encoded_right)
Run Code Online (Sandbox Code Playgroud)

然后将它们传递到您的 LSTM 中。