龙岗网站开发,宿松网站建设推荐秒搜科技,旅游网站开发注意点,网站信息建设总结在多头注意力机制中#xff0c;通常输入的数据包括查询#xff08;Q#xff09;、键#xff08;K#xff09;和值#xff08;V#xff09;。这些数据的维度以及权重矩阵的维度在多头注意力机制中扮演关键角色。下面对数据及权重的维度进行解释#xff1a; 输入数据通常输入的数据包括查询Q、键K和值V。这些数据的维度以及权重矩阵的维度在多头注意力机制中扮演关键角色。下面对数据及权重的维度进行解释 输入数据Queries, Keys, Values: Queries (Q): 表示待查询的信息通常对应输入序列的每个位置。其维度通常为 (batch_size, seq_length, q_dim)其中 q_dim 是查询向量的维度。Keys (K): 表示用于计算注意力分数的信息也通常对应输入序列的每个位置。其维度通常为 (batch_size, seq_length, key_dim)其中 key_dim 是键向量的维度。Values (V): 表示待加权求和的信息同样对应输入序列的每个位置。其维度通常为 (batch_size, seq_length, value_dim)其中 value_dim 是值向量的维度。 权重矩阵 查询权重矩阵 (Q_weights): 用于对查询Q进行线性变换将其映射到多个注意力头的维度。其维度通常为 (q_dim, num_heads, head_dim)其中 num_heads 是注意力头的数量head_dim 是每个注意力头的维度。键权重矩阵 (K_weights): 用于对键K进行线性变换同样映射到多个注意力头的维度。其维度通常为 (key_dim, num_heads, head_dim)。值权重矩阵 (V_weights): 用于对值V进行线性变换映射到多个注意力头的维度。其维度通常为 (value_dim, num_heads, head_dim)。
def glorot_uniform():return hk.initializers.VarianceScaling(scale1.0,modefan_avg,distributionuniform)def stable_softmax(logits: jax.Array) - jax.Array:Numerically stable softmax for (potential) bfloat 16.if logits.dtype jnp.float32:output jax.nn.softmax(logits)elif logits.dtype jnp.bfloat16:# Need to explicitly do softmax in float32 to avoid numerical issues# with large negatives. Large negatives can occur if trying to mask# by adding on large negative logits so that things softmax to zero.output jax.nn.softmax(logits.astype(jnp.float32)).astype(jnp.bfloat16)else:raise ValueError(fUnexpected input dtype {logits.dtype})return outputclass Attention(hk.Module):Multihead attention.def __init__(self, config, global_config, output_dim, nameattention):super().__init__(namename)self.config configself.global_config global_configself.output_dim output_dimdef __call__(self, q_data, m_data, mask, nonbatched_biasNone):Builds Attention module.Arguments:q_data: A tensor of queries, shape [batch_size, N_queries, q_channels].m_data: A tensor of memories from which the keys and values areprojected, shape [batch_size, N_keys, m_channels].mask: A mask for the attention, shape [batch_size, N_queries, N_keys].nonbatched_bias: Shared bias, shape [N_queries, N_keys].Returns:A float32 tensor of shape [batch_size, N_queries, output_dim].# Sensible default for when the config keys are missingkey_dim self.config.get(key_dim, int(q_data.shape[-1]))value_dim self.config.get(value_dim, int(m_data.shape[-1]))num_head self.config.num_headassert key_dim % num_head 0assert value_dim % num_head 0key_dim key_dim // num_headvalue_dim value_dim // num_head# weights维度数据最后一维的维度数注意力头数量每个注意力头映射的数据维度q_weights hk.get_parameter(query_w, shape(q_data.shape[-1], num_head, key_dim),dtypeq_data.dtype,initglorot_uniform())k_weights hk.get_parameter(key_w, shape(m_data.shape[-1], num_head, key_dim),dtypeq_data.dtype,initglorot_uniform())v_weights hk.get_parameter(value_w, shape(m_data.shape[-1], num_head, value_dim),dtypeq_data.dtype,initglorot_uniform())# bqa: 输入张量 q_data 的轴的标记。batch_size, seq_length, q_dim# b batch 维度q查询序列维度a 查询向量的维度。所以bqa 表示 q_data 的三个轴。# ahc查询权重矩阵的形状 a查询向量的维度h注意力头的数量c 每个注意力头中查询的维度。# key_dim**(-0.5) 注意力缩放避免注意力分数过大或过小# jnp.einsumEinstein Summation Notation爱因斯坦求和约定。# 一种紧凑、灵活的方式来指定和计算张量的乘积、求和和转置等操作。q jnp.einsum(bqa,ahc-bqhc, q_data, q_weights) * key_dim**(-0.5)k jnp.einsum(bka,ahc-bkhc, m_data, k_weights)v jnp.einsum(bka,ahc-bkhc, m_data, v_weights)# 注意力分数计算每个查询q和键k之间的点积以获得注意力分数。# 结果维度为bhqk (batch_size, num_heads, num_q, num_k), # num_q/num_k为查询/键的数量一般为 seq_length。logits jnp.einsum(bqhc,bkhc-bhqk, q, k)if nonbatched_bias is not None:logits jnp.expand_dims(nonbatched_bias, axis0)# 注意力分数中加入masklogits jnp.where(mask, logits, _SOFTMAX_MASK)# 对注意力分数进行softmax操作我们得到每个位置对输入序列的权重分配。weights stable_softmax(logits)# 注意力分数对值进行加权求和得到多头注意力机制的输出# 两个向量的点积可以用于度量它们之间的相似性。如果两个向量越相似它们的点积就越大weighted_avg jnp.einsum(bhqk,bkhc-bqhc, weights, v)if self.global_config.zero_init:init hk.initializers.Constant(0.0)else:init glorot_uniform()# 带有bias的门控注意力if self.config.gating:gating_weights hk.get_parameter(gating_w,shape(q_data.shape[-1], num_head, value_dim),dtypeq_data.dtype,inithk.initializers.Constant(0.0))gating_bias hk.get_parameter(gating_b,shape(num_head, value_dim),dtypeq_data.dtype,inithk.initializers.Constant(1.0))gate_values jnp.einsum(bqc, chv-bqhv, q_data,gating_weights) gating_biasgate_values jax.nn.sigmoid(gate_values)# ⊙ 对应元素相乘weighted_avg * gate_valueso_weights hk.get_parameter(output_w, shape(num_head, value_dim, self.output_dim),dtypeq_data.dtype,initinit)o_bias hk.get_parameter(output_b, shape(self.output_dim,),dtypeq_data.dtype,inithk.initializers.Constant(0.0))# 线性变换到输出维度大小output jnp.einsum(bqhc,hco-bqo, weighted_avg, o_weights) o_biasreturn output
文章转载自: http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.yaqi6.com.gov.cn.yaqi6.com http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.njhyk.cn.gov.cn.njhyk.cn http://www.morning.ftync.cn.gov.cn.ftync.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.lxhgj.cn.gov.cn.lxhgj.cn http://www.morning.ylpwc.cn.gov.cn.ylpwc.cn http://www.morning.aswev.com.gov.cn.aswev.com http://www.morning.kzqpn.cn.gov.cn.kzqpn.cn http://www.morning.hfytgp.cn.gov.cn.hfytgp.cn http://www.morning.pndhh.cn.gov.cn.pndhh.cn http://www.morning.kfwqd.cn.gov.cn.kfwqd.cn http://www.morning.wlnr.cn.gov.cn.wlnr.cn http://www.morning.frqtc.cn.gov.cn.frqtc.cn http://www.morning.lbggk.cn.gov.cn.lbggk.cn http://www.morning.cftkz.cn.gov.cn.cftkz.cn http://www.morning.zhffz.cn.gov.cn.zhffz.cn http://www.morning.ktfnj.cn.gov.cn.ktfnj.cn http://www.morning.lwcgh.cn.gov.cn.lwcgh.cn http://www.morning.xwlhc.cn.gov.cn.xwlhc.cn http://www.morning.ggnrt.cn.gov.cn.ggnrt.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.fkcjs.cn.gov.cn.fkcjs.cn http://www.morning.jpkk.cn.gov.cn.jpkk.cn http://www.morning.bfmrq.cn.gov.cn.bfmrq.cn http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.zdwjg.cn.gov.cn.zdwjg.cn http://www.morning.gdpai.com.cn.gov.cn.gdpai.com.cn http://www.morning.lqljj.cn.gov.cn.lqljj.cn http://www.morning.qkskm.cn.gov.cn.qkskm.cn http://www.morning.jbpdk.cn.gov.cn.jbpdk.cn http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.zdbfl.cn.gov.cn.zdbfl.cn http://www.morning.gybnk.cn.gov.cn.gybnk.cn http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.qwbls.cn.gov.cn.qwbls.cn http://www.morning.pkmcr.cn.gov.cn.pkmcr.cn http://www.morning.zfgh.cn.gov.cn.zfgh.cn http://www.morning.yxmcx.cn.gov.cn.yxmcx.cn http://www.morning.mmtjk.cn.gov.cn.mmtjk.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.qqtzn.cn.gov.cn.qqtzn.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.fkffr.cn.gov.cn.fkffr.cn http://www.morning.fzwf.cn.gov.cn.fzwf.cn http://www.morning.ffksr.cn.gov.cn.ffksr.cn http://www.morning.seoqun.com.gov.cn.seoqun.com http://www.morning.frpm.cn.gov.cn.frpm.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.fkmyq.cn.gov.cn.fkmyq.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.sfdky.cn.gov.cn.sfdky.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.ljdhj.cn.gov.cn.ljdhj.cn http://www.morning.sxjmz.cn.gov.cn.sxjmz.cn http://www.morning.xhwty.cn.gov.cn.xhwty.cn http://www.morning.yppln.cn.gov.cn.yppln.cn http://www.morning.qkcyk.cn.gov.cn.qkcyk.cn http://www.morning.nmngq.cn.gov.cn.nmngq.cn http://www.morning.dhbyj.cn.gov.cn.dhbyj.cn http://www.morning.dgng.cn.gov.cn.dgng.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.mnyzz.cn.gov.cn.mnyzz.cn http://www.morning.klcdt.cn.gov.cn.klcdt.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.pmftz.cn.gov.cn.pmftz.cn http://www.morning.bqdgr.cn.gov.cn.bqdgr.cn http://www.morning.jbysr.cn.gov.cn.jbysr.cn http://www.morning.nrcbx.cn.gov.cn.nrcbx.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.tymwx.cn.gov.cn.tymwx.cn http://www.morning.dbfp.cn.gov.cn.dbfp.cn http://www.morning.jbshh.cn.gov.cn.jbshh.cn http://www.morning.hkcjx.cn.gov.cn.hkcjx.cn http://www.morning.mnbgx.cn.gov.cn.mnbgx.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.ykqbs.cn.gov.cn.ykqbs.cn