{"id":5880,"date":"2022-08-17T22:55:18","date_gmt":"2022-08-17T14:55:18","guid":{"rendered":"http:\/\/139.9.1.231\/?p=5880"},"modified":"2022-08-17T22:55:19","modified_gmt":"2022-08-17T14:55:19","slug":"ce-loss-bce-loss","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/08\/17\/ce-loss-bce-loss\/","title":{"rendered":"CE Loss \u4e0e BCE Loss  (\u5206\u7c7b\u95ee\u9898\u635f\u5931\u51fd\u6570)"},"content":{"rendered":"\n<p>\u6709\u4e24\u4e2a\u95ee\u9898\u66fe\u56f0\u6270\u7740\u6211\uff1a<\/p>\n\n\n\n<ol><li>\u4e3a\u4f55MSE loss\u662f\u4e00\u79cd\u56de\u5f52\u95ee\u9898\u7684loss\uff0c\u4e0d\u53ef\u4ee5\u7528\u5728\u5206\u7c7b\u95ee\u9898\uff1f\u800c\u975e\u8981\u7528CE\u6216BCE\u5462\uff1f<\/li><li>\u4e3a\u4f55CE\u4e0esoftmax\u6fc0\u6d3b\u51fd\u6570\u642d\u914d\uff0c\u800cBCE\u4e0esigmoid\u642d\u914d\uff1f\u6709\u4ec0\u4e48\u7406\u7531\uff1f<\/li><\/ol>\n\n\n\n<p>\u5728\u5b66\u4e60\u8fc7\u540e\uff0c\u6211\u53d1\u73b0\u8fd9\u4e2a\u95ee\u9898\u5728\u6570\u5b66\u4e0a\u6709\u591a\u79cd\u7406\u89e3\u7684\u89d2\u5ea6\uff0c\u800c\u7ed3\u8bba\u5374\u662f\u4e00\u81f4\u7684\u3002\u5728\u8fd9\u91cc\u6211\u68b3\u7406\u51fa\u4e00\u79cd\u89d2\u5ea6\uff0c\u5728\u672a\u6765\u5982\u679c\u6709\u65b0\u7684\u7406\u89e3\u518d\u8fdb\u884c\u8865\u5145\u3002<\/p>\n\n\n\n<p>\u8be5\u601d\u8def\u5b66\u4e60\u81ea<a rel=\"noreferrer noopener\" href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/www.youtube.com\/watch%3Fv%3DhSXFuypLukA%26list%3DPLJV_el3uVTsPy9oCRY30oBPNLCo89yu49%26index%3D10\" target=\"_blank\">\u674e\u5b8f\u6bc5\u8001\u5e08\u7684\u673a\u5668\u5b66\u4e60\u8bfe\u7a0b<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"697\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-221-1024x697.png\" alt=\"\" class=\"wp-image-5883\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-221-1024x697.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-221-300x204.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-221-768x523.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-221.png 1202w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"379\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-222-1024x379.png\" alt=\"\" class=\"wp-image-5884\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-222-1024x379.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-222-300x111.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-222-768x284.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-222.png 1088w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u8fd9\u8bf4\u660e\uff0c<strong>\u5982\u679c\u7528MSE loss\u6765\u8bad\u7ec3\u5206\u7c7b\u95ee\u9898\uff0c\u4e0d\u8bba\u9884\u6d4b\u63a5\u8fd1\u771f\u5b9e\u503c\u6216\u662f\u63a5\u8fd1\u9519\u8bef\u503c\uff0c\u68af\u5ea6\u90fd\u5f88\u5c0f<\/strong>\u3002\u8fd9\u4e5f\u5c31\u89e3\u91ca\u4e86\u4e3a\u4f55\u6211\u4eec\u9700\u8981CE\u6216BCE\u635f\u5931\u6765\u5904\u7406\u5206\u7c7b\u95ee\u9898\u3002<\/p>\n\n\n\n<h2>2. BCE \u635f\u5931\u51fd\u6570<\/h2>\n\n\n\n<p>\u65e2\u7136\u5728\u5206\u7c7b\u95ee\u9898\u4e2d\uff0cMSE\u635f\u5931\u51fd\u6570\u7684\u68af\u5ea6\u4e0d\u80fd\u6ee1\u8db3\u9700\u8981\uff0c\u73b0\u5728\u6211\u4eec\u6765\u63a8\u5bfcBCE\u635f\u5931\u51fd\u6570\u7684\u68af\u5ea6\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"711\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-223-1024x711.png\" alt=\"\" class=\"wp-image-5887\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-223-1024x711.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-223-300x208.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-223-768x533.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-223.png 1124w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2>3. CE \u635f\u5931\u51fd\u6570<\/h2>\n\n\n\n<p>\u6c5f\u6e56\u4e0a\u6d41\u4f20\u4e00\u53e5\u8bdd\uff0c\u4ea4\u53c9\u71b5\u635f\u5931\u53ef\u4ee5\u91c7\u7528\u201csigmoid+BCE\u201d\u6216\u662f\u201csoftmax+CE\u201d\u3002\u5bf9\u4e8e\u524d\u8005\uff0c\u4e0a\u4e00\u90e8\u5206\u5df2\u7ecf\u5bf9\u5176\u56de\u4f20\u7684\u68af\u5ea6\u8fdb\u884c\u4e86\u63a8\u5bfc\uff0c\u8bc1\u5b9e\u4e86\u5176\u5408\u7406\u6027\u3002<\/p>\n\n\n\n<p>\u5728\u8fd9\u4e00\u90e8\u5206\u6211\u4eec\u5bf9\u201csoftmax+CE\u201d\u7684\u68af\u5ea6\u8fdb\u884c\u63a8\u5bfc\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"649\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-224-1024x649.png\" alt=\"\" class=\"wp-image-5889\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-224-1024x649.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-224-300x190.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-224-768x487.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-224.png 1082w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"206\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-225-1024x206.png\" alt=\"\" class=\"wp-image-5890\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-225-1024x206.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-225-300x60.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-225-768x154.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-225.png 1154w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2>4. \u5e94\u7528<\/h2>\n\n\n\n<p>\u5728Pytorch\u4e2d\uff0c\u201csigmoid+BCE\u201d\u5bf9\u5e94\u7684\u662f<code>torch.nn.BCEWithLogitsLoss<\/code>\uff0c\u800c\u201csoftmax+CE\u201d\u5bf9\u5e94\u7684\u662f<code>torch.nn.CrossEntropyLoss<\/code><\/p>\n\n\n\n<p>\u5177\u4f53\u53c2\u6570\u548c\u7528\u6cd5\u53ef\u4ee5\u53c2\u8003&nbsp;<a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/pytorch.org\/docs\/master\/generated\/torch.nn.BCEWithLogitsLoss.html%23bcewithlogitsloss\" target=\"_blank\" rel=\"noreferrer noopener\">BCEWithLogitsLoss<\/a>&nbsp;\u548c&nbsp;<a href=\"https:\/\/link.zhihu.com\/?target=https%3A\/\/pytorch.org\/docs\/stable\/generated\/torch.nn.CrossEntropyLoss.html%3Fhighlight%3Dcrossentropy%23torch.nn.CrossEntropyLoss\" target=\"_blank\" rel=\"noreferrer noopener\">CrossEntropyLoss<\/a><\/p>\n\n\n\n<p>\u5728\u5206\u7c7b\u95ee\u9898\u4e2d\uff0c\u5982\u679c\u9047\u5230<strong>\u7c7b\u522b\u95f4\u4e0d\u4e92\u65a5<\/strong>\u7684\u60c5\u51b5\uff0c\u53ea\u80fd\u91c7\u7528\u201csigmoid+BCE\u201d\uff1b<\/p>\n\n\n\n<p>\u5982\u679c\u9047\u5230<strong>\u7c7b\u522b\u95f4\u4e92\u65a5<\/strong>\u7684\u60c5\u51b5\uff08\u53ea\u80fd\u6709\u4e00\u7c7b\u80dc\u51fa\uff09\uff0c\u201csigmoid+BCE\u201d\u5316\u4e3a\u591a\u4e2a\u4e8c\u5206\u7c7b\u95ee\u9898\u4e0e\u201csoftmax+CE\u201d\u76f4\u63a5\u8fdb\u884c\u5206\u7c7b\u90fd\u662f\u6709\u88ab\u7528\u5230\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h2>Softmax\u51fd\u6570\u548cSigmoid\u51fd\u6570\u7684\u533a\u522b\u4e0e\u8054\u7cfb<\/h2>\n\n\n\n<h2>1. \u524d\u8a00<\/h2>\n\n\n\n<p>\u5bf9\u4e8eSoftmax\u51fd\u6570\u548cSigmoid\u51fd\u6570\uff0c\u6211\u4eec\u5206\u4e3a\u4e24\u90e8\u5206\u8bb2\u89e3\uff0c<strong>\u7b2c\u4e00\u90e8\u5206\uff1a\u5bf9\u4e8e\u5206\u7c7b\u4efb\u52a1\uff0c\u7b2c\u4e8c\u90e8\u5206\uff1a\u5bf9\u4e8e\u4e8c\u5206\u7c7b\u4efb\u52a1\uff08\u8be6\u7ec6\u8bb2\u89e3\uff09\u3002<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"464\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-226-1024x464.png\" alt=\"\" class=\"wp-image-5894\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-226-1024x464.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-226-300x136.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-226-768x348.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-226.png 1056w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>\u4f18\u70b9<\/strong>\uff1a1. Sigmoid\u51fd\u6570\u7684\u8f93\u51fa\u5728(0,1)\u4e4b\u95f4\uff0c\u8f93\u51fa\u8303\u56f4\u6709\u9650\uff0c\u4f18\u5316\u7a33\u5b9a\uff0c\u53ef\u4ee5\u7528\u4f5c\u8f93\u51fa\u5c42\u30022. \u8fde\u7eed\u51fd\u6570\uff0c\u4fbf\u4e8e\u6c42\u5bfc\u3002<\/p>\n\n\n\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a1. \u6700\u660e\u663e\u7684\u5c31\u662f\u9971\u548c\u6027\uff0c\u4ece\u4e0a\u56fe\u4e5f\u4e0d\u96be\u770b\u51fa\u5176\u4e24\u4fa7\u5bfc\u6570\u9010\u6e10\u8d8b\u8fd1\u4e8e0\uff0c\u5bb9\u6613\u9020\u6210\u68af\u5ea6\u6d88\u5931\u30022.\u6fc0\u6d3b\u51fd\u6570\u7684\u504f\u79fb\u73b0\u8c61\u3002Sigmoid\u51fd\u6570\u7684\u8f93\u51fa\u503c\u5747\u5927\u4e8e0\uff0c\u4f7f\u5f97\u8f93\u51fa\u4e0d\u662f0\u7684\u5747\u503c\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u540e\u4e00\u5c42\u7684\u795e\u7ecf\u5143\u5c06\u5f97\u5230\u4e0a\u4e00\u5c42\u975e0\u5747\u503c\u7684\u4fe1\u53f7\u4f5c\u4e3a\u8f93\u5165\uff0c\u8fd9\u4f1a\u5bf9\u68af\u5ea6\u4ea7\u751f\u5f71\u54cd\u3002 3. \u8ba1\u7b97\u590d\u6742\u5ea6\u9ad8\uff0c\u56e0\u4e3aSigmoid\u51fd\u6570\u662f\u6307\u6570\u5f62\u5f0f\u3002<\/p>\n\n\n\n<h3>2.2 Softmax\u51fd\u6570<\/h3>\n\n\n\n<p>Softmax =<strong>\u591a\u7c7b\u522b\u5206\u7c7b\u95ee\u9898<\/strong>=\u53ea\u6709\u4e00\u4e2a\u6b63\u786e\u7b54\u6848=\u4e92\u65a5\u8f93\u51fa\uff08\u4f8b\u5982\u624b\u5199\u6570\u5b57\uff0c\u9e22\u5c3e\u82b1\uff09\u3002\u6784\u5efa\u5206\u7c7b\u5668\uff0c\u89e3\u51b3\u53ea\u6709\u552f\u4e00\u6b63\u786e\u7b54\u6848\u7684\u95ee\u9898\u65f6\uff0c\u7528Softmax\u51fd\u6570\u5904\u7406\u5404\u4e2a\u539f\u59cb\u8f93\u51fa\u503c\u3002Softmax\u51fd\u6570\u7684\u5206\u6bcd\u7efc\u5408\u4e86\u539f\u59cb\u8f93\u51fa\u503c\u7684\u6240\u6709\u56e0\u7d20\uff0c\u8fd9\u610f\u5473\u7740\uff0cSoftmax\u51fd\u6570\u5f97\u5230\u7684\u4e0d\u540c\u6982\u7387\u4e4b\u95f4\u76f8\u4e92\u5173\u8054\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1024\" height=\"677\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-227-1024x677.png\" alt=\"\" class=\"wp-image-5897\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-227-1024x677.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-227-300x198.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-227-768x508.png 768w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-227.png 1040w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>Softmax\u51fd\u6570\u662f\u4e8c\u5206\u7c7b\u51fd\u6570Sigmoid\u5728\u591a\u5206\u7c7b\u4e0a\u7684\u63a8\u5e7f\uff0c\u76ee\u7684\u662f\u5c06\u591a\u5206\u7c7b\u7684\u7ed3\u679c\u4ee5\u6982\u7387\u7684\u5f62\u5f0f\u5c55\u73b0\u51fa\u6765\u3002<\/strong>\u5982\u56fe2\u6240\u793a\uff0cSoftmax\u76f4\u767d\u6765\u8bf4\u5c31\u662f\u5c06\u539f\u6765\u8f93\u51fa\u662f3,1,-3\u901a\u8fc7Softmax\u51fd\u6570\u4e00\u4f5c\u7528\uff0c\u5c31\u6620\u5c04\u6210\u4e3a(0,1)\u7684\u503c\uff0c\u800c\u8fd9\u4e9b\u503c\u7684\u7d2f\u548c\u4e3a1\uff08\u6ee1\u8db3\u6982\u7387\u7684\u6027\u8d28\uff09\uff0c\u90a3\u4e48\u6211\u4eec\u5c31\u53ef\u4ee5\u5c06\u5b83\u7406\u89e3\u6210\u6982\u7387\uff0c\u5728\u6700\u540e\u9009\u53d6\u8f93\u51fa\u7ed3\u70b9\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u9009\u53d6\u6982\u7387\u6700\u5927\uff08\u4e5f\u5c31\u662f\u503c\u5bf9\u5e94\u6700\u5927\u7684\uff09\u7ed3\u70b9\uff0c\u4f5c\u4e3a\u6211\u4eec\u7684\u9884\u6d4b\u76ee\u6807\u3002<\/p>\n\n\n\n<p>\u7531\u4e8eSoftmax\u51fd\u6570\u5148\u62c9\u5927\u4e86\u8f93\u5165\u5411\u91cf\u5143\u7d20\u4e4b\u95f4\u7684\u5dee\u5f02\uff08\u901a\u8fc7\u6307\u6570\u51fd\u6570\uff09\uff0c\u7136\u540e\u624d\u5f52\u4e00\u5316\u4e3a\u4e00\u4e2a\u6982\u7387\u5206\u5e03\uff0c\u5728\u5e94\u7528\u5230\u5206\u7c7b\u95ee\u9898\u65f6\uff0c\u5b83\u4f7f\u5f97\u5404\u4e2a\u7c7b\u522b\u7684\u6982\u7387\u5dee\u5f02\u6bd4\u8f83\u663e\u7740\uff0c\u6700\u5927\u503c\u4ea7\u751f\u7684\u6982\u7387\u66f4\u63a5\u8fd11\uff0c\u8fd9\u6837\u8f93\u51fa\u5206\u5e03\u7684\u5f62\u5f0f\u66f4\u63a5\u8fd1\u771f\u5b9e\u5206\u5e03\u3002<\/p>\n\n\n\n<p><strong>Softmax\u53ef\u4ee5\u7531\u4e09\u4e2a\u4e0d\u540c\u7684\u89d2\u5ea6\u6765\u89e3\u91ca\u3002\u4ece\u4e0d\u540c\u89d2\u5ea6\u6765\u770bsoftmax\u51fd\u6570\uff0c\u53ef\u4ee5\u5bf9\u5176\u5e94\u7528\u573a\u666f\u6709\u66f4\u6df1\u523b\u7684\u7406\u89e3\uff1a<\/strong><\/p>\n\n\n\n<ol><li>softmax\u53ef\u4ee5\u5f53\u4f5carg max\u7684\u4e00\u79cd\u5e73\u6ed1\u8fd1\u4f3c\uff0c\u4e0earg max\u64cd\u4f5c\u4e2d\u66b4\u529b\u5730\u9009\u51fa\u4e00\u4e2a\u6700\u5927\u503c\uff08\u4ea7\u751f\u4e00\u4e2aone-hot\u5411\u91cf\uff09\u4e0d\u540c\uff0csoftmax\u5c06\u8fd9\u79cd\u8f93\u51fa\u4f5c\u4e86\u4e00\u5b9a\u7684\u5e73\u6ed1\uff0c\u5373\u5c06one-hot\u8f93\u51fa\u4e2d\u6700\u5927\u503c\u5bf9\u5e94\u76841\u6309\u8f93\u5165\u5143\u7d20\u503c\u7684\u5927\u5c0f\u5206\u914d\u7ed9\u5176\u4ed6\u4f4d\u7f6e\u3002<\/li><li>softmax\u5c06\u8f93\u5165\u5411\u91cf\u5f52\u4e00\u5316\u6620\u5c04\u5230\u4e00\u4e2a\u7c7b\u522b\u6982\u7387\u5206\u5e03\uff0c\u5373&nbsp;n&nbsp;\u4e2a\u7c7b\u522b\u4e0a\u7684\u6982\u7387\u5206\u5e03\uff08\u524d\u6587\u4e5f\u6709\u63d0\u5230\uff09\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48\u5728\u6df1\u5ea6\u5b66\u4e60\u4e2d\u5e38\u5e38\u5c06softmax\u4f5c\u4e3aMLP\u7684\u6700\u540e\u4e00\u5c42\uff0c\u5e76\u914d\u5408\u4ee5\u4ea4\u53c9\u71b5\u635f\u5931\u51fd\u6570\uff08\u5bf9\u5206\u5e03\u95f4\u5dee\u5f02\u7684\u4e00\u79cd\u5ea6\u91cf\uff09\u3002<\/li><li>\u4ece\u6982\u7387\u56fe\u6a21\u578b\u7684\u89d2\u5ea6\u6765\u770b\uff0csoftmax\u7684\u8fd9\u79cd\u5f62\u5f0f\u53ef\u4ee5\u7406\u89e3\u4e3a\u4e00\u4e2a\u6982\u7387\u65e0\u5411\u56fe\u4e0a\u7684\u8054\u5408\u6982\u7387\u3002\u56e0\u6b64\u4f60\u4f1a\u53d1\u73b0\uff0c\u6761\u4ef6\u6700\u5927\u71b5\u6a21\u578b\u4e0esoftmax\u56de\u5f52\u6a21\u578b\u5b9e\u9645\u4e0a\u662f\u4e00\u81f4\u7684\uff0c\u8bf8\u5982\u8fd9\u6837\u7684\u4f8b\u5b50\u8fd8\u6709\u5f88\u591a\u3002\u7531\u4e8e\u6982\u7387\u56fe\u6a21\u578b\u5f88\u5927\u7a0b\u5ea6\u4e0a\u501f\u7528\u4e86\u4e00\u4e9b\u70ed\u529b\u5b66\u7cfb\u7edf\u7684\u7406\u8bba\uff0c\u56e0\u6b64\u4e5f\u53ef\u4ee5\u4ece\u7269\u7406\u7cfb\u7edf\u7684\u89d2\u5ea6\u8d4b\u4e88softmax\u4e00\u5b9a\u7684\u5185\u6db5\u3002<\/li><\/ol>\n\n\n\n<h3>2.3 \u603b\u7ed3<\/h3>\n\n\n\n<ol><li>\u5982\u679c\u6a21\u578b\u8f93\u51fa\u4e3a\u975e\u4e92\u65a5\u7c7b\u522b\uff0c\u4e14\u53ef\u4ee5\u540c\u65f6\u9009\u62e9\u591a\u4e2a\u7c7b\u522b\uff0c\u5219\u91c7\u7528Sigmoid\u51fd\u6570\u8ba1\u7b97\u8be5\u7f51\u7edc\u7684\u539f\u59cb\u8f93\u51fa\u503c\u3002<\/li><li>\u5982\u679c\u6a21\u578b\u8f93\u51fa\u4e3a\u4e92\u65a5\u7c7b\u522b\uff0c\u4e14\u53ea\u80fd\u9009\u62e9\u4e00\u4e2a\u7c7b\u522b\uff0c\u5219\u91c7\u7528Softmax\u51fd\u6570\u8ba1\u7b97\u8be5\u7f51\u7edc\u7684\u539f\u59cb\u8f93\u51fa\u503c\u3002<\/li><li>Sigmoid\u51fd\u6570\u53ef\u4ee5\u7528\u6765\u89e3\u51b3\u591a\u6807\u7b7e\u95ee\u9898\uff0cSoftmax\u51fd\u6570\u7528\u6765\u89e3\u51b3\u5355\u6807\u7b7e\u95ee\u9898\u3002<sup><a href=\"https:\/\/zhuanlan.zhihu.com\/p\/356976844#ref_1\">[1]<\/a><\/sup><\/li><li>\u5bf9\u4e8e\u67d0\u4e2a\u5206\u7c7b\u573a\u666f\uff0c\u5f53Softmax\u51fd\u6570\u80fd\u7528\u65f6\uff0cSigmoid\u51fd\u6570\u4e00\u5b9a\u53ef\u4ee5\u7528\u3002<\/li><\/ol>\n\n\n\n<h2>3. \u4e8c\u5206\u7c7b\u4efb\u52a1<\/h2>\n\n\n\n<p>\u5bf9\u4e8e\u4e8c\u5206\u7c7b\u95ee\u9898\u6765\u8bf4\uff0c<strong>\u7406\u8bba\u4e0a\uff0c\u4e24\u8005\u662f\u6ca1\u6709\u4efb\u4f55\u533a\u522b\u7684\u3002<\/strong>\u7531\u4e8e\u6211\u4eec\u73b0\u5728\u7528\u7684Pytorch\u3001TensorFlow\u7b49\u6846\u67b6\u8ba1\u7b97\u77e9\u9635\u65b9\u5f0f\u7684\u95ee\u9898\uff0c\u5bfc\u81f4\u4e24\u8005\u5728\u53cd\u5411\u4f20\u64ad\u7684\u8fc7\u7a0b\u4e2d\u8fd8\u662f\u6709\u533a\u522b\u7684\u3002\u5b9e\u9a8c\u7ed3\u679c\u8868\u660e\uff0c\u4e24\u8005\u8fd8\u662f\u5b58\u5728\u5dee\u5f02\u7684\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u5206\u7c7b\u6a21\u578b\uff0c\u53ef\u80fdSigmoid\u51fd\u6570\u6548\u679c\u597d\uff0c\u4e5f\u53ef\u80fd\u662fSoftmax\u51fd\u6570\u6548\u679c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" width=\"1024\" height=\"319\" src=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-228.png\" alt=\"\" class=\"wp-image-5899\" srcset=\"http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-228.png 1024w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-228-300x93.png 300w, http:\/\/139.9.1.231\/wp-content\/uploads\/2022\/08\/image-228-768x239.png 768w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><strong>\u7136\u540e\u6211\u4eec\u518d\u5206\u6790\u4e3a\u4ec0\u4e48\u4e24\u8005\u4e4b\u95f4\u8fd8\u5b58\u7740\u5dee\u5f02\uff08\u4ee5Pytorch\u4e3a\u4f8b\uff09\uff1a<\/strong><\/p>\n\n\n\n<p>\u9996\u5148\u6211\u4eec\u8981\u660e\u767d\uff0c\u5f53\u4f60\u7528Sigmoid\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4f60\u7684\u6700\u540e\u4e00\u5c42\u5168\u8fde\u63a5\u5c42\u7684\u795e\u7ecf\u5143\u4e2a\u6570\u4e3a1\uff0c\u800c\u5f53\u4f60\u7528Softmax\u51fd\u6570\u7684\u65f6\u5019\uff0c\u4f60\u7684\u6700\u540e\u4e00\u5c42\u5168\u8fde\u63a5\u5c42\u7684\u795e\u7ecf\u5143\u4e2a\u6570\u662f2\u3002\u8fd9\u4e2a\u5f88\u597d\u7406\u89e3\uff0c\u56e0\u4e3aSigmoid\u51fd\u6570\u53ea\u6709\u662f\u76ee\u6807\u548c\u4e0d\u662f\u76ee\u6807\u4e4b\u5206\uff0c\u5b9e\u9645\u4e0a\u53ea\u5b58\u5728\u4e00\u7c7b\u76ee\u6807\u7c7b\uff0c\u53e6\u5916\u4e00\u4e2a\u662f\u80cc\u666f\u7c7b\u3002\u800cSoftmax\u51fd\u6570\u5c06\u76ee\u6807\u5206\u7c7b\u4e3a\u4e86\u4e8c\u7c7b\uff0c\u6240\u4ee5\u6709\u4e24\u4e2a\u795e\u7ecf\u5143\u3002\u8fd9\u4e5f\u662f\u5bfc\u81f4\u4e24\u8005\u5b58\u5728\u5dee\u5f02\u7684\u4e3b\u8981\u539f\u56e0\u3002<\/p>\n\n\n\n<p><strong>Sigmoid\u51fd\u6570<\/strong>\u9488\u5bf9\u4e24\u70b9\u5206\u5e03\u63d0\u51fa\u3002\u795e\u7ecf\u7f51\u7edc\u7684\u8f93\u51fa\u7ecf\u8fc7\u5b83\u7684\u8f6c\u6362\uff0c\u53ef\u4ee5\u5c06\u6570\u503c\u538b\u7f29\u5230(0,1)\u4e4b\u95f4\uff0c\u5f97\u5230\u7684\u7ed3\u679c\u53ef\u4ee5\u7406\u89e3\u6210<strong>\u5206\u7c7b\u6210\u76ee\u6807\u7c7b\u522b\u7684\u6982\u7387P\uff0c\u800c\u4e0d\u5206\u7c7b\u5230\u8be5\u7c7b\u522b\u7684\u6982\u7387\u662f(1 &#8211; P)<\/strong>\uff0c\u8fd9\u4e5f\u662f\u5178\u578b\u7684\u4e24\u70b9\u5206\u5e03\u7684\u5f62\u5f0f\u3002<\/p>\n\n\n\n<p><strong>Softmax\u51fd\u6570<\/strong>\u672c\u8eab\u9488\u5bf9\u591a\u9879\u5206\u5e03\u63d0\u51fa\uff0c\u5f53\u7c7b\u522b\u6570\u662f2\u65f6\uff0c\u5b83\u9000\u5316\u4e3a\u4e8c\u9879\u5206\u5e03\u3002\u800c\u5b83\u548cSigmoid\u51fd\u6570\u771f\u6b63\u7684\u533a\u522b\u5c31\u5728\u2014\u2014\u4e8c\u9879\u5206\u5e03\u5305\u542b\u4e24\u4e2a\u5206\u7c7b\u7c7b\u522b\uff08\u59d1\u4e14\u5206\u522b\u79f0\u4e3aA\u548cB\uff09\uff0c\u800c\u4e24\u70b9\u5206\u5e03\u5176\u5b9e\u662f\u9488\u5bf9\u4e00\u4e2a\u7c7b\u522b\u7684\u6982\u7387\u5206\u5e03\uff0c\u5176\u5bf9\u5e94\u7684\u90a3\u4e2a\u7c7b\u522b\u7684\u5206\u5e03\u76f4\u63a5\u75311-P\u5f97\u51fa\u3002<\/p>\n\n\n\n<p>\u7b80\u5355\u70b9\u7406\u89e3\u5c31\u662f\uff0c<strong>Sigmoid\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5f53\u4f5c\u6210\u5b83\u662f\u5bf9\u4e00\u4e2a\u7c7b\u522b\u7684\u201c\u5efa\u6a21\u201d<\/strong>\uff0c\u5c06\u8be5\u7c7b\u522b\u5efa\u6a21\u5b8c\u6210\uff0c\u53e6\u4e00\u4e2a\u76f8\u5bf9\u7684\u7c7b\u522b\u5c31\u76f4\u63a5\u901a\u8fc71\u51cf\u53bb\u5f97\u5230\u3002<strong>\u800csoftmax\u51fd\u6570\uff0c\u662f\u5bf9\u4e24\u4e2a\u7c7b\u522b\u5efa\u6a21<\/strong>\uff0c\u540c\u6837\u7684\uff0c\u5f97\u5230\u4e24\u4e2a\u7c7b\u522b\u7684\u6982\u7387\u4e4b\u548c\u662f1\u3002<\/p>\n\n\n\n<p>\u795e\u7ecf\u7f51\u7edc\u5728\u505a\u4e8c\u5206\u7c7b\u65f6\uff0c\u4f7f\u7528Softmax\u8fd8\u662fSigmoid\uff0c\u505a\u6cd5\u5176\u5b9e\u6709\u660e\u663e\u5dee\u522b\u3002\u7531\u4e8eSoftmax\u662f\u5bf9\u4e24\u4e2a\u7c7b\u522b\uff08\u6b63\u53cd\u4e24\u7c7b\uff0c\u901a\u5e38\u5b9a\u4e49\u4e3a0\/1\u7684label\uff09\u5efa\u6a21\uff0c\u6240\u4ee5\u5bf9\u4e8eNLP\u6a21\u578b\u800c\u8a00\uff08\u6bd4\u5982\u6cdbBERT\u6a21\u578b\uff09\uff0cBert\u8f93\u51fa\u5c42\u9700\u8981\u901a\u8fc7\u4e00\u4e2ann.Linear()\u5168\u8fde\u63a5\u5c42\u538b\u7f29\u81f32\u7ef4\uff0c\u7136\u540e\u63a5Softmax\uff08Pytorch\u7684\u505a\u6cd5\uff0c\u5c31\u662f\u76f4\u63a5\u63a5\u4e0atorch.nn.CrossEntropyLoss\uff09\uff1b\u800cSigmoid\u53ea\u5bf9\u4e00\u4e2a\u7c7b\u522b\u5efa\u6a21\uff08\u901a\u5e38\u5c31\u662f\u6b63\u786e\u7684\u90a3\u4e2a\u7c7b\u522b\uff09\uff0c\u6240\u4ee5Bert\u8f93\u51fa\u5c42\u9700\u8981\u901a\u8fc7\u4e00\u4e2ann.Linear()\u5168\u8fde\u63a5\u5c42\u538b\u7f29\u81f31\u7ef4\uff0c\u7136\u540e\u63a5Sigmoid\uff08torch\u5c31\u662f\u63a5torch.nn.BCEWithLogitsLoss\uff09\u3002<\/p>\n\n\n\n<p>\u603b\u800c\u8a00\u4e4b\uff0cSotfmax\u548cSigmoid\u786e\u5b9e\u5728\u4e8c\u5206\u7c7b\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u5316\u4e3a\u76f8\u540c\u7684\u6570\u5b66\u8868\u8fbe\u5f62\u5f0f\uff0c\u4f46\u5e76\u4e0d\u610f\u5473\u7740\u4e8c\u8005\u6709\u4e00\u6837\u7684\u542b\u4e49\uff0c\u800c\u4e14\u4e8c\u8005\u7684\u8f93\u5165\u8f93\u51fa\u90fd\u662f\u4e0d\u540c\u7684\u3002Sigmoid\u5f97\u5230\u7684\u7ed3\u679c\u662f\u201c\u5206\u5230\u6b63\u786e\u7c7b\u522b\u7684\u6982\u7387\u548c\u672a\u5206\u5230\u6b63\u786e\u7c7b\u522b\u7684\u6982\u7387\u201d\uff0cSoftmax\u5f97\u5230\u7684\u662f\u201c\u5206\u5230\u6b63\u786e\u7c7b\u522b\u7684\u6982\u7387\u548c\u5206\u5230\u9519\u8bef\u7c7b\u522b\u7684\u6982\u7387\u201d\u3002<\/p>\n\n\n\n<p><strong>\u4e00\u79cd\u5e38\u89c1\u7684\u9519\u6cd5\uff08NLP\u4e2d\uff09\uff1a<\/strong>\u5373\u9519\u8bef\u5730\u5c06Softmax\u548cSigmoid\u6df7\u4e3a\u4e00\u8c08\uff0c\u518d\u628aBERT\u8f93\u51fa\u5c42\u538b\u7f29\u81f32\u7ef4\u7684\u60c5\u51b5\u4e0b\uff0c\u5374\u7528Sigmoid\u5bf9\u7ed3\u679c\u8fdb\u884c\u8ba1\u7b97\u3002\u8fd9\u6837\u6211\u4eec\u5f97\u5230\u7684\u7ed3\u679c\u5176\u610f\u4e49\u662f\u4ec0\u4e48\u5462\uff1f<\/p>\n\n\n\n<p>\u5047\u8bbe\u6211\u4eec\u73b0\u5728BERT\u8f93\u51fa\u5c42\u7ecfnn.Linear()\u538b\u7f29\u540e\uff0c\u5f97\u5230\u4e00\u4e2a\u4e8c\u7ef4\u7684\u5411\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;-0.9419267177581787, 1.944047451019287]<\/code><\/pre>\n\n\n\n<p>\u5bf9\u5e94\u7c7b\u522b\u5206\u522b\u662f(0,1)\u3002\u6211\u4eec\u7ecf\u8fc7Sigmoid\u8fd0\u7b97\u5f97\u5230\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tensor(&#91;0.2805, 0.8748])<\/code><\/pre>\n\n\n\n<p>\u524d\u80050.2805\u6307\u7684\u662f\u5206\u7c7b\u7c7b\u522b\u4e3a0\u7684\u6982\u7387\uff0c0.8748\u6307\u7684\u662f\u5206\u7c7b\u7c7b\u522b\u4e3a1\u7684\u6982\u7387\u3002\u4e8c\u8005\u76f8\u4e92\u72ec\u7acb\uff0c\u53ef\u770b\u4f5c\u4e24\u6b21\u72ec\u7acb\u7684\u5b9e\u9a8c\uff08\u663e\u7136\u5728\u8fd9\u91cc\u4e0d\u9002\u7528\uff0c\u56e0\u4e3a0-1\u7c7b\u522b\u4e4b\u95f4\u663e\u7136\u4e0d\u662f\u76f8\u4e92\u72ec\u7acb\u7684\u4e24\u6b21\u4f2f\u52aa\u5229\u4e8b\u4ef6\uff09\u3002\u6240\u4ee5\u663e\u800c\u6613\u89c1\u7684\uff0c\u4e8c\u8005\u52a0\u548c\u5e76\u4e0d\u7b49\u4e8e1\u3002<\/p>\n\n\n\n<p>\u82e5\u7528softmax\u8fdb\u884c\u8ba1\u7b97\uff0c\u53ef\u5f97\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>tensor(&#91;0.0529, 0.9471])<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u4e24\u8005\u52a0\u548c\u662f1\uff0c\u624d\u662f\u6b63\u786e\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<p><strong>\u7ecf\u9a8c\uff1a<\/strong><\/p>\n\n\n\n<p><strong>\u5bf9\u4e8eNLP\u800c\u8a00<\/strong>\uff0c\u8fd9\u4e24\u8005\u4e4b\u95f4\u786e\u5b9e\u6709\u5dee\u522b\uff0cSoftmax\u7684\u5904\u7406\u65b9\u5f0f\u6709\u65f6\u5019\u4f1a\u6bd4Sigmoid\u7684\u5904\u7406\u65b9\u5f0f\u597d\u4e00\u70b9\u3002<\/p>\n\n\n\n<p><strong>\u5bf9\u4e8eCV\u800c\u8a00<\/strong>\uff0c\u8fd9\u4e24\u8005\u4e4b\u95f4\u4e5f\u662f\u6709\u5dee\u522b\u7684\uff0cSigmoid\u7684\u5904\u7406\u65b9\u5f0f\u6709\u65f6\u5019\u4f1a\u6bd4Softmax\u7684\u5904\u7406\u65b9\u5f0f\u597d\u4e00\u70b9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6709\u4e24\u4e2a\u95ee\u9898\u66fe\u56f0\u6270\u7740\u6211\uff1a \u4e3a\u4f55MSE loss\u662f\u4e00\u79cd\u56de\u5f52\u95ee\u9898\u7684loss\uff0c\u4e0d\u53ef\u4ee5\u7528\u5728\u5206\u7c7b\u95ee\u9898\uff1f\u800c\u975e\u8981\u7528CE\u6216BCE &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/08\/17\/ce-loss-bce-loss\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">CE Loss \u4e0e BCE Loss  (\u5206\u7c7b\u95ee\u9898\u635f\u5931\u51fd\u6570)<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,4],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/5880"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=5880"}],"version-history":[{"count":14,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/5880\/revisions"}],"predecessor-version":[{"id":5902,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/5880\/revisions\/5902"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=5880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=5880"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=5880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}