Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何连接预训练的嵌入层和输入层_Python_Tensorflow_Keras_Concatenation_Word Embedding - Fatal编程技术网

Python 如何连接预训练的嵌入层和输入层

Python 如何连接预训练的嵌入层和输入层,python,tensorflow,keras,concatenation,word-embedding,Python,Tensorflow,Keras,Concatenation,Word Embedding,我的想法是创建一个256维的输入,并将其传递到一个密集层 我犯了以下错误 ValueError:调用层连接_10时使用的输入不是符号张量。收到的类型:。完整输入:[,]。层的所有输入都应该是张量 请帮助我如何执行此操作。您需要输入以选择正在使用的嵌入 因为您使用的是150个单词,所以您的嵌入将具有形状(批次,150200),这是不可能以任何方式与(批次,56)连接的。您需要以某种方式变换某些内容以匹配形状。我建议你尝试一个密度的层,将56转换成200 normal_input = Input(s

我的想法是创建一个256维的输入,并将其传递到一个密集层

我犯了以下错误

ValueError:调用层连接_10时使用的输入不是符号张量。收到的类型:。完整输入:[,]。层的所有输入都应该是张量


请帮助我如何执行此操作。

您需要输入以选择正在使用的嵌入

因为您使用的是150个单词,所以您的嵌入将具有形状
(批次,150200)
,这是不可能以任何方式与
(批次,56)
连接的。您需要以某种方式变换某些内容以匹配形状。我建议你尝试一个密度
层,将56转换成200

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)
我还建议,由于嵌入和您的输入来自不同的性质,您应该应用规范化,使它们变得更相似:

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]) 

另一种可能性(我不能说哪一种是最好的)是在另一个维度中连接,将56转换为150:

embedding = BatchNormalization(center=False, scale=False)(embedding)
normal = BatchNormalization(center=False, scale=False)(normal)
我相信这更适合于循环和卷积网络,你可以添加一个新的通道,而不是添加一个新的步骤


你甚至可以尝试双重串联,听起来很酷:D

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]) 

这是因为连接层的名称如下:

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])

谢谢你的回复,我已经输入了长度150(单词)和向量维数200。当我调用Concatenate时,它抛出
ValueError:新数组的总大小必须保持不变
150个字,这是一个全新的故事。。。。不能将150x200与56连接起来,这是不可能的。我将添加一个建议。如果您使用150个单词作为输入,是的,
(?,150200)
concatenated = Concatenate()([normal_input, pretrained_embeddings])