Keras在功能API中嵌入具有可变长度的层

时间:2017-08-02 11:57:28

标签: tensorflow keras word-embedding

我有以下用于可变长度输入的顺序模型:

m = Sequential()
m.add(Embedding(len(chars), 4, name="embedding"))
m.add(Bidirectional(LSTM(16, unit_forget_bias=True, name="lstm")))
m.add(Dense(len(chars),name="dense"))
m.add(Activation("softmax"))
m.summary()

提供以下摘要:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
embedding (Embedding)        (None, None, 4)           204       
_________________________________________________________________
bidirectional_2 (Bidirection (None, 32)                2688      
_________________________________________________________________
dense (Dense)                (None, 51)                1683      
_________________________________________________________________
activation_2 (Activation)    (None, 51)                0         
=================================================================
Total params: 4,575
Trainable params: 4,575
Non-trainable params: 0

然而,当我尝试在功能API中实现相同的模型时,我不知道我尝试什么,因为输入图层形状似乎与顺序模型不同。这是我的尝试之一:

charinput = Input(shape=(4,),name="input",dtype='int32')
embedding = Embedding(len(chars), 4, name="embedding")(charinput)
lstm = Bidirectional(LSTM(16, unit_forget_bias=True, name="lstm"))(embedding)
dense = Dense(len(chars),name="dense")(lstm)
output = Activation("softmax")(dense)

以下是摘要:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input (InputLayer)           (None, 4)                 0         
_________________________________________________________________
embedding (Embedding)        (None, 4, 4)              204       
_________________________________________________________________
bidirectional_1 (Bidirection (None, 32)                2688      
_________________________________________________________________
dense (Dense)                (None, 51)                1683      
_________________________________________________________________
activation_1 (Activation)    (None, 51)                0         
=================================================================
Total params: 4,575
Trainable params: 4,575
Non-trainable params: 0

2 个答案:

答案 0 :(得分:1)

在您的情况下,请在输入层中使用shape=(None,)

charinput = Input(shape=(None,),name="input",dtype='int32')

答案 1 :(得分:-1)

尝试将参数input_length=None添加到嵌入层。