{"id":987751,"date":"2024-12-27T07:56:09","date_gmt":"2024-12-26T23:56:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/987751.html"},"modified":"2024-12-27T07:56:11","modified_gmt":"2024-12-26T23:56:11","slug":"python%e4%b8%adsigmoid%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/987751.html","title":{"rendered":"python\u4e2dsigmoid\u5982\u4f55\u8c03\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25063722\/a191d76f-bde1-438d-9994-82487afb0aa3.webp\" alt=\"python\u4e2dsigmoid\u5982\u4f55\u8c03\u7528\" \/><\/p>\n<p><p> \u5728Python\u4e2d\uff0c\u8c03\u7528sigmoid\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u81ea\u5df1\u7f16\u5199\u51fd\u6570\u3001\u4f7f\u7528NumPy\u5e93\u6216\u8005\u901a\u8fc7<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u5e93\u5982TensorFlow\u548cPyTorch\u6765\u5b9e\u73b0\u3002<strong>\u76f4\u63a5\u8c03\u7528sigmoid\u51fd\u6570\u7684\u65b9\u5f0f\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u81ea\u5df1\u7f16\u5199sigmoid\u51fd\u6570\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4f7f\u7528TensorFlow\u6216PyTorch\u7b49\u5e93\u4e2d\u7684\u5185\u7f6e\u51fd\u6570<\/strong>\u3002\u4e0b\u9762\u6211\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\u6765\u8c03\u7528sigmoid\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u81ea\u5df1\u7f16\u5199sigmoid\u51fd\u6570<\/p>\n<\/p>\n<p><p>\u81ea\u5df1\u7f16\u5199sigmoid\u51fd\u6570\u662f\u6700\u7b80\u5355\u76f4\u63a5\u7684\u65b9\u6cd5\u3002Sigmoid\u51fd\u6570\u7684\u6570\u5b66\u8868\u8fbe\u5f0f\u4e3a\uff1a<\/p>\n<\/p>\n<p><p>[ \\sigma(x) = \\frac{1}{1 + e^{-x}} ]<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u8c03\u6574\u51fd\u6570\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u5e76\u4e14\u5bf9\u51fd\u6570\u7684\u5185\u90e8\u673a\u5236\u6709\u66f4\u6df1\u5165\u7684\u7406\u89e3\u3002\u7f16\u5199sigmoid\u51fd\u6570\u7684\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<p>def sigmoid(x):<\/p>\n<p>    return 1 \/ (1 + math.exp(-x))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u4e86Python\u6807\u51c6\u5e93\u4e2d\u7684math\u6a21\u5757\u6765\u8ba1\u7b97\u6307\u6570\u51fd\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u6570\u503c\u6216\u6570\u503c\u5217\u8868\u4f5c\u4e3a\u8f93\u5165\uff0c\u5e76\u8fd4\u56de\u5176sigmoid\u503c\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528NumPy\u5e93<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5bf9\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\u3002\u4f7f\u7528NumPy\u5b9e\u73b0sigmoid\u51fd\u6570\u4e0d\u4ec5\u53ef\u4ee5\u5904\u7406\u5355\u4e2a\u6570\u503c\uff0c\u8fd8\u53ef\u4ee5\u5904\u7406\u6570\u7ec4\uff0c\u8fd9\u5728\u9700\u8981\u5bf9\u4e00\u7ec4\u6570\u636e\u8fdb\u884c\u6279\u91cf\u64cd\u4f5c\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p>def sigmoid(x):<\/p>\n<p>    return 1 \/ (1 + np.exp(-x))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b9e\u73b0\u4e2d\uff0cNumPy\u7684exp\u51fd\u6570\u88ab\u7528\u4e8e\u8ba1\u7b97\u6307\u6570\u3002\u7531\u4e8eNumPy\u6570\u7ec4\u652f\u6301\u5e7f\u64ad\u673a\u5236\uff0c\u8fd9\u4e2a\u5b9e\u73b0\u53ef\u4ee5\u76f4\u63a5\u5bf9NumPy\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\uff0c\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u8f93\u5165\u503csigmoid\u503c\u7684\u6570\u7ec4\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528TensorFlow<\/p>\n<\/p>\n<p><p>TensorFlow\u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u673a\u5668\u5b66\u4e60\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u7684\u6fc0\u6d3b\u51fd\u6570\uff0c\u5305\u62ecsigmoid\u3002\u4f7f\u7528TensorFlow\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u795e\u7ecf\u7f51\u7edc\u4e2d\u5e94\u7528sigmoid\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>x = tf.constant([1.0, 2.0, 3.0])<\/p>\n<p>sigmoid_values = tf.math.sigmoid(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2aTensorFlow\u5e38\u91cf\uff0c\u7136\u540e\u4f7f\u7528tf.math.sigmoid\u51fd\u6570\u8ba1\u7b97sigmoid\u503c\u3002TensorFlow\u4e2d\u7684\u64cd\u4f5c\u662f\u57fa\u4e8e\u5f20\u91cf\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u8f7b\u677e\u5904\u7406\u5927\u89c4\u6a21\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528PyTorch<\/p>\n<\/p>\n<p><p>PyTorch\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u4e5f\u63d0\u4f9b\u4e86sigmoid\u51fd\u6570\u3002\u4e0eTensorFlow\u7c7b\u4f3c\uff0cPyTorch\u7684\u64cd\u4f5c\u662f\u57fa\u4e8e\u5f20\u91cf\u7684\uff0c\u8fd9\u4f7f\u5f97\u5b83\u975e\u5e38\u9002\u5408\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<p>x = torch.tensor([1.0, 2.0, 3.0])<\/p>\n<p>sigmoid_values = torch.sigmoid(x)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2aPyTorch\u5f20\u91cf\uff0c\u7136\u540e\u4f7f\u7528torch.sigmoid\u51fd\u6570\u8ba1\u7b97sigmoid\u503c\u3002PyTorch\u7684API\u8bbe\u8ba1\u7b80\u6d01\uff0c\u6613\u4e8e\u4f7f\u7528\uff0c\u7279\u522b\u9002\u5408\u5feb\u901f\u539f\u578b\u8bbe\u8ba1\u548c\u7814\u7a76\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5e94\u7528\u573a\u666f\u4e0e\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5e94\u7528\u573a\u666f\uff1a<\/strong><\/p>\n<\/p>\n<p><p>Sigmoid\u51fd\u6570\u88ab\u5e7f\u6cdb\u5e94\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u4e2d\uff0c\u5c24\u5176\u662f\u5728\u4e8c\u5206\u7c7b\u95ee\u9898\u4e2d\u4f5c\u4e3a\u8f93\u51fa\u5c42\u7684\u6fc0\u6d3b\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u5c06\u4efb\u4f55\u5b9e\u6570\u6620\u5c04\u5230(0, 1)\u533a\u95f4\uff0c\u8fd9\u4f7f\u5f97\u5b83\u975e\u5e38\u9002\u5408\u5904\u7406\u6982\u7387\u95ee\u9898\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6570\u503c\u7a33\u5b9a\u6027\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97sigmoid\u51fd\u6570\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u6570\u503c\u4e0d\u7a33\u5b9a\u7684\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u8f93\u5165\u503c\u975e\u5e38\u5927\u6216\u975e\u5e38\u5c0f\u65f6\u3002\u4e3a\u4e86\u63d0\u9ad8\u8ba1\u7b97\u7684\u7a33\u5b9a\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9b\u6570\u503c\u6280\u5de7\uff0c\u5982\u4f7f\u7528expit\u51fd\u6570\uff08SciPy\u5e93\u4e2d\u63d0\u4f9b\uff09\u6765\u907f\u514d\u6ea2\u51fa\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u68af\u5ea6\u6d88\u5931\u95ee\u9898\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc\u7684\u8bad\u7ec3\u4e2d\uff0csigmoid\u51fd\u6570\u53ef\u80fd\u5bfc\u81f4\u68af\u5ea6\u6d88\u5931\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u7f51\u7edc\u6df1\u5ea6\u8f83\u5927\u65f6\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u73b0\u4ee3\u795e\u7ecf\u7f51\u7edc\u4e2d\u901a\u5e38\u4f7f\u7528ReLU\u7b49\u66ff\u4ee3\u6fc0\u6d3b\u51fd\u6570\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u6027\u80fd\u4f18\u5316\uff1a<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u9700\u8981\u5bf9\u5927\u89c4\u6a21\u6570\u636e\u8fdb\u884c\u6279\u91cf\u5904\u7406\u65f6\uff0c\u5c3d\u91cf\u4f7f\u7528NumPy\u3001TensorFlow\u6216PyTorch\u7b49\u5e93\uff0c\u8fd9\u4e9b\u5e93\u7ecf\u8fc7\u9ad8\u5ea6\u4f18\u5316\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u8c03\u7528sigmoid\u51fd\u6570\uff0c\u5e76\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\u7075\u6d3b\u8fd0\u7528\u3002\u65e0\u8bba\u662f\u7528\u4e8e\u7b80\u5355\u7684\u8ba1\u7b97\u4efb\u52a1\uff0c\u8fd8\u662f\u7528\u4e8e\u590d\u6742\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u7406\u89e3\u548c\u638c\u63e1sigmoid\u51fd\u6570\u7684\u8c03\u7528\u65b9\u6cd5\u90fd\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\uff0csigmoid\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>Sigmoid\u51fd\u6570\u662f\u4e00\u79cd\u5e38\u7528\u7684\u6fc0\u6d3b\u51fd\u6570\uff0c\u901a\u5e38\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u4e2d\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u5e93\u8f7b\u677e\u5b9e\u73b0sigmoid\u51fd\u6570\u3002\u5176\u57fa\u672c\u516c\u5f0f\u4e3a\uff1asigmoid(x) = 1 \/ (1 + exp(-x))\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5b9e\u73b0\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import numpy as np\n\ndef sigmoid(x):\n    return 1 \/ (1 + np.exp(-x))\n\n# \u793a\u4f8b\u8c03\u7528\nresult = sigmoid(0.5)\nprint(result)\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u6807\u91cf\u3001\u5411\u91cf\u6216\u77e9\u9635\u4f5c\u4e3a\u8f93\u5165\uff0c\u8fd4\u56de\u76f8\u5e94\u7684sigmoid\u503c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u4e2d\u4f7f\u7528sigmoid\u51fd\u6570\uff1f<\/strong><br \/>\u5728\u4f7f\u7528\u5982TensorFlow\u6216PyTorch\u7b49\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\u65f6\uff0csigmoid\u51fd\u6570\u901a\u5e38\u4f5c\u4e3a\u5185\u7f6e\u51fd\u6570\u76f4\u63a5\u8c03\u7528\u3002\u4f8b\u5982\uff0c\u5728TensorFlow\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tf.keras.activations.sigmoid<\/code>\uff0c\u800c\u5728PyTorch\u4e2d\u5219\u53ef\u4ee5\u4f7f\u7528<code>torch.sigmoid<\/code>\u3002\u4ee5\u4e0b\u662f\u4e24\u4e2a\u6846\u67b6\u7684\u793a\u4f8b\uff1a<\/p>\n<p>TensorFlow \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import tensorflow as tf\n\nx = tf.constant(0.5)\nresult = tf.keras.activations.sigmoid(x)\nprint(result.numpy())\n<\/code><\/pre>\n<p>PyTorch \u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import torch\n\nx = torch.tensor(0.5)\nresult = torch.sigmoid(x)\nprint(result.item())\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728\u795e\u7ecf\u7f51\u7edc\u7684\u5404\u4e2a\u5c42\u4e2d\u5e94\u7528sigmoid\u6fc0\u6d3b\u51fd\u6570\u3002<\/p>\n<p><strong>sigmoid\u51fd\u6570\u5728\u673a\u5668\u5b66\u4e60\u4e2d\u6709\u54ea\u4e9b\u5e94\u7528\uff1f<\/strong><br \/>Sigmoid\u51fd\u6570\u5e7f\u6cdb\u5e94\u7528\u4e8e\u4e8c\u5206\u7c7b\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u903b\u8f91\u56de\u5f52\u6a21\u578b\u4e2d\u3002\u5b83\u53ef\u4ee5\u5c06\u6a21\u578b\u7684\u8f93\u51fa\u503c\u6620\u5c04\u52300\u52301\u4e4b\u95f4\uff0c\u8868\u793a\u67d0\u4e2a\u7c7b\u522b\u7684\u6982\u7387\u3002\u5728\u795e\u7ecf\u7f51\u7edc\u4e2d\uff0csigmoid\u51fd\u6570\u53ef\u4ee5\u7528\u4f5c\u8f93\u51fa\u5c42\u7684\u6fc0\u6d3b\u51fd\u6570\uff0c\u4ee5\u4fbf\u4e8e\u9884\u6d4b\u4e8c\u5206\u7c7b\u7ed3\u679c\u3002\u6b64\u5916\uff0c\u7531\u4e8e\u5176\u5bfc\u6570\u8ba1\u7b97\u7b80\u5355\uff0csigmoid\u4e5f\u5e38\u7528\u4e8e\u9690\u85cf\u5c42\u7684\u6fc0\u6d3b\u51fd\u6570\uff0c\u5c3d\u7ba1\u5728\u6df1\u5ea6\u5b66\u4e60\u4e2d\uff0cReLU\u7b49\u6fc0\u6d3b\u51fd\u6570\u56e0\u5176\u66f4\u4f18\u7684\u6027\u80fd\u800c\u9010\u6e10\u53d6\u4ee3\u4e86sigmoid\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u8c03\u7528sigmoid\u51fd\u6570\u53ef\u4ee5\u901a\u8fc7\u591a\u4e2a\u65b9\u6cd5\u5b9e\u73b0\uff0c\u5305\u62ec\u81ea\u5df1\u7f16\u5199\u51fd\u6570\u3001\u4f7f\u7528NumPy\u5e93\u6216\u8005\u901a\u8fc7\u673a\u5668\u5b66 [&hellip;]","protected":false},"author":3,"featured_media":987758,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/987751"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=987751"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/987751\/revisions"}],"predecessor-version":[{"id":987759,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/987751\/revisions\/987759"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/987758"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=987751"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=987751"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=987751"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}