{"id":2754,"date":"2020-01-13T15:59:01","date_gmt":"2020-01-13T07:59:01","guid":{"rendered":"http:\/\/www.sniper97.cn\/?p=2754"},"modified":"2020-01-13T15:59:01","modified_gmt":"2020-01-13T07:59:01","slug":"%e3%80%90%e5%90%b4%e6%81%a9%e8%be%be%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%e3%80%91%e5%85%b7%e6%9c%89%e7%a5%9e%e7%bb%8f%e7%bd%91%e7%bb%9c%e6%80%9d%e7%bb%b4%e7%9a%84logistic%e5%9b%9e%e5%bd%92","status":"publish","type":"post","link":"http:\/\/www.sniper97.cn\/index.php\/note\/deep-learning\/2754\/","title":{"rendered":"\u3010\u5434\u6069\u8fbe\u6df1\u5ea6\u5b66\u4e60\u3011\u5177\u6709\u795e\u7ecf\u7f51\u7edc\u601d\u7ef4\u7684Logistic\u56de\u5f52"},"content":{"rendered":"\n<p> \u5434\u6069\u8fbe\u6df1\u5ea6\u5b66\u4e60\u7b2c\u4e00\u8bfe\u7b2c\u4e8c\u5468  \u5177\u6709\u795e\u7ecf\u7f51\u7edc\u601d\u7ef4\u7684Logistic\u56de\u5f52\uff08\u5305\u62ec\u4f5c\u4e1a\uff09 <\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"318\" height=\"198\" src=\"\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-43.png\" alt=\"\" class=\"wp-image-2768\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-43.png 318w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-43-300x187.png 300w\" sizes=\"(max-width: 318px) 100vw, 318px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u5206\u5206\u7c7b<\/h2>\n\n\n<h3 class=\"wp-block-heading\">\u4ec0\u4e48\u662f\u4e8c\u5206\u7c7b\uff1f<\/h3>\n\n\n<p>\u4e8c\u5206\u7c7b\u95ee\u9898\u7b80\u5355\u8bf4\u5c31\u662f\u975e0\u53731\u7684\u95ee\u9898\uff0c\u4e3e\u4e2a\u4f8b\u5b50<\/p>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"947\" height=\"274\" src=\"\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-40.png\" alt=\"\" class=\"wp-image-2758\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-40.png 947w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-40-300x87.png 300w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-40-768x222.png 768w\" sizes=\"(max-width: 947px) 100vw, 947px\" \/><\/figure>\n\n\n<p>\u56fe\u7247\u4e2d\u7684\u662f\u4e0d\u662f\u732b\uff1f\u53ea\u6709\u662f\uff081\uff09\u6216\u8005\u4e0d\u662f\uff080\uff09\u3002<\/p>\n\n\n<h2 class=\"wp-block-heading\">\u903b\u8f91\u56de\u5f52<\/h2>\n\n\n<p>\u5177\u4f53\u67e5\u770b<a rel=\"noreferrer noopener\" aria-label=\"\u3010\u5434\u6069\u8fbe\u673a\u5668\u5b66\u4e60\u3011\u903b\u8f91\u56de\u5f52\uff08\u5728\u65b0\u7a97\u53e3\u6253\u5f00\uff09\" href=\"http:\/\/www.sniper97.cn\/index.php\/note\/machine-learning-in-action\/1519\/\" target=\"_blank\">\u3010\u5434\u6069\u8fbe\u673a\u5668\u5b66\u4e60\u3011\u903b\u8f91\u56de\u5f52<\/a><\/p>\n\n\n<h2 class=\"wp-block-heading\">\u5e7f\u64ad<\/h2>\n\n\n<p>\u6211\u4eec\u770b\u4e0b\u9762\u8fd9\u7ec4\u6570\u636e\uff0c\u8bf4\u660e\u4e86\u6bcf100g\u5404\u79cd\u98df\u7269\u4ece\u78b3\u6c34\u5316\u5408\u7269\u3001\u86cb\u767d\u8d28\u548c\u8102\u80aa\u4e2d\u83b7\u53d6\u5361\u8def\u91cc\u7684\u6570\u91cf\uff08g\uff09<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"544\" height=\"182\" src=\"\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-41.png\" alt=\"\" class=\"wp-image-2759\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-41.png 544w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-41-300x100.png 300w\" sizes=\"(max-width: 544px) 100vw, 544px\" \/><\/figure><\/div>\n\n\n<p>\u4ece\u56fe\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u6bcf100g\u82f9\u679c\u4e2d\uff0c\u5171\u670956+1.2+1.8=59g\u7684\u5361\u5e93\u91cc\uff0c\u5176\u4e2d\u63a5\u8fd195%\u90fd\u662f\u4ece\u78b3\u6c34\u5316\u5408\u7269\u4e2d\u83b7\u53d6\u7684\uff0c\u90a3\u4e48\u5982\u679c\u6211\u4eec\u60f3\u77e5\u9053\u6bcf\u4e00\u4e2a\u4f4d\u7f6e\u7684\u767e\u5206\u6bd4\u800c\u53c8\u4e0d\u4f7f\u7528for\u5faa\u73af\u8be5\u600e\u4e48\u505a\u5462\uff1f<\/p>\n\n\n<p>\u601d\u8003\u4e0b\u9762\u4ee3\u7801<\/p>\n\n\n<pre class=\"wp-block-code\"><code>cal = A.sum(axis=0)\npercentage = A \/ cal\nprint(percentage)<\/code><\/pre>\n\n\n<p>\u6211\u4eec\u7ecf\u8fc7\u7b2c\u4e00\u6b65\u8ba1\u7b97\uff0c\u83b7\u53d6\u4e86\u4e00\u4e2a\uff081\uff0c4\uff09\u7684cal\u77e9\u9635\uff0c\u800cA\u662f\u4e00\u4e2a\uff083\uff0c4\uff09\u7684\u77e9\u9635\uff0c\u8fd9\u65f6\u5019\u6211\u4eec\u5982\u679c\u6267\u884c\u7b2c\u4e8c\u884c\u64cd\u4f5c\uff0cpython\u4f1a\u81ea\u52a8\u5c06cal\u4ece\uff081\uff0c4\uff09\u6269\u5c55\u5230\uff083\uff0c4\uff09\u7136\u540e\u548cA\u8ba1\u7b97\uff0c\u6700\u7ec8\u8fbe\u5230\u6211\u4eec\u60f3\u8981\u7684\u8ba1\u7b97\u7ed3\u679c\u3002<\/p>\n\n\n<p>\u540c\u7406\uff0c\u5bf9\u4e8e\u4e00\u4e2a \uff08m, n\uff09\u7684\u77e9\u9635\u52a0\u4e0a\uff081, n\uff09\u7684\u77e9\u9635\uff0c\u53ef\u4ee5\u76f4\u63a5\u76f8\u52a0\uff0cpython\u4f1a\u81ea\u52a8\u6269\u5c55\u81f3\uff08m,n\uff09\u7136\u540e\u518d\u76f8\u52a0 \u3002<\/p>\n\n\n<h2 class=\"wp-block-heading\">\u6d4b\u9a8c<\/h2>\n\n\n<p><strong>1. \u795e\u7ecf\u5143\u8282\u70b9\u8ba1\u7b97\u4ec0\u4e48\uff1f <\/strong><\/p>\n\n\n<ol><li>\u795e\u7ecf\u5143\u8282\u70b9\u5148\u8ba1\u7b97\u6fc0\u6d3b\u51fd\u6570\uff0c\u518d\u8ba1\u7b97\u7ebf\u6027\u51fd\u6570(z = Wx + b)<\/li><li>\u795e\u7ecf\u5143\u8282\u70b9\u5148\u8ba1\u7b97\u7ebf\u6027\u51fd\u6570\uff08z = Wx + b\uff09\uff0c\u518d\u8ba1\u7b97\u6fc0\u6d3b\u3002<\/li><li>\u795e\u7ecf\u5143\u8282\u70b9\u8ba1\u7b97\u51fd\u6570g\uff0c\u51fd\u6570g\u8ba1\u7b97(Wx + b)\u3002<\/li><li>\u5728 \u5c06\u8f93\u51fa\u5e94\u7528\u4e8e\u6fc0\u6d3b\u51fd\u6570\u4e4b\u524d\uff0c\u795e\u7ecf\u5143\u8282\u70b9\u8ba1\u7b97\u6240\u6709\u7279\u5f81\u7684\u5e73\u5747\u503c<\/li><\/ol>\n\n\n<p>\uff082\uff09<\/p>\n\n\n<p><strong> 2.\u4e0b\u9762\u54ea\u4e00\u4e2a\u662fLogistic\u635f\u5931\uff1f <\/strong><\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"271\" height=\"57\" src=\"\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-42.png\" alt=\"\" class=\"wp-image-2761\"\/><\/figure><\/div>\n\n\n<p><strong>3. \u5047\u8bbeimg\u662f\u4e00\u4e2a\uff0832,32,3\uff09\u6570\u7ec4\uff0c\u5177\u67093\u4e2a\u989c\u8272\u901a\u9053\uff1a\u7ea2\u8272\u3001\u7eff\u8272\u548c\u84dd\u8272\u768432&#215;32\u50cf\u7d20\u7684\u56fe\u50cf\u3002 \u5982\u4f55\u5c06\u5176\u91cd\u65b0\u8f6c\u6362\u4e3a\u5217\u5411\u91cf\uff1f <\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><code>x = img.reshape(32*32*3,1)<\/code><\/pre>\n\n\n<p><strong>4. \u4e0b\u9762\u7684\u8fd9\u4e24\u4e2a\u968f\u673a\u6570\u7ec4\u201ca\u201d\u548c\u201cb\u201d, \u8bf7\u95ee\u6570\u7ec4<\/strong><code><strong>c<\/strong><\/code><strong>\u7684\u7ef4\u5ea6\u662f\u591a\u5c11\uff1f <\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><code>a = np.random.randn(2, 3) # a.shape = (2, 3)\nb = np.random.randn(2, 1) # b.shape = (2, 1)\nc = a + b<\/code><\/pre>\n\n\n<p>(2,3)<\/p>\n\n\n<p><strong>5. \u4e0b\u9762\u7684\u8fd9\u4e24\u4e2a\u968f\u673a\u6570\u7ec4\u201ca\u201d\u548c\u201cb\u201d  ,\u8bf7\u95ee\u6570\u7ec4\u201c<\/strong><code><strong>c<\/strong><\/code><strong>\u201d\u7684\u7ef4\u5ea6\u662f\u591a\u5c11\uff1f <\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><code>a = np.random.randn(4, 3) # a.shape = (4, 3)\nb = np.random.randn(3, 2) # b.shape = (3, 2)\nc = a * b<\/code><\/pre>\n\n\n<p>\u65e0\u6cd5\u8ba1\u7b97\u3002\u56e0\u4e3aa\u3001b\u4e24\u4e2a\u77e9\u9635\u7ef4\u5ea6\u4e0d\u540c\u3002.<\/p>\n\n\n<p><strong>6. \u5047\u8bbe\u4f60\u7684\u6bcf\u4e00\u4e2a\u5b9e\u4f8b\u6709n_x\u4e2a\u8f93\u5165\u7279\u5f81\uff0c\u60f3\u4e00\u4e0b\u5728X=[x^(1), x^(2)\u2026x^(m)]\u4e2d\uff0cX\u7684\u7ef4\u5ea6\u662f\u591a\u5c11\uff1f <\/strong><\/p>\n\n\n<p>\uff08n_x\uff0cm\uff09\u3002\u5c06\u8f93\u5165\u7279\u5f81\u8f6c\u5316\u4e3a\u5217\u5411\u91cf\uff0c\u7136\u540e\u53e0\u52a0m\u4e2a\u3002<\/p>\n\n\n<p><strong>7.  \u770b\u4e00\u4e0b\u4e0b\u9762\u7684\u8fd9\u4e24\u4e2a\u968f\u673a\u6570\u7ec4\u201ca\u201d\u548c\u201cb\u201d  \u8bf7\u95eec\u7684\u7ef4\u5ea6\u662f\u591a\u5c11\uff1f <\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><code>a = np.random.randn(12288, 150) # a.shape = (12288, 150)\nb = np.random.randn(150, 45) # b.shape = (150, 45)\nc = np.dot(a, b)<\/code><\/pre>\n\n\n<p>\uff0812288\uff0c45\uff09\u3002dot\u662f\u77e9\u9635\u70b9\u4e58\uff08\u77e9\u9635\u4e58\u6cd5\uff09\u3002<\/p>\n\n\n<p><strong>8. \u770b\u4e00\u4e0b\u4e0b\u9762\u7684\u8fd9\u4e2a\u4ee3\u7801\u7247\u6bb5\uff0c \u8bf7\u95ee\u8981\u600e\u4e48\u628a\u5b83\u4eec\u5411\u91cf\u5316\uff1f <\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><code># a.shape = (3,4)\n# b.shape = (4,1)\nfor i in range(3):\n  for j in range(4):\n    c[i][j] = a[i][j] + b[j]<\/code><\/pre>\n\n\n<pre class=\"wp-block-code\"><code>c = a + b.T<\/code><\/pre>\n\n\n<p><strong>9. \u4e0b\u9762\u7684\u4ee3\u7801  \u8bf7\u95eec\u7684\u7ef4\u5ea6\u4f1a\u662f\u591a\u5c11\uff1f <\/strong><\/p>\n\n\n<pre class=\"wp-block-code\"><code>a = np.random.randn(3, 3)\nb = np.random.randn(3, 1)\nc = a * b<\/code><\/pre>\n\n\n<p>\uff083\uff0c3\uff09\u3002\u7531\u4e8e\u5e7f\u64ad\u673a\u5236\uff0cb\u4f1a\u88ab\u6269\u5c55\u6210\uff083\uff0c3\uff09\u7136\u540e\u6267\u884c\u77e9\u9635\u5143\u7d20\u76f8\u4e58\u3002<\/p>\n\n\n<h2 class=\"wp-block-heading\">\u7f16\u7a0b\u4f5c\u4e1a<\/h2>\n\n\n<p>\u6211\u4eec\u5728\u8fd9\u4e00\u8282\u8981\u505a\u7684\u5c31\u662f\u5199\u4e00\u4e2a\u80fd\u591f\u8bc6\u522b\u732b\u7684\u795e\u7ecf\u7f51\u7edc\uff0c\u5176\u5b9e\u8bf4\u662f\u795e\u7ecf\u7f51\u7edc\uff0c\u672c\u8d28\u4e0a\u5c31\u662f\u4e00\u4e2a\u903b\u8f91\u56de\u5f52\u52a0\u4e0a\u4e00\u4e2a\u53c2\u6570b\uff0c\u672c\u8d28\u4e0a\u90fd\u662f\u4e00\u4e2aax+b\u7684\u4e8c\u5206\u7c7b\u95ee\u9898\u3002<\/p>\n\n\n<p>\u8fd9\u5757\u8bf4\u660e\u7684\u6bd4\u8f83\u5c11\uff0c\u56e0\u4e3a\u603b\u4f53\u4e0a\u548c\u903b\u8f91\u56de\u5f52\u86ee\u50cf\u7684\uff0c\u5c31\u662f\u5dee\u4e86\u4e2a\u6743\u91cdb\u3002<\/p>\n\n\n<p>\u9996\u5148\u5bfc\u5305\uff1a<\/p>\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nimport matplotlib.pyplot as plt\nimport h5py\nfrom lr_utils import load_dataset<\/code><\/pre>\n\n\n<p>\u7136\u540e\u6211\u4eec\u521d\u59cb\u5316\u6570\u636e<\/p>\n\n\n<pre class=\"wp-block-code\"><code>train_set_x_orig , train_set_y , test_set_x_orig , test_set_y , classes = load_dataset()<\/code><\/pre>\n\n\n<p>\u6211\u4eec\u67e5\u770b\u4e00\u4e0b\u56fe\u7247\u6570\u636e\u5e76\u7b80\u5355\u67e5\u770b\u4e00\u4e0b\u6570\u636e\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\"># \u67e5\u770b\u4e00\u4e0b\u56fe\u7247<br \/>plt.imshow(train_set_x_pic[0])<br \/>plt.show()<br \/><br \/>print('\u8bad\u7ec3\u96c6\u5927\u5c0f:', train_set_x_pic.shape[0])<br \/>print('\u6d4b\u8bd5\u96c6\u5927\u5c0f:', test_set_x_pic.shape[0])<br \/>print('\u56fe\u7247\u7684\u5927\u5c0f:', train_set_x_pic.shape[1:])<\/pre>\n\n\n<p>\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-44.png\" alt=\"\" class=\"wp-image-2769\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-44.png 640w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-44-300x225.png 300w\" sizes=\"(max-width: 640px) 100vw, 640px\" \/><\/figure><\/div>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"251\" height=\"78\" src=\"\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-45.png\" alt=\"\" class=\"wp-image-2770\"\/><\/figure><\/div>\n\n\n<p>\u7136\u540e\u505a\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u636e\u9884\u5904\u7406\uff0c\u6bd4\u5982\u5c06\u6240\u6709\u7684\u56fe\u7247RGB\u6570\u636e\u7ec4\u6210\u6210\u4e00\u7ef4\u7684\u5e76\u8fdb\u884c\u6807\u51c6\u5316\u3002<\/p>\n\n\n<pre class=\"wp-block-preformatted\">train_set_x_flatten = train_set_x_pic.reshape(train_set_x_pic.shape[0], -1).T<br \/>test_set_x_flatten = test_set_x_pic.reshape(test_set_x_pic.shape[0], -1).T<br \/># \u6807\u51c6\u5316RGB\u989c\u8272\uff0c\u56e0\u6b64\u539f\u503c\u8303\u56f4\u8f83\u5927\uff0c\u6807\u51c6\u5316\u52300~1\u4e4b\u95f4<br \/>train_set_x = train_set_x_flatten \/ 255<br \/>test_set_x = test_set_x_flatten \/ 255<\/pre>\n\n\n<p>\u7136\u540e\u5c31\u662f\u51e0\u4e2a\u9700\u8981\u7528\u5230\u7684\u65b9\u6cd5\uff0c<\/p>\n\n\n<p>\u9996\u5148\u662f\u6fc0\u6d3b\u51fd\u6570sigmoid\u51fd\u6570<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def sigmoid(z):<br \/>    <em>\"\"\"<br \/><\/em><em>    sigmoid<\/em><em>\u51fd\u6570<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> z:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>return 1 \/ (1 + np.exp(-z))<\/pre>\n\n\n<p>\u7136\u540e\u662f\u524d\u5411\u4f20\u64ad\u548c\u53cd\u9988\u7684\u65b9\u6cd5<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def propagate(w, b, X, Y):<br \/>    <em>\"\"\"<br \/><\/em><em>    <\/em><em>\u524d\u540e\u4f20\u64ad\u4ee5\u53ca\u6210\u672c\u8ba1\u7b97<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> w:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> b:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> Y:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>n = X.shape[1]<br \/>    # \u8ba1\u7b97\u4ee3\u4ef7<br \/>    A = sigmoid(np.dot(w.T, X) + b)<br \/>    cost = np.mean(-Y * np.log(A) - (1 - Y) * np.log(1 - A))<br \/>    # \u53cd\u5411\u4f20\u64ad<br \/>    dw = (1 \/ n) * (X @ (A - Y).T)<br \/>    db = (1 \/ n) * np.sum(A - Y)<br \/>    return dw, db, cost<\/pre>\n\n\n<p>\u7136\u540e\u662f\u8c03\u7528n\u6b21\u4f20\u64ad\u4ee5\u83b7\u53d6\u4e00\u4e2a\u6bd4\u8f83\u597d\u7684\u7ed3\u679c\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def optimize(w, b, X, Y, iterations_times, learning_rate, print_cost=False):<br \/>    <em>\"\"\"<br \/><\/em><em>    <\/em><em>\u8fd0\u884c\u68af\u5ea6\u4e0b\u964d\u6765\u4f18\u5316<\/em><em>w<\/em><em>\u548c<\/em><em>b<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> w:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> b:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> Y:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> iterations_times:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> learning_rate:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> print_cost:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>costs = []<br \/>    for i in range(iterations_times):<br \/>        wb, db, cost = propagate(w, b, X, Y)<br \/>        w = w - learning_rate * wb<br \/>        b = b - learning_rate * db<br \/><br \/>        # \u8bb0\u5f55\u6210\u672c<br \/>        if i % 100 == 0:<br \/>            costs.append(cost)<br \/><br \/>        if print_cost and i % 100 == 0:<br \/>            print('\u8fed\u4ee3\u6b21\u6570\uff1a%i\uff0c\u8bef\u5dee\u503c\uff1a%f' % (i, cost))<br \/><br \/>    return w, b, costs<\/pre>\n\n\n<p>\u9884\u6d4b\u51fd\u6570\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def predict(w, b, X):<br \/>    <em>\"\"\"<br \/><\/em><em>    <\/em><em>\u9884\u6d4b<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> w:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> b:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>n = X.shape[1]<br \/>    Y_prediction = np.zeros((1, n))<br \/><br \/>    A = sigmoid((w.T @ X) + b)<br \/>    for i in range(A.shape[1]):<br \/>        Y_prediction[0, i] = 1 if A[0, i] &gt; 0.5 else 0<br \/>    return Y_prediction<\/pre>\n\n\n<p>\u5c06\u6240\u6709\u7684\u65b9\u6cd5\u6574\u5408\u8d77\u6765\uff0c\u65b9\u4fbf\u8c03\u7528\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\">def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.01, print_cost=True):<br \/>    <em>\"\"\"<br \/><\/em><em>    <\/em><em>\u603b\u4f53\u6a21\u578b<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X_train:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> Y_train:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X_test:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> Y_test:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> num_iterations:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> learning_rate:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> print_cost:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>w, b = init_weight(X_train.shape[0])<br \/>    wb, db, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)<br \/>    Y_prediction_test = predict(wb, db, X_test)<br \/><br \/>    print('\u8bad\u7ec3\u96c6\u51c6\u786e\u7387:', (1 - (np.mean(np.abs(Y_prediction_test - Y_test)))) * 100, '%')<br \/>    return wb, db, costs<\/pre>\n\n\n<p>\u6700\u540e\u6211\u4eec\u6765\u8c03\u7528\u4e00\u4e0b\u770b\u770b<\/p>\n\n\n<pre class=\"wp-block-preformatted\">d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=2000, learning_rate=0.005, print_cost=True)<br \/><\/pre>\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u8f93\u51fa\u5982\u4e0b\uff1a<\/p>\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"279\" height=\"427\" src=\"\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-46.png\" alt=\"\" class=\"wp-image-2771\" srcset=\"http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-46.png 279w, http:\/\/www.sniper97.cn\/wp-content\/uploads\/2020\/01\/\u56fe\u7247-46-196x300.png 196w\" sizes=\"(max-width: 279px) 100vw, 279px\" \/><\/figure><\/div>\n\n\n<p>\u5b8c\u6574\u4ee3\u7801\uff1a<\/p>\n\n\n<pre class=\"wp-block-preformatted\"># -*- coding:utf-8 -*-<br \/><br \/>import numpy as np<br \/>import matplotlib.pyplot as plt<br \/>import h5py<br \/>from course_1_week_2.lr_utils import load_dataset<br \/><br \/># \u8bad\u7ec3\u96c6\u56fe\u7247\uff0c \u8bad\u7ec3\u96c6\u6807\u7b7e\uff0c \u6d4b\u8bd5\u96c6\u56fe\u7247\uff0c\u6d4b\u8bd5\u96c6\u6807\u7b7e\uff0c\u5206\u7c7b\u6807\u7b7e\u6587\u672c\u63cf\u8ff0<br \/>train_set_x_pic, train_set_y, test_set_x_pic, test_set_y, classes = load_dataset()<br \/><br \/># \u67e5\u770b\u4e00\u4e0b\u56fe\u7247<br \/>plt.imshow(train_set_x_pic[0])<br \/>plt.show()<br \/><br \/>print('\u8bad\u7ec3\u96c6\u5927\u5c0f:', train_set_x_pic.shape[0])<br \/>print('\u6d4b\u8bd5\u96c6\u5927\u5c0f:', test_set_x_pic.shape[0])<br \/>print('\u56fe\u7247\u7684\u5927\u5c0f:', train_set_x_pic.shape[1:])<br \/><br \/>train_set_x_flatten = train_set_x_pic.reshape(train_set_x_pic.shape[0], -1).T<br \/>test_set_x_flatten = test_set_x_pic.reshape(test_set_x_pic.shape[0], -1).T<br \/># \u6807\u51c6\u5316RGB\u989c\u8272\uff0c\u56e0\u6b64\u539f\u503c\u8303\u56f4\u8f83\u5927\uff0c\u6807\u51c6\u5316\u52300~1\u4e4b\u95f4<br \/>train_set_x = train_set_x_flatten \/ 255<br \/>test_set_x = test_set_x_flatten \/ 255<br \/><br \/><br \/>def sigmoid(z):<br \/>    <em>\"\"\"<br \/><\/em><em>    sigmoid<\/em><em>\u51fd\u6570<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> z:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>return 1 \/ (1 + np.exp(-z))<br \/><br \/><br \/>def init_weight(dim):<br \/>    <em>\"\"\"<br \/><\/em><em>    <\/em><em>\u521d\u59cb\u5316\u7ef4\u5ea6<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> dim:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>w = np.zeros((dim, 1))<br \/>    b = 0<br \/>    return w, b<br \/><br \/><br \/>def propagate(w, b, X, Y):<br \/>    <em>\"\"\"<br \/><\/em><em>    <\/em><em>\u524d\u540e\u4f20\u64ad\u4ee5\u53ca\u6210\u672c\u8ba1\u7b97<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> w:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> b:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> Y:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>n = X.shape[1]<br \/>    # \u8ba1\u7b97\u4ee3\u4ef7<br \/>    A = sigmoid(np.dot(w.T, X) + b)<br \/>    cost = np.mean(-Y * np.log(A) - (1 - Y) * np.log(1 - A))<br \/>    # \u53cd\u5411\u4f20\u64ad<br \/>    dw = (1 \/ n) * (X @ (A - Y).T)<br \/>    db = (1 \/ n) * np.sum(A - Y)<br \/>    return dw, db, cost<br \/><br \/><br \/>def optimize(w, b, X, Y, iterations_times, learning_rate, print_cost=False):<br \/>    <em>\"\"\"<br \/><\/em><em>    <\/em><em>\u8fd0\u884c\u68af\u5ea6\u4e0b\u964d\u6765\u4f18\u5316<\/em><em>w<\/em><em>\u548c<\/em><em>b<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> w:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> b:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> Y:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> iterations_times:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> learning_rate:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> print_cost:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>costs = []<br \/>    for i in range(iterations_times):<br \/>        wb, db, cost = propagate(w, b, X, Y)<br \/>        w = w - learning_rate * wb<br \/>        b = b - learning_rate * db<br \/><br \/>        # \u8bb0\u5f55\u6210\u672c<br \/>        if i % 100 == 0:<br \/>            costs.append(cost)<br \/><br \/>        if print_cost and i % 100 == 0:<br \/>            print('\u8fed\u4ee3\u6b21\u6570\uff1a%i\uff0c\u8bef\u5dee\u503c\uff1a%f' % (i, cost))<br \/><br \/>    return w, b, costs<br \/><br \/><br \/>def predict(w, b, X):<br \/>    <em>\"\"\"<br \/><\/em><em>    <\/em><em>\u9884\u6d4b<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> w:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> b:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>n = X.shape[1]<br \/>    Y_prediction = np.zeros((1, n))<br \/><br \/>    A = sigmoid((w.T @ X) + b)<br \/>    for i in range(A.shape[1]):<br \/>        Y_prediction[0, i] = 1 if A[0, i] &gt; 0.5 else 0<br \/>    return Y_prediction<br \/><br \/><br \/>def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.01, print_cost=True):<br \/>    <em>\"\"\"<br \/><\/em><em>    <\/em><em>\u603b\u4f53\u6a21\u578b<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X_train:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> Y_train:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> X_test:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> Y_test:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> num_iterations:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> learning_rate:<br \/><\/em><em>    <\/em><strong><em>:param<\/em><\/strong><em> print_cost:<br \/><\/em><em>    <\/em><strong><em>:return<\/em><\/strong><em>:<br \/><\/em><em>    \"\"\"<br \/><\/em><em>    <\/em>w, b = init_weight(X_train.shape[0])<br \/>    wb, db, costs = optimize(w, b, X_train, Y_train, num_iterations, learning_rate, print_cost)<br \/>    Y_prediction_test = predict(wb, db, X_test)<br \/><br \/>    print('\u8bad\u7ec3\u96c6\u51c6\u786e\u7387:', (1 - (np.mean(np.abs(Y_prediction_test - Y_test)))) * 100, '%')<br \/>    return wb, db, costs<br \/><br \/><br \/>d = model(train_set_x, train_set_y, test_set_x, test_set_y, num_iterations=2000, learning_rate=0.005, print_cost=True)<br \/><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5434\u6069\u8fbe\u6df1\u5ea6\u5b66\u4e60\u7b2c\u4e00\u8bfe\u7b2c\u4e8c\u5468 \u5177\u6709\u795e\u7ecf\u7f51\u7edc\u601d\u7ef4\u7684Logistic\u56de\u5f52\uff08\u5305\u62ec\u4f5c\u4e1a\uff09 \u4e8c\u5206\u5206\u7c7b \u4ec0\u4e48\u662f\u4e8c\u5206 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[],"views":4389,"_links":{"self":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2754"}],"collection":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/comments?post=2754"}],"version-history":[{"count":0,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/posts\/2754\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/media?parent=2754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/categories?post=2754"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sniper97.cn\/index.php\/wp-json\/wp\/v2\/tags?post=2754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}