{"id":304190,"date":"2024-05-20T19:16:56","date_gmt":"2024-05-20T11:16:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/304190.html"},"modified":"2024-05-20T19:17:18","modified_gmt":"2024-05-20T11:17:18","slug":"torch-stack-%e4%b8%8e-torch-cat-%e7%9a%84%e5%8c%ba%e5%88%ab%e6%98%af%e4%bb%80%e4%b9%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/304190.html","title":{"rendered":"torch.stack \u4e0e  torch.cat \u7684\u533a\u522b\u662f\u4ec0\u4e48"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26220424\/29fb63d6-23c4-4d19-9083-2947e2caa95b.webp\" alt=\"torch.stack \u4e0e  torch.cat \u7684\u533a\u522b\u662f\u4ec0\u4e48\" \/><\/p>\n<p><p><strong>torch.stack<\/strong> \u4e0e <strong>torch.cat<\/strong> \u662fPyTorch\u4e2d\u7528\u4e8e\u5f20\u91cf\uff08Tensors\uff09\u62fc\u63a5\u7684\u4e24\u4e2a\u4e0d\u540c\u51fd\u6570\u3002\u4e8c\u8005\u7684\u4e3b\u8981\u533a\u522b\u5728\u4e8e\uff1a<strong>torch.stack<\/strong>\u7528\u4e8e\u5728\u65b0\u7684\u7ef4\u5ea6\u4e0a\u8fde\u63a5\u4e00\u7cfb\u5217\u76f8\u540c\u5f62\u72b6\u7684\u5f20\u91cf\u3001\u751f\u6210\u66f4\u9ad8\u7ef4\u7684\u5f20\u91cf\uff0c\u800c<strong>torch.cat<\/strong>\u5219\u7528\u4e8e\u5728\u73b0\u6709\u7684\u7ef4\u5ea6\u4e0a\u8fde\u63a5\u5f20\u91cf\u3001\u4e0d\u589e\u52a0\u989d\u5916\u7ef4\u5ea6\u3002\u5177\u4f53\u6765\u8bf4\uff0c<strong>torch.stack<\/strong>\u4f1a\u589e\u52a0\u4e00\u4e2a\u65b0\u7684\u7ef4\u5ea6\u8fdb\u884c\u5806\u53e0\uff0c\u6240\u4ee5\u53c2\u4e0e\u5806\u53e0\u7684\u5404\u5f20\u91cf\u5f62\u72b6\u5fc5\u987b\u5b8c\u5168\u76f8\u540c\uff1b\u76f8\u6bd4\u4e4b\u4e0b\uff0c<strong>torch.cat<\/strong>\u5728\u62fc\u63a5\u7684\u7ef4\u5ea6\u4e0a\u4e0d\u8981\u6c42\u5176\u4ed6\u7ef4\u5ea6\u7684\u957f\u5ea6\u76f8\u540c\u3001\u4f46\u8981\u6c42\u9664\u4e86\u62fc\u63a5\u7684\u7ef4\u5ea6\u4ee5\u5916\u7684\u5176\u4ed6\u7ef4\u5ea6\u5fc5\u987b\u7b26\u5408\u5bf9\u5e94\u7684\u5f62\u72b6\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u8be6\u7ec6\u63cf\u8ff0<strong>torch.stack<\/strong>\u7684\u4f7f\u7528\u573a\u666f\uff1a\u5047\u8bbe\u4f60\u6709\u4e00\u7cfb\u5217\u5f62\u72b6\u76f8\u540c\u76842D\u5f20\u91cf\uff0c\u4e5f\u5c31\u662f\u77e9\u9635\uff0c\u4f60\u60f3\u5728\u4e00\u4e2a\u65b0\u7684\u7ef4\u5ea6\u4e0a\u5c06\u5b83\u4eec\u5408\u5e76\u6210\u4e00\u4e2a3D\u5f20\u91cf\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u4f7f\u7528<strong>torch.stack<\/strong>\u5c31\u53ef\u4ee5\u5b9e\u73b0\u4f60\u7684\u76ee\u6807\uff0c\u4f60\u5c06\u83b7\u5f97\u4e00\u4e2a3D\u5f20\u91cf\uff0c\u5176\u4e2d\u65b0\u7684\u7ef4\u5ea6\u7684\u5927\u5c0f\u7b49\u4e8e\u88abstack\u7684\u77e9\u9635\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001STACK AND CAT IN PYTORCH<\/h2>\n<\/p>\n<p><h3>\u5f20\u91cf\u62fc\u63a5\u7684\u6982\u5ff5<\/h3>\n<\/p>\n<p><p>\u5728\u6df1\u5ea6\u5b66\u4e60\u548c\u5f20\u91cf\u8ba1\u7b97\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u5c06\u591a\u4e2a\u5f20\u91cf\u5408\u5e76\u4e3a\u4e00\u4e2a\u66f4\u5927\u7684\u5f20\u91cf\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u7684\u64cd\u4f5c\u5b8c\u6210\uff0c\u6700\u5e38\u89c1\u7684\u662fstacking\uff08\u5806\u53e0\uff09\u548cconcatenation\uff08\u62fc\u63a5\uff09\u3002\u5c3d\u7ba1\u8fd9\u4e24\u79cd\u64cd\u4f5c\u6709\u65f6\u53ef\u4ee5\u8fbe\u5230\u76f8\u4f3c\u7684\u6548\u679c\uff0c\u4f46\u5b83\u4eec\u5728\u7ec6\u8282\u4e0a\u6709\u7740\u6839\u672c\u7684\u4e0d\u540c\u3002<\/p>\n<\/p>\n<p><h3>TORCH.STACK\u4f7f\u7528\u8bf4\u660e<\/h3>\n<\/p>\n<p><p><strong>torch.stack<\/strong>\u7684API\u5b9a\u4e49\u5982\u4e0b\uff1a<code>torch.stack(tensors, dim=0, out=None)<\/code>\uff0c\u5176\u4e2d<code>tensors<\/code>\u662f\u4e00\u4e2a\u5f20\u91cf\u5e8f\u5217\u7684\u5217\u8868\u6216\u5143\u7ec4\uff0c<code>dim<\/code>\u662f\u5728\u54ea\u4e2a\u7ef4\u5ea6\u4e0a\u8fdb\u884cstack\u64cd\u4f5c\uff0c\u9ed8\u8ba4\u503c\u4e3a0\u8868\u793a\u65b0\u7ef4\u5ea6\u63d2\u5165\u4e8e\u539f\u6709\u7ef4\u5ea6\u4e4b\u524d\u3002<code>out<\/code>\u662f\u7ed3\u679c\u5f20\u91cf\u7684\u8f93\u51fa\u3002\u6240\u6709\u8f93\u5165\u5f20\u91cf\u5fc5\u987b\u5177\u6709\u76f8\u540c\u7684\u5f62\u72b6\u3002<\/p>\n<\/p>\n<p><h3>TORCH.CAT\u4f7f\u7528\u8bf4\u660e<\/h3>\n<\/p>\n<p><p>\u800c<strong>torch.cat<\/strong>\u7684API\u5b9a\u4e49\u4e3a\uff1a<code>torch.cat(tensors, dim=0, out=None)<\/code>\u3002\u8fd9\u91cc\u4e5f\u8981\u63d0\u4f9b\u4e00\u4e2a\u5f20\u91cf\u5e8f\u5217\uff0c\u5e76\u4e14\u8fd8\u9700\u8981\u6307\u5b9a\u6cbf\u54ea\u4e2a\u7ef4\u5ea6\u8fde\u63a5\u3002\u4e0e<strong>torch.stack<\/strong>\u4e0d\u540c\uff0c\u62fc\u63a5\u64cd\u4f5c\u4e0d\u4f1a\u521b\u5efa\u65b0\u7684\u7ef4\u5ea6\uff0c\u800c\u662f\u5728\u73b0\u6709\u7ef4\u5ea6\u4e0a\u62d3\u5c55\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001FUNCTIONAL DIFFERENCES<\/h2>\n<\/p>\n<p><h3>DIMENSIONALITY EFFECTS<\/h3>\n<\/p>\n<p><p>\u5f53\u4f7f\u7528<strong>torch.stack<\/strong>\u65f6\uff0c\u5047\u8bbe\u8f93\u5165\u5f20\u91cf\u7684\u5f62\u72b6\u662f\uff08A, B\uff09\uff0c\u5982\u679c\u5728\u7b2c0\u7ef4\u5ea6\u4e0a\u5806\u53e0N\u4e2a\u8fd9\u6837\u5f62\u72b6\u7684\u5f20\u91cf\uff0c\u5219\u8f93\u51fa\u5f20\u91cf\u7684\u5f62\u72b6\u5c06\u662f\uff08N, A, B\uff09\u2014\u2014\u65b0\u589e\u4e86\u4e00\u4e2a\u7ef4\u5ea6\u3002\u800c\u5bf9\u4e8e<strong>torch.cat<\/strong>\uff0c\u5982\u679c\u6cbf\u7740\u7b2c0\u7ef4\u5ea6\u62fc\u63a5\u8fd9\u4e9b\u5f20\u91cf\uff0c\u5219\u8f93\u51fa\u5f62\u72b6\u5c06\u662f\uff08N*A, B\uff09\u2014\u2014\u6cbf\u7740\u62fc\u63a5\u7684\u7ef4\u5ea6\u6269\u5c55\u4e86\u5f62\u72b6\uff0c\u5176\u4ed6\u7ef4\u5ea6\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<\/p>\n<p><h3>SIZE REQUIREMENTS<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e<strong>torch.stack<\/strong>\u6765\u8bf4\uff0c\u6240\u6709\u88ab\u5806\u53e0\u7684\u5f20\u91cf\u5f62\u72b6\u5fc5\u987b\u5b8c\u5168\u76f8\u540c\u3002\u800c<strong>torch.cat<\/strong>\u5219\u5bf9\u62fc\u63a5\u7ef4\u5ea6\u4e4b\u5916\u7684\u5176\u4ed6\u7ef4\u5ea6\u7684\u957f\u5ea6\u6709\u8981\u6c42\uff0c\u5b83\u53ea\u9700\u8981\u8fd9\u4e9b\u975e\u62fc\u63a5\u7ef4\u5ea6\u7684\u957f\u5ea6\u662f\u4e00\u6837\u7684\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001PRACTICAL EXAMPLES<\/h2>\n<\/p>\n<p><h3>STACKING TENSORS<\/h3>\n<\/p>\n<p><p>\u4e3e\u4f8b\u6765\u8bf4\uff0c\u5728\u5904\u7406\u591a\u5f20\u56fe\u50cf\u6570\u636e\u65f6\uff0c\u5982\u679c\u9700\u8981\u5c06\u591a\u4e2a\u5355\u901a\u9053\u56fe\u50cf\uff08\u5f62\u72b6\u4e3a[H, W]\uff09\u5806\u53e0\u6210\u4e00\u4e2a\u65b0\u7684\u591a\u901a\u9053\u56fe\u50cf\u5f20\u91cf\uff08\u5f62\u72b6\u4e3a[N, H, W]\uff09\uff0c<strong>torch.stack<\/strong>\u662f\u7406\u60f3\u7684\u9009\u62e9\u3002\u5f20\u91cf\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a2D\u5f20\u91cf\u5c06\u6210\u4e3a\u7ed3\u679c3D\u5f20\u91cf\u4e2d\u7684\u4e00\u4e2a\u201c\u5c42\u201d\u3002<\/p>\n<\/p>\n<p><h3>CONCATENATING TENSORS<\/h3>\n<\/p>\n<p><p>\u76f8\u53cd\uff0c\u5982\u679c\u4f60\u60f3\u5c06\u4e24\u5e45\u56fe\u7247\u7684\u50cf\u7d20\u884c\u62fc\u63a5\u5230\u4e00\u8d77\uff0c\u5176\u4e2d\u6bcf\u5e45\u56fe\u7247\u5f62\u72b6\u4e3a[2, 3]\uff0c\u62fc\u63a5\u540e\u5f97\u5230\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u5f62\u72b6\u4e3a[4, 3]\u7684\u5f20\u91cf\uff0c<strong>torch.cat<\/strong>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u5de5\u4f5c\u5f97\u5f88\u597d\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001WHEN TO USE WHICH<\/h2>\n<\/p>\n<p><h3>CHOOSING BASED ON THE TASK<\/h3>\n<\/p>\n<p><p>\u9009\u62e9\u4f7f\u7528<strong>torch.stack<\/strong>\u8fd8\u662f<strong>torch.cat<\/strong>\u4f9d\u8d56\u4e8e\u5177\u4f53\u7684\u4efb\u52a1\u9700\u6c42\u3002<strong>torch.stack<\/strong>\u4e3a\u4e86\u6784\u5efa\u7ef4\u5ea6\u8f83\u9ad8\u7684\u5f20\u91cf\u800c\u751f\uff0c\u4f8b\u5982\u5c06\u591a\u4e2a\u6837\u672c\u7ec4\u5408\u6210\u4e00\u4e2a\u6279\u6b21(batch)\uff0c\u6216\u8005\u5c06\u4e0d\u540c\u7279\u5f81\u7684\u63cf\u8ff0\u5408\u5e76\u5230\u66f4\u9ad8\u7ef4\u5ea6\u7684\u8868\u793a\u4e2d\u3002\u800c<strong>torch.cat<\/strong>\u5728\u62fc\u63a5\u5e8f\u5217\u6216\u6269\u5c55\u73b0\u6709\u6570\u636e\u65f6\u66f4\u4e3a\u9002\u7528\uff0c\u4f8b\u5982\u5728\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u5206\u6790\u6216\u662f\u5c06\u4e24\u4e2a\u4e0d\u540c\u7684\u4fe1\u606f\u6e90\u5728\u7279\u5f88\u7ea7\u4e0a\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><h3>CONSIDERING GRADIENTS AND COMPUTATION GRAPH<\/h3>\n<\/p>\n<p><p>\u5728\u795e\u7ecf\u7f51\u7edc\u7684\u53cd\u5411\u4f20\u64ad\u4e2d\uff0c\u8fde\u63a5\u64cd\u4f5c\u4f1a\u5f71\u54cd\u68af\u5ea6\u7684\u6d41\u52a8\u3002\u5fc5\u987b\u6ce8\u610f\uff0c\u65e0\u8bba\u662f\u4f7f\u7528<strong>torch.stack<\/strong>\u8fd8\u662f<strong>torch.cat<\/strong>\uff0c\u5408\u5e76\u64cd\u4f5c\u540e\u7684\u5f20\u91cf\u5728\u8ba1\u7b97\u56fe\u4e2d\u7684\u4f4d\u7f6e\u662f\u4e0d\u53ef\u9006\u7684\uff0c\u4e14\u5f71\u54cd\u7740\u68af\u5ea6\u7684\u56de\u4f20\u3002\u56e0\u6b64\uff0c\u5b83\u4eec\u5728\u6784\u5efa\u590d\u6742\u7f51\u7edc\u7ed3\u6784\u4e2d\u626e\u6f14\u7740\u91cd\u8981\u89d2\u8272\uff0c\u5e76\u4e14\u6709\u65f6\u66ff\u6362\u4f7f\u7528\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e0d\u540c\u7684\u5b66\u4e60\u884c\u4e3a\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001PERFORMANCE CONSIDERATIONS<\/h2>\n<\/p>\n<p><h3>MEMORY ALLOCATION<\/h3>\n<\/p>\n<p><p>\u5728\u4f7f\u7528<strong>torch.stack<\/strong>\u65f6\uff0c\u7531\u4e8e\u521b\u5efa\u65b0\u7684\u7ef4\u5ea6\uff0c\u901a\u5e38\u9700\u8981\u66f4\u591a\u7684\u5185\u5b58\u5206\u914d\u3002\u76f8\u6bd4\u4e4b\u4e0b\uff0c<strong>torch.cat<\/strong>\u53ef\u80fd\u4f1a\u66f4\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5728\u8fdb\u884c\u5927\u89c4\u6a21\u5f20\u91cf\u64cd\u4f5c\u65f6\u3002\u7136\u800c\uff0c\u6027\u80fd\u4e5f\u53d7\u5230\u5e95\u5c42\u786c\u4ef6\u548c\u5e76\u884c\u8ba1\u7b97\u80fd\u529b\u7684\u5f71\u54cd\u3002<\/p>\n<\/p>\n<p><h3>SPEED OF EXECUTION<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c<strong>torch.cat<\/strong>\u7684\u6267\u884c\u901f\u5ea6\u53ef\u80fd\u4f1a\u7565\u5feb\u4e8e<strong>torch.stack<\/strong>\uff0c\u56e0\u4e3a\u540e\u8005\u9700\u8981\u5904\u7406\u66f4\u591a\u7684\u7d22\u5f15\u548c\u7ef4\u5ea6\u64cd\u4f5c\u3002\u4f46\u662f\uff0c\u8fd9\u79cd\u5dee\u5f02\u901a\u5e38\u5f88\u5c0f\uff0c\u771f\u6b63\u7684\u74f6\u9888\u5f80\u5f80\u5728\u5176\u4ed6\u8ba1\u7b97\u5bc6\u96c6\u578b\u64cd\u4f5c\u4e0a\u3002<\/p>\n<\/p>\n<p><h2>\u516d\u3001BEST PRACTICES<\/h2>\n<\/p>\n<p><h3>CONSISTENCY IS KEY<\/h3>\n<\/p>\n<p><p>\u5728\u9879\u76ee\u6216\u56e2\u961f\u4e2d\uff0c\u4fdd\u6301\u4ee3\u7801\u7684\u4e00\u81f4\u6027\u662f\u91cd\u8981\u7684\u3002\u8981\u786e\u4fdd\u5728\u5408\u9002\u7684\u4e0a\u4e0b\u6587\u4e2d\u4f7f\u7528<strong>torch.stack<\/strong>\u548c<strong>torch.cat<\/strong>\u3002\u5236\u5b9a\u56e2\u961f\u5185\u90e8\u7684\u6807\u51c6\u5e76\u9075\u5faa\u53ef\u4ee5\u51cf\u5c11\u6df7\u6dc6\u5e76\u63d0\u5347\u4ee3\u7801\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><h3>UNDERSTANDING THE DATA STRUCTURE<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u521d\u5b66\u8005\u6765\u8bf4\uff0c\u7406\u89e3\u6570\u636e\u7ed3\u6784\u548c\u6bcf\u4e2a\u64cd\u4f5c\u7684\u5f71\u54cd\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u5728\u5b9e\u65bd\u4efb\u4f55stack\u6216cat\u64cd\u4f5c\u4e4b\u524d\uff0c\u6700\u597d\u660e\u786e\u77e5\u9053\u6bcf\u4e2a\u7ef4\u5ea6\u7684\u610f\u4e49\u4ee5\u53ca\u9884\u671f\u7684\u8f93\u51fa\u5f62\u72b6\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7\u6253\u5370\u5f20\u91cf\u7684\u5f62\u72b6\u6216\u5728\u7eb8\u4e0a\u753b\u51fa\u7ef4\u5ea6\u53d8\u6362\u6765\u8f85\u52a9\u7406\u89e3\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8be6\u7ec6\u5730\u7406\u89e3<strong>torch.stack<\/strong>\u548c<strong>torch.cat<\/strong>\u7684\u4e0d\u540c\u70b9\u4ee5\u53ca\u5982\u4f55\u6839\u636e\u4e0d\u540c\u573a\u666f\u9009\u62e9\u5b83\u4eec\uff0c\u4f60\u5c06\u80fd\u591f\u66f4\u6709\u6548\u5730\u5904\u7406\u548c\u7ec4\u7ec7\u4f60\u7684\u5f20\u91cf\u6570\u636e\uff0c\u4ece\u800c\u4e3a\u4f60\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u6216\u6570\u636e\u5206\u6790\u9879\u76ee\u63d0\u4f9b\u575a\u5b9e\u7684\u57fa\u7840\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. torch.stack\u4e0etorch.cat\u6709\u4ec0\u4e48\u4e0d\u540c\uff1f<\/strong><\/p>\n<p>torch.stack\u548ctorch.cat\u90fd\u662fPyTorch\u5e93\u4e2d\u7528\u4e8e\u5408\u5e76\u5f20\u91cf\u7684\u51fd\u6570\uff0c\u4f46\u5b83\u4eec\u6709\u4e00\u4e9b\u5173\u952e\u533a\u522b\u3002<\/p>\n<p>torch.cat\u662f\u6cbf\u7740\u6307\u5b9a\u7ef4\u5ea6\u5c06\u591a\u4e2a\u5f20\u91cf\u8fdb\u884c\u62fc\u63a5\uff0c\u8fd4\u56de\u7684\u5f20\u91cf\u7ef4\u5ea6\u5927\u5c0f\u4e3a\u8be5\u6307\u5b9a\u7ef4\u5ea6\u4e0a\u62fc\u63a5\u540e\u7684\u603b\u5927\u5c0f\u3002\u800ctorch.stack\u662f\u5728\u65b0\u7684\u7ef4\u5ea6\u4e0a\u5806\u53e0\u591a\u4e2a\u5f20\u91cf\uff0c\u8fd4\u56de\u7684\u5f20\u91cf\u7ef4\u5ea6\u4f1a\u589e\u52a0\u4e00\u4e2a\u65b0\u7684\u7ef4\u5ea6\u3002<\/p>\n<p><strong>2. \u662f\u5426\u53ef\u4ee5\u7528torch.stack\u4ee3\u66fftorch.cat\uff1f<\/strong><\/p>\n<p>\u867d\u7136torch.stack\u548ctorch.cat\u90fd\u53ef\u4ee5\u7528\u4e8e\u5408\u5e76\u5f20\u91cf\uff0c\u4f46\u6839\u636e\u4f60\u7684\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u51fd\u6570\u662f\u5f88\u91cd\u8981\u7684\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5e0c\u671b\u5728\u65b0\u7684\u7ef4\u5ea6\u4e0a\u5806\u53e0\u5f20\u91cf\uff0c\u5c06\u5b83\u4eec\u53d8\u6210\u65b0\u7684\u7ec4\uff08batch\uff09\u7684\u4e00\u90e8\u5206\uff0c\u90a3\u4e48torch.stack\u662f\u66f4\u9002\u5408\u7684\u9009\u62e9\u3002\u800c\u5982\u679c\u4f60\u53ea\u662f\u7b80\u5355\u5730\u60f3\u5728\u73b0\u6709\u7ef4\u5ea6\u4e0a\u62fc\u63a5\u5f20\u91cf\uff0c\u90a3\u4e48torch.cat\u53ef\u80fd\u66f4\u5408\u9002\u3002<\/p>\n<p><strong>3. torch.stack\u548ctorch.cat\u5728\u6027\u80fd\u65b9\u9762\u6709\u5dee\u5f02\u5417\uff1f<\/strong><\/p>\n<p>\u5728\u6027\u80fd\u65b9\u9762\uff0ctorch.stack\u548ctorch.cat\u53ef\u80fd\u4f1a\u6709\u4e00\u4e9b\u5dee\u5f02\uff0c\u4f46\u901a\u5e38\u60c5\u51b5\u4e0b\u8fd9\u4e9b\u5dee\u5f02\u662f\u5fae\u4e0d\u8db3\u9053\u7684\u3002<\/p>\n<p>\u7531\u4e8etorch.stack\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u7ef4\u5ea6\u5e76\u590d\u5236\u6570\u636e\uff0c\u56e0\u6b64\u5b83\u53ef\u80fd\u7a0d\u5fae\u6162\u4e00\u4e9b\u3002\u800ctorch.cat\u53ea\u9700\u5728\u6307\u5b9a\u7ef4\u5ea6\u4e0a\u62fc\u63a5\u6570\u636e\uff0c\u56e0\u6b64\u5b83\u53ef\u80fd\u7a0d\u5fae\u5feb\u4e00\u4e9b\u3002<\/p>\n<p>\u7136\u800c\uff0c\u8fd9\u4e9b\u5dee\u5f02\u901a\u5e38\u53ea\u5728\u5927\u89c4\u6a21\u6570\u636e\u64cd\u4f5c\u65f6\u624d\u4f1a\u6709\u6240\u4f53\u73b0\u3002\u5728\u4e00\u822c\u60c5\u51b5\u4e0b\uff0c\u4e24\u8005\u7684\u6027\u80fd\u5dee\u5f02\u53ef\u4ee5\u5ffd\u7565\u4e0d\u8ba1\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u9700\u6c42\u9009\u62e9\u66f4\u5408\u9002\u7684\u51fd\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"torch.stack \u4e0e torch.cat \u662fPyTorch\u4e2d\u7528\u4e8e\u5f20\u91cf\uff08Tensors\uff09\u62fc\u63a5\u7684\u4e24\u4e2a\u4e0d\u540c\u51fd\u6570 [&hellip;]","protected":false},"author":3,"featured_media":304224,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/304190"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=304190"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/304190\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/304224"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=304190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=304190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=304190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}