github:https://github.com/sephiroce/tfsr/tree/exprimental
我试图复制语音转换器论文1中描述的识别准确性。注意惩罚是一种我无法完全理解的技术。这是本文对注意惩罚的描述。
“此外,我们鼓励模型注意更近的位置,对距离较远的位置对的注意权重增加更大的惩罚。”
我的理解是,这意味着增加更小的负值更远离对角线上的注意逻辑(在掩蔽之前),除了在解码器中的第一个多头注意力。
这是一个计算注意力权重的代码片段。
# Q * trans(K): (..., seq_len_q, seq_len_k)
matmul_qk = tf.matmul(query, key, transpose_b=True)
# scaled matmul_qk: ( Q * trans(K) ) / sqrt(d_k)
dimension_of_key = tf.cast(tf.shape(key)[-1], tf.float32)
scaled_attention_logits = matmul_qk / tf.math.sqrt(dimension_of_key)
# add the mask to the scaled tensor
if mask is not None:
scaled_attention_logits += (mask * -1e9)
# softmax is normalized on the last axis (seq_len_k) so that the scores
# add up to 1.
attention_weights = tf.nn.softmax(scaled_attention_logits, axis=-1)
# Adding penalty to attention weights and linearly re-normalize it.
if attention_penalty is not None and att_penalty_scale > 0:
attention_weights += (attention_penalty * att_penalty_scale)
attention_weights += tf.math.abs(tf.math.reduce_min(attention_weights))
inv_sum = 1 / tf.math.reduce_sum(attention_weights, axis=-1)
attention_weights = tf.einsum('ijlm,ijl->ijlm', attention_weights, inv_sum)下面的源代码片段用于创建注意损失矩阵。由于注意映射不是对角线的,所以我找不到任何有效的方法来为解码器中的第二个多头注意力权重创建一个注意惩罚矩阵。因此,首先,我试图将注意力惩罚应用于编码器。源代码为从对角线到更远的元素线性地分配更大的惩罚。
有两个超参数,如attention_penalty_scale (这类似于Jindřich建议的penalty_values )和对角线的宽度。
我可能可以添加一个选项,如stripe_step_size。目前,stripe_step_size可以解释为1。
def create_attention_penalty(inp_len, tar_len, num_heads, attention_penalty_width):
max_inp_len = tf.cast(tf.math.reduce_max(inp_len), tf.int32)
n_batch = tf.shape(inp_len)[0]
enc_att_penalty = tf.ones([n_batch, num_heads, max_inp_len, max_inp_len])
accum = tf.zeros(([n_batch, num_heads, max_inp_len, max_inp_len]))
for i in range(attention_penalty_width - 1, max_inp_len - 1):
accum += tf.linalg.band_part(enc_att_penalty, i, i, name=None) - 1
enc_att_penalty = accum
return enc_att_penalty, None即使我按我所理解的那样实现了,我也无法获得任何准确性的改进。而且这个实现还有另一个缺点。训练的速度越来越慢。
( Q)如何有效地运用这种关注惩罚方法对正方形和非方形注意力权重进行惩罚?
参考文献
1董林豪,徐双,徐博,语音变压器:一种用于语音识别的无递归序列-序列-序列模型,icassp2018,https://ieeexplore.ieee.org/document/8462506
发布于 2020-01-13 10:33:48
我想你很明白这一点。他们可能在对角线上画了条条纹,就像:
attention_penalty = (1 - tf.linalg.band_part(scaled_attention_logits, stripe_size, stripe_size)) * penalty然而,您可能需要更多地尝试strip_size和penalty_values应该是什么,因为论文没有说太多。或者你可以试着给作者写信。
https://stackoverflow.com/questions/59646954
复制相似问题