相关疑难解决方法(0)

验证 Transformer 中多头注意力的实现

我已经实施了MultiAttention headTransformers. 周围有太多的实现,所以很混乱。有人可以验证我的实施是否正确:

DotProductAttention 引用自:https://www.tensorflow.org/tutorials/text/transformer#setup

import tensorflow as tf

def scaled_dot_product(q,k,v):
    #calculates Q . K(transpose)
    qkt = tf.matmul(q,k,transpose_b=True)
    #caculates scaling factor
    dk = tf.math.sqrt(tf.cast(q.shape[-1],dtype=tf.float32))
    scaled_qkt = qkt/dk
    softmax = tf.nn.softmax(scaled_qkt,axis=-1)
    
    z = tf.matmul(softmax,v)
    #shape: (m,Tx,depth), same shape as q,k,v
    return z

class MultiAttention(tf.keras.layers.Layer):
    def __init__(self,d_model,num_of_heads):
        super(MultiAttention,self).__init__()
        self.d_model = d_model
        self.num_of_heads = num_of_heads
        self.depth = d_model//num_of_heads
        self.wq = [tf.keras.layers.Dense(self.depth) for i in range(num_of_heads)]
        self.wk = [tf.keras.layers.Dense(self.depth) for i in range(num_of_heads)]
        self.wv = [tf.keras.layers.Dense(self.depth) for i in …
Run Code Online (Sandbox Code Playgroud)

nlp deep-learning lstm keras tensorflow

5
推荐指数
1
解决办法
2394
查看次数

标签 统计

deep-learning ×1

keras ×1

lstm ×1

nlp ×1

tensorflow ×1