我想在自定义子模型中使用keras.layers.Embedding。但是输出形状是“多个”。然后我尝试编写一个演示并对其进行测试
这两种方法的结果不同,我不确定是否是我的问题。
model1给我一个清晰的输出形状
但是model2给我一个“倍数”
这是完整的演示代码:
import tensorflow as tf
import tensorflow.keras as keras
import numpy as np
myPath = "./data/"
embedding_matrix = np.load(myPath + "embeddings_matrix.npy",allow_pickle=True)
model1=keras.Sequential([
keras.layers.InputLayer(5),
keras.layers.Embedding(len(embedding_matrix), 100,weights=[embedding_matrix],trainable=False,mask_zero=True,name="embedding_layer"),
keras.layers.Dense(200)
],name="model1")
model1.build((None,5))
model1.summary()
class myModel(keras.Model):
def __init__(self,
embedding_matrix,
embedding_out=100,
**kwargs):
super(myModel, self).__init__(name='model2')
self.input_x = keras.layers.InputLayer(input_shape=5)
self.embedding_layer = keras.layers.Embedding(len(embedding_matrix), embedding_out,weights=[embedding_matrix],trainable=False,mask_zero=True,name="embedding",input_length=5)
self.dense = keras.layers.Dense(200)
def call(self,x):
x = self.input_x(x)
y = self.embedding_layer(x)
y = self.dense(y)
return y
model2 = myModel(embedding_matrix)
model2.build((None,5))
model2.summary()
这是模型。summary()信息:
Model: "model1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_layer (Embedding) (None, 5, 100) 5000400
_________________________________________________________________
dense (Dense) (None, 5, 200) 20200
=================================================================
Total params: 5,020,600
Trainable params: 20,200
Non-trainable params: 5,000,400
_________________________________________________________________
Model: "model2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_2 (InputLayer) [(None, 5)] 0
_________________________________________________________________
embedding (Embedding) multiple 5000400
_________________________________________________________________
dense_1 (Dense) multiple 20200
=================================================================
Total params: 5,020,600
Trainable params: 20,200
Non-trainable params: 5,000,400
_________________________________________________________________
我想知道在自定义子模型中使用keras.layers.embedding的正确方法