{"id":72570,"date":"2026-02-06T00:32:50","date_gmt":"2026-02-05T16:32:50","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/72570.html"},"modified":"2026-02-06T00:32:50","modified_gmt":"2026-02-05T16:32:50","slug":"%e6%89%8b%e6%8e%a8transformer%e5%bc%a0%e9%87%8f%e4%bc%a0%e9%80%92%e8%bf%87%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/72570.html","title":{"rendered":"\u624b\u63a8Transformer\u5f20\u91cf\u4f20\u9012\u8fc7\u7a0b"},"content":{"rendered":"<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"450\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205163249-6984c63177b9b.png\" width=\"554\" \/><\/p>\n<h3>\u6b65\u9aa41&#xff1a;\u4f4d\u7f6e\u5d4c\u5165\u4e0e\u8bcd\u6620\u5c04<\/h3>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"102\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205163249-6984c6319df8e.png\" width=\"142\" \/><\/p>\n<p>import torch<br \/>\nimport torch.nn as nn<br \/>\nimport math<\/p>\n<p>class TransformerInput(nn.Module):<br \/>\n    def __init__(self, vocab_size, d_model, max_len&#061;5000):<br \/>\n        super().__init__()<br \/>\n        # 1. Embedding \u5c42&#xff1a;\u5c06\u5355\u8bcd\u7d22\u5f15\u8f6c\u4e3a 512 \u7ef4\u5411\u91cf<br \/>\n        self.embedding &#061; nn.Embedding(vocab_size, d_model)<\/p>\n<p>        # 2. Positional Encoding&#xff1a;\u751f\u6210\u4f4d\u7f6e\u504f\u7f6e<br \/>\n        pe &#061; torch.zeros(max_len, d_model)<br \/>\n        position &#061; torch.arange(0, max_len, dtype&#061;torch.float).unsqueeze(1)<br \/>\n        # \u4f7f\u7528\u6b63\u4f59\u5f26\u516c\u5f0f&#xff1a;PE(pos, 2i) &#061; sin(pos\/10000^(2i\/d_model))<br \/>\n        div_term &#061; torch.exp(torch.arange(0, d_model, 2).float() * (-math.log(10000.0) \/ d_model))<br \/>\n        pe[:, 0::2] &#061; torch.sin(position * div_term)<br \/>\n        pe[:, 1::2] &#061; torch.cos(position * div_term)<br \/>\n        self.register_buffer(&#039;pe&#039;, pe.unsqueeze(0)) # \u56fa\u5b9a\u6743\u91cd&#xff0c;\u4e0d\u53c2\u4e0e\u8bad\u7ec3<\/p>\n<p>    def forward(self, x):<br \/>\n        # x \u5f62\u72b6: [batch_size, seq_len] -&gt; \u8bcd\u5411\u91cf: [batch_size, seq_len, 512]<br \/>\n        x &#061; self.embedding(x) * math.sqrt(x.size(-1)) # \u7f29\u653e\u4ee5\u5339\u914d PE \u5f3a\u5ea6<br \/>\n        # \u76f4\u63a5\u76f8\u52a0&#xff1a;\u56fe\u4e2d\u90a3\u4e2a\u5708\u5708\u91cc\u7684 &#034;&#043;&#034; \u53f7<br \/>\n        x &#061; x &#043; self.pe[:, :x.size(1)]<br \/>\n        return x<\/p>\n<p>\u76f8\u6bd4\u4f20\u7edf RNN \u7684\u9010\u8bcd\u8f93\u5165&#xff0c;Transformer \u7684\u8f93\u5165\u662f\u4e00\u6574\u53e5\u8bdd\u3002\u4e3a\u4e86\u533a\u5206\u6bcf\u4e2a\u8bcd\u7684\u4f4d\u7f6e\u4fe1\u606f&#xff0c;\u9700\u8981\u7ed9\u5b83\u52a0\u4e0a\u4f4d\u7f6e\u7f16\u7801\u3002<\/p>\n<p>\u6ce8\u610f&#xff1a;vocab_size \u6d89\u53ca\u5230\u53e5\u5b50\u89e3\u7801\u6240\u6d89\u53ca\u7684 token \u91cf&#xff0c;\u5373 BPE \u7b97\u6cd5\u6700\u7ec8\u505c\u6b65\u7684\u9650\u5236\u503c\u3002\u4f46\u662f Embedding \u8fd9\u4e2a\u4ece token \u6620\u5c04\u5230\u5411\u91cf\u7684\u6b65\u9aa4\u662f\u9700\u8981\u8bad\u7ec3\u7684\u3002<\/p>\n<table>\n<tr>\n<td>\u7ec4\u4ef6\u540d\u79f0<\/td>\n<td>\u6765\u6e90<\/td>\n<td>\u662f\u5426\u6d89\u53ca Transformer \u8bad\u7ec3<\/td>\n<td>\u6bd4\u55bb<\/td>\n<\/tr>\n<tbody>\n<tr>\n<td>\u8bcd\u8868 (Vocabulary)<\/td>\n<td>\u7edf\u8ba1\u7b97\u6cd5 (Tokenizer) \u626b\u63cf\u8bed\u6599\u5e93<\/td>\n<td>\u5426 (\u9884\u5904\u7406\u9636\u6bb5\u5b8c\u6210)<\/td>\n<td>\u5b57\u5178\u7684\u76ee\u5f55&#xff08;\u51b3\u5b9a\u4e86\u4e66\u91cc\u6536\u5f55\u54ea\u4e9b\u5b57&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>vocab_size<\/td>\n<td>\u4eba\u4e3a\u8bbe\u5b9a\u7684\u9608\u503c<\/td>\n<td>\u5426 (\u914d\u7f6e\u53c2\u6570)<\/td>\n<td>\u5b57\u5178\u7684\u539a\u5ea6&#xff08;\u51b3\u5b9a\u4e86\u5b57\u5178\u80fd\u67e5\u591a\u5c11\u4e2a\u5b57&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>Embedding \u77e9\u9635<\/td>\n<td>nn.Embedding<\/td>\n<td>\u662f (\u6838\u5fc3\u8bad\u7ec3\u5bf9\u8c61)<\/td>\n<td>\u5bf9\u5b57\u7684\u7406\u89e3&#xff08;\u4e00\u5f00\u59cb\u778e\u731c&#xff0c;\u8d8a\u5b66\u8d8a\u61c2&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>d_model<\/td>\n<td>\u4eba\u4e3a\u8bbe\u5b9a\u7684\u5bbd\u5ea6<\/td>\n<td>\u5426 (\u914d\u7f6e\u53c2\u6570)<\/td>\n<td>\u8111\u5bb9\u91cf&#xff08;\u7528\u591a\u5c11\u4e2a\u8111\u7ec6\u80de\u53bb\u8bb0\u4e00\u4e2a\u8bcd&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u4f4d\u7f6e\u7f16\u7801 (PE)<\/td>\n<td>sin cos \u6570\u5b66\u516c\u5f0f<\/td>\n<td>\u5426 (\u56fa\u5b9a\u53c2\u6570)<\/td>\n<td>\u9875\u7801&#xff08;\u65e0\u8bba\u4e66\u5199\u4ec0\u4e48\u5185\u5bb9&#xff0c;\u9875\u7801\u6c38\u8fdc\u662f 1, 2, 3&#8230;&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<h3>\u6b65\u9aa42&#xff1a;\u7f16\u7801\u5668\u90e8\u5206&#xff08;\u65c1\u8fb9\u7684 Nx \u8868\u793a\u53ef\u4ee5\u7ec4\u88c5 N \u4e2a\u7f16\u7801\u5668&#xff09;<\/h3>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"213\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205163249-6984c631b9dde.png\" width=\"177\" \/><\/p>\n<p>\u5bf9\u4e8e\u8fd9\u4e2a\u5c0f\u7070\u6846&#xff0c;\u6211\u4eec\u8ba4\u4e3a\u662f\u4e00\u5c42\u7684 Encoder&#xff0c;\u79f0\u4e3a EncoderLayer&#xff0c;\u5305\u542b\u4e24\u4e2a\u5b50\u5c42&#xff08;\u591a\u5934\u6ce8\u610f\u529b &#043; \u524d\u9988 FFN&#xff09;\u3002\u6bcf\u4e2a\u5b50\u5c42\u540e\u90fd\u5305\u542b\u4e86\u5c42\u5f52\u4e00\u4e0e\u6b8b\u5dee\u94fe\u63a5\u3002<\/p>\n<p>import torch<br \/>\nimport torch.nn as nn<br \/>\nimport copy<\/p>\n<p># \u8f85\u52a9\u51fd\u6570&#xff1a;\u7528\u4e8e\u514b\u9686 N \u4e2a\u76f8\u540c\u7684\u5c42&#xff08;\u540e\u9762\u6784\u5efa Encoder \u65f6\u7528\u5230&#xff09;<br \/>\ndef clones(module, N):<br \/>\n    return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])<\/p>\n<p>class EncoderLayer(nn.Module):<br \/>\n    def __init__(self, d_model, num_heads, d_ff, dropout&#061;0.1):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u521d\u59cb\u5316\u5355\u5c42\u7f16\u7801\u5668<br \/>\n        :param d_model: \u8bcd\u5411\u91cf\u7ef4\u5ea6 (\u4f8b\u5982 512)<br \/>\n        :param num_heads: \u591a\u5934\u6ce8\u610f\u529b\u7684\u5934\u6570 (\u4f8b\u5982 8)<br \/>\n        :param d_ff: \u524d\u9988\u795e\u7ecf\u7f51\u7edc\u7684\u4e2d\u95f4\u5c42\u7ef4\u5ea6 (\u4f8b\u5982 2048)<br \/>\n        :param dropout: \u4e22\u5f03\u7387&#xff0c;\u9632\u6b62\u8fc7\u62df\u5408<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<\/p>\n<p>        # &#8212; \u5b50\u5c42 1: \u591a\u5934\u81ea\u6ce8\u610f\u529b &#8212;<br \/>\n        # \u5bf9\u5e94\u56fe\u4e2d: Multi-Head Attention<br \/>\n        self.self_attn &#061; MultiHeadAttention(d_model, num_heads)<\/p>\n<p>        # &#8212; \u5b50\u5c42 2: \u524d\u9988\u795e\u7ecf\u7f51\u7edc &#8212;<br \/>\n        # \u5bf9\u5e94\u56fe\u4e2d: Feed Forward<br \/>\n        self.feed_forward &#061; PositionwiseFeedForward(d_model, d_ff)<\/p>\n<p>        # &#8212; \u8fde\u63a5\u7ec4\u4ef6 &#8212;<br \/>\n        # \u5bf9\u5e94\u56fe\u4e2d: Add &amp; Norm (\u6709\u4e24\u4e2a&#xff0c;\u5206\u522b\u8ddf\u5728\u4e24\u4e2a\u5b50\u5c42\u540e\u9762)<br \/>\n        self.norm1 &#061; nn.LayerNorm(d_model)<br \/>\n        self.norm2 &#061; nn.LayerNorm(d_model)<\/p>\n<p>        # Dropout (\u653e\u5728\u6b8b\u5dee\u8fde\u63a5\u76f8\u52a0\u4e4b\u524d)<br \/>\n        self.dropout &#061; nn.Dropout(dropout)<\/p>\n<p>    def forward(self, x, mask):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u524d\u5411\u4f20\u64ad\u903b\u8f91<br \/>\n        :param x: \u8f93\u5165\u6570\u636e [batch_size, seq_len, d_model]<br \/>\n        :param mask: \u63a9\u7801\u77e9\u9635 (\u7528\u4e8e\u5c4f\u853d PAD token)<br \/>\n        &#034;&#034;&#034;<\/p>\n<p>        # &#8212; \u6b65\u9aa4 1: Self-Attention &#043; Add &#043; Norm &#8212;<br \/>\n        # 1.1 \u4fdd\u7559\u539f\u59cb\u8f93\u5165\u7528\u4e8e\u6b8b\u5dee\u8fde\u63a5 (Residue)<br \/>\n        residual &#061; x <\/p>\n<p>        # 1.2 \u6267\u884c\u591a\u5934\u6ce8\u610f\u529b<br \/>\n        # \u6ce8\u610f&#xff1a;Encoder \u4e2d Q, K, V \u5168\u90e8\u6765\u6e90\u4e8e\u540c\u4e00\u4e2a x (\u6240\u4ee5\u53eb&#034;\u81ea&#034;\u6ce8\u610f\u529b)<br \/>\n        x_attn &#061; self.self_attn(x, x, x, mask)<\/p>\n<p>        # 1.3 Dropout &#043; Add (\u6b8b\u5dee\u8fde\u63a5) &#043; Norm<br \/>\n        # \u516c\u5f0f: LayerNorm(x &#043; Sublayer(x))<br \/>\n        x &#061; self.norm1(residual &#043; self.dropout(x_attn))<\/p>\n<p>        # &#8212; \u6b65\u9aa4 2: Feed Forward &#043; Add &#043; Norm &#8212;<br \/>\n        # 2.1 \u518d\u6b21\u4fdd\u7559\u5f53\u524d\u72b6\u6001\u7528\u4e8e\u6b8b\u5dee\u8fde\u63a5<br \/>\n        residual &#061; x<\/p>\n<p>        # 2.2 \u6267\u884c\u524d\u9988\u7f51\u7edc<br \/>\n        x_ff &#061; self.feed_forward(x)<\/p>\n<p>        # 2.3 Dropout &#043; Add &#043; Norm<br \/>\n        x &#061; self.norm2(residual &#043; self.dropout(x_ff))<\/p>\n<p>        return x<\/p>\n<p>\u591a\u5934\u6ce8\u610f\u529b\u5b9e\u9645\u4e0a\u5c31\u662f\u4f7f\u7528\u4e09\u4e2a\u77e9\u9635\u5bf9\u8f93\u5165\u8fdb\u884c\u7ebf\u6027\u53d8\u6362&#xff08;\u77e9\u9635\u4e58\u6cd5&#xff09;\u3002<\/p>\n<p>import torch<br \/>\nimport torch.nn as nn<br \/>\nimport math<\/p>\n<p>class MultiHeadAttention(nn.Module):<br \/>\n    def __init__(self, d_model, num_heads):<br \/>\n        super().__init__()<br \/>\n        assert d_model % num_heads &#061;&#061; 0<\/p>\n<p>        self.d_k &#061; d_model \/\/ num_heads  # 64<br \/>\n        self.num_heads &#061; num_heads       # 8<br \/>\n        self.d_model &#061; d_model           # 512<\/p>\n<p>        # \u3010\u5173\u952e\u70b91\u3011\u5b9a\u4e49\u53c2\u6570\u77e9\u9635<br \/>\n        # \u6ce8\u610f&#xff1a;\u8fd9\u91cc\u5b9a\u4e49\u7684\u662f&#034;\u6240\u6709\u5934\u52a0\u8d77\u6765&#034;\u7684\u5927\u77e9\u9635<br \/>\n        # W_q, W_k, W_v \u7684\u5f62\u72b6\u90fd\u662f [512, 512]<br \/>\n        self.linear_q &#061; nn.Linear(d_model, d_model)<br \/>\n        self.linear_k &#061; nn.Linear(d_model, d_model)<br \/>\n        self.linear_v &#061; nn.Linear(d_model, d_model)<\/p>\n<p>        # \u6700\u540e\u8f93\u51fa\u7684\u7ebf\u6027\u5c42 W_o<br \/>\n        self.linear_out &#061; nn.Linear(d_model, d_model)<\/p>\n<p>    def forward(self, query, key, value, mask&#061;None):<br \/>\n        batch_size &#061; query.size(0) # 32<\/p>\n<p>        # &#8212; \u7b2c\u4e00\u6b65&#xff1a;\u7ebf\u6027\u6295\u5f71 &#043; \u5207\u5206\u5934 &#8212;<br \/>\n        # 1. \u7ebf\u6027\u53d8\u6362: [32, 10, 512] * [512, 512] -&gt; [32, 10, 512]<br \/>\n        # 2. view\u91cd\u5851: -&gt; [32, 10, 8, 64] (\u628a 512 \u62c6\u6210 8 * 64)<br \/>\n        # 3. transpose\u8f6c\u7f6e: -&gt; [32, 8, 10, 64] (\u4ea4\u6362 seq_len \u548c num_heads \u7ef4\u5ea6)<br \/>\n        #    \u4e3a\u4ec0\u4e48\u8981\u8f6c\u7f6e&#xff1f;\u56e0\u4e3a\u6211\u4eec\u8981\u8ba9 Attention \u53d1\u751f\u5728 seq_len \u7ef4\u5ea6\u4e0a&#xff0c;<br \/>\n        #    \u628a num_heads \u653e\u5230\u524d\u9762&#xff0c;PyTorch \u5c31\u4f1a\u628a\u5b83\u4eec\u5f53\u6210\u72ec\u7acb\u7684&#034;\u6279\u6b21&#034;\u5e76\u884c\u8ba1\u7b97\u3002<\/p>\n<p>        Q &#061; self.linear_q(query).view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2)<br \/>\n        K &#061; self.linear_k(key).view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2)<br \/>\n        V &#061; self.linear_v(value).view(batch_size, -1, self.num_heads, self.d_k).transpose(1, 2)<\/p>\n<p>        # &#8212; \u7b2c\u4e8c\u6b65&#xff1a;\u7f29\u653e\u70b9\u79ef\u6ce8\u610f\u529b (Scaled Dot-Product Attention) &#8212;<br \/>\n        # Q: [32, 8, 10, 64]<br \/>\n        # K.T: [32, 8, 64, 10] (\u6700\u540e\u4e24\u4e2a\u7ef4\u5ea6\u8f6c\u7f6e)<br \/>\n        # Matmul: [32, 8, 10, 10] -&gt; \u5f97\u5230\u6bcf\u4e2a\u5934\u5185\u90e8\u7684\u8bcd\u4e0e\u8bcd\u7684\u5173\u7cfb\u5206\u6570<br \/>\n        scores &#061; torch.matmul(Q, K.transpose(-2, -1)) \/ math.sqrt(self.d_k)<\/p>\n<p>        if mask is not None:<br \/>\n            scores &#061; scores.masked_fill(mask &#061;&#061; 0, -1e9)<\/p>\n<p>        attn_weights &#061; torch.softmax(scores, dim&#061;-1) # [32, 8, 10, 10]<\/p>\n<p>        # \u52a0\u6743\u6c42\u548c: [32, 8, 10, 10] * [32, 8, 10, 64] -&gt; [32, 8, 10, 64]<br \/>\n        context &#061; torch.matmul(attn_weights, V)<\/p>\n<p>        # &#8212; \u7b2c\u4e09\u6b65&#xff1a;\u62fc\u63a5\u5934 (Concat) &#8212;<br \/>\n        # transpose: -&gt; [32, 10, 8, 64] (\u628a seq_len \u6362\u56de\u6765)<br \/>\n        # contiguous().view: -&gt; [32, 10, 512] (\u628a 8 \u548c 64 \u91cd\u65b0\u7c98\u5408\u5728\u4e00\u8d77)<br \/>\n        context &#061; context.transpose(1, 2).contiguous().view(batch_size, -1, self.d_model)<\/p>\n<p>        # &#8212; \u7b2c\u56db\u6b65&#xff1a;\u6700\u7ec8\u7ebf\u6027\u53d8\u6362 &#8212;<br \/>\n        return self.linear_out(context)<\/p>\n<p>\u8981\u70b9&#xff1a;\u81ea\u6ce8\u610f\u529b\u7684\u201c\u81ea\u201c\u7684\u5355\u4f4d\u662f\u53e5\u5b50&#xff0c;\u56e0\u6b64\u7f16\u7801\u5668 layer \u4e2d x_attn &#061; self.self_attn(x, x, x, mask)&#xff0c;\u5b9e\u9645\u4e0a\u8f93\u51fa\u4e86\u53e5\u5b50\u4e2d\u6bcf\u4e2a token \u5bf9\u5176\u5b83 token \u7684\u6ce8\u610f\u529b\u54cd\u5e94\u3002<\/p>\n<p>\u8f93\u5165\u5f20\u91cf&#xff0c;\u5f62\u72b6&#xff1a;[32, 8, 10, 64]<\/p>\n<table>\n<tr>\n<td>\u7ef4\u5ea6\u7d22\u5f15<\/td>\n<td>\u7ef4\u5ea6\u540d\u79f0<\/td>\n<td>\u793a\u4f8b\u503c<\/td>\n<td>\u89e3\u91ca\u4e0e\u4ea4\u4e92\u60c5\u51b5<\/td>\n<\/tr>\n<tbody>\n<tr>\n<td>Dim 0<\/td>\n<td>batch_size<\/td>\n<td>32<\/td>\n<td>\u72ec\u7acb\u7684\u6837\u672c\u7d22\u5f15\u3002 \u4ee3\u8868 32 \u4e2a\u6beb\u65e0\u5173\u7cfb\u7684\u53e5\u5b50\u540c\u65f6\u5904\u7406&#xff08;\u4f8b\u5982\u53e5\u5b50 A \u548c\u53e5\u5b50 B&#xff09;\u3002<\/td>\n<\/tr>\n<tr>\n<td>Dim 1<\/td>\n<td>num_heads<\/td>\n<td>8<\/td>\n<td>\u8bed\u4e49\u5b50\u7a7a\u95f4\u3002 \u5c06 512 \u7ef4\u62c6\u5206\u6210 8 \u7ec4&#xff0c;\u6bcf\u7ec4\u5173\u6ce8\u4e0d\u540c\u7279\u5f81&#xff08;\u5982 Head 1 \u770b\u8bed\u6cd5&#xff0c;Head 2 \u770b\u6307\u4ee3&#xff09;\u3002<\/td>\n<\/tr>\n<tr>\n<td>Dim 2<\/td>\n<td>seq_len<\/td>\n<td>10<\/td>\n<td>\u8bcd\u7684\u4f4d\u7f6e\u3002 \u4ee3\u8868\u53e5\u5b50\u91cc\u7684\u5177\u4f53\u54ea\u4e2a\u8bcd&#xff08;\u5982\u7b2c 3 \u4e2a\u8bcd\u201c\u7231\u201d&#xff09;\u3002<\/td>\n<\/tr>\n<tr>\n<td>Dim 3<\/td>\n<td>d_k<\/td>\n<td>64<\/td>\n<td>\u7279\u5f81\u5c5e\u6027\u5411\u91cf\u3002 \u63cf\u8ff0\u8be5\u8bcd\u5728\u5f53\u524d\u89c6\u89d2\u4e0b\u7684\u5177\u4f53\u5c5e\u6027&#xff08;\u5982\u201c\u6211\u662f\u7ea2\u8272\u7684\u201d\u6216\u201c\u6211\u662f\u540d\u8bcd\u201d&#xff09;\u3002Q \u548c K \u5728\u6b64\u7ef4\u5ea6\u8fdb\u884c\u70b9\u79ef&#xff0c;\u7b97\u51fa\u5339\u914d\u5206\u6570\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>d_k \u5b9e\u9645\u4e0a\u5c31\u662f\u5bf9\u8bcd\u5411\u91cf\u7684\u5206\u5272\u3002\u56e0\u4e3a\u5e0c\u671b\u8ba9\u6ce8\u610f\u529b\u7684\u6bcf\u4e2a\u5934\u80fd\u9488\u5bf9\u4e0d\u540c\u7684\u8bed\u4e49\u505a\u89c2\u5bdf&#xff0c;\u5c06\u8bcd\u5411\u91cf d_model \u5206\u4e3a\u201c\u6ce8\u610f\u529b\u5934\u201d\u6570\u4e2a\u5b50\u5411\u91cf d_k &#061; d_model \/\/ num_heads\u3002<\/p>\n<p>\u62c6\u89e3\u89c6\u89d2&#xff1a;\u5c06\u5f85\u8bad\u7ec3\u7684\u53e5\u5b50\u7528\u4e00\u4e2a 32 * 10 \u7ef4\u7684\u8868\u683c&#xff0c;\u5176\u4e2d&#xff0c;\u53e5\u5b50\u603b\u6570\u662f 32&#xff08;\u4e00\u4e2a batch&#xff09;&#xff0c;\u6bcf\u4e2a\u53e5\u5b50\u957f\u5ea6\u56fa\u5b9a\u4e3a 10&#xff08;seq_len&#xff09;\u3002\u8868\u683c\u4e2d\u7684\u6bcf\u4e2a\u5404\u81ea\u586b\u5199\u4e86\u4e00\u4e2a 8 * 64 \u7ef4\u7684\u8868\u683c\u7528\u4e8e\u8868\u793a\u4e00\u4e2a\u8bcd\u3002<\/p>\n<p>QKV \u7684\u5f62\u72b6\u5206\u6790&#xff1a;$W_{Q,K,V} &#061; [512 * 512]$, \u53d8\u6362\u540e\u7684 $Q,K,V &#061; [32, 8, 10, 64]$\u3002<\/p>\n<p>$W_{Q,K,V} &#061; [512 * 512]$ \u662f\u5bf9\u8f93\u5165\u7684\u7ebf\u6027\u53d8\u5316&#xff0c;\u4e0d\u8003\u8651\u610f\u4e49\u7684\u60c5\u51b5\u4e0b&#xff0c;\u5c31\u662f\u77e9\u9635\u4e58\u6cd5\u3002\u4e3a\u4e86\u5339\u914d\u6bcf\u4e2a\u8bcd\u7684\u7279\u5f81\u5411\u91cf\u957f\u5ea6\u4e14\u8f6c\u6362\u4e0d\u4e22\u5931\u8bed\u4e49&#xff0c;\u56e0\u6b64\u53d8\u5316\u77e9\u9635\u5927\u5c0f\u540c\u8bcd\u5411\u91cf\u957f\u5ea6\u5339\u914d\u3002<\/p>\n<p>$Q,K,V &#061; [32, 8, 10, 64]$ \u662f\u7ecf\u8fc7\u4e0a\u8ff0\u53d8\u5316\u540e\u7684\u7ed3\u679c\u300232 \u4e3a batch \u4e2d\u5305\u542b\u7684\u53e5\u5b50\u6570\u91cf&#xff0c;10 \u4e3a\u786e\u5b9a\u7684\u53e5\u5b50\u957f\u5ea6\u3002\u6709\u533a\u522b\u7684\u662f 8 * 64 \u7684\u5411\u91cf\u542b\u4e49\u3002QKV \u5206\u522b\u5bf9\u5e94\u7684\u662f Q: \u4e00\u4e2a\u8bcd\u5411\u5916\u67e5\u8be2\u7684\u5185\u5bb9, K: \u4e00\u4e2a\u8bcd\u5305\u542b\u7684\u8bed\u4e49&#xff0c;V: QK \u4f1a\u5bfc\u81f4\u7684\u4fe1\u606f\u6536\u76ca\u3002&#xff08;\u4ee5\u4e0a\u4e3a\u4e2a\u4eba\u7406\u89e3&#xff09;<\/p>\n<p>\u6ce8\u610f&#xff0c;\u4e58\u79ef\u7ed3\u679c\u7684\u539f\u59cb QKV \u662f $Q,K,V &#061; [32, 10, 512]$&#xff0c;\u63a5\u4e0b\u6765\u7b80\u5355\u5206\u5272\u7ef4\u5ea6\u5c31\u53ef\u4ee5\u5f97\u5230 $Q,K,V &#061; [32, 8, 10, 64]$\u3002<\/p>\n<p>QK \u78b0\u649e\u7684\u7ed3\u679c<\/p>\n<table>\n<tr>\n<td>\u64cd\u4f5c\u6b65\u9aa4<\/td>\n<td>\u8f93\u5165\u5f62\u72b6<\/td>\n<td>\u53d8\u6362\u540e\u5f62\u72b6<\/td>\n<td>\u7269\u7406\u610f\u4e49\u53d8\u5316<\/td>\n<\/tr>\n<tbody>\n<tr>\n<td>1. \u51c6\u5907<\/td>\n<td>\n<p>Q: [32, 8, 10, 64]<\/p>\n<p>\u00a0<\/p>\n<p>K: [32, 8, 10, 64]<\/p>\n<\/td>\n<td>\n<p>K \u8f6c\u7f6e\u4e3a:<\/p>\n<p>\u00a0<\/p>\n<p>[32, 8, 64, 10]<\/p>\n<\/td>\n<td>\u4e3a\u4e86\u8ba9 K \u7684\u7279\u5f81\u7ef4\u5ea6 (64) \u5bf9\u9f50 Q \u7684\u7279\u5f81\u7ef4\u5ea6&#xff0c;\u51c6\u5907\u8fdb\u884c\u70b9\u79ef\u3002<\/td>\n<\/tr>\n<tr>\n<td>2. \u78b0\u649e (Matmul)<\/td>\n<td>Q $\\\\times$ K\u1d40<\/td>\n<td>[32, 8, 10, 10]<\/td>\n<td>\u7ef4\u5ea6 64 \u88ab\u6d88\u8017 (\u6c42\u548c) \u6389\u4e86\u3002\u5269\u4e0b\u7684 [10, 10] \u662f Attention Map (\u6ce8\u610f\u529b\u56fe)\u3002<\/td>\n<\/tr>\n<tr>\n<td>3. \u7ed3\u679c\u89e3\u8bfb<\/td>\n<td>Score[b, h, i, j]<\/td>\n<td>(\u6807\u91cf\u6570\u503c)<\/td>\n<td>\u5173\u6ce8\u5ea6 \/ \u6743\u91cd\u3002 \u4ee3\u8868\u7b2c b \u4e2a\u53e5\u5b50\u7684\u7b2c h \u4e2a\u89c6\u89d2\u4e0b&#xff0c;\u7b2c i \u4e2a\u8bcd\u5bf9\u7b2c j \u4e2a\u8bcd\u7684\u5173\u6ce8\u7a0b\u5ea6\u3002\u4f8b\u5982&#xff1a;Score(&#034;it&#034;, &#034;animal&#034;) &#061; 0.9 (\u9ad8\u5ea6\u5173\u6ce8)\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5c06 QK \u8fdb\u884c\u78b0\u649e&#xff08;\u77e9\u9635\u4e58\u6cd5&#xff09;&#xff0c;\u5f97\u5230\u4e86\u4e00\u4e2a [32, 8, 10, 10] \u7684\u77e9\u9635&#xff0c;\u8fd9\u4e2a\u77e9\u9635\u524d\u4e24\u4e2a\u6807\u8bc6\u662f\u54ea\u4e00\u4e2a\u53e5\u5b50\u7684\u90a3\u4e00\u4e2a\u5173\u6ce8\u70b9&#xff0c;\u540e\u4e24\u4e2a 10 \u8868\u793a\u5355\u4e2a\u53e5\u5b50\u7684\u4efb\u610f\u4e24\u4e2a\u8bcd\u4e4b\u95f4\u7684\u5173\u7cfb\u5f3a\u5f31\u3002<\/p>\n<p>\u4e3a\u4e86\u8ba9\u8fd9\u4e2a\u5173\u7cfb\u8868\u73b0\u4e3a\u6982\u7387&#xff0c;\u6ce8\u610f\u5bf9\u5176\u8fdb\u884c Softmax\u3002<\/p>\n<p>\u57fa\u4e8e\u8fd9\u4e2a\u5173\u7cfb\u5f3a\u5f31\u7684\u5173\u7cfb&#xff1a;score &#061; [32, 8, 10, 10], \u6211\u4eec\u9700\u8981\u57fa\u4e8e Value \u83b7\u5f97\u771f\u6b63\u7684\u8bed\u4e49\u4fe1\u606f&#xff0c;\u5373\u6709&#xff1a;<\/p>\n<p># [10, 10] * [10, 64] -&gt; [10, 64]<br \/>\ncontext &#061; torch.matmul(attn_weights, V)<\/p>\n<p>\u524d\u4e24\u4e2a\u7ef4\u5ea6\u7684 batch \u4e2d\u53e5\u5b50\u603b\u6570\u548c\u6ce8\u610f\u529b\u5934\u6570\u4e0d\u53d8&#xff0c;\u540e\u9762\u53d8\u6210\u4e86\u53e5\u5b50\u4e2d\u6bcf\u4e2a\u8bcd\u6240\u643a\u5e26\u7684\u5176\u4ed6\u8bcd\u8d4b\u4e88\u7684\u8bed\u4e49\u91cf&#xff08;\u4f8b\u5982\u4ee3\u8bcd &#034;it&#034; \u539f\u672c\u7684\u8f93\u5165\u7279\u5f81\u53ea\u6709\u5176\u672c\u8eab\u7684\u201c\u5b83\u201d&#xff0c;\u73b0\u5728\u548c\u53e5\u5b50\u4e2d\u5176\u5b83\u8bcd\u4f8b\u5982\u201cdog\u201d\u7684 value \u4e58\u4e86\u4e0b&#xff0c;\u73b0\u5728 &#034;it&#034; \u4e5f\u5305\u542b &#034;dog&#034; \u7684\u8bed\u4e49\u4fe1\u606f\u4e86&#xff09;\u3002<\/p>\n<p>\u6700\u540e\u63d0\u4f9b\u7ed9\u4e0b\u4e00\u5c42 FFN \u7684\u8bed\u4e49\u4fe1\u606f\u4e3a&#xff1a;<\/p>\n<p># [32, 8, 10, 64] -&gt; \u8f6c\u7f6e -&gt; [32, 10, 8, 64]<br \/>\n# view -&gt; [32, 10, 512] (8 * 64 &#061; 512)<br \/>\ncontext &#061; context.transpose(1, 2).contiguous().view(batch_size, -1, self.d_model)<\/p>\n<p># &#8212; \u7b2c\u56db\u6b65&#xff1a;\u6700\u7ec8\u7ebf\u6027\u53d8\u6362 &#8212;<br \/>\nreturn self.linear_out(context)<\/p>\n<p>\u9996\u5148\u8fdb\u884c\u76f4\u63a5\u7684\u591a\u5934\u62fc\u63a5&#xff0c;\u4f46\u662f\u8fd9\u79cd\u62fc\u63a5\u592a\u786c\u4e86&#xff0c;\u56e0\u6b64\u4f7f\u7528\u4e00\u4e2a\u7ebf\u6027\u5c42&#xff08;\u77e9\u9635\u4e58\u6cd5&#xff09;\u505a\u5f62\u72b6\u4e0d\u53d8\u7684\u4eff\u5c04\u53d8\u6362&#xff08;\u4e58\u4ee5\u4e00\u4e2a [512, 512] \u7684\u77e9\u9635&#xff0c;\u8bed\u4e49\u77e9\u9635\u7684\u5f62\u72b6\u4e0d\u53d8&#xff0c;\u4f46\u662f\u5177\u4f53\u8bed\u4e49\u4fe1\u606f\u6709\u4e00\u5b9a\u7684\u878d\u5408&#xff09;\u3002<\/p>\n<p>\u6c47\u603b\u4e0b\u6570\u636e\u5f62\u72b6\u7684\u53d8\u5316&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8f93\u5165\u4e00\u4e2a batch \u7684\u53e5\u5b50&#xff1a;input[32, 10, 512] (32 \u53e5\u8bdd&#xff0c; \u6bcf\u4e2a\u53e5\u5b50\u957f\u5ea6 10 token&#xff0c;\u6bcf\u4e2a token \u7684\u8bed\u4e49\u7528 512 \u7ef4\u7684\u8bed\u4e49\u5411\u91cf\u8868\u793a)\u3002<\/p>\n<\/li>\n<li>\n<p>\u8f93\u5165\u5230\u591a\u5934&#xff08;\u5047\u8bbe 8 \u4e2a\u5934&#xff09;\u6ce8\u610f\u529b\u5c42\u3002<\/p>\n<\/li>\n<li>\n<p>\u901a\u8fc7\u4e09\u4e2a\u4eff\u5c04\u53d8\u6362\u83b7\u5f97\u4e0d\u540c\u7528\u9014\u7684\u8bcd\u8bed\u8bed\u4e49 Q, K, V \u4e09\u4e2a\u8bed\u4e49\u77e9\u9635\u90fd\u662f\u7531 [512 * 512] \u5927\u5c0f\u7684\u4e09\u4e2a\u53d8\u6362\u9635\u4e58\u51fa\u6765\u7684\u3002\u7136\u540e\u5bf9\u4e8e\u8f93\u51fa\u7684 QKV \u62c6\u5206\u5230\u5c06\u8bed\u4e49\u5207\u5206\u4e3a input_multi[32, 8, 10, 64] (32 \u53e5\u8bdd&#xff0c;\u9700\u8981\u5173\u6ce8 8 \u4e2a\u7ef4\u5ea6\u7684\u8bed\u4e49&#xff0c; \u6bcf\u4e2a\u53e5\u5b50\u957f\u5ea6 10 token&#xff0c;\u6bcf\u4e2a token \u7684\u5728\u67d0\u4e2a\u7ef4\u5ea6\u7684\u8bed\u4e49\u7528 64 \u7ef4\u7684\u8bed\u4e49\u5411\u91cf\u8868\u793a)\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e4b\u540e\u901a\u8fc7 Q, K \u78b0\u649e\u5f97\u5230\u8bcd\u4e0e\u8bcd\u4e4b\u95f4\u7684\u5173\u7cfb\u5206\u6570 Score[32, 8, 10, 10]&#xff0c;\u8fd9\u4e2a Score \u4f1a\u518d\u548c V \u505a\u4e58\u79ef\u4ee5\u901a\u8fc7\u5176\u5b83\u8bcd\u8bed\u7684\u8bed\u4e49\u6765\u5f97\u5230\u67d0\u4e2a\u8bcd\u7684\u8868\u8fbe context[32, 8, 10, 64]\u3002<\/p>\n<\/li>\n<li>\n<p>\u6700\u540e\u5c06 context \u5404\u7ef4\u5ea6\u62fc\u63a5&#xff0c;\u8f6c\u4e3a [32, 10, 512]&#xff0c;\u63a5\u7740\u4e3a\u4e86\u9632\u6b62\u62fc\u63a5\u8fc7\u4e8e\u751f\u786c&#xff0c;\u901a\u8fc7\u4e00\u5c42\u53cd\u5c04\u53d8\u6362&#xff08;\u4e0e\u4e00\u4e2a [512, 512] \u7684\u77e9\u9635\u505a\u4e58\u79ef&#xff09;\u5f97\u5230\u6a59\u8272\u90e8\u5206\u7684\u8f93\u51fa Atten_out[32, 10, 512]\u3002<\/p>\n<\/li>\n<li>\n<p>\u5c06\u8f93\u5165 input[32, 10, 512] \u548c\u8f93\u51fa Atten_out \u76f8\u52a0\u7136\u540e\u8fdb\u884c\u6b63\u6001\u5206\u5e03\u5f52\u4e00\u5316&#xff0c;\u6700\u540e\u5728\u8fdb\u884c\u4e00\u6b21\u7f29\u653e\u5e73\u79fb\u4ee5\u5f25\u8865\u5f52\u4e00\u5316\u5bfc\u81f4\u7684\u8bed\u4e49\u635f\u5931\u3002&#xff08;\u7f29\u653e\u5e73\u79fb\u662f\u5728\u505a\u9010\u5143\u7d20\u4e58\u6cd5&#xff0c;\u4e0d\u662f\u5f88\u6e05\u695a\u5f25\u8865\u7684\u80fd\u529b&#xff0c;\u5e94\u8be5\u662f\u6d88\u878d\u5b9e\u9a8c\u7ed3\u679c&#xff09;\u8f93\u51fa\u5230 FFN \u7684\u8f93\u5165\u5f62\u72b6\u4fdd\u6301\u4e3a [32, 10, 512]\u3002\u8fd9\u90e8\u5206\u8017\u65f6\u57fa\u672c\u53ef\u5ffd\u7565\u3002<\/p>\n<\/li>\n<li>\n<p>FFN \u5c31\u662f\u4e24\u4e2a\u57fa\u672c\u7ebf\u6027\u5c42&#xff0c;\u539f\u59cb\u64cd\u4f5c\u662f\u5148\u5347\u4e3a 2048 \u7ef4&#xff0c;\u7136\u540e\u964d\u56de 512 \u7ef4&#xff1a;[32, 10, 512] \u2192 [32, 10, 2048] \u2192 [32, 10, 512]\u3002<\/p>\n<\/li>\n<li>\n<p>\u4e4b\u540e\u5bf9\u4e8e\u7ebf\u6027\u5c42\u8f93\u51fa\u5185\u5bb9&#xff0c;\u91cd\u65b0\u8f93\u56de\u7b2c\u4e00\u6b65&#xff0c;\u91cd\u590d N \u6b21\u540e\u8fdb\u5165\u89e3\u7801\u5668\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h3>\u6b65\u9aa43&#xff1a;\u89e3\u7801\u5668\u90e8\u5206<\/h3>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"542\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205163249-6984c631c8e1a.png\" width=\"180\" \/><\/p>\n<p>import torch<br \/>\nimport torch.nn as nn<br \/>\nfrom copy import deepcopy<\/p>\n<p>class Decoder(nn.Module):<br \/>\n    def __init__(self, layer, N):<br \/>\n        &#034;&#034;&#034;<br \/>\n        \u6574\u4f53\u89e3\u7801\u5668 (\u7531 N \u4e2a DecoderLayer \u5806\u53e0\u800c\u6210)<br \/>\n        &#034;&#034;&#034;<br \/>\n        super().__init__()<br \/>\n        # \u514b\u9686 N \u4e2a DecoderLayer<br \/>\n        self.layers &#061; nn.ModuleList([deepcopy(layer) for _ in range(N)])<br \/>\n        self.norm &#061; nn.LayerNorm(layer.norm1.normalized_shape[0])<\/p>\n<p>    def forward(self, x, enc_output, src_mask, trg_mask):<br \/>\n        &#034;&#034;&#034;<br \/>\n        :param x: \u7ecf\u8fc7 Embedding &#043; Positional Encoding \u540e\u7684\u76ee\u6807\u5e8f\u5217\u8f93\u5165<br \/>\n        :param enc_output: Encoder \u7684\u8f93\u51fa\u7ed3\u679c (Memory)<br \/>\n        &#034;&#034;&#034;<br \/>\n        # \u4e32\u884c\u6d41\u7ecf\u6bcf\u4e00\u5c42<br \/>\n        for layer in self.layers:<br \/>\n            x &#061; layer(x, enc_output, src_mask, trg_mask)<\/p>\n<p>        return self.norm(x)<\/p>\n<h4>\u7b2c\u4e00\u90e8\u5206&#xff1a;\u5e95\u90e8 outputs \u8f93\u5165\u6a21\u578b<\/h4>\n<p>\u8fd9\u4e2a\u5730\u65b9\u548c RNN \u4e00\u6837&#xff0c;\u9996\u5148\u6211\u4eec\u83b7\u5f97\u4e00\u4e2a\u7a7a\u5e8f\u5217 [&lt;SOS&gt;] \u8868\u793a\u53e5\u5b50\u5f00\u59cb&#xff08;Start of the Sentence&#xff09;\u3002<\/p>\n<p>\u7136\u540e\u6bcf\u4e00\u6b21\u89e3\u7801\u8fc7\u7a0b\u540e&#xff0c;\u9876\u90e8\u7684 Output \u8f93\u51fa\u9700\u8981\u52a0\u5165\u8fd9\u4e2a\u5e8f\u5217\u3002<\/p>\n<p>\u4e00\u76f4\u8fd9\u6837\u91cd\u590d&#xff0c;\u76f4\u5230\u9876\u90e8 Output \u8f93\u51fa\u4e86 &lt;EOS&gt; (End of Sentence)\u3002<\/p>\n<p>\u5177\u4f53\u7684\u5904\u7406\u65b9\u5f0f\u8fd8\u662f\u548c\u4e4b\u524d\u7684\u7f16\u7801\u5668\u90e8\u5206\u4e00\u6837&#xff0c;\u52a0\u4e0a\u4f4d\u7f6e\u7f16\u7801&#xff0c;\u7136\u540e\u8f93\u5165\u5230\u591a\u5934\u4e2d\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u8fd9\u4e2a outputs \u4e0d\u662f\u52a8\u6001\u751f\u6210\u7684&#xff0c;\u800c\u662f\u4e00\u4e2a\u5b57\u4e00\u4e2a\u5b57\u6f0f\u7ed9\u6a21\u578b\u770b&#xff0c;\u5e76\u884c\u8bad\u7ec3&#xff08;\u5e94\u8be5\u662f\u5e76\u884c&#xff0c;\u6bcf\u4e2a\u6279\u6b21\u66f4\u65b0\u4e00\u6b21\u53c2\u6570&#xff09;\u3002<\/p>\n<h4>\u7b2c\u4e8c\u90e8\u5206&#xff1a;\u63a9\u7801\u6ce8\u610f\u529b<\/h4>\n<p>\u5728\u7f16\u7801\u5668\u4e2d\u7684\u53e5\u5b50\u5185\u5bb9\u662f\u6211\u4eec\u7684\u8f93\u5165\u7684 prompt&#xff0c;\u8fd9\u4e2a\u89e3\u7801\u5668\u90e8\u5206\u7684\u53e5\u5b50\u5185\u5bb9\u662f\u8f93\u51fa\u53e5\u5b50&#xff08;LLM \u7684\u56de\u590d&#xff09;\u3002\u6a21\u578b\u7684\u5176\u5b83\u8d85\u53c2\u6570\u90fd\u662f\u4e00\u81f4\u7684&#xff0c;\u53ea\u6709 Seq_len \u4e0d\u540c&#xff08;\u5c31\u662f\u4e4b\u524d\u662f 10 \u7684\u90a3\u4e2a\u53c2\u6570&#xff09;\u3002<\/p>\n<p>\u5047\u8bbe\u5b83\u73b0\u5728\u662f 4\u3002<\/p>\n<p>\u91cd\u65b0\u5217\u4e00\u904d\u53c2\u6570&#xff1a;<\/p>\n<ul>\n<li>\n<p>Batch Size (B): 32 (\u4e00\u6b21\u5e76\u884c\u5904\u7406 32 \u53e5\u8bdd)<\/p>\n<\/li>\n<li>\n<p>Seq Len (L): 4 (\u5047\u8bbe\u8fd9\u53e5\u8bdd\u662f [&lt;SOS&gt;, A, B, C])<\/p>\n<\/li>\n<li>\n<p>d_model (D): 512<\/p>\n<\/li>\n<li>\n<p>Heads (H): 8<\/p>\n<\/li>\n<li>\n<p>d_k: 64<\/p>\n<\/li>\n<\/ul>\n<p>\u524d\u9762 QKV \u751f\u6210\u8fc7\u7a0b\u8fd8\u662f\u4e00\u6837\u7684&#xff0c;\u5177\u4f53\u7684\u5f62\u72b6\u53d8\u6362\u8fc7\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<p>Input: [32, 4, 512] \u2192 W_{Q,K,V}: [512, 512] \u21d2 Q,K,V: [32, 4, 512] \u2192 \u591a\u5934\u5212\u5206&#xff1a;[32, 8, 4, 64]<\/p>\n<p>\u7136\u540e\u8ba1\u7b97 Score &#061; Q \u00d7 K^t : [32, 8, 4, 4]\u3002<\/p>\n<p>\u8fd9\u4e00\u6b65\u91cc\u9762\u9700\u8981\u4f7f\u7528\u63a9\u7801\u3002<\/p>\n<p>\u8fd9\u4e2a Score \u8868\u793a\u7684\u662f\u5f53\u524d\u884c\u7d22\u5f15\u7684\u5bf9\u5e94\u8bcd\u6c47\u5bf9\u5217\u7d22\u5f15\u7684\u8bcd\u6c47\u7684\u76f8\u5173\u5ea6\u3002<\/p>\n<p>\u4f46\u662f\u73b0\u5728\u7684\u8bcd\u6c47\u4e0d\u80fd\u770b\u5230\u540e\u9762\u8fd8\u672a\u751f\u6210\u7684\u8bcd\u6c47\u3002\u63a9\u7801\u903b\u8f91\u53c2\u8003\u4ee5\u4e0b\u5185\u5bb9&#xff1a;<\/p>\n<p>Mask \u77e9\u9635<\/p>\n<p>      SOS    A      B      C<br \/>\nSOS [[ 0,    1,     1,     1 ],  &lt;- \u906e\u6321 A, B, C<br \/>\n A   [ 0,    0,     1,     1 ],  &lt;- \u906e\u6321 B, C<br \/>\n B   [ 0,    0,     0,     1 ],  &lt;- \u906e\u6321 C<br \/>\n C   [ 0,    0,     0,     0 ]]  &lt;- \u5168\u90fd\u80fd\u770b<\/p>\n<p>Score \u77e9\u9635\u4e2d\u63a9\u7801\u4e3a 1 \u7684\u5730\u65b9\u8bbe\u7f6e\u4e3a -1e9 (\u6781\u5c0f\u503c)\u3002<\/p>\n<p>\u56e0\u6b64\u53d8\u6362\u540e\u7684 Score \u77e9\u9635\u5927\u6982\u5982\u4e0b&#xff1a;<\/p>\n<p>       SOS    A     B     C<br \/>\nSOS [[ 2.5, -1e9, -1e9, -1e9 ],  &lt;- \u540e\u9762\u5168\u5e9f\u4e86<br \/>\n A   [ 1.8,  3.2, -1e9, -1e9 ],<br \/>\n B   [ 0.5,  1.5,  2.8, -1e9 ],<br \/>\n C   [ 0.2,  0.4,  1.2,  3.0 ]]<\/p>\n<p>\u5bf9\u6bcf\u4e00\u884c Softmax \u540e, \u6781\u5c0f\u503c\u5c31\u8f6c\u4e3a\u6982\u7387 0 \u4e86\u3002<\/p>\n<p>\u7136\u540e\u548c\u4e4b\u524d\u4e00\u6837&#xff0c;Score \u00d7 V &#061; context[32, 4, 512]\u3002<\/p>\n<h4>\u7b2c\u4e09\u90e8\u5206&#xff1a;\u7f16\u7801\u5668\u4ea4\u4e92\u7684\u591a\u5934\u6ce8\u610f\u529b (\u4ea4\u53c9\u6ce8\u610f\u529b&#xff0c;\u533a\u522b\u4e8e\u4e4b\u524d\u7684\u81ea\u6ce8\u610f\u529b)<\/h4>\n<p>\u53c2\u8003\u539f\u59cb\u7684\u6ce8\u610f\u529b\u4e09\u4e2a\u77e9\u9635&#xff1a;<\/p>\n<ul>\n<li>\n<p>Q&#xff1a;\u76ee\u524d\u7684\u95ee\u9898<\/p>\n<\/li>\n<li>\n<p>K&#xff1a;\u76ee\u524d\u53ef\u4ee5\u53c2\u8003\u7684\u4fe1\u606f<\/p>\n<\/li>\n<li>\n<p>V&#xff1a;\u53e5\u5b50\u4e2d\u5404\u4e2a\u8bcd\u5bf9\u5e94\u7684\u542b\u4e49<\/p>\n<\/li>\n<\/ul>\n<p># x:          \u89e3\u7801\u5668\u76ee\u524d\u7684\u8fdb\u5ea6 (\u6bd4\u5982\u8bf4\u5df2\u7ecf\u751f\u6210\u4e86\u524d3\u4e2a\u8bcd)<br \/>\n# enc_output: \u7f16\u7801\u5668\u8f93\u51fa\u7684\u8bb0\u5fc6 (\u4e5f\u5c31\u662f\u539f\u6587\u7684\u5b8c\u6574\u7406\u89e3)<\/p>\n<p>x &#061; self.cross_attn(q&#061;x, k&#061;enc_output, v&#061;enc_output, mask&#061;src_mask)<\/p>\n<p>\u5047\u8bbe\u6700\u7ec8\u7684 Ground Truth \u662f\u201c\u6211\u7231\u4f60&#xff01;\u201d&#xff0c;\u7136\u540e LLM \u5df2\u7ecf\u8f93\u51fa\u4e86\u201c\u6211\u201d\u3002<\/p>\n<p>\u73b0\u5728\u9700\u8981\u89e3\u7b54\u7684\u95ee\u9898\u662f&#xff1a;\u4e0b\u4e00\u4e2a\u5b57\u7b26\u5e94\u8be5\u8f93\u51fa\u4ec0\u4e48&#xff0c;\u56e0\u6b64\u9700\u8981\u8f93\u5165\u5df2\u6709\u7684\u8f93\u51fa\u4f5c\u4e3a\u67e5\u8be2\u4e5f\u5c31\u662f\u76ee\u524d\u8f93\u51fa\u7684 \u201c&lt;SOS&gt; \u6211\u201d\u3002<\/p>\n<p>\u90a3\u4e48\u4f5c\u4e3a\u53c2\u8003\u7684\u5e94\u8be5\u662f\u4f60\u8f93\u5165\u7684 prompt&#xff08;\u8981\u8f93\u51fa\u201c\u6211\u7231\u4f60\u201d\u5927\u7ea6\u662f\u8f93\u5165\u4e86\u201c\u4f60\u7231\u6211\u5417&#xff1f;\u201d\u8fd9\u79cd input&#xff09;\u3002<\/p>\n<p>\u7136\u540e\u6211\u9700\u8981\u7684\u8bed\u4e49\u4e5f\u662f\u4ece\u539f\u59cb\u8f93\u5165\u4e2d\u62ff\u7684\u3002<\/p>\n<p>\u6240\u4ee5 Q&#061;x&#xff08;\u5f53\u524d\u8f93\u51fa\u4e00\u534a\u7684\u5185\u5bb9&#xff09;&#xff0c;KV \u90fd\u662f encoder \u5728\u7ecf\u8fc7 N \u5c42\u540e\u7684\u539f\u59cb\u8f93\u5165\u3002<\/p>\n<p>\u6765\u770b\u770b\u5f20\u91cf\u5f62\u72b6&#xff1a;<\/p>\n<p>\u4e4b\u524d\u8bf4\u5230 Encoder \u7684\u8f93\u51fa\u6309\u7167\u8bbe\u5b9a\u662f [32, 10, 512]\u3002<\/p>\n<p>\u8fd9\u91cc\u7684 x \u867d\u7136\u6ca1\u751f\u6210\u5b8c&#xff0c;\u4f46\u662f\u4e5f\u4f1a\u88ab\u586b\u5145\u5230\u9884\u671f\u7684\u5927\u5c0f&#xff0c;\u662f [32, 4, 512]\u3002<\/p>\n<p>\u7136\u540e\u6211\u4eec QKV \u8fd8\u662f\u548c\u4e4b\u524d\u4e00\u6837\u4f7f\u7528 [512, 512] \u5927\u5c0f\u7684\u8f6c\u6362\u77e9\u9635\u3002\u90a3\u4e48\u5f97\u5230&#xff1a;<\/p>\n<ul>\n<li>\n<p>Q&#xff1a;[32, 4, 512]<\/p>\n<\/li>\n<li>\n<p>K&#xff1a;[32, 10, 512]<\/p>\n<\/li>\n<li>\n<p>V&#xff1a;[32, 10, 512]<\/p>\n<\/li>\n<\/ul>\n<p>\u7136\u540e\u6211\u4eec $QK^T$ \u5f97\u5230 score [32, 4, 10]\u3002<\/p>\n<p>\u5bf9\u4e8e prompt \u4e2d\u7684\u586b\u5145\u90e8\u5206&#xff08;\u4e00\u53e5\u8bdd\u957f\u5ea6\u8fd8\u4e0d\u5230 10&#xff0c;\u5269\u4e0b\u7684\u7528 &lt;PAD&gt; \u586b\u5145&#xff09;\u7528\u6781\u5c0f\u503c\u8fdb\u884c\u586b\u5145\u3002<\/p>\n<p>\u5bf9\u4e8e decoder \u7684\u8f93\u51fa&#xff0c;\u7531\u4e8e\u4e0a\u4e00\u6b65\u7684\u63a9\u7801\u64cd\u4f5c&#xff0c;\u8fd9\u4e00\u6b65 decoder \u5185\u5bb9\u4e0d\u9700\u8981\u91cd\u65b0\u63a9\u7801&#xff0c;\u76f4\u63a5\u6cbf\u7528\u4e0a\u4e00\u8f6e\u7684\u4e0a\u4e0b\u6587\u7ed3\u679c\u5373\u53ef\u3002<\/p>\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u5f97\u5230\u4e86 Q \u4e2d\u6bcf\u4e2a\u5b57\u7b26\u5bf9\u5e94 K \u4e2d\u6bcf\u4e2a\u5b57\u7b26\u7684\u76f8\u5173\u6027\u4e86\u3002<\/p>\n<p>Transformer \u5f3a\u5927\u7684\u5730\u65b9\u5728\u4e8e&#xff0c;\u53ea\u8981\u8bcd\u7684\u5411\u91cf\u8868\u793a\u7ef4\u5ea6\u76f8\u540c&#xff0c;\u90a3\u4e48\u4ea4\u53c9\u6ce8\u610f\u529b\u53ef\u4ee5\u6355\u83b7\u4e24\u4e2a\u4efb\u610f\u957f\u5e8f\u5217\u7684\u6bcf\u4e2a\u8bcd\u4e4b\u95f4\u7684\u76f8\u5173\u6027\u3002<\/p>\n<p>\u6700\u7ec8\u6211\u4eec\u4e58\u4e0a V \u77e9\u9635&#xff0c;\u5f97\u5230\u4e86 context[32, 4, 512], \u4e5f\u5c31\u662f\u8fd9\u53e5\u8bdd\u4e2d\u7684\u6bcf\u4e2a\u8bcd\u6240\u5305\u542b\u7684\u5185\u5bb9\u542b\u4e49\u3002<\/p>\n<h4>\u7b2c\u56db\u90e8\u5206&#xff1a;FFN<\/h4>\n<p>\u548c\u7f16\u7801\u5668\u90e8\u5206 FFN \u7684\u903b\u8f91\u5b8c\u5168\u4e00\u81f4&#xff0c;\u9996\u5148\u8fdb\u884c\u76f4\u63a5\u7684\u591a\u5934\u62fc\u63a5&#xff0c;\u4f46\u662f\u8fd9\u79cd\u62fc\u63a5\u592a\u786c\u4e86&#xff0c;\u56e0\u6b64\u4f7f\u7528\u4e00\u4e2a\u7ebf\u6027\u5c42&#xff08;\u77e9\u9635\u4e58\u6cd5&#xff09;\u505a\u5f62\u72b6\u4e0d\u53d8\u7684\u4eff\u5c04\u53d8\u6362&#xff08;\u4e58\u4ee5\u4e00\u4e2a [512, 512] \u7684\u77e9\u9635&#xff0c;\u8bed\u4e49\u77e9\u9635\u7684\u5f62\u72b6\u4e0d\u53d8&#xff0c;\u4f46\u662f\u5177\u4f53\u8bed\u4e49\u4fe1\u606f\u6709\u4e00\u5b9a\u7684\u878d\u5408&#xff09;\u3002<\/p>\n<p>\u6700\u540e\u8f93\u51fa\u4e86\u4e00\u4e2a\u5e73\u6ed1\u539a\u7684 context[32, 4, 512]\u3002<\/p>\n<hr \/>\n<h3>\u6b65\u9aa44&#xff1a;\u6700\u7ec8\u7ebf\u6027\u5c42\u4e0e\u5206\u7c7b\u8f93\u51fa<\/h3>\n<p>\u6700\u540e\u4e00\u5c42\u7684\u4ea4\u53c9\u6ce8\u610f\u529b\u6211\u4eec\u83b7\u5f97\u4e86 context[32, 4, 512]&#xff0c;\u5176\u5177\u4f53\u542b\u4e49\u662f&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u8fd9\u4e2a batch \u4e2d\u7684\u53e5\u5b50\u4e2a\u6570&#xff1a;32<\/p>\n<\/li>\n<li>\n<p>\u6bcf\u4e2a\u53e5\u5b50\u5728\u586b\u5145\u540e\u7684 token \u5b9a\u957f&#xff1a;4<\/p>\n<\/li>\n<li>\n<p>\u6bcf\u4e2a token \u7684\u8bed\u4e49\u5411\u91cf&#xff1a;512&#xff08;\u878d\u5408\u4e86\u8fd9\u4e2a token \u5bf9\u5176\u5b83 token \u7684\u8bed\u4e49\u6444\u5165&#xff0c;\u8fd9\u91cc\u7684 &#034;it&#034; \u6216 &#034;\u4f60\u6211\u4ed6&#034; \u4e0d\u662f\u7b80\u5355\u4ee3\u8bcd\u4e86&#xff0c;\u5df2\u7ecf\u878d\u5408\u4e86\u5b83\u5177\u4f53\u6307\u5e26\u7684\u5bf9\u8c61\u7684\u8bed\u4e49&#xff09;\u3002<\/p>\n<\/li>\n<\/ul>\n<p>\u6b64\u65f6\u6211\u4eec\u505a\u4e00\u6b21\u7ebf\u6027\u6620\u5c04&#xff0c;\u628a\u8fd9\u4e2a\u53e5\u5b50 4 \u4e2a token \u7684\u8bed\u4e49\u5411\u91cf\u6620\u5c04\u5230\u8bcd\u8868\u5927\u5c0f&#xff1a;<\/p>\n<ul>\n<li>\n<p>pred_result: [32, 4, 30000] \u8868\u793a\u6bcf\u4e2a\u4f4d\u7f6e\u7684\u8bcd\u4e3a\u8bcd\u8868\u4e2d\u67d0\u4e2a\u8bcd\u7684\u6982\u7387&#xff0c;\u6982\u7387\u6700\u9ad8\u7684\u90a3\u4e2a\u8bcd\u5373\u4e3a\u7ed3\u679c\u3002<\/p>\n<\/li>\n<li>\n<p>ground_truth: [32, 4, 1] \u2192 \u63a8\u5e7f\u5230 30000 \u7ef4&#xff0c;\u5373\u53ea\u6709\u6b63\u786e\u7684\u8bcd\u4e3a 1&#xff0c;\u5176\u4f59\u4e3a 0 \u2192 [32, 4, 30000]\u3002<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u6b65\u9aa41&#xff1a;\u4f4d\u7f6e\u5d4c\u5165\u4e0e\u8bcd\u6620\u5c04import torch<br \/>\nimport torch.nn as nn<br \/>\nimport mathclass TransformerInput(nn.Module):def __init__(self, vocab_size, d_model, max_len5000):super().__init__()# 1. Embedding \u5c42&#xff1a;\u5c06\u5355\u8bcd\u7d22\u5f15\u8f6c\u4e3a 512 \u7ef4\u5411\u91cfself.embedding  nn.Embedding(vo<\/p>\n","protected":false},"author":2,"featured_media":72566,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[841,50,86],"topic":[],"class_list":["post-72570","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-transformer","tag-50","tag-86"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u624b\u63a8Transformer\u5f20\u91cf\u4f20\u9012\u8fc7\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/72570.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u624b\u63a8Transformer\u5f20\u91cf\u4f20\u9012\u8fc7\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6b65\u9aa41&#xff1a;\u4f4d\u7f6e\u5d4c\u5165\u4e0e\u8bcd\u6620\u5c04import torch import torch.nn as nn import mathclass TransformerInput(nn.Module):def __init__(self, vocab_size, d_model, max_len5000):super().__init__()# 1. Embedding \u5c42&#xff1a;\u5c06\u5355\u8bcd\u7d22\u5f15\u8f6c\u4e3a 512 \u7ef4\u5411\u91cfself.embedding nn.Embedding(vo\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/72570.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-05T16:32:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205163249-6984c63177b9b.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/72570.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/72570.html\",\"name\":\"\u624b\u63a8Transformer\u5f20\u91cf\u4f20\u9012\u8fc7\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-05T16:32:50+00:00\",\"dateModified\":\"2026-02-05T16:32:50+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/72570.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/72570.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/72570.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u624b\u63a8Transformer\u5f20\u91cf\u4f20\u9012\u8fc7\u7a0b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u624b\u63a8Transformer\u5f20\u91cf\u4f20\u9012\u8fc7\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/72570.html","og_locale":"zh_CN","og_type":"article","og_title":"\u624b\u63a8Transformer\u5f20\u91cf\u4f20\u9012\u8fc7\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6b65\u9aa41&#xff1a;\u4f4d\u7f6e\u5d4c\u5165\u4e0e\u8bcd\u6620\u5c04import torch import torch.nn as nn import mathclass TransformerInput(nn.Module):def __init__(self, vocab_size, d_model, max_len5000):super().__init__()# 1. Embedding \u5c42&#xff1a;\u5c06\u5355\u8bcd\u7d22\u5f15\u8f6c\u4e3a 512 \u7ef4\u5411\u91cfself.embedding nn.Embedding(vo","og_url":"https:\/\/www.wsisp.com\/helps\/72570.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-05T16:32:50+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260205163249-6984c63177b9b.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"7 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/72570.html","url":"https:\/\/www.wsisp.com\/helps\/72570.html","name":"\u624b\u63a8Transformer\u5f20\u91cf\u4f20\u9012\u8fc7\u7a0b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-05T16:32:50+00:00","dateModified":"2026-02-05T16:32:50+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/72570.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/72570.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/72570.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u624b\u63a8Transformer\u5f20\u91cf\u4f20\u9012\u8fc7\u7a0b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/72570","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=72570"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/72570\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/72566"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=72570"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=72570"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=72570"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=72570"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}