简体   繁体   English

在GRU中使用嵌入层

[英]Using embedding layer with GRU

I have a working code using GRU creating the input manually as a 3D array (None,10,64). 我有一个使用GRU的工作代码,将其手动创建为3D数组(无,10,64)。 The code is: 代码是:

model = Sequential()
model.add(GRU(300, return_sequences=False, input_shape=(None, 64)))
model.add(Dropout(0.8)) 
model.add(Dense(64, input_dim=300))       
model.add(Activation("linear"))

This returns the predicted embedding given the input window. 给定输入窗口,这将返回预测的嵌入。 Now I want to use the keras embedding layer on top of GRU. 现在,我想在GRU上使用keras嵌入层。 My idea is to input a 2D array (None, 10) and use the embedding layer to convert each sample to the corresponding embedding vector. 我的想法是输入一个2D数组(None,10),并使用嵌入层将每个样本转换为相应的嵌入向量。

So now I have this: 所以现在我有了这个:

model = Sequential()
model.add(Embedding(vocab_size, 64, weights=[embedding_matrix], input_length=10, trainable=False))
model.add(GRU(300, return_sequences=False))
model.add(Dropout(0.8)) 
model.add(Dense(64))  
model.add(Activation("linear")) 

I see from the summary that the output of the embedding layer is: 从摘要中我看到嵌入层的输出是:

embedding_2 (Embedding)      (None, 10, 64) 

which is what I expected. 这是我所期望的。 But when I try to fit the model I get this error: 但是,当我尝试拟合模型时,出现此错误:

expected activation_2 to have shape (64,) but got array with shape (1,)

If I comment the other layers and leave only the embedding and gru I get: 如果我评论其他图层并仅保留嵌入和gru,我将得到:

expected gru_5 to have shape (300,) but got array with shape (1,)

So my question is what is the difference between fitting a manually constructed 3D array and an embedding layer generated one?. 因此,我的问题是,将手动构建的3D数组与生成的嵌入层进行拟合之间有什么区别?

Your model reflects the desired computation; 您的模型反映了所需的计算; however, the error is Y you are passing to the model. 但是,错误是传递给模型的Y You are passing a scalar target instead of an array of size (64,) . 您正在传递标量目标,而不是大小(64,)的数组。 To clarify your inputs should be sequence of integers, but your targets still need to be vectors. 为了澄清您的输入应该是整数序列,但是您的目标仍然需要是向量。

Also, Dense by default has linear activation, so you don't need the Activation('linear') after Dense(64) . 另外,默认情况下, Dense具有线性激活,因此您不需要在Dense(64)之后使用Activation('linear') Dense(64)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM