位置编码导致更差的收敛,语言建模

2024-10-05 10:42:06 发布

您现在位置:Python中文网/ 问答频道 /正文

这是一个很难回答的问题,但我还是试试看。我正在实现本文中的体系结构https://arxiv.org/pdf/1503.08895.pdf,用于语言建模。图见第2页,位置或“时间”编码部分见第5页顶部。关于位置编码的更多信息可以在这里找到,https://arxiv.org/pdf/1706.03762.pdf在第5页的底部/第6页的顶部。(第一篇论文的作者指导我阅读第二篇论文。)

简而言之,下面是我的keras实现:

word_seq = Input(shape = (SEQ_LEN,), dtype = "int32", name = "word_seq")

query = Input(shape = (EMBED_DIM, ), dtype = "float32", name = "q_input")
#the query for lang. modeling is a constant vector filled with 0.1, as described at the bottom of page 7 in the first linked paper

T_A = Added_Weights(input_dim = (SEQ_LEN, EMBED_DIM))
#Added_Weights is a custom layer I wrote, which I'll post below
#These are the "positional encoding" components

T_C = Added_Weights(input_dim = (SEQ_LEN, EMBED_DIM))

Emb_A = Embedding(output_dim = EMBED_DIM, input_dim = VOCAB_SIZE, input_length = SEQ_LEN, name = "Emb_A")

Emb_C = Embedding(output_dim = EMBED_DIM, input_dim = VOCAB_SIZE, input_length = SEQ_LEN, name = "Emb_C")

int_state_weights = Dense(units = EMBED_DIM, activation = 'linear',
           kernel_initializer=RandomNormal(mean=0., stddev = 0.05, seed = None))

layer_output = query
#the loop uses the output from the previous layer as the query, but the first layer's query is just that constant vector

for i in range(0, NUM_LAYERS - 1):
    memories = Emb_A(word_seq) #these all re-use the weights instantiated earlier.

    memories = T_A(memories)

    memories = Dropout(DROPOUT_R)(memories)

    content = Emb_C(word_seq)

    content = T_C(content)

    mem_relevance = Dot(axes=[1, 2])([layer_output, memories])

    weighted_internal_state = int_state_weights(mem_relevance)

    mem_relevance = Softmax()(mem_relevance)

    content_relevance = Dot(axes=1)([mem_relevance,
                                content])  # weight each piece of content by it's probability of being relevant

    layer_output = Add()([content_relevance, weighted_internal_state])

    layer_output = Dropout(DROPOUT_R)(layer_output)

final_output = Dense(units = VOCAB_SIZE, activation ='relu',
                 kernel_initializer=RandomNormal(mean=0., stddev = 0.05, seed = None))(layer_output)

model = Model(inputs = [word_seq, query], outputs = prediction)
model.compile(optimizer = SGD(lr = 0.01, clipnorm = 50.), loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.fit(x = [td_seqs, td_query], y = [td_labels],
      batch_size = BATCH_SIZE, callbacks = [lr_adjust, lr_termination, for_csv], epochs=200, verbose = 1)

批处理大小当前为128。在我添加T_A和T_C部分之前,这在大约35000个训练样本上进行得很好,最终准确率为96%。一旦我实现了T_A和T_C(位置编码),训练就以大约10%的准确率和5.2英寸的训练损失而结束。我把培训数据增加了10倍,但没有看到任何真正的改善。以下是我的附加重量课程:

^{pr2}$

在阅读了这两篇明确指出它应该起作用的优秀论文后,我正在为为什么这不起作用而苦恼。如果有人能帮上忙,那就太棒了。在


Tags: thelayerinputoutputembedcontentqueryseq

热门问题