【问题标题】:Embedding and Concatenate - Tensorflow Keras嵌入和连接 - Tensorflow Keras
【发布时间】:2020-04-06 11:13:55
【问题描述】:

我正在尝试复制神经网络。神经网络的架构是LSTM Model。 第一个输入是一个散列词,作为大小为 2^18 的二进制向量,使用嵌入层嵌入到可训练的 500 维分布式表示中。

每个批次元素的字数不同。在嵌入单词并应用 dropout 之后,我需要与特征向量连接,每个单词有 24 个特征。

问题是嵌入后的第一个输入具有不同维度的特征向量。嵌入词有一个维度(无、无、18、500),而特征向量有一个维度(无、无、24)。第一个 None elem 是批大小,第二个 None 是每个批 elem 的字数。

如何将嵌入词与特征向量连接起来?

下面是我的代码:

        inputs = Input(shape=(None, 18,), dtype=np.int16, name="Inp1")
        embbed_input = Embedding(input_dim=1, output_dim=500, input_length=18)
        aux = embbed_input(inputs)
        aux = Dropout(rate=self.dropout_rate)(aux)
        inputs_feat = Input(shape=(None, 24,), dtype=np.float32, name="Inp2")
        aux = concatenate([aux, inputs_feat], axis=2) #ValueError here 
        aux = Dense(units=600, activation="relu")(aux)
        aux = Dense(units=600, activation="relu")(aux)
        aux = Bidirectional(LSTM(units=400, return_sequences=True))(aux)
        aux = Dropout(rate=self.dropout_rate)(aux)
        aux = Dense(units=600, activation="relu")(aux)
        aux = Dense(units=600, activation="relu")(aux)
        aux = Dense(units=29, activation="sigmoid")(aux)


        ValueError: A 'Concatenate' layer require inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, None, 18, 500), (None, None, 24)]

【问题讨论】:

    标签: python tensorflow keras concatenation embedding


    【解决方案1】:

    由于单步输入包含 18 个嵌入层输入,我们可能需要重塑嵌入层输出以展平最后两个维度。

        # import Reshape layer
        from tf.keras.layers import Reshape
    
        inputs = Input(shape=(None, 18,), dtype=np.int16, name="Inp1")
        embbed_input = Embedding(input_dim=1, output_dim=500, input_length=18)
        aux = embbed_input(inputs)
        # Note: it is generally not a great idea to add dropout just after the embedding layer
        aux = Dropout(rate=self.dropout_rate)(aux)
        # before the concatenate layer, reshape it to (None, None, 500*18)
        aux = Reshape(target_shape=(-1,-1,500*18))(aux)
        inputs_feat = Input(shape=(None, 24,), dtype=np.float32, name="Inp2")
        aux = concatenate([aux, inputs_feat], axis=2) # no need to specify axis when it is the final dimension
        aux = Dense(units=600, activation="relu")(aux)
        aux = Dense(units=600, activation="relu")(aux)
        aux = Bidirectional(LSTM(units=400, return_sequences=True))(aux)
        aux = Dropout(rate=self.dropout_rate)(aux)
        aux = Dense(units=600, activation="relu")(aux)
        aux = Dense(units=600, activation="relu")(aux)
        aux = Dense(units=29, activation="sigmoid")(aux)
    

    【讨论】:

    • 输出是否可以是 (None, None, 500) 而不是 (None, None, 500 * 18)?也就是说,在嵌入之前或嵌入中重新表述,因为 18 个输入是一个二进制向量(0 和 1),对应于单词的相同哈希。
    • 你的意思是说你有 2^18 个单词 hash 的唯一值?在这种情况下,您可能必须将二进制向量转换为整数,在您的情况下,范围从 0 到 2^(18)-1。然后将嵌入层更改如下 embbed_input = Embedding(input_dim=2^18, output_dim=500) 在这种情况下,嵌入输出维度将为 (None, None, 500) 并且不需要 concat 层。请注意,嵌入层中可训练参数的数量为 2^18 * 500。确保您有更大的数据集来证明可训练参数的数量。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-12
    • 2022-11-13
    相关资源
    最近更新 更多