Keras官方的关于Transformer在时间序列中应用的示例代码是否包含位置嵌入部分?

gcuhipw9  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(176)

从url引用的示例代码:https://keras.io/examples/timeseries/timeseries_transformer_classification/
我在上面的网址中找不到任何关于“位置嵌入”内容的描述。当我浏览NLP中应用的Transformer时,我可以清楚地看到名为“TokenAndPositionEmbeding”的类。
如果它不包含“Position Embedding”,我如何在示例代码的时间序列中应用Position Embedding?

piv4azn7

piv4azn71#

据我所知,它不包含位置嵌入。像这样的东西应该可以工作。

class PositionEmbeddingFixedWeights(Layer):
def __init__(self, sequence_length, vocab_size, output_dim, **kwargs):
    super(PositionEmbeddingFixedWeights, self).__init__(**kwargs)
    word_embedding_matrix = self.get_position_encoding(vocab_size, output_dim)   
    position_embedding_matrix = self.get_position_encoding(sequence_length, output_dim)                                          
    self.word_embedding_layer = Embedding(
        input_dim=vocab_size, output_dim=output_dim,
        weights=[word_embedding_matrix],
        trainable=False
    )
    self.position_embedding_layer = Embedding(
        input_dim=sequence_length, output_dim=output_dim,
        weights=[position_embedding_matrix],
        trainable=False
    )
         
def get_position_encoding(self, seq_len, d, n=10000):
    P = np.zeros((seq_len, d))
    for k in range(seq_len):
        for i in np.arange(int(d/2)):
            denominator = np.power(n, 2*i/d)
            P[k, 2*i] = np.sin(k/denominator)
            P[k, 2*i+1] = np.cos(k/denominator)
    return P

def call(self, inputs):        
    position_indices = tf.range(tf.shape(inputs)[-1])
    embedded_words = self.word_embedding_layer(inputs)
    embedded_indices = self.position_embedding_layer(position_indices)
    return embedded_words + embedded_indices

此类源自https://machinelearningmastery.com/the-transformer-positional-encoding-layer-in-keras-part-2/

相关问题