将类别特征通过Embedding层映射并进行拼接
文章目录
背景
最近有一道题,想把类别特征也放到模型里去,跟bert输出拼接到一起。
所以便设计了,使用Embedding层,将类别特征的词表映射到X维向量空间里。
keras.layers.Embedding(input_dim, output_dim, embeddings_initializer='uniform', embeddings_regularizer=None, activity_regularizer=None, embeddings_constraint=None, mask_zero=False, input_length=None)
其中,input_dim 是词表长度。
同时,因为Embedding层的输出会带上扩展的层数,变成(batch_size, input_length, output_dim)。不方便拼接,所以还需要Flatten层,帮忙拉平。
代码实现
|
|
|
|
|
|
KerasTensor(type_spec=TensorSpec(shape=(None, 256), dtype=tf.float32, name=None), name='flatten/Reshape:0', description="created by layer 'flatten'")
KerasTensor(type_spec=TensorSpec(shape=(None, 32), dtype=tf.float32, name=None), name='flatten_1/Reshape:0', description="created by layer 'flatten_1'")
KerasTensor(type_spec=TensorSpec(shape=(None, 1061), dtype=tf.float32, name=None), name='concatenate/concat:0', description="created by layer 'concatenate'")
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_1 (InputLayer) [(None, None)] 0
__________________________________________________________________________________________________
input_2 (InputLayer) [(None, None)] 0
__________________________________________________________________________________________________
input_4 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
input_5 (InputLayer) [(None, 1)] 0
__________________________________________________________________________________________________
tf_bert_model (TFBertModel) TFBaseModelOutputWit 102267648 input_1[0][0]
input_2[0][0]
__________________________________________________________________________________________________
embedding (Embedding) (None, 1, 256) 1426944 input_4[0][0]
__________________________________________________________________________________________________
embedding_1 (Embedding) (None, 1, 32) 960 input_5[0][0]
__________________________________________________________________________________________________
lambda (Lambda) (None, 768) 0 tf_bert_model[0][0]
__________________________________________________________________________________________________
input_3 (InputLayer) [(None, 5)] 0
__________________________________________________________________________________________________
flatten (Flatten) (None, 256) 0 embedding[0][0]
__________________________________________________________________________________________________
flatten_1 (Flatten) (None, 32) 0 embedding_1[0][0]
__________________________________________________________________________________________________
concatenate (Concatenate) (None, 1061) 0 lambda[0][0]
input_3[0][0]
flatten[0][0]
flatten_1[0][0]
__________________________________________________________________________________________________
dense (Dense) (None, 1024) 1087488 concatenate[0][0]
__________________________________________________________________________________________________
dropout_37 (Dropout) (None, 1024) 0 dense[0][0]
__________________________________________________________________________________________________
dense_1 (Dense) (None, 256) 262400 dropout_37[0][0]
__________________________________________________________________________________________________
dropout_38 (Dropout) (None, 256) 0 dense_1[0][0]
__________________________________________________________________________________________________
dense_2 (Dense) (None, 30) 7710 dropout_38[0][0]
==================================================================================================
Total params: 105,053,150
Trainable params: 105,053,150
Non-trainable params: 0
__________________________________________________________________________________________________