{"id":9026,"date":"2023-10-19T17:06:25","date_gmt":"2023-10-19T09:06:25","guid":{"rendered":"\/?p=9026"},"modified":"2023-10-19T17:06:25","modified_gmt":"2023-10-19T09:06:25","slug":"02-pytorch%e5%9f%ba%e7%a1%80%e8%af%ad%e6%b3%95","status":"publish","type":"post","link":"\/?p=9026","title":{"rendered":"02-pytorch\u57fa\u7840\u8bed\u6cd5"},"content":{"rendered":"<h2>\u5e38\u7528\u6a21\u5757\u4ee5\u53ca\u8bbe\u7f6e<\/h2>\n<pre><code class=\"language-python\">import torch\nimport numpy as np\nfrom matplotlib import pyplot as plt\n\ndtype = torch.double\n# GPU\u8fd0\u7b97\ndevice = torch.device(&quot;cuda&quot;)\n# GPU\u8fd0\u7b97\u65b9\u6848\u63a8\u8350\u5199\u6cd5\uff1a\ndevice = torch.device(&#039;cuda:0&#039; if torch.cuda.is_available() else &#039;cpu&#039;)\n****.to(device, \u53ef\u4ee5\u8f6c\u6362\u7c7b\u578b)\n# \u201c:0 \u8868\u793a\u5728\u54ea\u4e2a\u663e\u5361\u4e0a\u8fdb\u884c\u8fd0\u7b97\uff0c\u5355\u663e\u5361\u4e0d\u5199\u4e0e\u5199\u4e0a:0\u53ef\u4ee5\u5212\u7b49\u53f7\uff0c\u4e0d\u5199\u81ea\u52a8\u5206\u914d\u201d\n\n# \u63a8\u8fdbGPU\u8fd0\u7b97\uff1a\nimport torch\n\ndtype = torch.double\n# GPU\u8fd0\u7b97\ndevice = torch.device(&quot;cuda&quot;)\ndevice = torch.device(&#039;cuda:0&#039; if torch.cuda.is_available() else &#039;cpu&#039;)\n\na = torch.rand(3, 4, device=device)\nb = torch.rand(4)\n\nc1 = a - b\nc2 = torch.sub(a, b)\nc2.to(device)\nprint(c2)<\/code><\/pre>\n<h2>\u5f20\u91cf<\/h2>\n<p>\u5b98\u65b9\u6587\u6863\uff1a<a href=\"https:\/\/pytorch.org\/docs\/stable\/tensors.html\">torch.Tensor \u2014 PyTorch 2.1 documentation<\/a><\/p>\n<h3>\u4ec0\u4e48\u662f\u5f20\u91cf<\/h3>\n<p>\u5728\u6df1\u5ea6\u5b66\u4e60\u91cc\uff0c\u5f20\u91cf\u53ef\u4ee5\u5c06\u5411\u91cf\u548c\u77e9\u9635\u63a8\u5e7f\u5230\u4efb\u610f\u7ef4\u5ea6\uff0c\u5176\u5b9e\u5c31\u662f\u591a\u7ef4\u6570\u7ec4\u3002<\/p>\n<p>\u5f20\u91cf\u7684<strong>\u7ef4\u5ea6<\/strong>\uff08rank\uff0cnumber of dimensions\uff09\u6307\u7684\u662f\u5f20\u91cf\u4e2d\u7528\u6765\u7d22\u5f15\u5143\u7d20\u7684\u7d22\u5f15\u4e2a\u6570\uff0c0\u7ef4\u5f20\u91cf\u5c31\u662f\u6807\u91cf\uff0c\u56e0\u4e3a\u4e0d\u9700\u8981\u7d22\u5f15\uff0c\u800c\u5bf9\u4e8e\u5411\u91cf\uff08vector\uff09\u800c\u8a00\uff0c\u53ea\u9700\u8981\u4e00\u4e2a\u7d22\u5f15\u5c31\u53ef\u4ee5\u5f97\u5230\u76f8\u5e94\u5143\u7d20\uff0c\u540e\u7eed\u540c\u7406\u3002\u9ad8\u7ef4\u7684\u5f20\u91cf\u5176\u5b9e\u5c31\u662f\u5bf9\u4f4e\u7ef4\u5f20\u91cf\u7684\u5806\u53e0\u3002<\/p>\n<p>\u5f20\u91cf\u7684<strong>\u5f62\u72b6<\/strong>\uff08shape\uff0cnumber of rows and columns\uff09\u6307\u7684\u662f\u5f20\u91cf\u4e2d\u6bcf\u4e00\u7ef4\u5ea6\u7684\u5927\u5c0f\u3002<\/p>\n<p>\u5f20\u91cf\u7684<strong>\u7c7b\u578b<\/strong>\uff08type\uff0cdata type of tensor's elements\uff09\u6307\u7684\u662f\u5f20\u91cf\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/v2-91fefb9a6227e8c11f8df316bc30cbb5_720w.jpg\" alt=\"img\" \/><\/p>\n<p>\u73b0\u5728\u5c06\u4e09\u7ef4\u7684\u5f20\u91cf\u7528\u4e00\u4e2a\u6b63\u65b9\u4f53\u6765\u8868\u793a\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231017223327414.png\" alt=\"image-20231017223327414\" \/><\/p>\n<p>\u8fd9\u6837\u5b50\u53ef\u4ee5\u8fdb\u4e00\u6b65\u751f\u6210\u66f4\u9ad8\u7ef4\u7684\u5f20\u91cf\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231017223349568.png\" alt=\"image-20231017223349568\" \/><\/p>\n<p>\u8fd9\u6709\u5565\u7528\u5462\uff1f\u5728\u7528TensorFlow\u5904\u7406\u66f4\u9ad8\u7ef4\u6570\u636e\u7ed3\u6784\u7684\u65f6\u5019\uff0c\u6700\u597d\u53ef\u4ee5\u80fd\u591f\u5728\u8111\u5b50\u91cc\u76f8\u51fa\u6570\u636e\u7684\u5f62\u72b6\u3002<\/p>\n<p>\u4e3e\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5f69\u8272\u56fe\u50cf\u6587\u4ef6\uff08RGB\uff09\u4e00\u822c\u90fd\u4f1a\u5904\u7406\u62103-d tensor\uff0c\u6bcf\u4e2a2d array\u4e2d\u7684element\u8868\u793a\u4e00\u4e2a\u50cf\u7d20\uff0cR\u4ee3\u8868Red\uff0cG\u4ee3\u8868Green\uff0cB\u4ee3\u8868Blue\uff1a<\/p>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231017223503243.png\" alt=\"image-20231017223503243\" \/><\/p>\n<p>\u800c\u7528Python\u4e3e\u4f8b\u5b50\u7684\u8bdd\uff0c\u6765\u770b\u770b\u4e0b\u9762\u8fd9\u4e2a\u8868\u683c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u9636<\/th>\n<th>\u6570\u5b66\u5b9e\u4f8b<\/th>\n<th>Python\u4f8b\u5b50<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>\u7eaf\u91cf\uff08\u53ea\u6709\u5927\u5c0f\uff09<\/td>\n<td>s = 483<\/td>\n<\/tr>\n<tr>\n<td>1<\/td>\n<td>\u5411\u91cf\uff08\u5927\u5c0f\u548c\u65b9\u5411\uff09<\/td>\n<td>v = [1.1, 2.2, 3.3]<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u77e9\u9635\uff08\u6570\u636e\u8868\uff09<\/td>\n<td>m = [[1,2,3], [4,5,6], [7,8,9]]<\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>3\u9636\u5f20\u91cf\uff08\u6570\u636e\u7acb\u4f53\uff09<\/td>\n<td>t = [[[2], [4], [6]], [[8], [10], [12]], [[14], [16], [18]]]<\/td>\n<\/tr>\n<tr>\n<td>n<\/td>\n<td>n\u9636\uff08\u81ea\u5df1\u60f3\u60f3\u770b\uff09<\/td>\n<td>... ...<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u6570\u636e\u7c7b\u578bdtype<\/h3>\n<p>\u5f20\u91cf\u6784\u9020\u51fd\u6570\uff08\u4f8b\u5982tensor()\uff0czeros()...\uff09\u5728\u6267\u884c\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u6709\u4e2a\u53c2\u6570\u662f\u53ebdtype\u7684<\/p>\n<pre><code class=\"language-python\">a = torch.ones(3,224,224, dtype=torch.int32)<\/code><\/pre>\n<p>dtype\u5236\u5b9a\u4e86\u5f20\u91cf\u4e2d\u6bcf\u4e2a\u5143\u7d20\u7684\u7c7b\u578b\u3002\u5728PyTorch\u4e2d\u6709\u4e00\u4e9b\u57fa\u672c\u7684\u6570\u636e\u7c7b\u578b\uff0c\u548cNumPy\u7c7b\u578b\u6545\u610f\u8bbe\u8ba1\u7684\u76f8\u4f3c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b<\/th>\n<th>\u8bf4\u660e<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>torch.float32\/torch.float<\/td>\n<td>32\u4f4d\u6d6e\u70b9\u6570-\u9ed8\u8ba4<\/td>\n<\/tr>\n<tr>\n<td>torch.float64\/torch.double<\/td>\n<td>64\u4f4d\u53cc\u7cbe\u5ea6\u6d6e\u70b9\u6570<\/td>\n<\/tr>\n<tr>\n<td>torch.float16\/torch.half<\/td>\n<td>16\u4f4d\u534a\u7cbe\u5ea6\u6d6e\u70b9\u6570<\/td>\n<\/tr>\n<tr>\n<td>torch.int8<\/td>\n<td>8\u4f4d\u6709\u7b26\u53f7\u6574\u6570<\/td>\n<\/tr>\n<tr>\n<td>torch.uint8<\/td>\n<td>8\u4f4d\u65e0\u7b26\u53f7\u6574\u6570<\/td>\n<\/tr>\n<tr>\n<td>torch.int16\u6216torch.short<\/td>\n<td>16\u4f4d\u6709\u7b26\u53f7\u6574\u6570<\/td>\n<\/tr>\n<tr>\n<td>torch.int32\u6216<a href=\"https:\/\/link.zhihu.com\/?target=http%3A\/\/torch.int\">http:\/\/torch.int<\/a><\/td>\n<td>32\u4f4d\u6709\u7b26\u53f7\u6574\u6570<\/td>\n<\/tr>\n<tr>\n<td>torch.int64\u6216torch.long<\/td>\n<td>64\u4f4d\u6709\u7b26\u53f7\u6574\u6570<\/td>\n<\/tr>\n<tr>\n<td>torch.bool<\/td>\n<td>\u5e03\u5c14\u578b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6253\u5f00\u6e90\u4ee3\u7801\u67e5\u770b\u66f4\u591a\u7c7b\u578b,\u81f3\u4e8e\u6709\u4ec0\u4e48\u5c4c\u7528\uff0c\u6211\u4e5f\u4e0d\u5230\uff0c\u78b0\u5230\u518d\u8bf4\uff1a<\/p>\n<pre><code class=\"language-python\">float32: dtype = ...\nfloat: dtype = ...\nfloat64: dtype = ...\ndouble: dtype = ...\nfloat16: dtype = ...\nbfloat16: dtype = ...\nhalf: dtype = ...\nuint8: dtype = ...\nint8: dtype = ...\nint16: dtype = ...\nshort: dtype = ...\nint32: dtype = ...\nint: dtype = ...\nint64: dtype = ...\nlong: dtype = ...\ncomplex32: dtype = ...\ncomplex64: dtype = ...\ncfloat: dtype = ...\ncomplex128: dtype = ...\ncdouble: dtype = ...\nquint8: dtype = ...\nqint8: dtype = ...\nqint32: dtype = ...\nbool: dtype = ...\nquint4x2: dtype = ...\nquint2x4: dtype = ...<\/code><\/pre>\n<p>\u8981\u6ce8\u610f\uff0c\u91c7\u7528\u66f4\u9ad8\u7684\u7cbe\u5ea6\u598264\u4f4d\uff0c\u5e76\u4e0d\u4f1a\u63d0\u9ad8\u6a21\u578b\u7cbe\u5ea6\uff0c\u76f8\u53cd\u9700\u8981\u66f4\u591a\u7684\u5185\u5b58\u548c\u8ba1\u7b97\u65f6\u95f4\u300216\u4f4d\u534a\u7cbe\u5ea6\u6d6e\u70b9\u6570\u7684\u6570\u636e\u7c7b\u578b\u5728\u6807\u51c6CPU\u4e2d\u5e76\u4e0d\u5b58\u5728\uff0c\u800c\u662f\u7531\u73b0\u4ee3GPU\u63d0\u4f9b\u3002\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u53ef\u4ee5\u5207\u6362\u5230\u8fd9\u79cd\u65b9\u5f0f\u6765\u51cf\u5c11\u795e\u7ecf\u7f51\u7edc\u7684\u5360\u7528\u7a7a\u95f4\uff0c\u8fd9\u6837\u5bf9\u7cbe\u5ea6\u635f\u5931\u4e5f\u4f1a\u6bd4\u8f83\u5c0f\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u5982\u4f55\u6539\u53d8\u5f20\u91cfa\u7684\u7c7b\u578b\u5462\uff1f\u53ef\u4ee5\u901a\u8fc7<code>to()<\/code>\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"language-python3\">a = a.to(torch.float)\na.dtype  # torch.float32<\/code><\/pre>\n<h3>\u521b\u5efa\u5f20\u91cf<\/h3>\n<pre><code class=\"language-python\">import torch\nimport numpy as np\nfrom matplotlib import pyplot as plt\ndtype = torch.double\ndevice = torch.device(&quot;cuda:0&quot;)\n\n# \u8f6c\u5316np\u77e9\u9635\n>&gt;&gt;li = [[402, 754, 327, 979], [713, 926, 879, 934], [540, 953, 209, 369]]\n>&gt;&gt;torch.Tensor(li)\n>&gt;&gt;tensor([[402., 754., 327., 979.],\n        [713., 926., 879., 934.],\n        [540., 953., 209., 369.]])\n\n# \u521b\u5efa\u4e00\u7ef4\u7b49\u8ddd\u5411\u91cf\n>&gt;&gt;torch.linspace(0, 1, 100, dtype=torch.double, \n                  # device=device\n                  )\n>&gt;&gt;tensor([0.0000, 0.0101, 0.0202, 0.0303, 0.0404, 0.0505, 0.0606, 0.0707, 0.0808,\n        0.0909, 0.1010, 0.1111, 0.1212, 0.1313, 0.1414, 0.1515, 0.1616, 0.1717,\n        0.1818, 0.1919, 0.2020, 0.2121, 0.2222, 0.2323, 0.2424, 0.2525, 0.2626,\n        0.2727, 0.2828, 0.2929, 0.3030, 0.3131, 0.3232, 0.3333, 0.3434, 0.3535,\n        0.3636, 0.3737, 0.3838, 0.3939, 0.4040, 0.4141, 0.4242, 0.4343, 0.4444,\n        0.4545, 0.4646, 0.4747, 0.4848, 0.4949, 0.5051, 0.5152, 0.5253, 0.5354,\n        0.5455, 0.5556, 0.5657, 0.5758, 0.5859, 0.5960, 0.6061, 0.6162, 0.6263,\n        0.6364, 0.6465, 0.6566, 0.6667, 0.6768, 0.6869, 0.6970, 0.7071, 0.7172,\n        0.7273, 0.7374, 0.7475, 0.7576, 0.7677, 0.7778, 0.7879, 0.7980, 0.8081,\n        0.8182, 0.8283, 0.8384, 0.8485, 0.8586, 0.8687, 0.8788, 0.8889, 0.8990,\n        0.9091, 0.9192, 0.9293, 0.9394, 0.9495, 0.9596, 0.9697, 0.9798, 0.9899,\n        1.0000], dtype=torch.float64)\n\n# \u521b\u5efa\u5168\u4e00\u77e9\u9635\uff0c\u96f6\u77e9\u9635\n>&gt;&gt;torch.ones(3, 4, dtype=dtype)\n>&gt;&gt;tensor([[1., 1., 1., 1.],\n        [1., 1., 1., 1.],\n        [1., 1., 1., 1.]], dtype=torch.float64)\n>&gt;&gt;torch.zeros(3,4,dtype=dtype)\n>&gt;&gt;tensor([[0., 0., 0., 0.],\n        [0., 0., 0., 0.],\n        [0., 0., 0., 0.]], dtype=torch.float64)\n\n# \u521b\u5efa\u968f\u673a\u77e9\u9635\nx = torch.rand(n, m, dtype=dtype, device=device)\nx = torch.randn(n, m, dtype=dtype, device=device)\nx = torch.normal(means, std, dtype=dtype, device=device)<\/code><\/pre>\n<h3>\u65b0\u5efaTensor\u7684\u51e0\u79cd\u65b9\u6cd5<\/h3>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570<\/th>\n<th>\u529f\u80fd<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Tensor(*sizes)<\/td>\n<td>\u57fa\u7840\u6784\u9020\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>tensor(data,)<\/td>\n<td>\u7c7b\u4f3cnp.array\u7684\u6784\u9020\u51fd\u6570<\/td>\n<\/tr>\n<tr>\n<td>ones(*sizes)<\/td>\n<td>\u51681Tensor<\/td>\n<\/tr>\n<tr>\n<td>zeros(*sizes)<\/td>\n<td>\u51680Tensor<\/td>\n<\/tr>\n<tr>\n<td>eye(*sizes)<\/td>\n<td>\u5bf9\u89d2\u7ebf\u4e3a1\uff0c\u5176\u4ed6\u4e3a0<\/td>\n<\/tr>\n<tr>\n<td>arange(s,e,step)<\/td>\n<td>\u4eces\u5230e\uff0c\u6b65\u957f\u4e3astep<\/td>\n<\/tr>\n<tr>\n<td>linspace(s,e,steps)<\/td>\n<td>\u4eces\u5230e\uff0c\u5747\u5300\u5207\u5206\u6210steps\u4efd<\/td>\n<\/tr>\n<tr>\n<td>rand\/randn(*sizes)<\/td>\n<td>\u5747\u5300\/\u6807\u51c6\u6b63\u6001\u5206\u5e03<\/td>\n<\/tr>\n<tr>\n<td>normal(mean,std)\/uniform(from,to)<\/td>\n<td>\u6b63\u6001\u5206\u5e03\/\u5747\u5300\u5206\u5e03<\/td>\n<\/tr>\n<tr>\n<td>randperm(m)<\/td>\n<td>\u968f\u673a\u6392\u5217<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>\u5f20\u91cf\u64cd\u4f5c<\/h3>\n<pre><code class=\"language-python\"># \u589e\u52a0\u7ef4\u5ea6\nx = x.unsqueeze(dim)    # dim=0,1,...\n\n# \u8f6c\u7f6e\nx = x.t()\n\n# \u5927\u5c0f\nprint(x.size())\n\n# \u5207\u7247\nx_1 = x[:,1:-2]<\/code><\/pre>\n<h3>\u5e38\u7528\u51fd\u6570<\/h3>\n<pre><code class=\"language-python\"># \u6570\u5b66\u51fd\u6570\ny = torch.sin(x)\ny = torch.tan(x)\ny = torch.atan(x)\ny = torch.sqrt(x)\ny = torch.relu(x)\ny = torch.tanh(x)\ny = torch.sigmoid(x)\n\n# \u5176\u4ed6\u51fd\u6570\ny = torch.sum(x, dim = 0)<\/code><\/pre>\n<h2>Tensor\/\u5f20\u91cf\u7684\u57fa\u672c\u8fd0\u7b97<\/h2>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMyNTA4NjE=,size_16,color_FFFFFF,t_70.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<h3>1. \u52a0\u6cd5\u8fd0\u7b97<\/h3>\n<pre><code class=\"language-python\">import torch\n\n# \u8fd9\u4e24\u4e2aTensor\u52a0\u51cf\u4e58\u9664\u4f1a\u5bf9b\u81ea\u52a8\u8fdb\u884cBroadcasting\na = torch.rand(3, 4)\nb = torch.rand(4)\n\nc1 = a + b\nc2 = torch.add(a, b)\nprint(c1.shape, c2.shape)\nprint(torch.all(torch.eq(c1, c2)))<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">torch.Size([3, 4]) torch.Size([3, 4])\ntensor(True)<\/code><\/pre>\n<h3>2. \u51cf\u6cd5\u8fd0\u7b97<\/h3>\n<pre><code class=\"language-python\">a = torch.rand(3, 4)\nb = torch.rand(4)\n\nc1 = a - b\n# \u4f7f\u7528PyTorch\u7684sub\u51fd\u6570\u6765\u6267\u884c\u5143\u7d20\u7ea7\u7684\u51cf\u6cd5\nc2 = torch.sub(a, b)\nprint(c1.shape, c2.shape)\nprint(torch.all(torch.eq(c1, c2)))<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">torch.Size([3, 4]) torch.Size([3, 4])\ntensor(1, dtype=torch.uint8)<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019113204394.png\" alt=\"image-20231019113204394\" \/><\/p>\n<h3>3. \u54c8\u8fbe\u739b\u79ef(element wise\uff0c\u5bf9\u5e94\u5143\u7d20\u76f8\u4e58)<\/h3>\n<pre><code class=\"language-python\">c1 = a * b\nc2 = torch.mul(a, b)\nprint(c1.shape, c2.shape)\nprint(torch.all(torch.eq(c1, c2)))<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">torch.Size([3, 4]) torch.Size([3, 4])\ntensor(1, dtype=torch.uint8)<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019113353491.png\" alt=\"image-20231019113353491\" \/><\/p>\n<h3>4. \u9664\u6cd5\u8fd0\u7b97<\/h3>\n<pre><code class=\"language-python\">c1 = a \/ b\nc2 = torch.div(a, b)\nprint(c1.shape, c2.shape)\nprint(torch.all(torch.eq(c1, c2)))<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">torch.Size([3, 4]) torch.Size([3, 4])\ntensor(1, dtype=torch.uint8)<\/code><\/pre>\n<h3>5. \u77e9\u9635\u4e58\u6cd5<\/h3>\n<h4>5.1 \u4e8c\u7ef4\u77e9\u9635\u76f8\u4e58<\/h4>\n<p>\u4e8c\u7ef4\u77e9\u9635\u4e58\u6cd5\u8fd0\u7b97\u64cd\u4f5c\u5305\u62ec<\/p>\n<ul>\n<li>torch.mm()\uff1a\u53ea\u9002\u7528\u4e8e2\u7ef4\u6570\u636e<\/li>\n<li>torch.matmul()\uff1a\u9002\u7528\u4e8e\u6240\u6709\u7ef4\u5ea6\u6570\u636e<\/li>\n<li>@\uff1a\u9002\u7528\u4e8e\u6240\u6709\u7ef4\u5ea6\u6570\u636e<\/li>\n<\/ul>\n<pre><code class=\"language-python\">import torch\n\na = torch.ones(2, 1)\nb = torch.ones(1, 2)\nprint(torch.mm(a, b).shape)\nprint(torch.matmul(a, b).shape)\nprint((a @ b).shape)<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">torch.Size([2, 2])\ntorch.Size([2, 2])\ntorch.Size([2, 2])<\/code><\/pre>\n<h4>5.2 \u591a\u7ef4\u77e9\u9635\u76f8\u4e58<\/h4>\n<p>\u5bf9\u4e8e\u9ad8\u7ef4\u7684Tensor\uff08dim&gt;2\uff09\uff0c\u5b9a\u4e49\u5176\u77e9\u9635\u4e58\u6cd5\u4ec5\u5728\u6700\u540e\u7684\u4e24\u4e2a\u7ef4\u5ea6\u4e0a\uff0c\u8981\u6c42\u524d\u9762\u7684\u7ef4\u5ea6\u5fc5\u987b\u4fdd\u6301\u4e00\u81f4\uff0c\u5c31\u50cf\u77e9\u9635\u7684\u7d22\u5f15\u4e00\u6837\u5e76\u4e14\u8fd0\u7b97\u64cd\u53ea\u6709torch.matmul()\u3002<br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTMyNTA4NjE=,size_16,color_FFFFFF,t_70-1697685963278-9.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<pre><code class=\"language-python\">c = torch.rand(4, 3, 28, 64)\nd = torch.rand(4, 3, 64, 32)\nprint(torch.matmul(c, d).shape)<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">torch.Size([4, 3, 28, 32])<\/code><\/pre>\n<p>\u6ce8\u610f\uff0c\u5728\u8fd9\u79cd\u60c5\u5f62\u4e0b\u7684\u77e9\u9635\u76f8\u4e58\uff0c\u524d\u9762\u7684&quot;\u77e9\u9635\u7d22\u5f15\u7ef4\u5ea6&quot;\u5982\u679c\u7b26\u5408Broadcasting\u673a\u5236\uff0c\u4e5f\u4f1a\u81ea\u52a8\u505a\u5e7f\u64ad\uff0c\u7136\u540e\u76f8\u4e58\u3002\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">c = torch.rand(4, 3, 28, 64)\nd = torch.rand(4, 1, 64, 32)\nprint(torch.matmul(c, d).shape)<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">torch.Size([4, 3, 28, 32])<\/code><\/pre>\n<h3>6. \u5e42\u8fd0\u7b97<\/h3>\n<pre><code class=\"language-python\">import torch\n\na = torch.full([2, 2], 3)\nb = a.pow(2) # \u4e5f\u53ef\u4ee5a**2\nprint(b)<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">tensor([[9, 9],\n        [9, 9]])<\/code><\/pre>\n<h3>7. \u5f00\u65b9\u8fd0\u7b97<\/h3>\n<pre><code class=\"language-python\">c = b.sqrt() # \u4e5f\u53ef\u4ee5a**(0.5)\nprint(c)\nd = b.rsqrt() # \u5e73\u65b9\u6839\u7684\u5012\u6570\nprint(d)<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">tensor([[3., 3.],\n        [3., 3.]])\ntensor([[0.3333, 0.3333],\n        [0.3333, 0.3333]])<\/code><\/pre>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019141225226.png\" alt=\"image-20231019141225226\" \/><\/p>\n<h3>8.\u6307\u6570\u4e0e\u5bf9\u6570\u8fd0\u7b97<\/h3>\n<p>\u6ce8\u610flog\u662f\u4ee5\u81ea\u7136\u5bf9\u6570\u4e3a\u5e95\u6570\u7684\uff0c\u4ee52\u4e3a\u5e95\u7684\u7528log2\uff0c\u4ee510\u4e3a\u5e95\u7684\u7528log10<br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019141255661.png\" alt=\"image-20231019141255661\" \/><\/p>\n<pre><code class=\"language-python\">import torch\n\na = torch.exp(torch.ones(2, 2)) # \u5f97\u52302*2\u7684\u5168\u662fe\u7684Tensor\nprint(a)\nprint(torch.log(a)) # \u53d6\u81ea\u7136\u5bf9\u6570<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">tensor([[2.7183, 2.7183],\n        [2.7183, 2.7183]])\ntensor([[1.0000, 1.0000],\n        [1.0000, 1.0000]])<\/code><\/pre>\n<h3>9.\u8fd1\u4f3c\u503c\u8fd0\u7b97<\/h3>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019141454964.png\" alt=\"image-20231019141454964\" \/><\/p>\n<pre><code class=\"language-python\">import torch\n\na = torch.tensor(3.14)\nprint(a.floor(), a.ceil(), a.trunc(), a.frac()) # \u53d6\u4e0b,\u53d6\u4e0a,\u53d6\u6574\u6570,\u53d6\u5c0f\u6570\nb = torch.tensor(3.49)\nc = torch.tensor(3.5)\nprint(b.round(), c.round()) # \u56db\u820d\u4e94\u5165<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">tensor(3.) tensor(4.) tensor(3.) tensor(0.1400)\ntensor(3.) tensor(4.)<\/code><\/pre>\n<h3>10. \u88c1\u526a\u8fd0\u7b97<\/h3>\n<p>\u5373\u5bf9Tensor\u4e2d\u7684\u5143\u7d20\u8fdb\u884c\u8303\u56f4\u8fc7\u6ee4\uff0c\u4e0d\u7b26\u5408\u6761\u4ef6\u7684\u53ef\u4ee5\u628a\u5b83\u53d8\u6362\u5230\u8303\u56f4\u5185\u90e8\uff08\u8fb9\u754c\uff09\u4e0a\uff0c\u5e38\u7528\u4e8e\u68af\u5ea6\u88c1\u526a\uff08gradient clipping\uff09\uff0c\u5373\u5728\u53d1\u751f\u68af\u5ea6\u79bb\u6563\u6216\u8005\u68af\u5ea6\u7206\u70b8\u65f6\u5bf9\u68af\u5ea6\u7684\u5904\u7406\uff0c\u5b9e\u9645\u4f7f\u7528\u65f6\u53ef\u4ee5\u67e5\u770b\u68af\u5ea6\u7684\uff08L2\u8303\u6570\uff09\u6a21\u6765\u770b\u770b\u9700\u4e0d\u9700\u8981\u505a\u5904\u7406\uff1aw.grad.norm(2)\u3002<br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019141645999.png\" alt=\"image-20231019141645999\" \/><\/p>\n<pre><code class=\"language-python\">import torch\n\ngrad = torch.rand(2, 3) * 15  # 0~15\u968f\u673a\u751f\u6210\nprint(grad.max(), grad.min(), grad.median())  # \u6700\u5927\u503c\u6700\u5c0f\u503c\u5e73\u5747\u503c\n\nprint(&#039;\\ngrad =\\n&#039;, grad)\nprint(&#039;\\ngrad.norm(2) = &#039;, grad.norm(2))\nprint(&#039;\\ngrad.clamp(10) = \\n&#039;, grad.clamp(10))  # \u6700\u5c0f\u662f10,\u5c0f\u4e8e10\u7684\u90fd\u53d8\u621010\nprint(&#039;\\ngrad.clamp(3, 10) = \\n&#039;, grad.clamp(3, 10))  # \u6700\u5c0f\u662f3,\u5c0f\u4e8e3\u7684\u90fd\u53d8\u62103;\u6700\u5927\u662f10,\u5927\u4e8e10\u7684\u90fd\u53d8\u621010<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">tensor(14.2548) tensor(3.5795) tensor(4.9580)\ngrad =\n tensor([[ 3.5795, 11.3405, 14.2548],\n        [ 4.0516, 13.8750,  4.9580]])\ngrad.norm(2) =  tensor(24.0444)\ngrad.clamp(10) = \n tensor([[10.0000, 11.3405, 14.2548],\n        [10.0000, 13.8750, 10.0000]])\ngrad.clamp(3, 10) = \n tensor([[ 3.5795, 10.0000, 10.0000],\n        [ 4.0516, 10.0000,  4.9580]])<\/code><\/pre>\n<h2>\u4e94\u3001Tensor\/\u5f20\u91cf\u7684\u5c5e\u6027\u7edf\u8ba1<\/h2>\n<h3>1. \u8303\u6570\uff1anorm()<\/h3>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019141942089.png\" alt=\"image-20231019141942089\" \/><\/p>\n<pre><code class=\"language-python\">import torch\n\na = torch.full([8], 1)\nb = a.reshape([2, 4])\nc = a.reshape([2, 2, 2])\n\n# \u6c42L1\u8303\u6570(\u6240\u6709\u5143\u7d20\u7edd\u5bf9\u503c\u6c42\u548c)\nprint(a.norm(1), b.norm(1), c.norm(1))\n# \u6c42L2\u8303\u6570(\u6240\u6709\u5143\u7d20\u7684\u5e73\u65b9\u548c\u518d\u5f00\u6839\u53f7)\nprint(a.norm(2), b.norm(2), c.norm(2))\n\n# \u5728b\u76841\u53f7\u7ef4\u5ea6\u4e0a\u6c42L1\u8303\u6570\nprint(b.norm(1, dim=1))\n# \u5728b\u76841\u53f7\u7ef4\u5ea6\u4e0a\u6c42L2\u8303\u6570\nprint(b.norm(2, dim=1))\n\n# \u5728c\u76840\u53f7\u7ef4\u5ea6\u4e0a\u6c42L1\u8303\u6570\nprint(c.norm(1, dim=0))\n# \u5728c\u76840\u53f7\u7ef4\u5ea6\u4e0a\u6c42L2\u8303\u6570\nprint(c.norm(2, dim=0))<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">tensor(8.) tensor(8.) tensor(8.)\ntensor(2.8284) tensor(2.8284) tensor(2.8284)\ntensor([4., 4.])\ntensor([2., 2.])\ntensor([[2., 2.],\n  [2., 2.]])\ntensor([[1.4142, 1.4142],\n  [1.4142, 1.4142]])<\/code><\/pre>\n<h3>2\u3001\u5747\u503cmean()\u3001\u7d2f\u52a0sum()\u3001\u6700\u5c0fmin()\u3001\u6700\u5927max()\u3001\u7d2f\u79efprod()<\/h3>\n<p>\u64cd\u4f5c\u9ed8\u8ba4\u4f1a\u5c06Tensor\u6253\u5e73\u540e\u53d6\u6700\u5927\u503c\u7d22\u5f15\u548c\u6700\u5c0f\u503c\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u5e0c\u671bTenosr\u6253\u5e73\uff0c\u800c\u662f\u6c42\u7ed9\u5b9a\u7ef4\u5ea6\u4e0a\u7684\u7d22\u5f15\uff0c\u9700\u8981\u6307\u5b9a\u5728\u54ea\u4e00\u4e2a\u7ef4\u5ea6\u4e0a\u6c42\u5747\u503cmean()\u3001\u7d2f\u52a0sum()\u3001\u6700\u5c0fmin()\u3001\u6700\u5927max()\u3001\u7d2f\u79efprod()\u3002<\/p>\n<pre><code class=\"language-python\">b = torch.arange(8).reshape(2, 4).float()\nprint(b)\n# \u5747\u503c,\u7d2f\u52a0,\u6700\u5c0f,\u6700\u5927,\u7d2f\u79ef\nprint(b.mean(), b.sum(), b.min(), b.max(), b.prod())\n# \u6253\u5e73\u540e\u7684\u6700\u5c0f\u6700\u5927\u503c\u7d22\u5f15\nprint(b.argmax(), b.argmin())<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">tensor([[0., 1., 2., 3.],\n        [4., 5., 6., 7.]])\ntensor(3.5000) tensor(28.) tensor(0.) tensor(7.) tensor(0.)\ntensor(7) tensor(0)\n<\/code><\/pre>\n<h3>3\u3001\u6700\u5c0f\u503c\u7d22\u5f15argmin()\u3001\u6700\u5927\u503c\u7d22\u5f15argmax()<\/h3>\n<p>\u64cd\u4f5c\u9ed8\u8ba4\u4f1a\u5c06Tensor\u6253\u5e73\u540e\u53d6\u6700\u5927\u503c\u7d22\u5f15\u548c\u6700\u5c0f\u503c\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u5e0c\u671bTenosr\u6253\u5e73\uff0c\u800c\u662f\u6c42\u7ed9\u5b9a\u7ef4\u5ea6\u4e0a\u7684\u7d22\u5f15\uff0c\u9700\u8981\u6307\u5b9a\u5728\u54ea\u4e00\u4e2a\u7ef4\u5ea6\u4e0a\u6c42\u6700\u5927\u503c\u7d22\u5f15\u6216\u6700\u5c0f\u503c\u7d22\u5f15\u3002<\/p>\n<pre><code class=\"language-python\">import torch\n\nb = torch.arange(8).reshape(2, 4).float()\nprint(&#039;b = &#039;, b)\n# \u6253\u5e73\u540e\u7684\u6700\u5c0f\u6700\u5927\u503c\u7d22\u5f15\nprint(&#039;\\nb.argmax() = {0}, b.argmin() = {1}&#039;.format(b.argmax(), b.argmin()))<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">b =  tensor([[0., 1., 2., 3.],\n        [4., 5., 6., 7.]])\nb.argmax() = 7, b.argmin() = 0<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u4e0a\u9762\u7684argmax\u3001argmin\u64cd\u4f5c\u9ed8\u8ba4\u4f1a\u5c06Tensor\u6253\u5e73\u540e\u53d6\u6700\u5927\u503c\u7d22\u5f15\u548c\u6700\u5c0f\u503c\u7d22\u5f15\uff0c\u5982\u679c\u4e0d\u5e0c\u671bTenosr\u6253\u5e73\uff0c\u800c\u662f\u6c42\u7ed9\u5b9a\u7ef4\u5ea6\u4e0a\u7684\u7d22\u5f15\uff0c\u9700\u8981\u6307\u5b9a\u5728\u54ea\u4e00\u4e2a\u7ef4\u5ea6\u4e0a\u6c42\u6700\u5927\u503c\u7d22\u5f15\u6216\u6700\u5c0f\u503c\u7d22\u5f15\u3002<br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019143512494.png\" alt=\"image-20231019143512494\" \/><br \/>\n\u6bd4\u5982\uff0c\u6709shape=[4, 10]\u7684Tensor\uff0c\u8868\u793a4\u5f20\u56fe\u7247\u572810\u5206\u7c7b\u7684\u6982\u7387\u7ed3\u679c\uff0c\u6211\u4eec\u9700\u8981\u77e5\u9053\u6bcf\u5f20\u56fe\u7247\u7684\u6700\u53ef\u80fd\u7684\u5206\u7c7b\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">a = torch.rand(4, 10)\nprint(a)\n# \u5728\u7b2c\u4e8c\u7ef4\u5ea6\u4e0a\u6c42\u6700\u5927\u503c\u7d22\u5f15\nprint(a.argmax(dim=1))<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">tensor([[0.0711, 0.5641, 0.7945, 0.6964, 0.3609, 0.5817, 0.1705, 0.6913, 0.1263,\n   0.8346],\n  [0.0810, 0.0771, 0.1983, 0.0344, 0.1067, 0.9591, 0.8515, 0.3046, 0.0491,\n   0.1291],\n  [0.3527, 0.2676, 0.9859, 0.2656, 0.1985, 0.3759, 0.8221, 0.3571, 0.5340,\n   0.7759],\n  [0.0969, 0.3954, 0.5478, 0.3543, 0.8253, 0.9291, 0.4960, 0.4390, 0.3780,\n   0.5858]])\ntensor([9, 5, 2, 5])<\/code><\/pre>\n<h3>3\u3001\u76f4\u63a5\u4f7f\u7528max\u548cmin\u914d\u5408dim\u53c2\u6570\u4e5f\u53ef\u4ee5\u83b7\u5f97\u6700\u503c\u7d22\u5f15\uff0c\u540c\u65f6\u5f97\u5230\u6700\u503c\u7684\u5177\u4f53\u503c\uff1a<\/h3>\n<pre><code class=\"language-python\">print(c.max(dim=1))\n1<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">(tensor([0.9589, 1.7394, 1.3448, 2.2079]), tensor([2, 2, 5, 7]))<\/code><\/pre>\n<h3>4\u3001\u4f7f\u7528keepdim=True<\/h3>\n<p>\u4f7f\u7528keepdim=True\u53ef\u4ee5\u4fdd\u6301\u5e94\u6709\u7684dim\uff0c\u5373\u4ec5\u4ec5\u662f\u5c06\u6c42\u6700\u503c\u7684\u90a3\u4e2adim\u7684size\u53d8\u6210\u4e861\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u4e0e\u539fTensor\u7ef4\u5ea6\u4e00\u81f4\u3002<\/p>\n<pre><code class=\"language-python\">print(c.argmax(dim=1, keepdim=True))\nprint(c.max(dim=1, keepdim=True))<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">tensor([[2],\n  [2],\n  [5],\n  [7]])\n\n(tensor([[0.9589],\n  [1.7394],\n  [1.3448],\n  [2.2079]]), tensor([[2],\n  [2],\n  [5],\n  [7]]))<\/code><\/pre>\n<h3>5\u3001\u53d6\u524dk\u5927\/\u524dk\u5c0f\/\u7b2ck\u5c0f\u7684\u6982\u7387\u503c\u53ca\u5176\u7d22\u5f15\uff1atopk\u3001kthvalue<\/h3>\n<p>\u4f7f\u7528topk\u4ee3\u66ffmax\u53ef\u4ee5\u5b8c\u6210\u66f4\u7075\u6d3b\u7684\u9700\u6c42\uff0c\u6709\u65f6\u5019\u4e0d\u662f\u4ec5\u4ec5\u8981\u6982\u7387\u6700\u5927\u7684\u90a3\u4e00\u4e2a\uff0c\u800c\u662f\u6982\u7387\u6700\u5927\u7684k\u4e2a\u3002\u5982\u679c\u4e0d\u662f\u6c42\u6700\u5927\u7684k\u4e2a\uff0c\u800c\u662f\u6c42\u6700\u5c0f\u7684k\u4e2a\uff0c\u53ea\u8981\u4f7f\u7528\u53c2\u6570largest=False\uff0ckthvalue\u8fd8\u53ef\u4ee5\u53d6\u7b2ck\u5c0f\u7684\u6982\u7387\u503c\u53ca\u5176\u7d22\u5f15\u3002<\/p>\n<pre><code class=\"language-python\"># 2\u4e2a\u6837\u672c,\u5206\u4e3a10\u4e2a\u7c7b\u522b\u7684\u7f6e\u4fe1\u5ea6\nd = torch.randn(2, 10) \n# \u6700\u5927\u6982\u7387\u76843\u4e2a\u7c7b\u522b\nprint(d.topk(3, dim=1)) \n# \u6700\u5c0f\u6982\u7387\u76843\u4e2a\u7c7b\u522b\nprint(d.topk(3, dim=1, largest=False)) \n# \u6c42\u7b2c8\u5c0f\u6982\u7387\u7684\u7c7b\u522b(\u4e00\u517110\u4e2a\u90a3\u5c31\u662f\u7b2c3\u5927)\nprint(d.kthvalue(8, dim=1)) <\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">(tensor([[2.0692, 1.6490, 0.9526], [1.5983, 1.5737, 1.5532]]), tensor([[6, 3, 5], [8, 1, 2]]))\n(tensor([[-1.0023, -0.6423, 0.0655], [-1.2959, -1.1504, -0.9859]]), tensor([[4, 0, 2], [0, 5, 3]]))\n(tensor([0.9526, 1.5532]), tensor([5, 2]))<\/code><\/pre>\n<h3>6\u3001\u6bd4\u8f83\u64cd\u4f5c\uff1agt\u3001eq<\/h3>\n<pre><code class=\"language-python\">import torch\n\na = torch.randn(2, 3)\nb = torch.randn(2, 3)\nprint(a)\nprint(b)\n# \u6bd4\u8f83\u662f\u5426\u5927\u4e8e0\uff0c\u662f\u5bf9\u5e94\u4f4d\u7f6e\u8fd4\u56de1\uff0c\u5426\u5bf9\u5e94\u4f4d\u7f6e\u8fd4\u56de0\uff0c\u6ce8\u610f\u5f97\u5230\u7684\u662fByteTensor\nprint(a &gt; 0) \nprint(torch.gt(a, 0))\n# \u662f\u5426\u4e0d\u7b49\u4e8e0\uff0c\u662f\u5bf9\u5e94\u4f4d\u7f6e\u8fd4\u56de1\uff0c\u5426\u5bf9\u5e94\u4f4d\u7f6e\u8fd4\u56de0\nprint(a != 0)\n# \u6bd4\u8f83\u6bcf\u4e2a\u4f4d\u7f6e\u662f\u5426\u76f8\u7b49\uff0c\u662f\u5bf9\u5e94\u4f4d\u7f6e\u8fd4\u56de1\uff0c\u5426\u5bf9\u5e94\u4f4d\u7f6e\u8fd4\u56de0\nprint(torch.eq(a, b)) \n# \u6bd4\u8f83\u6bcf\u4e2a\u4f4d\u7f6e\u662f\u5426\u76f8\u7b49\uff0c\u5168\u90e8\u76f8\u7b49\u65f6\u624d\u8fd4\u56deTrue\nprint(torch.equal(a, b), torch.equal(a, a)) <\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">tensor([[-0.1425, -1.1142, 0.2224],\n  [ 0.6142, 1.7455, -1.1776]])\ntensor([[-0.0774, -1.1012, -0.4862],\n  [-0.3110, -0.2110, 0.0381]])\ntensor([[0, 0, 1],\n  [1, 1, 0]], dtype=torch.uint8)\ntensor([[0, 0, 1],\n  [1, 1, 0]], dtype=torch.uint8)\ntensor([[1, 1, 1],\n  [1, 1, 1]], dtype=torch.uint8)\ntensor([[0, 0, 0],\n  [0, 0, 0]], dtype=torch.uint8)\nFalse True<\/code><\/pre>\n<h2>\u4e94\u3001Tensor\/\u5f20\u91cf\u7684\u9ad8\u9636\u64cd\u4f5c<\/h2>\n<h3>1\u3001\u6761\u4ef6\u9009\u53d6\uff1atorch.where(condition, x, y) \u2192 Tensor<\/h3>\n<ul>\n<li>\u8fd4\u56de\u4ece x \u6216 y \u4e2d\u9009\u62e9\u5143\u7d20\u540e\u5f62\u6210\u7684\u5f20\u91cf\/tensor\uff0c\u6bcf\u4e2a\u5143\u7d20\u65f6\u6765\u81eax\u8fd8\u662f\u6765\u81eay\u5219\u53d6\u51b3\u4e8e condition\u9009\u62e9\u5668\u4e2d\u6bcf\u4e2a\u5143\u7d20\u4f4d\u7f6e\u7684\u6761\u4ef6<br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/20210120120148299.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019143621739.png\" alt=\"image-20231019143621739\" \/><\/li>\n<\/ul>\n<pre><code class=\"language-python\">cond = torch.tensor([[0.6, 0.7], [0.3, 0.6]])\na = torch.tensor([[1., 1.], [1., 1.]])\nb = torch.tensor([[0., 0.], [0., 0.]])\nc = torch.where(cond &gt; 0.5, a, b) # \u6b64where\u5728GPU\u8fd0\u884c\uff0c\u9ad8\u5ea6\u5e76\u884c\u3002\u6b64\u65f6cond\u53ea\u67090\u548c1\u7684\u503c\nprint(c)<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">tensor([[1., 1.], [0., 1.]])<\/code><\/pre>\n<p>\u628a\u5f20\u91cf\u4e2d\u7684\u6bcf\u4e2a\u6570\u636e\u90fd\u4ee3\u5165\u6761\u4ef6\u4e2d\uff0c\u5982\u679c\u5176\u5927\u4e8e 0 \u5c31\u5f97\u51fa a\uff0c\u5176\u5b83\u60c5\u51b5\u5c31\u5f97\u51fa b\uff0c\u540c\u6837\u662f\u628a a \u548c b \u7684\u76f8\u540c\u4f4d\u7f6e\u7684\u6570\u636e\u5bfc\u51fa\u3002<\/p>\n<h3>2\u3001\u67e5\u8868\u641c\u96c6\uff1atorch.gather(input, dim, index, out=None) \u2192 Tensor<\/h3>\n<ul>\n<li>\n<p>\u76f8\u5f53\u4e8e\u67e5\u8868\u64cd\u4f5c<\/p>\n<\/li>\n<li>\n<p>\u6cbf\u7ed9\u5b9a\u8f74 dim\uff0c\u5c06\u8f93\u5165\u7d22\u5f15\u5f20\u91cf index \u6307\u5b9a\u4f4d\u7f6e\u7684\u503c\u8fdb\u884c\u805a\u5408<\/p>\n<\/li>\n<li>\n<p>\u5bf9\u4e00\u4e2a3\u7ef4\u5f20\u91cf\uff0c\u8f93\u51fa\u53ef\u4ee5\u5b9a\u4e49\u4e3a\uff1a<\/p>\n<ul>\n<li>\n<pre><code>out[i][j][k] = input[index[i][j][k]][j][k] # dim==0\nout[i][j][k] = input[i][index[i][j][k]][k] # dim==1\nout[i][j][k] = input[i][j][index[i][j][k]] # dim==2<\/code><\/pre>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019143729864.png\" alt=\"image-20231019143729864\" \/><\/p>\n<pre><code class=\"language-python\">prob = torch.randn(4, 10)\nidx = prob.topk(dim=1, k=3)  # prob\u5728\u7ef4\u5ea61\u4e2d\u524d\u4e09\u4e2a\u6700\u5927\u7684\u6570\uff0c\u4e00\u5171\u67094\u884c\uff0c\u8fd4\u56de\u503c\u548c\u5bf9\u5e94\u7684\u4e0b\u6807\nprint(&quot;all of topk idx: &quot;, idx)\nidx = idx[1]\nprint(&quot;idx[1]: &quot;, idx)\nlabel = torch.arange(10) + 100  # \u4e3e\u4e2a\u4f8b\u5b50\uff0c\u8fd9\u91cc\u7684\u5217\u8868\u8868\u793a\u4e3a\n# 0\u5bf9\u5e94\u4e8e100,1\u5bf9\u5e94\u4e8e101\uff0c\u4ee5\u6b64\u7c7b\u63a8\uff0c\u6839\u636e\u5b9e\u9645\u5e94\u7528\u4fee\u6539\nresult = torch.gather(label.expand(4, 10), dim=1, index=idx.long())  # lable\u76f8\u5f53\u4e8eone-hot\u7f16\u7801\uff0cindex\u8868\u793a\u7d22\u5f15\n# \u6362\u800c\u8a00\u662f\u662fy\u4e0ex\u7684\u51fd\u6570\u6620\u5c04\u5173\u7cfb\uff0cindex\u8868\u793ax\nprint(&quot;result:&quot;, result)<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">all of topk idx:  torch.return_types.topk(\nvalues=tensor([[0.7878, 0.2928, 0.2062],\n        [0.2524, 0.2094, 0.0350],\n        [1.5519, 0.8405, 0.7521],\n        [1.3380, 0.9290, 0.5655]]),\nindices=tensor([[2, 0, 8],\n        [9, 5, 6],\n        [1, 2, 0],\n        [3, 7, 8]]))\nidx[1]:  tensor([[2, 0, 8],\n        [9, 5, 6],\n        [1, 2, 0],\n        [3, 7, 8]])\nresult: tensor([[102, 100, 108],\n        [109, 105, 106],\n        [101, 102, 100],\n        [103, 107, 108]])<\/code><\/pre>\n<p>\u628a label \u6269\u5c55\u4e3a\u4e8c\u7ef4\u6570\u636e\u540e\uff0c\u4ee5 index \u4e2d\u7684\u6bcf\u4e2a\u6570\u636e\u4e3a\u7d22\u5f15\uff0c\u53d6\u51fa\u5728 label \u4e2d\u7d22\u5f15\u4f4d\u7f6e\u7684\u6570\u636e\uff0c\u518d\u4ee5 index \u7684\u7684\u4f4d\u7f6e\u6446\u653e\u3002<\/p>\n<p>\u6bd4\u5982\uff0c\u6700\u540e\u5f97\u51fa\u7684\u7ed3\u679c\u4e2d\uff0c\u7b2c\u4e00\u884c\u7684 105 \u5c31\u662f label.expand(4, 10) \u4e2d\u7b2c\u4e00\u884c\u4e2d\u7d22\u5f15\u4e3a 5 \u7684\u6570\u636e\uff0c\u63d0\u53d6\u51fa\u6765\u540e\u653e\u5728 5 \u6240\u5728\u7684\u4f4d\u7f6e\u3002<\/p>\n<h2>\u4e94\u3001\u6a21\u5757\u7c7b<\/h2>\n<pre><code class=\"language-python\">class SLNN(torch.nn.Module):\n    def __init__(self, N):\n        super(SLNN, self).__init__()\n        self.dense1 = torch.nn.Linear(N, N)\n        self.dense2 = torch.nn.Linear(N, N)\n        self.tanh = torch.tanh()\n\n    def forward(x):\n        out = self.dense1(x)\n        out = self.tanh(out)\n        out = self.dense2(out)<\/code><\/pre>\n<h2>\u516d\u3001\u68af\u5ea6\u6c42\u89e3\uff1a<\/h2>\n<h3>1\u3001\u65b9\u6cd5\u2460\uff1aAutograd<\/h3>\n<ul>\n<li>torch.autograd.grad(loss, [w1, w2,\u2026])<br \/>\n\u5b9a\u4e49\u53c2\u6570\u65f6\uff0c\u5982\u679c\u8be5\u53c2\u6570\u9700\u8981\u8fdb\u884c\u6c42\u5bfc\uff0c\u5219\u9700\u8981\u5728\u5b9a\u4e49\u53c2\u6570\u7684\u65f6\u5019\u6307\u5b9a\u5176\u9700\u8981\u8fdb\u884c\u6c42\u5bfc\uff0c\u5426\u5219\u62a5\u9519\u3002<\/li>\n<\/ul>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019143800080.png\" alt=\"image-20231019143800080\" \/><\/p>\n<pre><code class=\"language-python\">import torch\n\n# Autograd\n# \u5728Tensor\u4e0a\u7684\u6240\u6709\u64cd\u4f5c\uff0cautograd\u90fd\u80fd\u4e3a\u5b83\u4eec\u81ea\u52a8\u63d0\u4f9b\u5fae\u5206\n# \u4f7f\u5f97Tensor\u4f7f\u7528autograd\u529f\u80fd\uff0c\u53ea\u9700\u8981\u8bbe\u7f6etensor.requries_grad=True.\n# Variable\u6b63\u5f0f\u5408\u5e76\u5165Tensor, Variable\u672c\u6765\u5b9e\u73b0\u7684\u81ea\u52a8\u5fae\u5206\u529f\u80fd\uff0cTensor\u5c31\u80fd\u652f\u6301\n# Variable\u4e3b\u8981\u5305\u542b\u4e09\u4e2a\u5c5e\u6027:\n# data\uff1a\u4fdd\u5b58Variable\u6240\u5305\u542b\u7684Tensor\n# grad\uff1a\u4fdd\u5b58data\u5bf9\u5e94\u7684\u68af\u5ea6\uff0cgrad\u4e5f\u662f\u4e2aVariable\uff0c\u800c\u4e0d\u662fTensor\uff0c\u5b83\u548cdata\u7684\u5f62\u72b6\u4e00\u6837\u3002\n# grad_fn\uff1a\u6307\u5411\u4e00\u4e2aFunction\u5bf9\u8c61\uff0c\u8fd9\u4e2aFunction\u7528\u6765\u53cd\u5411\u4f20\u64ad\u8ba1\u7b97\u8f93\u5165\u7684\u68af\u5ea6\nx = torch.ones(2, 2, requires_grad=True)  # \u4e3atensor\u8bbe\u7f6e requires_grad \u6807\u8bc6\uff0c\u4ee3\u8868\u7740\u9700\u8981\u6c42\u5bfc\u6570\ny = x.sum()\ny.backward()  # \u53cd\u5411\u4f20\u64ad,\u8ba1\u7b97\u68af\u5ea6\nprint(&#039;x.grad =\\n&#039; , x.grad)  # tensor([[ 1.,  1.],[ 1.,  1.]])\nx.grad.data.zero_()  # grad\u5728\u53cd\u5411\u4f20\u64ad\u8fc7\u7a0b\u4e2d\u662f\u7d2f\u52a0\u7684\uff0c\u6bcf\u4e00\u6b21\u8fd0\u884c\u53cd\u5411\u4f20\u64ad\uff0c\u68af\u5ea6\u90fd\u4f1a\u7d2f\u52a0\u4e4b\u524d\u7684\u68af\u5ea6\uff0c\u6240\u4ee5\u53cd\u5411\u4f20\u64ad\u4e4b\u524d\u9700\u628a\u68af\u5ea6\u6e05\u96f6\u3002\nprint(&#039;\\nx.grad =\\n&#039; , x.grad)  # tensor([[ 0.,  0.],[ 0.,  0.]])<\/code><\/pre>\n<p>\u6253\u5370\u7ed3\u679c\uff1a<\/p>\n<pre><code class=\"language-python\">x.grad =\n tensor([[1., 1.],\n        [1., 1.]])\n\nx.grad =\n tensor([[0., 0.],\n        [0., 0.]])<\/code><\/pre>\n<h3>2\u3001\u65b9\u6cd5\u2461\uff1aBackward()<\/h3>\n<ul>\n<li>loss.backward()<br \/>\nbackward()\u51fd\u6570\u7528\u4e8e\u53cd\u5411\u6c42\u5bfc\u6570\uff0c\u4f7f\u7528\u94fe\u5f0f\u6cd5\u5219\u6c42\u5bfc\uff0c\u5f53\u81ea\u53d8\u91cf\u4e3a\u4e0d\u540c\u53d8\u91cf\u5f62\u5f0f\u65f6\uff0c\u6c42\u5bfc\u65b9\u5f0f\u548c\u7ed3\u679c\u6709\u53d8\u5316\u3002<\/li>\n<\/ul>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019143820603.png\" alt=\"image-20231019143820603\" \/><\/p>\n<h4>2.1 scalar\u6807\u91cf<\/h4>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/20210120103755216.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/20210120103802304.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<pre><code class=\"language-python\">import torch as t\nfrom torch.autograd import Variable\n\na = Variable(t.FloatTensor([2, 3]), requires_grad=True)    # \u8fd9\u91cc\u4e3a\u4e00\u7ef4\u6807\u91cf\nb = a + 3\nc = b * b * 3\nout = c.mean()\nout.backward()      \nprint(a.grad)       # tensor([15., 18.])<\/code><\/pre>\n<h4>2.2 Tensor\u5f20\u91cf<\/h4>\n<pre><code class=\"language-python\"># y1 = x1^2  y2 = x2^3\n# dy1\/dx1 | x1=2 = 2*x1 = 2*2 =4\n# dy2\/dx2 | x2=3 = 3*x2*x2 = 27\nm = Variable(t.FloatTensor([[2, 3]]), requires_grad=True)   # \u6ce8\u610f\u8fd9\u91cc\u6709\u4e24\u5c42\u62ec\u53f7\uff0c\u975e\u6807\u91cf\nn = Variable(t.zeros(1, 2))\nn[0, 0] = m[0, 0] ** 2\nn[0, 1] = m[0, 1] ** 3\n\nn.backward(t.Tensor([[1, 1]]),retain_graph=True)            # \u8fd9\u91cc[[1, 1]]\u4f5c\u4e3a\u68af\u5ea6\u7684\u7cfb\u6570\u770b\u5f85\nprint(m.grad)              <\/code><\/pre>\n<h4>2.3 \u94fe\u5f0f\u6c42\u5bfc<\/h4>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/20210120104006458.png\" alt=\"\u5728\u8fd9\u91cc\u63d2\u5165\u56fe\u7247\u63cf\u8ff0\" \/><\/p>\n<pre><code class=\"language-python\"># y = x*w\n# z = y + b\n# k.backward(p)\u63a5\u53d7\u7684\u53c2\u6570p\u5fc5\u987b\u8981\u548ck\u7684\u5927\u5c0f\u4e00\u6837\uff0cx.grad = p*dk\/dx\nw = Variable(t.randn(3), requires_grad=True)\nx = Variable(t.randn(3), requires_grad=True)\nb = Variable(t.randn(3), requires_grad=True)\ny = w + x\nz = y.dot(b)\ny.backward(b,retain_graph=True)\nprint(x.grad,w.grad,b)   # x.gard=w.gard=b```\n\n# \u635f\u5931\u51fd\u6570\u4e0e\u4f18\u5316\u5668\n\n```python\ncriterion = torch.nn.MSELoss(reduction=&#039;sum&#039;)       # \u5b9a\u4e49\u635f\u5931\u51fd\u6570\noptimizer = torch.optim.Adam(model_eign.parameters(), lr=1e-4)      # \u4f18\u5316\u5668<\/code><\/pre>\n<h2>\u516b\u3001\u8fed\u4ee3<\/h2>\n<pre><code class=\"language-python\">Epoch = 10000\nfor epoch in range(Epoch):\n    y_pred = model(x)\n\n    loss = criterion(y, y_pred)\n    if epoch % 100 == 99:\n        print(&#039;epoch[{}\/{}],loss:{:.6f}&#039;.format(epoch, Epoch, loss.item()))\n\n    optimizer.zero_grad()\n    loss.backward(retain_graph=True)\n    optimizer.step()<\/code><\/pre>\n<h2>\u4e5d\u3001\u753b\u56fe<\/h2>\n<pre><code class=\"language-python\">plt.plot(x.cpu(),y.cpu())           # \u753b\u56fe\u65f6\u9700\u8981\u4e34\u65f6\u8f6c\u5316\u53d8\u91cf\u5230cpu\u4e0a\nplt.show()<\/code><\/pre>\n<h2>\u5341\u3001\u5408\u5e76\u4e0e\u5206\u5272\uff1acat\u3001stack\u3001Split\u3001chunk<\/h2>\n<table>\n<thead>\n<tr>\n<th>\u65b9\u6cd5<\/th>\n<th>\u4f5c\u7528<\/th>\n<th>\u533a\u522b<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>cat<\/td>\n<td>\u5408\u5e76<\/td>\n<td>\u4fdd\u6301\u539f\u6709\u7ef4\u5ea6\u7684\u6570\u91cf<\/td>\n<\/tr>\n<tr>\n<td>stack<\/td>\n<td>\u5408\u5e76<\/td>\n<td>\u539f\u6709\u7ef4\u5ea6\u6570\u91cf\u52a01<\/td>\n<\/tr>\n<tr>\n<td>split<\/td>\n<td>\u5206\u5272<\/td>\n<td>\u6309\u7167\u957f\u5ea6\u53bb\u5206\u5272<\/td>\n<\/tr>\n<tr>\n<td>chunk<\/td>\n<td>\u5206\u5272<\/td>\n<td>\u7b49\u5206<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>1\u3001cat<\/h3>\n<ul>\n<li>cat\u662fconcatenate\uff08\u8fde\u63a5\uff09\u7684\u7f29\u5199\uff0c\u800c\u4e0d\u662f\u6307\uff08\u732b\uff09\u3002\u4f5c\u7528\u662f\u628a2\u4e2atensor\u6309\u7167\u7279\u5b9a\u7684\u7ef4\u5ea6\u8fde\u63a5\u8d77\u6765\u3002<\/li>\n<li>\u8981\u6c42\uff1a\u9664\u88ab\u62fc\u63a5\u7684\u7ef4\u5ea6\u5916\uff0c\u5176\u4ed6\u7ef4\u5ea6\u5fc5\u987b\u76f8\u540c<\/li>\n<li>\u6e90\u7801\u5b9a\u4e49\uff1atorch.cat(tensors,dim=0,out=None)\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570tensors\u662f\u4f60\u60f3\u8981\u8fde\u63a5\u7684\u82e5\u5e72\u4e2a\u5f20\u91cf\uff0c\u6309\u4f60\u6240\u4f20\u5165\u7684\u987a\u5e8f\u8fdb\u884c\u8fde\u63a5\uff0c\u6ce8\u610f\u6bcf\u4e00\u4e2a\u5f20\u91cf\u9700\u8981\u5f62\u72b6\u76f8\u540c\uff0c\u6216\u8005\u66f4\u51c6\u786e\u7684\u8bf4\uff0c\u8fdb\u884c\u884c\u8fde\u63a5\u7684\u5f20\u91cf\u8981\u6c42\u5217\u6570\u76f8\u540c\uff0c\u8fdb\u884c\u5217\u8fde\u63a5\u7684\u5f20\u91cf\u8981\u6c42\u884c\u6570\u76f8\u540c<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u53c2\u6570dim\u8868\u793a\u7ef4\u5ea6\uff0cdim=0\u5219\u8868\u793a\u6309\u884c\u8fde\u63a5\uff0cdim=1\u8868\u793a\u6309\u5217\u8fde\u63a5<br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019143908078.png\" alt=\"image-20231019143908078\" \/><br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019143920876.png\" alt=\"image-20231019143920876\" \/><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"language-python\">import torch\na=torch.randn(3,4) #\u968f\u673a\u751f\u6210\u4e00\u4e2ashape\uff083\uff0c4\uff09\u7684tensort\nb=torch.randn(2,4) #\u968f\u673a\u751f\u6210\u4e00\u4e2ashape\uff082\uff0c4\uff09\u7684tensor\n\ntorch.cat([a,b],dim=0) \n#\u8fd4\u56de\u4e00\u4e2ashape\uff085\uff0c4\uff09\u7684tensor\n#\u628aa\u548cb\u62fc\u63a5\u6210\u4e00\u4e2ashape\uff085\uff0c4\uff09\u7684tensor\uff0c\n#\u53ef\u7406\u89e3\u4e3a\u6cbf\u7740\u884c\u589e\u52a0\u7684\u65b9\u5411\uff08\u5373\u7eb5\u5411\uff09\u62fc\u63a5<\/code><\/pre>\n<h3>2\u3001stack<\/h3>\n<ul>\n<li>stack\u4f1a\u589e\u52a0\u4e00\u4e2a\u65b0\u7684\u7ef4\u5ea6\uff0c\u6765\u8868\u793a\u62fc\u63a5\u540e\u76842\u4e2atensor\uff0c\u76f4\u89c2\u4e9b\u7406\u89e3\u7684\u8bdd\uff0c\u54b1\u4eec\u4e0d\u59a8\u628a\u4e00\u4e2a2\u7ef4\u7684tensor\u7406\u89e3\u6210\u4e00\u5f20\u957f\u65b9\u5f62\u7684\u7eb8\u5f20\uff0ccat\u76f8\u5f53\u4e8e\u662f\u628a\u4e24\u5f20\u7eb8\u7f1d\u5408\u5728\u4e00\u8d77\uff0c\u5f62\u6210\u4e00\u5f20\u66f4\u5927\u7684\u7eb8\uff0c\u800cstack\u76f8\u5f53\u4e8e\u662f\u628a\u4e24\u5f20\u7eb8\u4e0a\u4e0b\u5806\u53e0\u5728\u4e00\u8d77\u3002<\/li>\n<li>\u8981\u6c42\uff1a\u4e24\u4e2atensor\u62fc\u63a5\u524d\u7684\u5f62\u72b6\u5b8c\u5168\u4e00\u81f4<br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019143939077.png\" alt=\"image-20231019143939077\" \/><\/li>\n<\/ul>\n<pre><code class=\"language-python\">a=torch.randn(3,4)\nb=torch.randn(3,4)\n\nc=torch.stack([a,b],dim=0)\n#\u8fd4\u56de\u4e00\u4e2ashape(2,3,4)\u7684tensor,\u65b0\u589e\u7684\u7ef4\u5ea62\u5206\u522b\u6307\u5411a\u548cb\n\nd=torch.stack([a,b],dim=1)\n#\u8fd4\u56de\u4e00\u4e2ashape\uff083,2,4\uff09\u7684tensor\uff0c\u65b0\u589e\u7684\u7ef4\u5ea62\u5206\u522b\u6307\u5411\u76f8\u5e94\u7684a\u7684\u7b2ci\u884c\u548cb\u7684\u7b2ci\u884c<\/code><\/pre>\n<ul>\n<li>\u8fd9\u91cc\u7684\u5173\u952e\u8bcd\u53c2\u6570dim\u7684\u7406\u89e3\u548ccat\u65b9\u6cd5\u4e2d\u6709\u4e9b\u533a\u522b\u3002\n<ul>\n<li>cat\u65b9\u6cd5\u4e2d\u53ef\u4ee5\u7406\u89e3\u4e3a\u539ftensor\u7684\u7ef4\u5ea6\uff0cdim=0\uff0c\u5c31\u662f\u6cbf\u7740\u539f\u6765\u76840\u8f74\u8fdb\u884c\u62fc\u63a5\uff0cdim=1\uff0c\u5c31\u662f\u6cbf\u7740\u539f\u6765\u76841\u8f74\u8fdb\u884c\u62fc\u63a5\u3002<\/li>\n<li>stack\u65b9\u6cd5\u4e2d\u7684dim\u5219\u662f\u6307\u5411\u65b0\u589e\u7ef4\u5ea6\u7684\u4f4d\u7f6e\uff0cdim=0\uff0c\u5c31\u662f\u5728\u65b0\u5f62\u6210\u7684tensor\u7684\u7ef4\u5ea6\u7684\u7b2c0\u4e2a\u4f4d\u7f6e\u65b0\u63d2\u5165\u7ef4\u5ea6<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3>3\u3001split<\/h3>\n<ul>\n<li>split\u662f\u6839\u636e\u957f\u5ea6\u53bb\u62c6\u5206tensor<\/li>\n<li>\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u8bf4\u662ftorch.chunk\uff08\uff09\u51fd\u6570\u7684\u5347\u7ea7\u7248\u672c\uff0c\u5b83\u4e0d\u4ec5\u53ef\u4ee5\u6309\u4efd\u6570\u5747\u5300\u5206\u5272\uff0c\u8fd8\u53ef\u4ee5\u6309\u7279\u5b9a\u65b9\u6848\u8fdb\u884c\u5206\u5272\u3002<\/li>\n<li>\u6e90\u7801\u5b9a\u4e49\uff1atorch.split(tensor,split_size_or_sections,dim=0)\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5f85\u5206\u5272\u5f20\u91cf<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6709\u4e24\u79cd\u5f62\u5f0f\u3002<\/li>\n<li>\u4e00\u79cd\u662f\u5206\u5272\u4efd\u6570\uff0c\u8fd9\u5c31\u548ctorch.chunk()\u4e00\u6837\u4e86\u3002<\/li>\n<li>\u7b2c\u4e8c\u79cd\u8fd9\u662f\u5206\u5272\u65b9\u6848\uff0c\u8fd9\u662f\u4e00\u4e2alist\uff0c\u5f85\u5206\u5272\u5f20\u91cf\u5c06\u4f1a\u5206\u5272\u4e3alen\uff08list\uff09\u4efd\uff0c\u6bcf\u4e00\u4efd\u7684\u5927\u5c0f\u53d6\u51b3\u4e8elist\u4e2d\u7684\u5143\u7d20<\/li>\n<li>\u7b2c\u4e09\u4e2a\u53c2\u6570\u4e3a\u5206\u5272\u7ef4\u5ea6<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019144000752.png\" alt=\"image-20231019144000752\" \/><\/p>\n<pre><code class=\"language-python\">a=torch.randn(3,4)\n\na.split([1,2],dim=0)\n#\u628a\u7ef4\u5ea60\u6309\u7167\u957f\u5ea6[1,2]\u62c6\u5206\uff0c\u5f62\u62102\u4e2atensor\uff0c\n#shape\uff081\uff0c4\uff09\u548cshape\uff082\uff0c4\uff09\n\na.split([2,2],dim=1)\n#\u628a\u7ef4\u5ea61\u6309\u7167\u957f\u5ea6[2,2]\u62c6\u5206\uff0c\u5f62\u62102\u4e2atensor\uff0c\n#shape\uff083\uff0c2\uff09\u548cshape\uff083\uff0c2\uff09<\/code><\/pre>\n<h3>4\u3001chunk<\/h3>\n<ul>\n<li>chunk\u53ef\u4ee5\u7406\u89e3\u4e3a\u5747\u7b49\u5206\u7684split\uff0c\u4f46\u662f\u5f53\u7ef4\u5ea6\u957f\u5ea6\u4e0d\u80fd\u88ab\u7b49\u5206\u4efd\u6570\u6574\u9664\u65f6\uff0c\u867d\u7136\u4e0d\u4f1a\u62a5\u9519\uff0c\u4f46\u53ef\u80fd\u7ed3\u679c\u4e0e\u9884\u671f\u7684\u4e0d\u4e00\u6837\uff0c\u5efa\u8bae\u53ea\u5728\u53ef\u4ee5\u88ab\u6574\u9664\u7684\u60c5\u51b5\u4e0b\u8fd0\u7528<\/li>\n<li>torch.chunk\uff08\uff09\u7684\u4f5c\u7528\u662f\u628a\u4e00\u4e2atensor\u5747\u5300\u5206\u5272\u6210\u82e5\u5e72\u4e2a\u5c0ftensor<\/li>\n<li>\u6e90\u7801\u5b9a\u4e49:torch.chunk(intput,chunks,dim=0)\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570input\u662f\u4f60\u60f3\u8981\u5206\u5272\u7684tensor<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u53c2\u6570chunks\u662f\u4f60\u60f3\u5747\u5300\u5206\u5272\u7684\u4efd\u6570\uff0c\u5982\u679c\u8be5tensor\u5728\u4f60\u8981\u8fdb\u884c\u5206\u5272\u7684\u7ef4\u5ea6\u4e0a\u7684size\u4e0d\u80fd\u88abchunks\u6574\u9664\uff0c\u5219\u6700\u540e\u4e00\u4efd\u4f1a\u7565\u5c0f\uff08\u4e5f\u53ef\u80fd\u4e3a\u7a7a\uff09<\/li>\n<li>\u7b2c\u4e09\u4e2a\u53c2\u6570\u8868\u793a\u5206\u5272\u7ef4\u5ea6\uff0cdim=0\u6309\u884c\u5206\u5272\uff0cdim=1\u8868\u793a\u6309\u5217\u5206\u5272<\/li>\n<li>\u8be5\u51fd\u6570\u8fd4\u56de\u7531\u5c0ftensor\u7ec4\u6210\u7684list<br \/>\n<img src=\"https:\/\/lemon-guess.oss-cn-hangzhou.aliyuncs.com\/img\/image-20231019144018099.png\" alt=\"image-20231019144018099\" \/><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<pre><code class=\"language-python\">a=torch.randn(4,6)\n\na.chunk(2,dim=0)\n#\u8fd4\u56de\u4e00\u4e2ashape\uff082\uff0c6\uff09\u7684tensor\na.chunk(2,dim=1)\n#\u8fd4\u56de\u4e00\u4e2ashape\uff084\uff0c3\uff09\u7684tensor\n123456<\/code><\/pre>\n<hr \/>\n<hr \/>\n<hr \/>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<br \/>\n<a href=\"https:\/\/www.jianshu.com\/p\/b024dfb00108\">Pytorch \u5e38\u7528\u8bed\u6cd5<\/a><br \/>\n<a href=\"https:\/\/www.cnblogs.com\/huxiaozhouzhou\/archive\/2004\/01\/13\/10611393.html\">Pytorch\u5b66\u4e60\u7b14\u8bb0\uff08\u4e00\uff09---- \u57fa\u7840\u8bed\u6cd5<\/a><br \/>\n<a href=\"https:\/\/www.jianshu.com\/p\/4e57dbe1d281\">Pytorch:Tensor\u7684\u5408\u5e76\u4e0e\u5206\u5272<\/a><br \/>\n<a href=\"https:\/\/www.cnblogs.com\/moon3\/p\/12685911.html\">pytorch\u4e2dtorch.cat(),torch.chunk(),torch.split()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5<\/a><br \/>\n<a href=\"https:\/\/www.jb51.net\/article\/177464.htm\">Pytorch Tensor\u57fa\u672c\u6570\u5b66\u8fd0\u7b97\u8be6\u89e3<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/qq_26369907\/article\/details\/89010672\">pytorch Tensor\u53ca\u5176\u57fa\u672c\u64cd\u4f5c<\/a><br \/>\n<a href=\"https:\/\/www.yht7.com\/news\/14296\">Pytorch Tensor\u7684\u7edf\u8ba1\u5c5e\u6027\u5b9e\u4f8b\u8bb2\u89e3<\/a><br \/>\n<a href=\"https:\/\/blog.csdn.net\/Thera_qing\/article\/details\/95647923\">pytorch\u5b66\u4e60\u7b14\u8bb0\uff08\u4e94\uff09\u2013tensor\u7684\u9ad8\u9636\u64cd\u4f5c<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e38\u7528\u6a21\u5757\u4ee5\u53ca\u8bbe\u7f6e import torch import numpy as np from matplotlib import pypl   \u2026 &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0},"categories":[216],"tags":[],"_links":{"self":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9026"}],"collection":[{"href":"\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9026"}],"version-history":[{"count":1,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9026\/revisions"}],"predecessor-version":[{"id":9027,"href":"\/index.php?rest_route=\/wp\/v2\/posts\/9026\/revisions\/9027"}],"wp:attachment":[{"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9026"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9026"},{"taxonomy":"post_tag","embeddable":true,"href":"\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9026"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}