tensorflow keras 嵌入 lstm

tensorflow keras embedding lstm

我想在将我的输入数据输入我尝试创建的 LSTM 网络之前使用嵌入层。这是代码的相关部分:

input_step1 = Input(shape=(SEQ_LENGTH_STEP1, NR_FEATURES_STEP1), 
                           name='input_step1')

step1_lstm = CuDNNLSTM(50,
                       return_sequences=True,
                       return_state = True,
                       name="step1_lstm")

out_step1, state_h_step1, state_c_step1 = step1_lstm(input_step1)

我对应该如何在此处添加嵌入层感到有点困惑..

下面是文档中对嵌入层的描述:

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 定义了序列长度和特征数量。再写一遍:

input_step1 = Input(shape=(SEQ_LENGTH_STEP1, NR_FEATURES_STEP1), 
                           name='input_step1')

在定义Embedding层时,我很困惑Embedding函数的哪些参数对应于"number of sequence"和"number of features in each time step"。谁能指导我如何将嵌入层集成到我上面的代码中?

附录:

如果我尝试以下操作:

SEQ_LENGTH_STEP1  = 5 
NR_FEATURES_STEP1 = 10 

input_step1 = Input(shape=(SEQ_LENGTH_STEP1, NR_FEATURES_STEP1), 
                           name='input_step1')

emb = Embedding(input_dim=NR_FEATURES_STEP1,
                output_dim=15,
                input_length=NR_FEATURES_STEP1)

input_step1_emb = emb(input_step1)

step1_lstm = CuDNNLSTM(50,
                       return_sequences=True,
                       return_state = True,
                       name="step1_lstm")

out_step1, state_h_step1, state_c_step1 = step1_lstm(input_step1_emb)

我收到以下错误:

ValueError: Input 0 of layer step1_lstm is incompatible with the layer:
expected ndim=3, found ndim=4. Full shape received: [None, 5, 10, 15]

我显然没有做正确的事。有没有办法将 Embedding 集成到我尝试尝试的 LSTM 网络中?

来自 Keras Embedding 文档:

Arguments

  • input_dim: int > 0. Size of the vocabulary, i.e. maximum integer index + 1.
  • output_dim: int >= 0. Dimension of the dense embedding.
  • input_length: Length of input sequences, when it is constant. This argument is required if you are going to connect Flatten then Dense layers upstream (without it, the shape of the dense outputs cannot be computed).

因此,根据您的描述,我认为:

  • input_dim 对应于数据集的词汇量(不同单词的数量)。例如,以下数据集的词汇表大小为 5:

    data = ["Come back Peter,",
            "Come back Paul"]
    
  • output_dim 是一个任意超参数,表示嵌入的维度 space。换句话说,如果你设置output_dim=x,那么句子中的每个词都会有x个特征。

  • input_length应该设置为SEQ_LENGTH_STEP1(表示每句长度的整数),假设所有句子的长度都相同。

嵌入层的输出形状是(batch_size, input_length, output_dim)


关于附录的进一步说明:

  • team_in_step1 未定义。
  • 假设你的第一层是Embedding层,输入张量input_step1的预期形状是(batch_size, input_length):

    input_step1 = Input(shape=(SEQ_LENGTH_STEP1,), 
                               name='input_step1')
    

    这个张量中的每个整数对应一个词

  • 如上所述,嵌入层可以实例化如下:

    emb = Embedding(input_dim=VOCAB_SIZE,
                    output_dim=15,
                    input_length=SEQ_LENGTH_STEP1)
    

    其中 VOCAB_SIZE 是您的词汇量。

  • 包含一个您可能会觉得有用的可重现示例。