{"id":228,"date":"2021-07-28T05:42:00","date_gmt":"2021-07-28T05:42:00","guid":{"rendered":"https:\/\/tensor.agenthub.uk\/?p=228"},"modified":"2024-05-17T06:18:03","modified_gmt":"2024-05-17T06:18:03","slug":"%e8%ae%be%e5%ae%9abias%e8%a7%a3%e5%86%b3%e6%a0%b7%e6%9c%ac%e4%b8%8d%e5%9d%87%e8%a1%a1%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/tensorzen.blog\/?p=228","title":{"rendered":"\u8bbe\u5b9abias\u89e3\u51b3\u6837\u672c\u4e0d\u5747\u8861\u95ee\u9898"},"content":{"rendered":"\n<p>\u521d\u59cb\u5316\u53c2\u6570\u76f4\u63a5\u51b3\u5b9a\u4e86\u6a21\u578b\u662f\u5426\u80fd\u5feb\u901f\u6536\u655b\uff0c\u4ece\u68af\u5ea6\u4e0b\u964d\u7684\u8fc7\u7a0b\u770b\uff0c\u521d\u59cb\u5316\u53c2\u6570$\\theta_0$\u5e94\u8be5\u662f\u5bf9$f(x;\\theta)$&amp; \u6570\u636e\u96c6 \u4ece\u5168\u5c40\u4e0a\u8003\u8651\u4e4b\u540e\u5f97\u5230\u4e00\u4e2a\u6bd4\u8f83\u76f4\u89c9\u7684\u731c\u6d4b\u3002 \u5982\u679c\u4f60\u7684\u76f4\u89c9\u51c6\u90a3\u4e48\u6a21\u578b\u6536\u655b\u7684\u901f\u5ea6\u4f1a\u6bd4\u8f83\u5feb\uff0c\u6216\u8005\u4e0d\u81f3\u4e8e\u6389\u8fdb\u4e00\u4e2a\u5c40\u90e8\u6700\u4f18\u89e3\u722c\u4e0d\u51fa\u6765\u3002<\/p>\n\n\n\n<p>\u4e4b\u524d\u770b\u8fc7\u4e00\u4e2a\u8c03\u6574\u521d\u59cbbias\u5904\u7406\u4e0d\u5747\u8861\u6837\u672c\u7684\u4f8b\u5b50\uff0c\u8fd8\u633a\u6709\u8da3\u7684\uff0c\u8fd9\u91cc\u7ed9\u4e61\u4eb2\u4eec\u4ecb\u7ecd\u4e0b\u3002\u4f8b\u5b50\u6765\u81eatenosrflow\u7684tutorials\uff0c\u5730\u5740\u5728\u8fd9 <a href=\"https:\/\/www.tensorflow.org\/tutorials\/structured_data\/imbalanced_data\">https:\/\/www.tensorflow.org\/tutorials\/structured_data\/imbalanced_data<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1 \u7b80\u8ff0\u4e00\u4e0b\u4f8b\u5b50<\/h2>\n\n\n\n<p>\u4f8b\u5b50\u6bd4\u8f83\u7b80\u5355\uff0c\u662f\u4e00\u4e2a\u4e25\u91cd\u4e0d\u5747\u8861\u7684\u4e8c\u5206\u7c7b\u95ee\u9898\uff0c\u6570\u636e\u6765\u81eaKaggle\u7684\u4fe1\u7528\u5361\u4ea4\u6613\u8bb0\u5f55\uff0c\u76ee\u6807\u8bc6\u522b\u91cc\u9762\u4f2a\u9020\u7684\u4ea4\u6613\u8bb0\u5f55\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.1 \u6570\u636e\u51c6\u5907<\/h3>\n\n\n\n<p>\u7528pandas\u52a0\u8f7d\u4e00\u4e0b\u6570\u636e\uff08\u770b\u5230URL\u91cc\u7684\u201cgoogle\u201d\u5b57\u6837\uff0c\u5927\u6982\u4e5f\u8bb8\u9700\u8981\u4e00\u628a\ud83e\ude9c\uff09\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"raw_df = pd.read_csv('https:\/\/storage.googleapis.com\/download.tensorflow.org\/data\/creditcard.csv')\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">raw_df <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> pd.<\/span><span style=\"color: #62E884\">read_csv<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #E7EE98\">&#39;https:\/\/storage.googleapis.com\/download.tensorflow.org\/data\/creditcard.csv&#39;<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u6837\u672c\u91cc\u7684Class\u5b57\u6bb5\u6807\u8bb0\u662f\u5426\u4e3a\u5047\u7684\u4ea4\u6613\u8bb0\u5f55\uff0cClass=1\u7684\u8868\u793a\u5047\u7684\u8bb0\u5f55\uff0c\u4ece\u6837\u672c\u6bd4\u4f8b\u4e0a\u770b\uff0c\u975e\u5e38\u4e0d\u5747\u8861<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"279\" src=\"https:\/\/tensor.agenthub.uk\/wp-content\/uploads\/2024\/03\/image-3-1024x279.png\" alt=\"\" class=\"wp-image-231\" style=\"width:459px;height:auto\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-3-1024x279.png 1024w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-3-300x82.png 300w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-3-768x209.png 768w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-3.png 1080w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\">1.2 \u6784\u9020\u6a21\u578b<\/h3>\n\n\n\n<p>\u4e3a\u4e86\u65b9\u4fbf\u6784\u9020\u6a21\u578b\uff0c\u8fd9\u91cc\u6709\u4e2a\u51fd\u6570\uff0c\u4e5f\u662f\u6765\u81eatutorials\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(2 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"def make_model(metrics = METRICS, output_bias = None):\n    if output_bias:\n        output_bias = tf.keras.initializers.Constant(output_bias)\n    \n    model = keras.Sequential([\n        keras.layers.Dense(16, activation = 'relu', input_shape = (train_features.shape[-1], )),\n        keras.layers.Dropout(0.5),\n        keras.layers.Dense(1, activation = 'sigmoid', bias_initializer = output_bias)\n    ])\n    \n    model.compile(\n        optimizer = keras.optimizers.Adam(lr=1e-3),\n        loss = keras.losses.BinaryCrossentropy(),\n        metrics=metrics\n    )\n\n    return model\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #97E1F1; font-style: italic\">def<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">make_model<\/span><span style=\"color: #F6F6F4\">(metrics <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> METRICS, output_bias <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> None):<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    if output_bias:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        output_bias = tf.keras.initializers.Constant(output_bias)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    model = keras.Sequential([<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #97E1F1; font-style: italic\">keras<\/span><span style=\"color: #F6F6F4\">.<\/span><span style=\"color: #97E1F1; font-style: italic\">layers<\/span><span style=\"color: #F6F6F4\">.<\/span><span style=\"color: #97E1F1; font-style: italic\">Dense<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">16<\/span><span style=\"color: #F6F6F4\">, activation <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #E7EE98\">&#39;relu&#39;<\/span><span style=\"color: #F6F6F4\">, input_shape <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> (train_features.shape[<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">], )),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #97E1F1; font-style: italic\">keras<\/span><span style=\"color: #F6F6F4\">.<\/span><span style=\"color: #97E1F1; font-style: italic\">layers<\/span><span style=\"color: #F6F6F4\">.<\/span><span style=\"color: #97E1F1; font-style: italic\">Dropout<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">0.5<\/span><span style=\"color: #F6F6F4\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        <\/span><span style=\"color: #97E1F1; font-style: italic\">keras<\/span><span style=\"color: #F6F6F4\">.<\/span><span style=\"color: #97E1F1; font-style: italic\">layers<\/span><span style=\"color: #F6F6F4\">.<\/span><span style=\"color: #97E1F1; font-style: italic\">Dense<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">1<\/span><span style=\"color: #F6F6F4\">, activation <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #E7EE98\">&#39;sigmoid&#39;<\/span><span style=\"color: #F6F6F4\">, bias_initializer <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> output_bias)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    ])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    model.compile(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        optimizer <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> keras.optimizers.<\/span><span style=\"color: #62E884\">Adam<\/span><span style=\"color: #F6F6F4\">(lr<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #BF9EEE\">1e<\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #BF9EEE\">3<\/span><span style=\"color: #F6F6F4\">),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        loss <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> keras.losses.<\/span><span style=\"color: #62E884\">BinaryCrossentropy<\/span><span style=\"color: #F6F6F4\">(),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">        metrics<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\">metrics<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    )<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">    return model<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u7ed3\u6784\u6bd4\u8f83\u7b80\u5355\uff0c\u6a21\u578b\u53ea\u6709\u4e00\u5c42\uff0c16\u4e2a\u795e\u7ecf\u5143\uff0c\u8f93\u51fa\u5c42\u53ea\u6709\u4e00\u4e2a\u795e\u7ecf\u5143\uff0c\u56e0\u4e3a\u662f\u4e8c\u5206\u7c7b\u95ee\u9898\uff0c\u5e26\u4e86\u4e00\u4e2asigmoid\u7684\u6fc0\u6d3b\u51fd\u6570\uff0c\u8f93\u51fa\u8be5\u6837\u672c\u662f\u4f2a\u9020\u4ea4\u6613\u7684\u6982\u7387\uff0c\u5373$P(\\hat{y}=1|x)$\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1.3 \u521d\u59cb\u6a21\u578b\u6548\u679c<\/h3>\n\n\n\n<p>\u73b0\u5728\u76f4\u63a5\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u7684\u6a21\u578b\uff08\u8fd8\u6ca1\u8bad\u7ec3\uff09\u8f93\u51fa\u6837\u672c\u7684\u524d1000\u4e2a\uff0c\u770b\u6982\u7387\u6570\u636e\u60c5\u51b5\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"output = model.predict(train_features[:1000])\nplt.hist(output, bins=20)\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">output <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> model.<\/span><span style=\"color: #62E884\">predict<\/span><span style=\"color: #F6F6F4\">(train_features[<\/span><span style=\"color: #F286C4\">:<\/span><span style=\"color: #BF9EEE\">1000<\/span><span style=\"color: #F6F6F4\">])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">plt.<\/span><span style=\"color: #62E884\">hist<\/span><span style=\"color: #F6F6F4\">(output, bins<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #BF9EEE\">20<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"775\" height=\"631\" src=\"https:\/\/tensor.agenthub.uk\/wp-content\/uploads\/2024\/03\/image-4.png\" alt=\"\" class=\"wp-image-232\" style=\"width:384px;height:auto\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-4.png 775w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-4-300x244.png 300w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-4-768x625.png 768w\" sizes=\"auto, (max-width: 775px) 100vw, 775px\" \/><\/figure>\n<\/div>\n\n\n<p><span style=\"font-size: revert; color: initial; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, sans-serif;\">\u4ece\u76f4\u65b9\u56fe\u4e0a\u770b\uff0c\u8f93\u51fa\u7684\u503c\u5927\u90fd\u96c6\u4e2d\u57280.6\u4ee5\u540e\uff0c\u56e0\u4e3a\u6a21\u578b\u8f93\u51fa\u7684\u6837\u672c\u662f\u4f2a\u9020\u4ea4\u6613\u7684\u6982\u7387\uff0c\u8fd9\u56fe\u660e\u663e\u4e0d\u7b26\u5408\u6570\u636e\u96c6\u7684\u5b9e\u9645\u5206\u5e03\uff0c\u4e8e\u662f\u4f1a\u5bfc\u81f4Loss\u975e\u5e38\u5de8\u5927\uff0c\u8981\u7ecf\u8fc7\u591a\u8f6e\u8fed\u4ee3\u624d\u80fd\u6536\u655b\uff5e<\/span><\/p>\n\n\n\n<p><div data-page-id=\"GGyMdgmFGos8MqxzEQbc1BRQnId\" data-docx-has-block-data=\"false\"><div class=\"ace-line ace-line old-record-id-KVRWdMCPMoGA7Px4w6bcUeyznle\">\u6a21\u578b\u7684loss\u4f7f\u7528BinaryCrossEntropy\uff0c\u5f53\u524d\u8fd91000\u4e2a\u6837\u672c\u7684loss<\/div><\/div><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"tf.reduce_mean(tf.keras.losses.binary_crossentropy(y_true=train_labels[:1000], y_pred=output))\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">tf.<\/span><span style=\"color: #62E884\">reduce_mean<\/span><span style=\"color: #F6F6F4\">(tf.keras.losses.<\/span><span style=\"color: #62E884\">binary_crossentropy<\/span><span style=\"color: #F6F6F4\">(y_true<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\">train_labels[<\/span><span style=\"color: #F286C4\">:<\/span><span style=\"color: #BF9EEE\">1000<\/span><span style=\"color: #F6F6F4\">], y_pred<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\">output))<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u6211\u8fd9\u8fb9\u8f93\u51fa:1.8788428\uff0c\u6211\u60f3\u4f60\u53ef\u80fd\u4f1a\u95ee\uff5e\u6ca1\u6709\u5bf9\u6bd4\u600e\u4e48\u77e5\u9053\u5927\u4e0d\u5927\uff1f\u662f\u76f4\u89c9\uff5e\u7eaf\u7eaf\u7684\u201c\u6211\u89c9\u5f97\u201d\uff5e\u56e0\u4e3a\u6a21\u578b\u8f93\u51fa\u7684\u5206\u5e03\u548c\u5b9e\u9645\u7684\u5206\u5e03\u5dee\u5f02\u5de8\u5927\uff0c\u6240\u4ee5\u4f60\u5c31\u60f3\uff5e\u51ed\u7a7a\u60f3\uff5e\u8fd9\u4e2aloss\u662f\u975e\u5e38\u5927\u7684\u3002<span data-lark-record-data=\"{&quot;isCut&quot;:false,&quot;rootId&quot;:&quot;GGyMdgmFGos8MqxzEQbc1BRQnId&quot;,&quot;parentId&quot;:&quot;GGyMdgmFGos8MqxzEQbc1BRQnId&quot;,&quot;blockIds&quot;:[34,36],&quot;recordIds&quot;:[&quot;QyEPd30cDorgF6xYlEPcTE4anwj&quot;,&quot;KVRWdMCPMoGA7Px4w6bcUeyznle&quot;],&quot;recordMap&quot;:{&quot;QyEPd30cDorgF6xYlEPcTE4anwj&quot;:{&quot;id&quot;:&quot;QyEPd30cDorgF6xYlEPcTE4anwj&quot;,&quot;snapshot&quot;:{&quot;type&quot;:&quot;text&quot;,&quot;children&quot;:[],&quot;comments&quot;:[],&quot;author&quot;:&quot;7033949406227333121&quot;,&quot;text&quot;:{&quot;initialAttributedTexts&quot;:{&quot;text&quot;:{&quot;0&quot;:&quot;\u4ece\u76f4\u65b9\u56fe\u4e0a\u770b\uff0c\u8f93\u51fa\u7684\u503c\u5927\u90fd\u96c6\u4e2d\u57280.6\u4ee5\u540e\uff0c\u56e0\u4e3a\u6a21\u578b\u8f93\u51fa\u7684\u6837\u672c\u662f\u4f2a\u9020\u4ea4\u6613\u7684\u6982\u7387\uff0c\u8fd9\u56fe\u660e\u663e\u4e0d\u7b26\u5408\u6570\u636e\u96c6\u7684\u5b9e\u9645\u5206\u5e03\uff0c\u4e8e\u662f\u4f1a\u5bfc\u81f4Loss\u975e\u5e38\u5de8\u5927\uff0c\u8981\u7ecf\u8fc7\u591a\u8f6e\u8fed\u4ee3\u624d\u80fd\u6536\u655b\uff5e&quot;},&quot;attribs&quot;:{&quot;0&quot;:&quot;*0+2a&quot;}},&quot;apool&quot;:{&quot;numToAttrib&quot;:{&quot;0&quot;:[&quot;author&quot;,&quot;7033949406227333121&quot;]},&quot;nextNum&quot;:1}},&quot;folded&quot;:false,&quot;parent_id&quot;:&quot;GGyMdgmFGos8MqxzEQbc1BRQnId&quot;}},&quot;KVRWdMCPMoGA7Px4w6bcUeyznle&quot;:{&quot;id&quot;:&quot;KVRWdMCPMoGA7Px4w6bcUeyznle&quot;,&quot;snapshot&quot;:{&quot;type&quot;:&quot;text&quot;,&quot;children&quot;:[],&quot;comments&quot;:[],&quot;author&quot;:&quot;7033949406227333121&quot;,&quot;text&quot;:{&quot;initialAttributedTexts&quot;:{&quot;text&quot;:{&quot;0&quot;:&quot;\u6a21\u578b\u7684loss\u4f7f\u7528BinaryCrossEntropy\uff0c\u5f53\u524d\u8fd91000\u4e2a\u6837\u672c\u7684loss\uff1a&quot;},&quot;attribs&quot;:{&quot;0&quot;:&quot;*0+18&quot;}},&quot;apool&quot;:{&quot;numToAttrib&quot;:{&quot;0&quot;:[&quot;author&quot;,&quot;7033949406227333121&quot;]},&quot;nextNum&quot;:1}},&quot;folded&quot;:false,&quot;parent_id&quot;:&quot;GGyMdgmFGos8MqxzEQbc1BRQnId&quot;}},&quot;GGyMdgmFGos8MqxzEQbc1BRQnId&quot;:{&quot;id&quot;:&quot;GGyMdgmFGos8MqxzEQbc1BRQnId&quot;,&quot;snapshot&quot;:{&quot;type&quot;:&quot;page&quot;,&quot;parent_id&quot;:&quot;&quot;,&quot;comments&quot;:null,&quot;revisions&quot;:null,&quot;locked&quot;:false,&quot;hidden&quot;:false,&quot;author&quot;:&quot;7072930266230079516&quot;,&quot;children&quot;:[&quot;CDHldNXCjoC3gyxwMJjcRxu4nYe&quot;,&quot;SxBOdJ69SoujBsxpskScDNj4nrh&quot;,&quot;RD1XdSCTeoZM9dx6u9OcldE8nde&quot;,&quot;SiSydOUYtoTXVqxZckec9IyknTf&quot;,&quot;ZD7DdlScUoMe7gxJ5dAcFnRCntb&quot;,&quot;ZrxJdafDzone4ZxEywNc9U1cnrf&quot;,&quot;EUfEdqorpoT4LQxUTOeco3oznPc&quot;,&quot;EUZfdp0ZjopSHhxTHX2cvhxlnth&quot;,&quot;MX1nd9HxdoF7m7xqnL6cJZu1nKc&quot;,&quot;TvhndUrV0oOOvnxwFFDc9Alen1c&quot;,&quot;IKbtd9vTnoGaNmxtY4kcPnPon7g&quot;,&quot;KcawdsikdoKSc6xmlQ4ceIQenmf&quot;,&quot;XI0Qd5FznozIXyxG5tLc7OyXn1d&quot;,&quot;SymWdUcT7oXiynxSUVRcKLHAn4d&quot;,&quot;GEeEd90i2oxwDzxLWDZchTRcnvh&quot;,&quot;FzSpdJM8pou13yxnCQ7cFBZHn3e&quot;,&quot;OYqydCWSYoddXIxcKDtcA90bn6f&quot;,&quot;IllRdBSpjo30IcxBwrAcaMXlnNb&quot;,&quot;QyEPd30cDorgF6xYlEPcTE4anwj&quot;,&quot;KVRWdMCPMoGA7Px4w6bcUeyznle&quot;,&quot;AhE3dGxv3o8U0Fx0uwhc2ODgnVg&quot;,&quot;WIyZd9DyMoR1YQxqRbGc4UbLnrs&quot;,&quot;JSRbd0U3QorA62xtlOkcGqqTn2A&quot;,&quot;EwiqdICuao9EvtxQvBucQ4xAnDg&quot;,&quot;J9AZdQrXdorNYDxjClPcOKbrnwe&quot;,&quot;ZNfAdETyloQRCRxBSSGcQbOtnWh&quot;,&quot;JkSLdVx1kob3Nyx7239cygTQnbg&quot;,&quot;UMP9dnVvHoLRlqxxuU3cjoPBnnc&quot;,&quot;Jsx0dxI3MoVvGSxp8c0cwCKRnrY&quot;,&quot;BcJQdZfRVoDP1Gx7QA2cHiN4nuf&quot;,&quot;Q1tldsc9nodRFdxNWzhcGs2HnTm&quot;,&quot;J2P7dv45HoSSs8xCq3rc9Fofnkf&quot;,&quot;HHCYdFdRzoJxDBxVGYNcvXchnoe&quot;,&quot;JxMjd27nLo2ttexRiZBcfT0Knnh&quot;,&quot;YeL6dtDRfodeeExVqsmcZgfRnqc&quot;,&quot;JgdGd0l38onwAZxS1rWcNVrfnqh&quot;,&quot;AyzLdlSXhoomijxajmlcM5Hfn0g&quot;,&quot;JtxUdRe3eo3WpCxy2FpcRO83n8a&quot;,&quot;JPzFdXZHiomXMixgdvXcWNiJnlf&quot;,&quot;TW0pdfe1foJ8sRxNHbgcGNpZn9d&quot;,&quot;CJ4xdwzW2okJgaxq7MFcjr3WnVf&quot;,&quot;UYO3dpNveonNUmx685AcF35AnYb&quot;,&quot;OLBqdhUWUoYjGzxywr1c9infnIb&quot;,&quot;XZwKdhLHVoxbY6xtqfoclFXJnth&quot;,&quot;YjZbdRjTRoOHEjxMjMMcQxjhnMc&quot;,&quot;ELmKdQPXCojGpuxBAaJcvDvRnag&quot;,&quot;EUUgdYDt6okRGQxk4x4cEH9Envc&quot;,&quot;T5yGdmz70oHZvdxVXIicytFOnWc&quot;,&quot;AhPLdRIptoNqpNx7ZOOcdTjDn4g&quot;,&quot;Pev8dL8zzoqIo5xH08gcYkYonDg&quot;,&quot;EI8tdGBuBo3vohxDuClcnG27nCd&quot;,&quot;EzoudViFzoWM61xFtg1cYPopnyg&quot;,&quot;Y63udBBS6ol7FhxZMuocVfqDnjd&quot;,&quot;WEEjdy19Oo3eiexVEgGcHvPln0g&quot;,&quot;Aq7tdVYrGoUcYqxjJcTctbxInpf&quot;,&quot;P54Jd9sI0oZzNcxhOOvcap6BnTg&quot;,&quot;FcEmdUyLyoaGnexqGd2c7TUUnTI&quot;,&quot;YBvIdBIwdozB7mxl762cVj5Enhd&quot;],&quot;text&quot;:{&quot;apool&quot;:{&quot;numToAttrib&quot;:{&quot;0&quot;:[&quot;author&quot;,&quot;7033949406227333121&quot;]},&quot;nextNum&quot;:1,&quot;attribToNum&quot;:{&quot;author,7033949406227333121&quot;:0}},&quot;initialAttributedTexts&quot;:{&quot;text&quot;:{&quot;0&quot;:&quot;\u3010\u65e0\u7ebf\u5e73\u53f0\u3011\u8bbe\u5b9abias\u89e3\u51b3\u6837\u672c\u4e0d\u5747\u8861\u95ee\u9898&quot;},&quot;attribs&quot;:{&quot;0&quot;:&quot;*0+l&quot;},&quot;rows&quot;:{},&quot;cols&quot;:{}}},&quot;align&quot;:&quot;&quot;}}},&quot;payloadMap&quot;:{&quot;QyEPd30cDorgF6xYlEPcTE4anwj&quot;:{&quot;level&quot;:1},&quot;KVRWdMCPMoGA7Px4w6bcUeyznle&quot;:{&quot;level&quot;:1}},&quot;extra&quot;:{&quot;channel&quot;:&quot;saas&quot;,&quot;mention_page_title&quot;:{},&quot;external_mention_url&quot;:{}},&quot;isKeepQuoteContainer&quot;:false,&quot;selection&quot;:[{&quot;id&quot;:34,&quot;type&quot;:&quot;text&quot;,&quot;selection&quot;:{&quot;start&quot;:0,&quot;end&quot;:82},&quot;recordId&quot;:&quot;QyEPd30cDorgF6xYlEPcTE4anwj&quot;},{&quot;id&quot;:36,&quot;type&quot;:&quot;text&quot;,&quot;selection&quot;:{&quot;start&quot;:0,&quot;end&quot;:44},&quot;recordId&quot;:&quot;KVRWdMCPMoGA7Px4w6bcUeyznle&quot;}],&quot;pasteFlag&quot;:&quot;24e5ae01-55cd-4fa0-a4dc-676c4f75b2b3&quot;}\" data-lark-record-format=\"docx\/record\" class=\"lark-record-clipboard\"><\/span><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2 \u8bbe\u5b9abias<\/h2>\n\n\n\n<p>\u521d\u59cb\u5316\u7684\u53c2\u6570\u8ddf\u5b9e\u9645\u6837\u672c\u7684\u6982\u7387\u5206\u5e03\u5dee\u5f02\u6bd4\u8f83\u5927\uff0c\u4f1a\u5bfc\u81f4loss\u6bd4\u8f83\u5927(Cross Entropy)\uff0c\u8fd9\u6837\u9700\u8981\u7ecf\u8fc7\u597d\u591a\u8f6e\u7684\u8fed\u4ee3\u624d\u80fd\u6536\u655b\uff0c\u6216\u8005\u6709\u53ef\u80fd\u65e0\u6cd5\u6536\u655b\u7684\u3002\u6211\u4eec\u76f4\u63a5\u7ed9\u4ed6\u4e00\u4e2a\u5927\u7684bias\uff0c\u8ba9\u6a21\u578b\u7684\u521d\u59cb\u8f93\u51fa\u5c31\u63a5\u8fd1\u771f\u5b9e\u7684\u5206\u5e03\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.1 \u7406\u8bba\u63a8\u5bfc<\/h3>\n\n\n\n<p>\u4e2d\u95f4\u5c42\u7684\u795e\u7ecf\u5143\u6bd4\u8f83\u591a\uff0c\u8fd8\u5f97\u8003\u8651\u94fe\u5f0f\u6cd5\u5219\uff0c\u8c03\u6574\u8d77\u6765\u662f\u6bd4\u8f83\u9ebb\u70e6\u7684\uff0c\u8f93\u51fa\u5c42\u53ea\u6709\u4e00\u4e2a\u795e\u7ecf\u5143\u6bd4\u8f83\u597d\u8c03\u6574\uff0c\u8ba1\u7b97\u903b\u8f91\u6bd4\u8f83\u7b80\u5355<\/p>\n\n\n\n<p>Linear\u5c42\u7684\u5f62\u5f0f\uff08\u4e00\u4e2a\u6837\u672c\u7684\u8ba1\u7b97\u8fc7\u7a0b\uff09<\/p>\n\n\n\n<p>$$\\hat{y}= \\textbf{w} \\cdot \\textbf{x}+ b$$<\/p>\n\n\n\n<p>\u56e0\u4e3a\u4f7f\u7528\u4e86sigmoid\u6fc0\u6d3b\u51fd\u6570\uff0c\u6700\u540e\u4e00\u5c42\u7684\u8f93\u51fa\u662f<\/p>\n\n\n\n<p>$$\\sigma(\\hat{y}) = \\frac{1}{1 + exp( -\\textbf{w} \\cdot \\textbf{x}- b)}$$<\/p>\n\n\n\n<p>\u4ece\u6982\u7387\u89d2\u5ea6\u770b\u4e0a\u9762\u8f93\u51fa\u7684\u662f$P(\\hat{y}=1|\\textbf{x})$\uff0c\u90a3\u4e48$P(\\hat{y}=0|\\textbf{x})$\u662f<\/p>\n\n\n\n<p>$$P(\\hat{y}=0|\\textbf{x}) = \\frac{exp(-\\textbf{w} \\cdot \\textbf{x}- b))}{1 + exp(-\\textbf{w} \\cdot \\textbf{x}- b))}$$<\/p>\n\n\n\n<p>\u6839\u636e1.1\u4e2d\u7ed9\u51fa\u7684\u5b9e\u9645\u6837\u672c\uff0c\u8d1f\u6837\u672c\u548c\u6b63\u6837\u672c\u7684\u6bd4\u4f8b\u5e94\u8be5\u662f$$\\frac{284315}{492}$$\uff0c\u4e5f\u5c31\u662f\u8bf4\u671f\u671b\u6a21\u578b\u8f93\u51fa\u6982\u7387\u6700\u597d\u662f\u7b26\u5408\u8fd9\u4e2a\u6982\u7387\u7684<\/p>\n\n\n\n<p>$$\\frac{P(\\hat{y}=0|\\textbf{x})}{P(\\hat{y}=1|\\textbf{x})} \\propto \\frac{284315}{492}$$<\/p>\n\n\n\n<p>\u8fd9\u91cc\u6ce8\u610f\uff0c\u6211\u6ca1\u4f7f\u7528\u7b49\u53f7\uff0c\u7528\u4e86\u7b49\u6bd4$\\propto$\uff0c\u56e0\u4e3a\u6234\u5e3d\u5b50\ud83c\udfa9\u7684$\\hat{y}$\u662f\u9884\u4f30\u503c\u53ea\u662f\u54b1\u4eec\u671f\u671b\u4ed6\u8f93\u51fa\u5927\u6982\u8fd9\u4e48\u4e2a\u6982\u7387\uff0c\u628a\u4e0a\u9762\u7684\u516c\u5f0f\u4eec\u5316\u7b80\u4e00\u4e0b:<\/p>\n\n\n\n<p>$$\\frac{P(\\hat{y}=0|\\textbf{x})}{P(\\hat{y}=1|\\textbf{x})} =\\frac{exp(-\\textbf{w} \\cdot \\textbf{x}- b))}{1 + exp(-\\textbf{w} \\cdot \\textbf{x}- b))} \\times (1 + exp(-\\textbf{w} \\cdot \\textbf{x}- b)))=exp(-\\textbf{w} \\cdot \\textbf{x}- b)$$<\/p>\n\n\n\n<p>\u6700\u540e\u5269\u4e0b\u7684\u8fd9\u90e8\u5206$exp(-\\textbf{w} \\cdot \\textbf{x}- b))=exp(-\\textbf{w} \\cdot \\textbf{x})exp(-b)$,$w$\u6211\u4eec\u4e0d\u52a8\uff0c$b$\u672c\u6765\u5c31\u662fbias\u561b\uff0c\u901a\u8fc7\u8c03\u6574\u5b83\u6765\u5f71\u54cd\u6a21\u578b\u7684\u8f93\u51fa\uff0c\u4f7f\u5f97\u8f93\u51fa\u5206\u5e03\u63a5\u8fd1\u6837\u672c\u7684\u5b9e\u9645\u5206\u5e03<\/p>\n\n\n\n<p>$$exp(-b) = \\frac{284315}{492} \\rightarrow b = -\\log(\\frac{284315}{492}) = -6.35936$$<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.2 \u5b9e\u8df5\u4e00\u4e0b<\/h3>\n\n\n\n<p>\u4e8e\u662f\u6211\u4eec\u6839\u636e\u8ba1\u7b97\u7684\u7ed3\u679c\u91cd\u65b0\u6784\u9020\u4e00\u4e2a\u6a21\u578b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"initial_bias = -np.log(284315 \/ 492)\nmodel2 = make_model(output_bias=initial_bias)\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">initial_bias <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">-<\/span><span style=\"color: #F6F6F4\">np.<\/span><span style=\"color: #62E884\">log<\/span><span style=\"color: #F6F6F4\">(<\/span><span style=\"color: #BF9EEE\">284315<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #F286C4\">\/<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #BF9EEE\">492<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">model2 <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> <\/span><span style=\"color: #62E884\">make_model<\/span><span style=\"color: #F6F6F4\">(output_bias<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\">initial_bias)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u770b\u4e00\u4e0b\u8c03\u6574\u540e\u521d\u59cb\u6a21\u578b\uff08\u672a\u8bad\u7ec3\uff09\u7684\u8f93\u51fa\u60c5\u51b5<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"output2 = model2.predict(train_features[:1000])\nplt.hist(output2, bins=20)\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">output2 <\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\"> model2.<\/span><span style=\"color: #62E884\">predict<\/span><span style=\"color: #F6F6F4\">(train_features[<\/span><span style=\"color: #F286C4\">:<\/span><span style=\"color: #BF9EEE\">1000<\/span><span style=\"color: #F6F6F4\">])<\/span><\/span>\n<span class=\"line\"><span style=\"color: #F6F6F4\">plt.<\/span><span style=\"color: #62E884\">hist<\/span><span style=\"color: #F6F6F4\">(output2, bins<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #BF9EEE\">20<\/span><span style=\"color: #F6F6F4\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"805\" height=\"635\" src=\"https:\/\/tensor.agenthub.uk\/wp-content\/uploads\/2024\/03\/image-5.png\" alt=\"\" class=\"wp-image-233\" style=\"width:403px;height:auto\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-5.png 805w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-5-300x237.png 300w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-5-768x606.png 768w\" sizes=\"auto, (max-width: 805px) 100vw, 805px\" \/><\/figure>\n<\/div>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"466\" height=\"342\" src=\"https:\/\/tensor.agenthub.uk\/wp-content\/uploads\/2024\/03\/image-6.png\" alt=\"\" class=\"wp-image-234\" style=\"width:87px;height:auto\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-6.png 466w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-6-300x220.png 300w\" sizes=\"auto, (max-width: 466px) 100vw, 466px\" \/><\/figure>\n<\/div>\n\n\n<p><span style=\"font-size: revert; color: initial; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Oxygen-Sans, Ubuntu, Cantarell, &quot;Helvetica Neue&quot;, sans-serif;\">\u6bd5\u7adf\u5b9e\u9645\u7684\u6570\u636e\u96c6\u5927\u591a\u6570\u90fd\u662f0\u561b\uff0c\u8fd9\u4e5f\u7b97\u5408\u7406\u3002\u770b\u4e00\u4e0b\u5f53\u524d\u7684Loss<\/span><span data-lark-record-data=\"{&quot;rootId&quot;:&quot;GGyMdgmFGos8MqxzEQbc1BRQnId&quot;,&quot;text&quot;:{&quot;initialAttributedTexts&quot;:{&quot;text&quot;:{&quot;0&quot;:&quot;\u6bd5\u7adf\u5b9e\u9645\u7684\u6570\u636e\u96c6\u5927\u591a\u6570\u90fd\u662f0\u561b\uff0c\u8fd9\u4e5f\u7b97\u5408\u7406\u3002\u770b\u4e00\u4e0b\u5f53\u524d\u7684Loss&quot;},&quot;attribs&quot;:{&quot;0&quot;:&quot;*0+w&quot;}},&quot;apool&quot;:{&quot;numToAttrib&quot;:{&quot;0&quot;:[&quot;author&quot;,&quot;7033949406227333121&quot;]},&quot;nextNum&quot;:1}},&quot;type&quot;:&quot;text&quot;,&quot;referenceRecordMap&quot;:{},&quot;extra&quot;:{&quot;mention_page_title&quot;:{},&quot;external_mention_url&quot;:{}},&quot;isKeepQuoteContainer&quot;:false,&quot;isFromCode&quot;:false,&quot;selection&quot;:[{&quot;id&quot;:77,&quot;type&quot;:&quot;text&quot;,&quot;selection&quot;:{&quot;start&quot;:0,&quot;end&quot;:32},&quot;recordId&quot;:&quot;T5yGdmz70oHZvdxVXIicytFOnWc&quot;}],&quot;payloadMap&quot;:{},&quot;isCut&quot;:false}\" data-lark-record-format=\"docx\/text\" class=\"lark-record-clipboard\"><\/span><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.75rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#f6f6f4;--cbp-line-number-width:calc(1 * 0.6 * .75rem);line-height:1rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#282A36\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"tf.reduce_mean(tf.keras.losses.binary_crossentropy(y_true=train_labels[:1000], y_pred=output2))\" style=\"color:#f6f6f4;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dracula-soft\" style=\"background-color: #282A36\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #F6F6F4\">tf.<\/span><span style=\"color: #62E884\">reduce_mean<\/span><span style=\"color: #F6F6F4\">(tf.keras.losses.<\/span><span style=\"color: #62E884\">binary_crossentropy<\/span><span style=\"color: #F6F6F4\">(y_true<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\">train_labels[<\/span><span style=\"color: #F286C4\">:<\/span><span style=\"color: #BF9EEE\">1000<\/span><span style=\"color: #F6F6F4\">], y_pred<\/span><span style=\"color: #F286C4\">=<\/span><span style=\"color: #F6F6F4\">output2))<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p>\u77ac\u95f4\u5c31\u8dcc\u5012\u4e86\uff1a0.008621918\uff0c\u8fd9\u4e48\u770b\u6765\u76f8\u6bd4\u4e4b\u524d\u6ca1\u8c03\u6574\u7684\u521d\u59cb\u5316\u53c2\u6570\uff0c\u7f29\u5c0f\u4e86200\u591a\u500d\uff5e\uff5e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3 \u5bf9\u6bd4<\/h2>\n\n\n\n<p>\u6765\u628a\u4e24\u4e2a\u6a21\u578b\u5206\u522b\u8fed\u4ee3\u4e8620\u8f6e\uff0c\u89c2\u5bdf\u4e00\u4e0bLoss\u7684\u53d8\u5316\u60c5\u51b5\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"893\" height=\"645\" src=\"https:\/\/tensor.agenthub.uk\/wp-content\/uploads\/2024\/03\/image-7.png\" alt=\"\" class=\"wp-image-235\" style=\"width:568px;height:auto\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-7.png 893w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-7-300x217.png 300w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-7-768x555.png 768w\" sizes=\"auto, (max-width: 893px) 100vw, 893px\" \/><\/figure>\n<\/div>\n\n\n<p>\u5b9e\u7ebf\u662ftrain loss\uff0c \u865a\u7ebf\u662fval loss\u3002\u84dd\u8272\u662f\u4f7f\u7528\u9ed8\u8ba4\u521d\u59cb\u5316\u53c2\u6570\u7684\u6a21\u578b\u8f93\u51fa\u7684\uff0c\u6a58\u8272\u662f\u4eba\u4e3a\u7ed9\u5b9abias\u7684\u6a21\u578b\uff0c\u660e\u663e\u6a58\u8272\u5728\u4e00\u5f00\u59cbloss\u5c31\u6bd4\u8f83\u4f4e\uff0c\u540e\u9762\u4e00\u76f4\u4e5f\u90fd\u4fdd\u6301\u7684\u6bd4\u8f83\u597d\uff5e\u84dd\u8272\u5728\u8bad\u7ec3\u4e8620\u8f6e\u540e\u4e5f\u6ca1\u6709\u8fbe\u5230\u6a58\u8272\u7684Loss\uff5e<\/p>\n\n\n\n<p>PS:\u611f\u6069\u8c37\u6b4c\uff5e<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"864\" height=\"856\" src=\"https:\/\/tensor.agenthub.uk\/wp-content\/uploads\/2024\/03\/image-8.png\" alt=\"\" class=\"wp-image-236\" style=\"width:118px;height:auto\" srcset=\"https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-8.png 864w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-8-300x297.png 300w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-8-150x150.png 150w, https:\/\/tensorzen.blog\/wp-content\/uploads\/2024\/03\/image-8-768x761.png 768w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e3b\u8981\u8fc7\u901a\u8fc7\u8bbe\u5b9abias\u6765\u89e3\u51b3\u6837\u672c\u4e25\u91cd\u4e0d\u5747\u8861\u7684\u95ee\u9898\uff0c\u5305\u62ec\u7406\u8bba\u63a8\u5bfc\u548c\u5b9e\u8df5\u8fc7\u7a0b\u3002\u4e4b\u524d\u770b\u8fc7\u4e00\u4e2a\u4f8b\u5b50\uff0c\u8fd8\u633a\u6709\u8da3\u7684\uff0c\u8fd9\u91cc\u7ed9\u4e61\u4eb2\u4eec\u4ecb\u7ecd\u4e0b\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,4],"tags":[24],"class_list":["post-228","post","type-post","status-publish","format-standard","hentry","category-base","category-machine-learning","tag-machine-learning"],"_links":{"self":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=228"}],"version-history":[{"count":8,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":337,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions\/337"}],"wp:attachment":[{"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tensorzen.blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}