{"id":928,"date":"2024-11-07T13:57:28","date_gmt":"2024-11-07T05:57:28","guid":{"rendered":"https:\/\/fwq.ai\/blog\/928\/"},"modified":"2024-11-07T13:57:28","modified_gmt":"2024-11-07T05:57:28","slug":"pytorch-%e4%b8%ad%e7%9a%84-eq-%e5%92%8c-ne","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/928\/","title":{"rendered":"PyTorch \u4e2d\u7684 eq \u548c ne"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-977\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173076897725959.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173076897725959.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173076897725959-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173076897725959-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/173076897725959-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"PyTorch \u4e2d\u7684 eq \u548c ne\u63d2\u56fe\" alt=\"PyTorch \u4e2d\u7684 eq \u548c ne\u63d2\u56fe\" \/><\/p>\n<p>\u8bf7\u6211\u559d\u676f\u5496\u5561<\/p>\n<p>*\u5907\u5fd8\u5f55\uff1a<\/p>\n<ul>\n<li> \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 gt() \u548c lt()\u3002<\/li>\n<li> \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 ge() \u548c le()\u3002<\/li>\n<li> \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 isclose() \u548c equal()\u3002<\/li>\n<\/ul>\n<p>eq() \u53ef\u4ee5\u68c0\u67e5\u7b2c\u4e00\u4e2a 0d \u6216\u66f4\u591a d \u5f20\u91cf\u7684\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\u662f\u5426\u7b49\u4e8e\u7b2c\u4e8c\u4e2a 0d \u6216\u66f4\u591a d \u5f20\u91cf\u7684\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\uff0c\u5f97\u5230 0d \u6216\u66f4\u591a d \u5f20\u91cf\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>*\u5907\u5fd8\u5f55\uff1a<\/p>\n<ul>\n<li> eq() \u53ef\u4ee5\u4e0e torch \u6216\u5f20\u91cf\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08\u8f93\u5165\uff09\u4f7f\u7528 torch \u6216\u4f7f\u7528\u5f20\u91cf\uff08\u5fc5\u9700\u7c7b\u578b\uff1aint\u3001float\u3001complex \u6216 bool \u7684\u5f20\u91cf\uff09\u3002<\/li>\n<li>\u5e26\u6709 torch \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6216\u5e26\u6709\u5f20\u91cf\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5176\u4ed6\uff08\u5fc5\u9700\u7c7b\u578b\uff1a\u5f20\u91cf\u6216 int\u3001float\u3001complex \u6216 bool \u6807\u91cf\uff09\u3002<\/li>\n<li>torch \u5b58\u5728 out \u53c2\u6570\uff08\u53ef\u9009-\u9ed8\u8ba4\uff1a\u65e0-\u7c7b\u578b\uff1a\u5f20\u91cf\uff09\uff1a *\u5907\u6ce8\uff1a\n<ul>\n<li> \u5fc5\u987b\u4f7f\u7528 out=\u3002<\/li>\n<li> \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86\u8bba\u70b9\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u7ed3\u679c\u662f\u5177\u6709\u66f4\u591a\u5143\u7d20\u7684\u66f4\u9ad8 d \u5f20\u91cf\u3002 <\/li>\n<\/ul>\n<pre>import torch\n\ntensor1 = torch.tensor([5, 0, 3])\ntensor2 = torch.tensor([7, 0, 3])\n\ntorch.eq(input=tensor1, other=tensor2)\ntensor1.eq(other=tensor2)\ntorch.eq(input=tensor2, other=tensor1)\n# tensor([false, true, true])\n\ntensor1 = torch.tensor(5)\ntensor2 = torch.tensor([[3, 5, 4],\n                        [6, 3, 5]])\ntorch.eq(input=tensor1, other=tensor2)\ntorch.eq(input=tensor2, other=tensor1)\n# tensor([[false, true, false],\n#         [false, false, true]])\n\ntorch.eq(input=tensor1, other=3)\n# tensor(false)\n\ntorch.eq(input=tensor2, other=3)\n# tensor([[true, false, false],\n#         [false, true, false]])\n\ntensor1 = torch.tensor([5, 0, 3])\ntensor2 = torch.tensor([[5, 5, 5],\n                        [0, 0, 0],\n                        [3, 3, 3]])\ntorch.eq(input=tensor1, other=tensor2)\ntorch.eq(input=tensor2, other=tensor1)\n# tensor([[true, false, false],\n#         [false, true, false], \n#         [false, false, true]])\n\ntorch.eq(input=tensor1, other=3)\n# tensor([false, false, true])\n\ntorch.eq(input=tensor2, other=3)\n# tensor([[false, false, false],\n#         [false, false, false],\n#         [true, true, true]])\n\ntensor1 = torch.tensor([5., 0., 3.])\ntensor2 = torch.tensor([[5., 5., 5.],\n                        [0., 0., 0.],\n                        [3., 3., 3.]])\ntorch.eq(input=tensor1, other=tensor2)\n# tensor([[true, false, false],\n#         [false, true, false], \n#         [false, false, true]])\n\ntorch.eq(input=tensor1, other=3.)\n# tensor([false, false, true])\n\ntensor1 = torch.tensor([5.+0.j, 0.+0.j, 3.+0.j])\ntensor2 = torch.tensor([[5.+0.j, 5.+0.j, 5.+0.j],\n                        [0.+0.j, 0.+0.j, 0.+0.j],\n                        [3.+0.j, 3.+0.j, 3.+.0j]])\ntorch.eq(input=tensor1, other=tensor2)\n# tensor([[true, false, false],\n#         [false, true, false],\n#         [false, false, true]])\n\ntorch.eq(input=tensor1, other=3.+0.j)\n# tensor([false, false, true])\n\ntensor1 = torch.tensor([true, false, true])\ntensor2 = torch.tensor([[true, false, true],\n                        [false, true, false],\n                        [true, false, true]])\ntorch.eq(input=tensor1, other=tensor2)\n# tensor([[true, true, true],\n#         [false, false, false],\n#         [true, true, true]])\n\ntorch.eq(input=tensor1, other=true)\n# tensor([true, false, true])\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>ne() \u53ef\u4ee5\u6309\u5143\u7d20\u68c0\u67e5\u7b2c\u4e00\u4e2a 0d \u6216\u66f4\u591a d \u5f20\u91cf\u7684\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\u662f\u5426\u4e0d\u7b49\u4e8e\u7b2c\u4e8c\u4e2a 0d \u6216\u66f4\u591a d \u5f20\u91cf\u7684\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\uff0c\u5f97\u5230 0d \u6216\u66f4\u591a d \u5f20\u91cf\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<p>*\u5907\u5fd8\u5f55\uff1a<\/p>\n<ul>\n<li> ne() \u53ef\u4ee5\u4e0e torch \u6216\u5f20\u91cf\u4e00\u8d77\u4f7f\u7528\u3002<\/li>\n<li>\u7b2c\u4e00\u4e2a\u53c2\u6570\uff08\u8f93\u5165\uff09\u4f7f\u7528 torch \u6216\u4f7f\u7528\u5f20\u91cf\uff08\u5fc5\u9700\u7c7b\u578b\uff1aint\u3001float\u3001complex \u6216 bool \u7684\u5f20\u91cf\uff09\u3002<\/li>\n<li>\u5e26\u6709 torch \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6216\u5e26\u6709\u5f20\u91cf\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5176\u4ed6\uff08\u5fc5\u9700\u7c7b\u578b\uff1a\u5f20\u91cf\u6216 int\u3001float\u3001complex \u6216 bool \u6807\u91cf\uff09\u3002<\/li>\n<li>torch \u5b58\u5728 out \u53c2\u6570\uff08\u53ef\u9009-\u9ed8\u8ba4\uff1a\u65e0-\u7c7b\u578b\uff1a\u5f20\u91cf\uff09\uff1a *\u5907\u6ce8\uff1a\n<ul>\n<li> \u5fc5\u987b\u4f7f\u7528 out=\u3002<\/li>\n<li> \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86\u8bba\u70b9\u3002<\/li>\n<\/ul>\n<\/li>\n<li> not_equal() \u662f ne() \u7684\u522b\u540d\u3002 <\/li>\n<\/ul>\n<pre>import torch\n\ntensor1 = torch.tensor([5, 0, 3])\ntensor2 = torch.tensor([7, 0, 3])\n\ntorch.ne(input=tensor1, other=tensor2)\ntensor1.ne(other=tensor2)\ntorch.ne(input=tensor2, other=tensor1)\n# tensor([True, False, False])\n\ntensor1 = torch.tensor(5)\ntensor2 = torch.tensor([[3, 5, 4],\n                        [6, 3, 5]])\ntorch.ne(input=tensor1, other=tensor2)\ntorch.ne(input=tensor2, other=tensor1)\n# tensor([[True, False, True],\n#         [True, True, False]])\n\ntorch.ne(input=tensor1, other=3)\n# tensor(True)\n\ntorch.ne(input=tensor2, other=3)\n# tensor([[False, True, True],\n#         [True, False, True]])\n\ntensor1 = torch.tensor([5, 0, 3])\ntensor2 = torch.tensor([[5, 5, 5],\n                        [0, 0, 0],\n                        [3, 3, 3]])\ntorch.ne(input=tensor1, other=tensor2)\ntorch.ne(input=tensor2, other=tensor1)\n# tensor([[False, True, True],\n#         [True, False, True],\n#         [True, True, False]])\n\ntorch.ne(input=tensor1, other=3)\n# tensor([True, True, False])\n\ntorch.ne(input=tensor2, other=3)\n# tensor([[True, True, True],\n#         [True, True, True],\n#         [False, False, False]])\n\ntensor1 = torch.tensor([5., 0., 3.])\ntensor2 = torch.tensor([[5., 5., 5.],\n                        [0., 0., 0.],\n                        [3., 3., 3.]])\ntorch.ne(input=tensor1, other=tensor2)\n# tensor([[False, True, True],\n#         [True, False, True],\n#         [True, True, False]])\n\ntorch.ne(input=tensor1, other=3.)\n# tensor([True, True, False])\n\ntensor1 = torch.tensor([5.+0.j, 0.+0.j, 3.+0.j])\ntensor2 = torch.tensor([[5.+0.j, 5.+0.j, 5.+0.j],\n                        [0.+0.j, 0.+0.j, 0.+0.j],\n                        [3.+0.j, 3.+0.j, 3.+.0j]])\ntorch.ne(input=tensor1, other=tensor2)\n# tensor([[False, True, True],\n#         [True, False, True],\n#         [True, True, False]])\n\ntorch.ne(input=tensor1, other=3.+0.j)\n# tensor([True, True, False])\n\ntensor1 = torch.tensor([True, False, True])\ntensor2 = torch.tensor([[True, False, True],\n                        [False, True, False],\n                        [True, False, True]])\ntorch.ne(input=tensor1, other=tensor2)\n# tensor([[False, False, False],\n#         [True, True, True],\n#         [False, False, False]])\n\ntorch.ne(input=tensor1, other=True)\n# tensor([False, True, False])\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fPyTorch \u4e2d\u7684 eq \u548c ne\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf7\u6211\u559d\u676f\u5496\u5561 *\u5907\u5fd8\u5f55\uff1a \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 gt() \u548c lt()\u3002 \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 ge() \u548c le()\u3002 \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86 isclose() \u548c equal()\u3002 eq() \u53ef\u4ee5\u68c0\u67e5\u7b2c\u4e00\u4e2a 0d \u6216\u66f4\u591a d \u5f20\u91cf\u7684\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\u662f\u5426\u7b49\u4e8e\u7b2c\u4e8c\u4e2a 0d \u6216\u66f4\u591a d \u5f20\u91cf\u7684\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\uff0c\u5f97\u5230 0d \u6216\u66f4\u591a d \u5f20\u91cf\u96f6\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\uff0c\u5982\u4e0b\u6240\u793a\uff1a *\u5907\u5fd8\u5f55\uff1a eq() \u53ef\u4ee5\u4e0e torch \u6216\u5f20\u91cf\u4e00\u8d77\u4f7f\u7528\u3002 \u7b2c\u4e00\u4e2a\u53c2\u6570\uff08\u8f93\u5165\uff09\u4f7f\u7528 torch \u6216\u4f7f\u7528\u5f20\u91cf\uff08\u5fc5\u9700\u7c7b\u578b\uff1aint\u3001float\u3001complex \u6216 bool \u7684\u5f20\u91cf\uff09\u3002 \u5e26\u6709 torch \u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6216\u5e26\u6709\u5f20\u91cf\u7684\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u5176\u4ed6\uff08\u5fc5\u9700\u7c7b\u578b\uff1a\u5f20\u91cf\u6216 int\u3001float\u3001complex \u6216 bool \u6807\u91cf\uff09\u3002 torch \u5b58\u5728 out \u53c2\u6570\uff08\u53ef\u9009-\u9ed8\u8ba4\uff1a\u65e0-\u7c7b\u578b\uff1a\u5f20\u91cf\uff09\uff1a *\u5907\u6ce8\uff1a \u5fc5\u987b\u4f7f\u7528 out=\u3002 \u6211\u7684\u5e16\u5b50\u89e3\u91ca\u4e86\u8bba\u70b9\u3002 \u7ed3\u679c\u662f\u5177\u6709\u66f4\u591a\u5143\u7d20\u7684\u66f4\u9ad8 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-928","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/928","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=928"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/928\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}