Keras Functional API:LTSM返回一个二维数组

时间:2017-08-08 10:39:11

标签: python deep-learning keras lstm keras-layer

我堆叠,我需要stackoverflow的智慧。

我使用 Functional API 在Keras中实现了两个输入神经网络,输入形状为:

X.shape, X_size.shape, y.shape

((123, 9), (123, 2), (123, 9, 10))

所以,我的问题是我想从LSTM获得具有三维形状的输出形状,以便使用我的 y 张量。我知道,我可以将 y 重塑为二维形状,但我想将它用作三维数组。

from keras.models import Model
from keras import layers
from keras import Input

# first input 

list_input = Input(shape=(None,), dtype='int32', name='li')
embedded_list = layers.Embedding(100,90)(list_input)
encoded_list = layers.LSTM(4,  name = "lstm1")(embedded_list)

# second input 
size_input = Input(shape=(None,), dtype='int32', name='si')
embedded_size = layers.Embedding(100,10)(size_input)
encoded_size = layers.LSTM(4, name = "lstm2")(embedded_size)

# concatenate
concatenated = layers.concatenate([encoded_size, encoded_list], axis=-1)

answer = layers.Dense(90, activation='sigmoid', name = 'outpuy_layer')(concatenated)


model = Model([list_input, size_input], answer)
model.compile(optimizer='adam',
              loss='binary_crossentropy',
              metrics=[f1])

模型摘要:

____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
si (InputLayer)                  (None, None)          0                                            
____________________________________________________________________________________________________
li (InputLayer)                  (None, None)          0                                            
____________________________________________________________________________________________________
embedding_16 (Embedding)         (None, None, 10)      1000        si[0][0]                         
____________________________________________________________________________________________________
embedding_15 (Embedding)         (None, None, 90)      9000        li[0][0]                         
____________________________________________________________________________________________________
lstm2 (LSTM)                     (None, 4)             240         embedding_16[0][0]               
____________________________________________________________________________________________________
lstm1 (LSTM)                     (None, 4)             1520        embedding_15[0][0]               
____________________________________________________________________________________________________
concatenate_8 (Concatenate)      (None, 8)             0           lstm2[0][0]                      
                                                                   lstm1[0][0]                      
____________________________________________________________________________________________________
outpuy_layer (Dense)             (None, 90)            810         concatenate_8[0][0]              
====================================================================================================
Total params: 12,570
Trainable params: 12,570
Non-trainable params: 0

再一次,问题是:

如何从LSTM获取输出形状,如(无,无,无/ 10)?

1 个答案:

答案 0 :(得分:1)

Keras忽略除了最后一个输出之外的每个时间步输出,这会创建一个2D数组。要获得3D数组(意味着您获得每个时间步的输出),请将return_sequences设置为True的图层实例化。例如:

encoded_list = layers.LSTM(4,  name = "lstm1", return_sequences=True)(embedded_list)