{"id":36928,"date":"2025-05-13T01:03:34","date_gmt":"2025-05-12T17:03:34","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/36928.html"},"modified":"2025-05-13T01:03:34","modified_gmt":"2025-05-12T17:03:34","slug":"pytorch-%e5%ae%9e%e6%88%98%ef%bc%9a%e4%bb%8e-0-%e5%bc%80%e5%a7%8b%e6%90%ad%e5%bb%ba-transformer","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/36928.html","title":{"rendered":"PyTorch \u5b9e\u6218\uff1a\u4ece 0 \u5f00\u59cb\u642d\u5efa Transformer"},"content":{"rendered":"<\/p>\n<li>\u5bfc\u5165\u5fc5\u8981\u7684\u5e93<\/li>\n<p>python<\/p>\n<p>import math<br \/>\nimport torch<br \/>\nimport torch.nn as nn<br \/>\nfrom LabmL_helpers.module import Module<br \/>\nfrom labml_n.utils import clone_module_List<br \/>\nfrom typing import Optional, List<br \/>\nfrom torch.utils.data import DataLoader, TensorDataset<br \/>\nfrom torch import optim<br \/>\nimport torch.nn.functional as F<\/p>\n<li>Transformer \u6a21\u578b\u6982\u8ff0 Transformer \u662f\u4e00\u79cd\u5e8f\u5217\u5230\u5e8f\u5217\u7684\u6a21\u578b&#xff0c;\u901a\u8fc7\u81ea\u6ce8\u610f\u529b\u673a\u5236\u5e76\u884c\u5904\u7406\u6574\u4e2a\u5e8f\u5217&#xff0c;\u80fd\u540c\u65f6\u8003\u8651\u5e8f\u5217\u4e2d\u7684\u6240\u6709\u5143\u7d20&#xff0c;\u5e76\u5b66\u4e60\u4e0a\u4e0b\u6587\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u5176\u67b6\u6784\u5305\u62ec\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u90e8\u5206&#xff0c;\u6bcf\u90e8\u5206\u90fd\u7531\u591a\u4e2a\u76f8\u540c\u7684\u5c42\u7ec4\u6210&#xff0c;\u8fd9\u4e9b\u5c42\u5305\u542b\u81ea\u6ce8\u610f\u529b\u673a\u5236\u3001\u524d\u9988\u795e\u7ecf\u7f51\u7edc&#xff0c;\u4ee5\u53ca\u5f52\u4e00\u5316\u548c Dropout \u6b65\u9aa4\u3002<\/li>\n<li>\u6838\u5fc3\u516c\u5f0f\n<ul>\n<li>\u81ea\u6ce8\u610f\u529b\u8ba1\u7b97&#xff1a;Attention(Q,K,V)&#061;softmax(dk\u200b\u200bQKT\u200b)V&#xff0c;\u5176\u4e2d&#xff0c;Q\u3001K\u3001V\u5206\u522b\u662f\u67e5\u8be2&#xff08;Query&#xff09;\u3001\u952e&#xff08;Key&#xff09;\u548c\u503c&#xff08;Value&#xff09;\u77e9\u9635&#xff0c;dk\u200b\u662f\u952e\u7684\u7ef4\u5ea6\u3002<\/li>\n<li>\u591a\u5934\u6ce8\u610f\u529b&#xff1a;\u5c06\u8f93\u5165\u5206\u5272\u4e3a\u591a\u4e2a\u5934&#xff0c;\u5206\u522b\u8ba1\u7b97\u6ce8\u610f\u529b&#xff0c;\u7136\u540e\u5c06\u7ed3\u679c\u62fc\u63a5\u8d77\u6765\u3002<\/li>\n<li>\u4f4d\u7f6e\u7f16\u7801&#xff1a;\u7531\u4e8e Transformer \u4e0d\u4f7f\u7528\u5faa\u73af\u7ed3\u6784&#xff0c;\u56e0\u6b64\u5f15\u5165\u4f4d\u7f6e\u7f16\u7801\u6765\u4fdd\u7559\u5e8f\u5217\u4e2d\u7684\u4f4d\u7f6e\u4fe1\u606f\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u81ea\u6ce8\u610f\u529b\u673a\u5236\n<ul>\n<li>\u6838\u5fc3\u539f\u7406&#xff1a;\u8ba1\u7b97\u53e5\u5b50\u5728\u7f16\u7801\u8fc7\u7a0b\u4e2d\u6bcf\u4e2a\u4f4d\u7f6e\u4e0a\u7684\u6ce8\u610f\u529b\u6743\u91cd&#xff0c;\u7136\u540e\u4ee5\u6743\u91cd\u548c\u7684\u65b9\u5f0f\u6765\u8ba1\u7b97\u6574\u4e2a\u53e5\u5b50\u7684\u9690\u542b\u5411\u91cf\u8868\u793a\u3002\u516c\u5f0f\u4e2d&#xff0c;\u9996\u5148\u5c06 query \u4e0e key \u7684\u8f6c\u7f6e\u505a\u70b9\u79ef&#xff0c;\u7136\u540e\u5c06\u7ed3\u679c\u9664\u4ee5dk\u200b\u200b\u00a0&#xff0c;\u518d\u8fdb\u884c softmax \u8ba1\u7b97&#xff0c;\u6700\u540e\u5c06\u7ed3\u679c\u4e0e value \u505a\u77e9\u9635\u4e58\u6cd5\u5f97\u5230 output\u3002\u9664\u4ee5dk\u200b\u200b\u662f\u4e3a\u4e86\u9632\u6b62QKT\u8fc7\u5927\u5bfc\u81f4 softmax \u8ba1\u7b97\u6ea2\u51fa&#xff0c;\u4e14\u53ef\u4f7fQKT\u7ed3\u679c\u6ee1\u8db3\u5747\u503c\u4e3a 0&#xff0c;\u65b9\u5dee 1 \u7684\u5206\u5e03\u3002QKT\u8ba1\u7b97\u672c\u8d28\u4e0a\u662f\u4f59\u5f26\u76f8\u4f3c\u5ea6&#xff0c;\u53ef\u8868\u793a\u4e24\u4e2a\u5411\u91cf\u5728\u65b9\u5411\u4e0a\u7684\u76f8\u4f3c\u5ea6\u3002<\/li>\n<li>\u5b9e\u73b0<\/li>\n<\/ul>\n<\/li>\n<p>python<\/p>\n<p>import numpy as np<br \/>\nfrom math import sqrt<br \/>\nimport torch<br \/>\nfrom torch import nn<\/p>\n<p>class Self_Attention(nn.Module):<br \/>\n    # input : batch_size * seq_len * input_dim<br \/>\n    # q : batch_size * input_dim * dim_k<br \/>\n    # k : batch_size * input_dim * dim_k<br \/>\n    # v : batch_size * input_dim * dim_v<br \/>\n    def __init__(self, input_dim, dim_k, dim_v):<br \/>\n        super(Self_Attention, self).__init__()<br \/>\n        self.q &#061; nn.Linear(input_dim, dim_k)<br \/>\n        self.k &#061; nn.Linear(input_dim, dim_k)<br \/>\n        self.v &#061; nn.Linear(input_dim, dim_v)<br \/>\n        self._norm_fact &#061; 1 \/ sqrt(dim_k)<\/p>\n<p>    def forward(self, x):<br \/>\n        Q &#061; self.q(x)  # Q: batch_size * seq_len * dim_k<br \/>\n        K &#061; self.k(x)  # K: batch_size * seq_len * dim_k<br \/>\n        V &#061; self.v(x)  # V: batch_size * seq_len * dim_v<br \/>\n        # Q * K.T() # batch_size * seq_len * seq_len<br \/>\n        atten &#061; nn.Softmax(<br \/>\n            dim&#061;-1)(torch.bmm(Q, K.permute(0, 2, 1))) * self._norm_fact<br \/>\n        # Q * K.T() * V # batch_size * seq_len * dim_v<br \/>\n        output &#061; torch.bmm(atten, V)<br \/>\n        return output<\/p>\n<p>X &#061; torch.randn(4, 3, 2)<br \/>\nprint(X)<br \/>\nself_atten &#061; Self_Attention(2, 4, 5)  # input_dim:2, k_dim:4, v_dim:5<br \/>\nres &#061; self_atten(X)<br \/>\nprint(res.shape)  # [4,3,5]<\/p>\n<li>\u591a\u5934\u6ce8\u610f\u529b\u673a\u5236 \u4e0d\u540c\u4e8e\u53ea\u4f7f\u7528\u4e00\u4e2a\u6ce8\u610f\u529b\u6c60\u5316&#xff0c;\u5c06\u8f93\u5165x\u62c6\u5206\u4e3ah\u4efd&#xff0c;\u72ec\u7acb\u8ba1\u7b97h\u7ec4\u4e0d\u540c\u7684\u7ebf\u6027\u6295\u5f71\u6765\u5f97\u5230\u5404\u81ea\u7684 QKV&#xff0c;\u7136\u540e\u5e76\u884c\u8ba1\u7b97\u6ce8\u610f\u529b&#xff0c;\u6700\u540e\u5c06h\u4e2a\u6ce8\u610f\u529b\u6c60\u5316\u62fc\u63a5\u8d77\u6765\u5e76\u901a\u8fc7\u53e6\u4e00\u4e2a\u53ef\u5b66\u4e60\u7684\u7ebf\u6027\u6295\u5f71\u8fdb\u884c\u53d8\u6362\u4ee5\u4ea7\u751f\u8f93\u51fa\u3002\u6bcf\u4e2a\u5934\u53ef\u80fd\u5173\u6ce8\u8f93\u5165\u7684\u4e0d\u540c\u90e8\u5206&#xff0c;\u53ef\u8868\u793a\u66f4\u590d\u6742\u7684\u51fd\u6570\u3002<\/li>\n<p>python<\/p>\n<p>from math import sqrt<br \/>\nimport torch<br \/>\nimport torch.nn as nn<\/p>\n<p>class Self_Attention_Muti_Head(nn.Module):<br \/>\n    # input : batch_size * seq_len * input_dim<br \/>\n    # q : batch_size * input_dim * dim_k<br \/>\n    # k : batch_size * input_dim * dim_k<br \/>\n    # v : batch_size * input_dim * dim_v<br \/>\n    def __init__(self, input_dim, dim_k, dim_v, nums_head):<br \/>\n        super(Self_Attention_Muti_Head, self).__init__()<br \/>\n        assert dim_k % nums_head &#061;&#061; 0<br \/>\n        assert dim_v % nums_head &#061;&#061; 0<br \/>\n        self.q &#061; nn.Linear(input_dim, dim_k)<br \/>\n        self.k &#061; nn.Linear(input_dim, dim_k)<br \/>\n        self.v &#061; nn.Linear(input_dim, dim_v)<br \/>\n        self.nums_head &#061; nums_head<br \/>\n        self.dim_k &#061; dim_k<br \/>\n        self.dim_v &#061; dim_v<br \/>\n        self._norm_fact &#061; 1 \/ sqrt(dim_k)<\/p>\n<p>    def forward(self, x):<br \/>\n        Q &#061; self.q(x).reshape(-1, x.shape[0], x.shape[1], self.dim_k \/\/<br \/>\n                              self.nums_head)<br \/>\n        K &#061; self.k(x).reshape(-1, x.shape[0], x.shape[1], self.dim_k \/\/<br \/>\n                              self.nums_head)<br \/>\n        V &#061; self.v(x).reshape(-1, x.shape[0], x.shape[1], self.dim_v \/\/<br \/>\n                              self.nums_head)<br \/>\n        print(x.shape)<br \/>\n        print(Q.size())<br \/>\n        atten &#061; nn.Softmax(dim&#061;-1)(torch.matmul(Q, K.permute(0, 1, 3, 2)))  # Q * K.T() # batch_size * seq_len * seq_len<br \/>\n        output &#061; torch.matmul(atten, V).reshape(x.shape[0], x.shape[1], -1)  # Q * K.T() * V # batch_size * seq_len * dim_v<br \/>\n        return output<\/p>\n<p>x &#061; torch.rand(1, 3, 4)<br \/>\nprint(x)<br \/>\natten &#061; Self_Attention_Muti_Head(4, 4, 4, 2)<br \/>\ny &#061; atten(x)<br \/>\nprint(y.shape)<\/p>\n<li>\u89c6\u89c9\u6ce8\u610f\u529b\u673a\u5236 attention \u673a\u5236\u672c\u8d28\u662f\u5229\u7528\u76f8\u5173\u7279\u5f81\u56fe\u5b66\u4e60\u6743\u91cd\u5206\u5e03&#xff0c;\u518d\u7528\u5b66\u51fa\u6765\u7684\u6743\u91cd\u65bd\u52a0\u5728\u539f\u7279\u5f81\u56fe\u4e0a\u6700\u540e\u8fdb\u884c\u52a0\u6743\u6c42\u548c\u3002\u8ba1\u7b97\u673a\u89c6\u89c9\u4e0a\u7684\u6ce8\u610f\u529b\u673a\u5236\u4e3b\u8981\u5206\u4e3a\u4e09\u79cd&#xff1a;\u7a7a\u95f4\u57df\u3001\u901a\u9053\u57df\u3001\u6df7\u5408\u57df\u3002\n<ul>\n<li>\u7a7a\u95f4\u57df&#xff1a;\u5c06\u56fe\u7247\u4e2d\u7684\u7a7a\u95f4\u57df\u4fe1\u606f\u505a\u5bf9\u5e94\u7684\u7a7a\u95f4\u53d8\u6362&#xff0c;\u63d0\u53d6\u5173\u952e\u4fe1\u606f&#xff0c;\u5bf9\u7a7a\u95f4\u8fdb\u884c\u63a9\u7801\u7684\u751f\u6210\u5e76\u6253\u5206&#xff0c;\u4ee3\u8868\u662f Spatial attention module\u3002<\/li>\n<li>\u901a\u9053\u57df&#xff1a;\u7ed9\u6bcf\u4e2a\u901a\u9053\u4e0a\u7684\u4fe1\u53f7\u589e\u52a0\u4e00\u4e2a\u6743\u91cd&#xff0c;\u4ee3\u8868\u8be5\u901a\u9053\u4e0e\u5173\u952e\u4fe1\u606f\u7684\u76f8\u5173\u5ea6&#xff0c;\u6743\u91cd\u8d8a\u5927\u76f8\u5173\u5ea6\u8d8a\u9ad8\u3002\u5bf9\u901a\u9053\u751f\u6210\u63a9\u7801 mask \u8fdb\u884c\u6253\u5206&#xff0c;\u4ee3\u8868\u662f senet\u3001channel attention module\u3002<\/li>\n<li>\u6df7\u5408\u57df&#xff1a;\u7a7a\u95f4\u57df\u7684\u6ce8\u610f\u529b\u5ffd\u7565\u4e86\u901a\u9053\u57df\u4e2d\u7684\u4fe1\u606f&#xff0c;\u5c06\u6bcf\u4e2a\u901a\u9053\u7684\u56fe\u7247\u7279\u5f81\u540c\u7b49\u5904\u7406&#xff0c;\u8fd9\u79cd\u505a\u6cd5\u4f1a\u5c06\u7a7a\u95f4\u57df\u53d8\u6362\u65b9\u6cd5\u5c40\u9650\u5728\u539f\u59cb\u7279\u5f81\u63d0\u53d6\u9636\u6bb5\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u901a\u9053\u57df\u6ce8\u610f\u529b&#xff08;SENet&#xff09; \u901a\u8fc7\u5168\u5c40\u6c60\u5316\u63d0\u53d6\u901a\u9053\u6743\u91cd&#xff0c;\u7136\u540e\u5bf9\u7279\u5f81\u56fe\u8fdb\u884c\u6539\u53d8&#xff0c;\u5f97\u5230\u52a0\u5f3a\u540e\u7684\u7279\u5f81\u56fe\u3002<\/li>\n<p>python<\/p>\n<p>class SELayer(nn.Module):<br \/>\n    def __init__(self, channel, reduction&#061;16):<br \/>\n        super(SELayer, self).__init__()<br \/>\n        self.avg_pool &#061; nn.AdaptiveAvgPool2d(1)<br \/>\n        self.fc &#061; nn.Sequential(<br \/>\n            nn.Linear(channel, channel \/\/ reduction, bias&#061;False),<br \/>\n            nn.ReLU(inplace&#061;True),<br \/>\n            nn.Linear(channel \/\/ reduction, channel, bias&#061;False),<br \/>\n            nn.Sigmoid()<br \/>\n        )<\/p>\n<p>    def forward(self, x):<br \/>\n        b, c, _, _ &#061; x.size()<br \/>\n        y &#061; self.avg_pool(x).view(b, c)  # \u5bf9\u5e94Squeeze\u64cd\u4f5c<br \/>\n        y &#061; self.fc(y).view(b, c, 1, 1)  # \u5bf9\u5e94Excitation\u64cd\u4f5c<br \/>\n        return x * y.expand_as(x)<\/p>\n<li>\u95e8\u63a7\u6ce8\u610f\u529b\u673a\u5236&#xff08;GCT&#xff0c;Gated Channel Transformation&#xff09; GCT \u662f\u4e00\u79cd\u7b80\u5355\u6709\u6548\u7684\u901a\u9053\u95f4\u5efa\u6a21\u5173\u7cfb\u4f53\u7cfb\u7ed3\u6784&#xff0c;\u80fd\u663e\u8457\u63d0\u9ad8\u5377\u79ef\u7f51\u7edc\u5728\u89c6\u89c9\u4efb\u52a1\u7684\u6cdb\u5316\u80fd\u529b\u3002\u8bba\u6587\u53d1\u73b0\u5c06\u95e8\u63a7\u673a\u5236\u653e\u5728 Conv \u5c42\u524d\u9762\u8bad\u7ec3\u6548\u679c\u6700\u597d\u3002GCT \u5305\u542b\u4e09\u4e2a\u90e8\u5206&#xff1a;\n<ul>\n<li>Global Context Embedding&#xff1a;\u8bbe\u8ba1\u4e86\u4e00\u79cd\u5168\u5c40\u4e0a\u4e0b\u6587\u5d4c\u5165\u6a21\u5757&#xff0c;\u7528\u4e8e\u6bcf\u4e2a\u901a\u9053\u7684\u5168\u5c40\u4e0a\u4e0b\u6587\u4fe1\u606f\u6c47\u805a&#xff0c;\u516c\u5f0f\u4e3asc\u200b&#061;\u03b1c\u200b\u2225xc\u200b\u22252\u200b&#061;\u03b1c\u200b{[\u2211i&#061;1H\u200b\u2211j&#061;1W\u200b(xci,j\u200b)2]&#043;\u03f5}21\u200b\u3002<\/li>\n<li>Channel Normalization&#xff1a;\u5bf9\u7b2c\u4e00\u6b65\u8ba1\u7b97\u7684 L2 \u8fdb\u884c\u89c4\u8303\u5316\u6765\u6784\u5efa\u795e\u7ecf\u5143\u7ade\u4e89\u5173\u7cfb&#xff0c;\u4f7f\u7528\u8de8\u901a\u9053\u7684\u7279\u5f81\u89c4\u8303\u5316&#xff0c;\u516c\u5f0f\u4e3as^c\u200b&#061;\u2225s\u22252\u200bC\u200bsc\u200b\u200b&#061;[(\u2211c&#061;1C\u200bsc2\u200b)&#043;\u03f5]21\u200bC\u200bsc\u200b\u200b\u3002<\/li>\n<li>Gating Adaptation&#xff1a;\u52a0\u5165\u95e8\u9650\u673a\u5236&#xff0c;\u516c\u5f0f\u4e3ax^c\u200b&#061;xc\u200b[1&#043;tanh(\u03b3c\u200bs^c\u200b&#043;\u03b2c\u200b)]\u00a0\u3002<\/li>\n<\/ul>\n<\/li>\n<p>python<\/p>\n<p>class GCT(nn.Module):<br \/>\n    def __init__(self, num_channels, epsilon&#061;1e-5, mode&#061;&#039;l2&#039;, after_relu&#061;False):<br \/>\n        super(GCT, self).__init__()<br \/>\n        self.alpha &#061; nn.Parameter(torch.ones(1, num_channels, 1, 1))<br \/>\n        self.gamma &#061; nn.Parameter(torch.zeros(1, num_channels, 1, 1))<br \/>\n        self.beta &#061; nn.Parameter(torch.zeros(1, num_channels, 1, 1))<br \/>\n        self.epsilon &#061; epsilon<br \/>\n        self.mode &#061; mode<br \/>\n        self.after_relu &#061; after_relu<\/p>\n<p>    def forward(self, x):<br \/>\n        if self.mode &#061;&#061; &#039;l2&#039;:<br \/>\n            embedding &#061; (x.pow(2).sum((2, 3), keepdim&#061;True) &#043;<br \/>\n                         self.epsilon).pow(0.5) * self.alpha<br \/>\n            norm &#061; self.gamma \/ \\\\<br \/>\n                   (embedding.pow(2).mean(dim&#061;1, keepdim&#061;True) &#043;<br \/>\n                    self.epsilon).pow(0.5)<br \/>\n        elif self.mode &#061;&#061; &#039;l1&#039;:<br \/>\n            if not self.after_relu:<br \/>\n                _x &#061; torch.abs(x)<br \/>\n            else:<br \/>\n                _x &#061; x<br \/>\n            embedding &#061; _x.sum((2, 3), keepdim&#061;True) * self.alpha<br \/>\n            norm &#061; self.gamma \/ \\\\<br \/>\n                   (torch.abs(embedding).mean(dim&#061;1, keepdim&#061;True) &#043; self.epsilon)<br \/>\n        gate &#061; 1. &#043; torch.tanh(embedding * norm &#043; self.beta)<br \/>\n        return x * gate<\/p>\n<p>GCT \u5efa\u8bae\u6dfb\u52a0\u5728 Conv \u5c42\u524d&#xff0c;\u4e00\u822c\u53ef\u4ee5\u5148\u51bb\u7ed3\u539f\u6765\u7684\u6a21\u578b&#xff0c;\u6765\u8bad\u7ec3 GCT&#xff0c;\u7136\u540e\u89e3\u51bb\u518d\u8fdb\u884c\u5fae\u8c03\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb994\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf8\u6b21\u3002GCT \u5efa\u8bae\u6dfb\u52a0\u5728 Conv \u5c42\u524d\uff0c\u4e00\u822c\u53ef\u4ee5\u5148\u51bb\u7ed3\u539f\u6765\u7684\u6a21\u578b\uff0c\u6765\u8bad\u7ec3 GCT\uff0c\u7136\u540e\u89e3\u51bb\u518d\u8fdb\u884c\u5fae\u8c03\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81,152,841,50,207,86,427],"topic":[],"class_list":["post-36928","post","type-post","status-publish","format-standard","hentry","category-server","tag-python","tag-pytorch","tag-transformer","tag-50","tag-207","tag-86","tag-427"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>PyTorch \u5b9e\u6218\uff1a\u4ece 0 \u5f00\u59cb\u642d\u5efa Transformer - \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\/36928.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PyTorch \u5b9e\u6218\uff1a\u4ece 0 \u5f00\u59cb\u642d\u5efa Transformer - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb994\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf8\u6b21\u3002GCT \u5efa\u8bae\u6dfb\u52a0\u5728 Conv \u5c42\u524d\uff0c\u4e00\u822c\u53ef\u4ee5\u5148\u51bb\u7ed3\u539f\u6765\u7684\u6a21\u578b\uff0c\u6765\u8bad\u7ec3 GCT\uff0c\u7136\u540e\u89e3\u51bb\u518d\u8fdb\u884c\u5fae\u8c03\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/36928.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-12T17:03:34+00:00\" \/>\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=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/36928.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/36928.html\",\"name\":\"PyTorch \u5b9e\u6218\uff1a\u4ece 0 \u5f00\u59cb\u642d\u5efa Transformer - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-12T17:03:34+00:00\",\"dateModified\":\"2025-05-12T17:03:34+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/36928.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/36928.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/36928.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PyTorch \u5b9e\u6218\uff1a\u4ece 0 \u5f00\u59cb\u642d\u5efa Transformer\"}]},{\"@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":"PyTorch \u5b9e\u6218\uff1a\u4ece 0 \u5f00\u59cb\u642d\u5efa Transformer - \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\/36928.html","og_locale":"zh_CN","og_type":"article","og_title":"PyTorch \u5b9e\u6218\uff1a\u4ece 0 \u5f00\u59cb\u642d\u5efa Transformer - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb994\u6b21\uff0c\u70b9\u8d5e20\u6b21\uff0c\u6536\u85cf8\u6b21\u3002GCT \u5efa\u8bae\u6dfb\u52a0\u5728 Conv \u5c42\u524d\uff0c\u4e00\u822c\u53ef\u4ee5\u5148\u51bb\u7ed3\u539f\u6765\u7684\u6a21\u578b\uff0c\u6765\u8bad\u7ec3 GCT\uff0c\u7136\u540e\u89e3\u51bb\u518d\u8fdb\u884c\u5fae\u8c03\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/36928.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-12T17:03:34+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/36928.html","url":"https:\/\/www.wsisp.com\/helps\/36928.html","name":"PyTorch \u5b9e\u6218\uff1a\u4ece 0 \u5f00\u59cb\u642d\u5efa Transformer - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-12T17:03:34+00:00","dateModified":"2025-05-12T17:03:34+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/36928.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/36928.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/36928.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"PyTorch \u5b9e\u6218\uff1a\u4ece 0 \u5f00\u59cb\u642d\u5efa Transformer"}]},{"@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\/36928","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=36928"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/36928\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=36928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=36928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=36928"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=36928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}