如何连接预训练的嵌入层和输入层
how to concatenate pre trained embedding layer and Input layer
normal_input = Input(shape=(56,))
pretrained_embeddings = Embedding(num_words, 200, input_length=max_length, trainable=False,
weights=[ft_embedding_matrix])
concatenated = concatenate([normal_input, pretrained_embeddings])
dense = Dense(256, activation='relu')(concatenated)
我的想法是创建一个 256 维的输入并将其传递给密集层。
我收到以下错误。
ValueError:调用层 concatenate_10 时输入不是符号张量。接收类型: .完整输入:[ ]。该层的所有输入都应该是张量。
请帮我看看怎么做。
您需要向 select 输入您正在使用的嵌入。
由于您使用了 150 个单词,因此您的嵌入将具有 (batch,150,200)
的形状,无法以任何方式与 (batch, 56)
连接。你需要以某种方式改变一些东西来匹配形状。我建议你尝试使用 Dense
图层将 56 转换为 200...
word_input = Input((150,))
normal_input = Input((56,))
embedding = pretrained_embeddings(word_input)
normal = Dense(200)(normal_input)
#you could add some normalization here - read below
normal = Reshape((1,200))(normal)
concatenated = Concatenate(axis=1)([normal, embedding])
我还建议,由于嵌入和您的输入来自不同的性质,您应用归一化使它们变得更相似:
embedding = BatchNormalization(center=False, scale=False)(embedding)
normal = BatchNormalization(center=False, scale=False)(normal)
另一种可能性(我不能说哪个最好)是在另一个维度上连接,将 56 转换为 150:
word_input = Input((150,))
normal_input = Input((56,))
embedding = pretrained_embeddings(word_input)
normal = Dense(150)(normal_input)
#you could add some normalization here - read below
normal = Reshape((150,1))(normal)
concatenated = Concatenate(axis=-1)([normal, embedding])
我相信这更适合循环网络和卷积网络,你添加一个新通道而不是添加一个新步骤。
您甚至可以尝试双连接,这听起来很酷 :D
word_input = Input((150,))
normal_input = Input((56,))
embedding = pretrained_embeddings(word_input)
normal150 = Dense(150)(normal_input)
normal201 = Dense(201)(normal_input)
embedding = BatchNormalization(center=False, scale=False)(embedding)
normal150 = BatchNormalization(center=False, scale=False)(normal150)
normal201 = BatchNormalization(center=False, scale=False)(normal201)
normal150 = Reshape((150,1))(normal150)
normal201 = Reshape((1,201))(normal201)
concatenated = Concatenate(axis=-1)([normal150, embedding])
concatenated = Concatenate(axis= 1)([normal201, concatenated])
那是因为连接层是这样调用的:
concatenated = Concatenate()([normal_input, pretrained_embeddings])
normal_input = Input(shape=(56,))
pretrained_embeddings = Embedding(num_words, 200, input_length=max_length, trainable=False,
weights=[ft_embedding_matrix])
concatenated = concatenate([normal_input, pretrained_embeddings])
dense = Dense(256, activation='relu')(concatenated)
我的想法是创建一个 256 维的输入并将其传递给密集层。
我收到以下错误。
ValueError:调用层 concatenate_10 时输入不是符号张量。接收类型: .完整输入:[ ]。该层的所有输入都应该是张量。
请帮我看看怎么做。
您需要向 select 输入您正在使用的嵌入。
由于您使用了 150 个单词,因此您的嵌入将具有 (batch,150,200)
的形状,无法以任何方式与 (batch, 56)
连接。你需要以某种方式改变一些东西来匹配形状。我建议你尝试使用 Dense
图层将 56 转换为 200...
word_input = Input((150,))
normal_input = Input((56,))
embedding = pretrained_embeddings(word_input)
normal = Dense(200)(normal_input)
#you could add some normalization here - read below
normal = Reshape((1,200))(normal)
concatenated = Concatenate(axis=1)([normal, embedding])
我还建议,由于嵌入和您的输入来自不同的性质,您应用归一化使它们变得更相似:
embedding = BatchNormalization(center=False, scale=False)(embedding)
normal = BatchNormalization(center=False, scale=False)(normal)
另一种可能性(我不能说哪个最好)是在另一个维度上连接,将 56 转换为 150:
word_input = Input((150,))
normal_input = Input((56,))
embedding = pretrained_embeddings(word_input)
normal = Dense(150)(normal_input)
#you could add some normalization here - read below
normal = Reshape((150,1))(normal)
concatenated = Concatenate(axis=-1)([normal, embedding])
我相信这更适合循环网络和卷积网络,你添加一个新通道而不是添加一个新步骤。
您甚至可以尝试双连接,这听起来很酷 :D
word_input = Input((150,))
normal_input = Input((56,))
embedding = pretrained_embeddings(word_input)
normal150 = Dense(150)(normal_input)
normal201 = Dense(201)(normal_input)
embedding = BatchNormalization(center=False, scale=False)(embedding)
normal150 = BatchNormalization(center=False, scale=False)(normal150)
normal201 = BatchNormalization(center=False, scale=False)(normal201)
normal150 = Reshape((150,1))(normal150)
normal201 = Reshape((1,201))(normal201)
concatenated = Concatenate(axis=-1)([normal150, embedding])
concatenated = Concatenate(axis= 1)([normal201, concatenated])
那是因为连接层是这样调用的:
concatenated = Concatenate()([normal_input, pretrained_embeddings])