{"id":2471,"date":"2025-05-12T17:08:36","date_gmt":"2025-05-12T09:08:36","guid":{"rendered":"https:\/\/thereisno.top\/?p=2471"},"modified":"2025-05-12T17:08:36","modified_gmt":"2025-05-12T09:08:36","slug":"torch-%e5%bc%a0%e9%87%8f","status":"publish","type":"post","link":"https:\/\/thereisno.top\/?p=2471","title":{"rendered":"torch \u5f20\u91cf"},"content":{"rendered":"\n<p>\u5f20\u91cf\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e0e\u6570\u7ec4\u548c\u77e9\u9635\u975e\u5e38\u76f8\u4f3c\u3002\u5728 PyTorch \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5f20\u91cf\u6765\u7f16\u7801\u6a21\u578b\u7684\u8f93\u5165\u548c\u8f93\u51fa\uff0c\u4ee5\u53ca\u6a21\u578b\u7684\u53c2\u6570\u3002<\/p>\n\n\n\n<p>\u5f20\u91cf\u7c7b\u4f3c\u4e8e&nbsp;<a href=\"https:\/\/numpy.com.cn\/\">NumPy \u7684<\/a>&nbsp;ndarray\uff0c\u4e0d\u540c\u4e4b\u5904\u5728\u4e8e\u5f20\u91cf\u53ef\u4ee5\u5728 GPU \u6216\u5176\u4ed6\u786c\u4ef6\u52a0\u901f\u5668\u4e0a\u8fd0\u884c\u3002\u5b9e\u9645\u4e0a\uff0c\u5f20\u91cf\u548c NumPy \u6570\u7ec4\u901a\u5e38\u53ef\u4ee5\u5171\u4eab\u5e95\u5c42\u5185\u5b58\uff0c\u4ece\u800c\u65e0\u9700\u590d\u5236\u6570\u636e\uff08\u8be6\u89c1<a href=\"https:\/\/pytorch.ac.cn\/tutorials\/beginner\/blitz\/tensor_tutorial.html#bridge-to-np-label\">\u4e0e NumPy \u7684\u6865\u63a5<\/a>\uff09\u3002\u5f20\u91cf\u8fd8\u9488\u5bf9\u81ea\u52a8\u5fae\u5206\u8fdb\u884c\u4e86\u4f18\u5316\uff08\u6211\u4eec\u5c06\u5728\u540e\u9762\u7684&nbsp;<a href=\"https:\/\/pytorch.ac.cn\/tutorials\/beginner\/basics\/autogradqs_tutorial.html\">Autograd<\/a>&nbsp;\u90e8\u5206\u8be6\u7ec6\u4ecb\u7ecd\uff09\u3002\u5982\u679c\u60a8\u719f\u6089 ndarrays\uff0c\u60a8\u4f1a\u5f88\u5feb\u9002\u5e94 Tensor API\u3002\u5982\u679c\u4e0d\u719f\u6089\uff0c\u8bf7\u7ee7\u7eed\u9605\u8bfb\uff01<\/p>\n\n\n\n<p><strong>import <\/strong><strong>torch<\/strong><br><strong>import <\/strong><strong>numpy <\/strong><strong>as <\/strong><strong>np<\/strong><\/p>\n\n\n\n<p>\u521d\u59cb\u5316\u5f20\u91cf<\/p>\n\n\n\n<p>\u5f20\u91cf\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u8fdb\u884c\u521d\u59cb\u5316\u3002\u8bf7\u770b\u4ee5\u4e0b\u793a\u4f8b<\/p>\n\n\n\n<p><strong>\u76f4\u63a5\u4ece\u6570\u636e\u521b\u5efa<\/strong><\/p>\n\n\n\n<p>\u5f20\u91cf\u53ef\u4ee5\u76f4\u63a5\u4ece\u6570\u636e\u521b\u5efa\u3002\u6570\u636e\u7c7b\u578b\u4f1a\u81ea\u52a8\u63a8\u65ad\u3002<\/p>\n\n\n\n<p><strong>data = [[<\/strong>1<strong>, <\/strong>2<strong>],[<\/strong>3<strong>, <\/strong>4<strong>]]<\/strong><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>x_data<\/strong><\/a><strong> = <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.tensor.html#torch.tensor\"><strong>torch.tensor<\/strong><\/a><strong>(data)<\/strong><\/p>\n\n\n\n<p><strong>\u4ece NumPy \u6570\u7ec4\u521b\u5efa<\/strong><\/p>\n\n\n\n<p>\u5f20\u91cf\u53ef\u4ee5\u4ece NumPy \u6570\u7ec4\u521b\u5efa\uff08\u53cd\u4e4b\u4ea6\u7136 &#8211; \u8be6\u89c1<a href=\"https:\/\/pytorch.ac.cn\/tutorials\/beginner\/blitz\/tensor_tutorial.html#bridge-to-np-label\">\u4e0e NumPy \u7684\u6865\u63a5<\/a>\uff09\u3002<\/p>\n\n\n\n<p><strong>np_array = np.array(data)<\/strong><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>x_np<\/strong><\/a><strong> = <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.from_numpy.html#torch.from_numpy\"><strong>torch.from_numpy<\/strong><\/a><strong>(np_array)<\/strong><\/p>\n\n\n\n<!--more-->\n\n\n\n<p><strong>\u4ece\u53e6\u4e00\u4e2a\u5f20\u91cf\u521b\u5efa<\/strong><\/p>\n\n\n\n<p>\u65b0\u5f20\u91cf\u4f1a\u4fdd\u7559\u53c2\u6570\u5f20\u91cf\u7684\u5c5e\u6027\uff08\u5f62\u72b6\u3001\u6570\u636e\u7c7b\u578b\uff09\uff0c\u9664\u975e\u663e\u5f0f\u8986\u76d6\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>x_ones<\/strong><\/a> <strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.ones_like.html#torch.ones_like\"><strong>torch.ones_like<\/strong><\/a><strong>(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>x_data<\/strong><\/a><strong>) <\/strong><em># retains the properties of x_data<\/em><br>print<strong>(<\/strong>f&#8221;Ones Tensor: <strong>\\n <\/strong><em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>x_ones<\/strong><\/a><em>} <\/em><strong>\\n<\/strong>&#8220;<strong>)<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>x_rand<\/strong><\/a> <strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.rand_like.html#torch.rand_like\"><strong>torch.rand_like<\/strong><\/a><strong>(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>x_data<\/strong><\/a><strong>, dtype=<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensor_attributes.html#torch.dtype\"><strong>torch.float<\/strong><\/a><strong>) <\/strong><em># overrides the datatype of x_data<\/em><br>print<strong>(<\/strong>f&#8221;Random Tensor: <strong>\\n <\/strong><em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>x_rand<\/strong><\/a><em>} <\/em><strong>\\n<\/strong>&#8220;<strong>)<\/strong><\/p>\n\n\n\n<p>Ones Tensor:<br>&nbsp;tensor([[1, 1],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1, 1]])<\/p>\n\n\n\n<p>Random Tensor:<br>&nbsp;tensor([[0.8823, 0.9150],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [0.3829, 0.9593]])<\/p>\n\n\n\n<p><strong>\u4f7f\u7528\u968f\u673a\u503c\u6216\u5e38\u91cf\u503c\u521b\u5efa<\/strong><\/p>\n\n\n\n<p>shape&nbsp;\u662f\u4e00\u4e2a\u5f20\u91cf\u7ef4\u5ea6\u7684\u5143\u7ec4\u3002\u5728\u4e0b\u9762\u7684\u51fd\u6570\u4e2d\uff0c\u5b83\u51b3\u5b9a\u4e86\u8f93\u51fa\u5f20\u91cf\u7684\u7ef4\u5ea6\u3002<\/p>\n\n\n\n<p><strong>shape = (<\/strong>2<strong>,<\/strong>3<strong>,)<\/strong><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>rand_tensor<\/strong><\/a><strong> = <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.rand.html#torch.rand\"><strong>torch.rand<\/strong><\/a><strong>(shape)<\/strong><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>ones_tensor<\/strong><\/a><strong> = <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.ones.html#torch.ones\"><strong>torch.ones<\/strong><\/a><strong>(shape)<\/strong><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>zeros_tensor<\/strong><\/a><strong> = <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.zeros.html#torch.zeros\"><strong>torch.zeros<\/strong><\/a><strong>(shape)<\/strong><\/p>\n\n\n\n<p>print<strong>(<\/strong>f&#8221;Random Tensor: <strong>\\n <\/strong><em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>rand_tensor<\/strong><\/a><em>} <\/em><strong>\\n<\/strong>&#8220;<strong>)<\/strong><br>print<strong>(<\/strong>f&#8221;Ones Tensor: <strong>\\n <\/strong><em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>ones_tensor<\/strong><\/a><em>} <\/em><strong>\\n<\/strong>&#8220;<strong>)<\/strong><br>print<strong>(<\/strong>f&#8221;Zeros Tensor: <strong>\\n <\/strong><em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>zeros_tensor<\/strong><\/a><em>}<\/em>&#8220;<strong>)<\/strong><\/p>\n\n\n\n<p>Random Tensor:<br>&nbsp;tensor([[0.3904, 0.6009, 0.2566],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [0.7936, 0.9408, 0.1332]])<\/p>\n\n\n\n<p>Ones Tensor:<br>&nbsp;tensor([[1., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 1., 1.]])<\/p>\n\n\n\n<p>Zeros Tensor:<br>&nbsp;tensor([[0., 0., 0.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [0., 0., 0.]])<\/p>\n\n\n\n<p>\u5f20\u91cf\u7684\u5c5e\u6027<\/p>\n\n\n\n<p>\u5f20\u91cf\u5c5e\u6027\u63cf\u8ff0\u4e86\u5b83\u4eec\u7684\u5f62\u72b6\u3001\u6570\u636e\u7c7b\u578b\u4ee5\u53ca\u5b58\u50a8\u5b83\u4eec\u7684\u8bbe\u5907\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a> <strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.rand.html#torch.rand\"><strong>torch.rand<\/strong><\/a><strong>(<\/strong>3<strong>,<\/strong>4<strong>)<\/strong><\/p>\n\n\n\n<p>print<strong>(<\/strong>f&#8221;Shape of tensor: <em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/size.html#torch.Size\"><strong>tensor.shape<\/strong><\/a><em>}<\/em>&#8220;<strong>)<\/strong><br>print<strong>(<\/strong>f&#8221;Datatype of tensor: <em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensor_attributes.html#torch.dtype\"><strong>tensor.dtype<\/strong><\/a><em>}<\/em>&#8220;<strong>)<\/strong><br>print<strong>(<\/strong>f&#8221;Device tensor is stored on: <em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensor_attributes.html#torch.device\"><strong>tensor.device<\/strong><\/a><em>}<\/em>&#8220;<strong>)<\/strong><\/p>\n\n\n\n<p>Shape of tensor: torch.Size([3, 4])<br>Datatype of tensor: torch.float32<br>Device tensor is stored on: cpu<\/p>\n\n\n\n<p>\u5f20\u91cf\u64cd\u4f5c<\/p>\n\n\n\n<p>\u8d85\u8fc7 1200 \u79cd\u5f20\u91cf\u64cd\u4f5c\uff0c\u5305\u62ec\u7b97\u672f\u3001\u7ebf\u6027\u4ee3\u6570\u3001\u77e9\u9635\u64cd\u4f5c\uff08\u8f6c\u7f6e\u3001\u7d22\u5f15\u3001\u5207\u7247\uff09\u3001\u91c7\u6837\u7b49\u7b49\uff0c\u90fd<a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/torch.html\">\u5728\u6b64\u5904<\/a>\u8fdb\u884c\u4e86\u5168\u9762\u63cf\u8ff0\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e9b\u64cd\u4f5c\u90fd\u53ef\u4ee5\u5728 CPU \u548c&nbsp;<a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/torch.html#accelerators\">\u52a0\u901f\u5668<\/a>\u4e0a\u8fd0\u884c\uff0c\u4f8b\u5982 CUDA\u3001MPS\u3001MTIA \u6216 XPU\u3002\u5982\u679c\u60a8\u4f7f\u7528 Colab\uff0c\u53ef\u4ee5\u901a\u8fc7\u524d\u5f80\u201c\u8fd0\u884c\u65f6\u201d&gt;\u201c\u66f4\u6539\u8fd0\u884c\u65f6\u7c7b\u578b\u201d&gt;\u201cGPU\u201d\u6765\u5206\u914d\u4e00\u4e2a\u52a0\u901f\u5668\u3002<\/p>\n\n\n\n<p>\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5f20\u91cf\u5728 CPU \u4e0a\u521b\u5efa\u3002\u6211\u4eec\u9700\u8981\u4f7f\u7528&nbsp;.to&nbsp;\u65b9\u6cd5\uff08\u5728\u68c0\u67e5\u52a0\u901f\u5668\u53ef\u7528\u6027\u540e\uff09\u5c06\u5f20\u91cf\u663e\u5f0f\u79fb\u52a8\u5230\u52a0\u901f\u5668\u4e0a\u3002\u8bf7\u8bb0\u4f4f\uff0c\u5728\u8bbe\u5907\u4e4b\u95f4\u590d\u5236\u5927\u578b\u5f20\u91cf\u53ef\u80fd\u4f1a\u6d88\u8017\u5927\u91cf\u65f6\u95f4\u548c\u5185\u5b58\uff01<\/p>\n\n\n\n<p><em># We move our tensor to the current accelerator if available<\/em><br><strong>if <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.accelerator.is_available.html#torch.accelerator.is_available\"><strong>torch.accelerator.is_available<\/strong><\/a><strong>():<\/strong><br>&nbsp;&nbsp;&nbsp; <a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><em> <\/em><strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>.to(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.accelerator.current_accelerator.html#torch.accelerator.current_accelerator\"><strong>torch.accelerator.current_accelerator<\/strong><\/a><strong>())<\/strong><\/p>\n\n\n\n<p>\u5c1d\u8bd5\u5217\u8868\u4e2d\u7684\u4e00\u4e9b\u64cd\u4f5c\u3002\u5982\u679c\u60a8\u719f\u6089 NumPy API\uff0c\u60a8\u4f1a\u53d1\u73b0 Tensor API \u975e\u5e38\u6613\u4e8e\u4f7f\u7528\u3002<\/p>\n\n\n\n<p><strong>\u6807\u51c6\u7684 NumPy \u5f0f\u7d22\u5f15\u548c\u5207\u7247<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a> <strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.ones.html#torch.ones\"><strong>torch.ones<\/strong><\/a><strong>(<\/strong>4<strong>, <\/strong>4<strong>)<\/strong><br>print<strong>(<\/strong>f&#8221;First row: <em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>[<\/strong>0<strong>]<\/strong><em>}<\/em>&#8220;<strong>)<\/strong><br>print<strong>(<\/strong>f&#8221;First column: <em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>[:, <\/strong>0<strong>]<\/strong><em>}<\/em>&#8220;<strong>)<\/strong><br>print<strong>(<\/strong>f&#8221;Last column: <em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>[&#8230;, &#8211;<\/strong>1<strong>]<\/strong><em>}<\/em>&#8220;<strong>)<\/strong><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>[:,<\/strong>1<strong>] = <\/strong>0<br>print<strong>(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>)<\/strong><\/p>\n\n\n\n<p>First row: tensor([1., 1., 1., 1.])<br>First column: tensor([1., 1., 1., 1.])<br>Last column: tensor([1., 1., 1., 1.])<br>tensor([[1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1.]])<\/p>\n\n\n\n<p><strong>\u8fde\u63a5\u5f20\u91cf<\/strong>\u00a0\u60a8\u53ef\u4ee5\u4f7f\u7528\u00a0torch.cat\u00a0\u6cbf\u7740\u7ed9\u5b9a\u7ef4\u5ea6\u8fde\u63a5\u4e00\u7cfb\u5217\u5f20\u91cf\uff08<mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6cbf\u7740\u5750\u6807\u8f74\u62fc\u63a5<\/mark>\uff09\u3002\u53e6\u8bf7\u53c2\u9605\u00a0<a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.stack.html\">torch.stack<\/a>\uff0c\u8fd9\u662f\u53e6\u4e00\u4e2a\u5f20\u91cf\u8fde\u63a5\u64cd\u4f5c\uff0c\u4e0e\u00a0torch.cat\u00a0\u6709\u5fae\u5999\u7684\u533a\u522b\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>t1<\/strong><\/a> <strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.cat.html#torch.cat\"><strong>torch.cat<\/strong><\/a><strong>([<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>, <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>, <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>], dim=<\/strong>1<strong>)<\/strong><br>print<strong>(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>t1<\/strong><\/a><strong>)<\/strong><\/p>\n\n\n\n<p>tensor([[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.]])<\/p>\n\n\n\n<p><strong>\u7b97\u672f\u8fd0\u7b97<\/strong><\/p>\n\n\n\n<p><em># This computes the matrix multiplication between two tensors. y1, y2, y3 will have the same value<\/em><br><em># &#8220;tensor.T&#8220; returns the transpose of a tensor&nbsp; <\/em><em>@\u77e9\u9635\u4e58\u79ef<\/em><em> *<\/em><em>\u54c8\u8fbe\u739b\u79ef<\/em><em>\u5bf9\u5e94\u5143\u7d20\u76f8\u4e58<\/em><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>y1<\/strong><\/a><em> <\/em><strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><em> <\/em><strong>@ <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor.T<\/strong><\/a><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>y2<\/strong><\/a><em> <\/em><strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>.matmul(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor.T<\/strong><\/a><strong>)<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>y3<\/strong><\/a> <strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.rand_like.html#torch.rand_like\"><strong>torch.rand_like<\/strong><\/a><strong>(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>y1<\/strong><\/a><strong>)<\/strong><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.matmul.html#torch.matmul\"><strong>torch.matmul<\/strong><\/a><strong>(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>, <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor.T<\/strong><\/a><strong>, out=<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>y3<\/strong><\/a><strong>)<\/strong><\/p>\n\n\n\n<p><em># This computes the element-wise product. z1, z2, z3 will have the same value<\/em><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>z1<\/strong><\/a><em> <\/em><strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><em> <\/em><strong>* <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>z2<\/strong><\/a><em> <\/em><strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>.mul(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>)<\/strong><\/p>\n\n\n\n<p><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>z3<\/strong><\/a> <strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.rand_like.html#torch.rand_like\"><strong>torch.rand_like<\/strong><\/a><strong>(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>)<\/strong><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.mul.html#torch.mul\"><strong>torch.mul<\/strong><\/a><strong>(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>, <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>, out=<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>z3<\/strong><\/a><strong>)<\/strong><\/p>\n\n\n\n<p>tensor([[1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1.]])<\/p>\n\n\n\n<p><strong>\u5355\u5143\u7d20\u5f20\u91cf<\/strong>&nbsp;\u5982\u679c\u60a8\u6709\u4e00\u4e2a\u5355\u5143\u7d20\u5f20\u91cf\uff0c\u4f8b\u5982\u901a\u8fc7\u5c06\u5f20\u91cf\u7684\u6240\u6709\u503c\u805a\u5408\u5230\u4e00\u4e2a\u503c\u4e2d\u83b7\u5f97\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528&nbsp;item()&nbsp;\u5c06\u5176\u8f6c\u6362\u4e3a Python \u6570\u503c\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>agg<\/strong><\/a> <strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>.sum()<\/strong><br><strong>agg_item = <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>agg<\/strong><\/a><strong>.item()<\/strong><br>print<strong>(agg_item, <\/strong>type<strong>(agg_item))<\/strong><\/p>\n\n\n\n<p>12.0 &lt;class &#8216;float&#8217;&gt;<\/p>\n\n\n\n<p><strong>\u5c31\u5730\u64cd\u4f5c<\/strong>&nbsp;\u5c06\u7ed3\u679c\u5b58\u50a8\u5230\u64cd\u4f5c\u6570\u4e2d\u7684\u64cd\u4f5c\u79f0\u4e3a\u5c31\u5730\u64cd\u4f5c\u3002\u5b83\u4eec\u4ee5&nbsp;_&nbsp;\u540e\u7f00\u8868\u793a\u3002\u4f8b\u5982\uff1ax.copy_(y)\u3001x.t_()&nbsp;\u4f1a\u6539\u53d8&nbsp;x\u3002<\/p>\n\n\n\n<p>print<strong>(<\/strong>f&#8221;<em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><em>} <\/em><strong>\\n<\/strong>&#8220;<strong>)<\/strong><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>.add_(<\/strong>5<strong>)<\/strong><br>print<strong>(<\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>tensor<\/strong><\/a><strong>)<\/strong><\/p>\n\n\n\n<p>tensor([[1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [1., 0., 1., 1.]])<\/p>\n\n\n\n<p>tensor([[6., 5., 6., 6.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [6., 5., 6., 6.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [6., 5., 6., 6.],<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [6., 5., 6., 6.]])<\/p>\n\n\n\n<p>\u6ce8\u610f<\/p>\n\n\n\n<p>\u5c31\u5730\u64cd\u4f5c\u8282\u7701\u4e86\u4e00\u4e9b\u5185\u5b58\uff0c\u4f46\u5728\u8ba1\u7b97\u5bfc\u6570\u65f6\u53ef\u80fd\u4f1a\u6709\u95ee\u9898\uff0c\u56e0\u4e3a\u4f1a\u7acb\u5373\u4e22\u5931\u5386\u53f2\u8bb0\u5f55\u3002\u56e0\u6b64\uff0c\u4e0d\u5efa\u8bae\u4f7f\u7528\u5b83\u4eec\u3002<\/p>\n\n\n\n<p>\u4e0e NumPy \u7684\u6865\u63a5<\/p>\n\n\n\n<p><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">CPU \u4e0a\u7684\u5f20\u91cf\u548c NumPy \u6570\u7ec4\u53ef\u4ee5\u5171\u4eab\u5176\u5e95\u5c42\u5185\u5b58\u4f4d\u7f6e\uff0c\u6539\u53d8\u5176\u4e2d\u4e00\u4e2a\u4f1a\u6539\u53d8\u53e6\u4e00\u4e2a\u3002<\/mark><\/p>\n\n\n\n<p>\u5f20\u91cf\u8f6c NumPy \u6570\u7ec4<\/p>\n\n\n\n<p><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>t<\/strong><\/a> <strong>= <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.ones.html#torch.ones\"><strong>torch.ones<\/strong><\/a><strong>(<\/strong>5<strong>)<\/strong><br>print<strong>(<\/strong>f&#8221;t: <em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>t<\/strong><\/a><em>}<\/em>&#8220;<strong>)<\/strong><br><strong>n = <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>t<\/strong><\/a><strong>.numpy()<\/strong><br>print<strong>(<\/strong>f&#8221;n: <em>{<\/em><strong>n<\/strong><em>}<\/em>&#8220;<strong>)<\/strong><\/p>\n\n\n\n<p>t: tensor([1., 1., 1., 1., 1.])<br>n: [1. 1. 1. 1. 1.]<\/p>\n\n\n\n<p>\u5f20\u91cf\u4e2d\u7684\u6539\u53d8\u4f1a\u53cd\u6620\u5728 NumPy \u6570\u7ec4\u4e2d\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>t<\/strong><\/a><strong>.add_(<\/strong>1<strong>)<\/strong><br>print<strong>(<\/strong>f&#8221;t: <em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>t<\/strong><\/a><em>}<\/em>&#8220;<strong>)<\/strong><br>print<strong>(<\/strong>f&#8221;n: <em>{<\/em><strong>n<\/strong><em>}<\/em>&#8220;<strong>)<\/strong><\/p>\n\n\n\n<p>t: tensor([2., 2., 2., 2., 2.])<br>n: [2. 2. 2. 2. 2.]<\/p>\n\n\n\n<p>NumPy \u6570\u7ec4\u8f6c\u5f20\u91cf<\/p>\n\n\n\n<p><strong>n = np.ones(<\/strong>5<strong>)<\/strong><br><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>t<\/strong><\/a><strong> = <\/strong><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/generated\/torch.from_numpy.html#torch.from_numpy\"><strong>torch.from_numpy<\/strong><\/a><strong>(n)<\/strong><\/p>\n\n\n\n<p>NumPy \u6570\u7ec4\u4e2d\u7684\u6539\u53d8\u4f1a\u53cd\u6620\u5728\u5f20\u91cf\u4e2d\u3002<\/p>\n\n\n\n<p><strong>np.add(n, <\/strong>1<strong>, out=n)<\/strong><br>print<strong>(<\/strong>f&#8221;t: <em>{<\/em><a href=\"https:\/\/pytorch.ac.cn\/docs\/stable\/tensors.html#torch.Tensor\"><strong>t<\/strong><\/a><em>}<\/em>&#8220;<strong>)<\/strong><br>print<strong>(<\/strong>f&#8221;n: <em>{<\/em><strong>n<\/strong><em>}<\/em>&#8220;<strong>)<\/strong><\/p>\n\n\n\n<p>t: tensor([2., 2., 2., 2., 2.], dtype=torch.float64)<br>n: [2. 2. 2. 2. 2.]<\/p>\n\n\n\n<p><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u5982\u679c\u5c06tensor\u79fb\u52a8\u5230GPU\uff0c\u548cNumPy\u4e0d\u518d\u5171\u4eab<\/mark><\/p>\n\n\n\n<p>t = torch.ones(5)<br>print(f&#8221;t: {t}&#8221;)<br>n = t.numpy()<br>print(f&#8221;n: {n}&#8221;)<br>t.add_(1)<br>print(f&#8221;t: {t}&#8221;)<br>print(f&#8221;n: {n}&#8221;)<\/p>\n\n\n\n<p>if torch.accelerator.is_available():<br>&nbsp;&nbsp;&nbsp; t = t.to(torch.accelerator.current_accelerator())<br>t.add_(1)<br>print(f&#8221;t: {t}&#8221;)<br>print(f&#8221;n: {n}&#8221;)<\/p>\n\n\n\n<p>t: tensor([1., 1., 1., 1., 1.])<br>n: [1. 1. 1. 1. 1.]<br>t: tensor([2., 2., 2., 2., 2.])<br>n: [2. 2. 2. 2. 2.]<br>t: tensor([3., 3., 3., 3., 3.], device=&#8217;mps:0&#8242;)<br>n: [2. 2. 2. 2. 2.]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f20\u91cf\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6570\u636e\u7ed3\u6784\uff0c\u4e0e\u6570\u7ec4\u548c\u77e9\u9635\u975e\u5e38\u76f8\u4f3c\u3002\u5728 PyTorch \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u5f20\u91cf\u6765\u7f16\u7801\u6a21\u578b\u7684\u8f93\u5165\u548c\u8f93\u51fa\uff0c\u4ee5 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/thereisno.top\/?p=2471\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201ctorch \u5f20\u91cf\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[246,14],"tags":[9,254,253],"class_list":["post-2471","post","type-post","status-publish","format-standard","hentry","category-ai","category-python","tag-python","tag-tensor","tag-torch"],"_links":{"self":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2471"}],"version-history":[{"count":1,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2471\/revisions"}],"predecessor-version":[{"id":2472,"href":"https:\/\/thereisno.top\/index.php?rest_route=\/wp\/v2\/posts\/2471\/revisions\/2472"}],"wp:attachment":[{"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thereisno.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}