{"id":973274,"date":"2024-12-27T05:55:36","date_gmt":"2024-12-26T21:55:36","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/973274.html"},"modified":"2024-12-27T05:55:37","modified_gmt":"2024-12-26T21:55:37","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%ae%a1%e7%ae%97%e6%a2%af%e5%ba%a6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/973274.html","title":{"rendered":"\u5982\u4f55\u7528python\u8ba1\u7b97\u68af\u5ea6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24195529\/11c96c96-11f8-4d54-9f1d-556f4e6d906a.webp\" alt=\"\u5982\u4f55\u7528python\u8ba1\u7b97\u68af\u5ea6\" \/><\/p>\n<p><p> \u5f00\u5934\u6bb5\u843d\uff1a<br \/><strong>\u7528Python\u8ba1\u7b97\u68af\u5ea6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u81ea\u52a8\u5fae\u5206\u5e93\uff08\u5982TensorFlow\u3001PyTorch\uff09\u3001\u6570\u503c\u5fae\u5206\uff08\u4f8b\u5982\u6709\u9650\u5dee\u5206\u6cd5\uff09\u548c\u7b26\u53f7\u5fae\u5206\uff08\u5982SymPy\uff09<\/strong>\u3002\u5176\u4e2d\uff0c\u81ea\u52a8\u5fae\u5206\u5e93\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u80fd\u591f\u9ad8\u6548\u3001\u51c6\u786e\u5730\u8ba1\u7b97\u68af\u5ea6\uff0c\u5c24\u5176\u662f\u5728\u6df1\u5ea6\u5b66\u4e60\u4e2d\u3002\u81ea\u52a8\u5fae\u5206\u901a\u8fc7\u8ddf\u8e2a\u8fd0\u7b97\u7684\u8ba1\u7b97\u56fe\uff0c\u4ece\u800c\u5728\u53cd\u5411\u4f20\u64ad\u8fc7\u7a0b\u4e2d\u81ea\u52a8\u8ba1\u7b97\u51fa\u68af\u5ea6\uff0c\u907f\u514d\u4e86\u624b\u52a8\u8ba1\u7b97\u7684\u590d\u6742\u6027\u548c\u53ef\u80fd\u7684\u8bef\u5dee\u3002\u6570\u503c\u5fae\u5206\u5219\u901a\u8fc7\u8fd1\u4f3c\u7684\u65b9\u6cd5\u8ba1\u7b97\u68af\u5ea6\uff0c\u9002\u7528\u4e8e\u51fd\u6570\u590d\u6742\u6216\u4e0d\u53ef\u5fae\u7684\u60c5\u51b5\uff0c\u4f46\u7cbe\u5ea6\u8f83\u4f4e\u3002\u7b26\u53f7\u5fae\u5206\u5219\u4f7f\u7528\u7b26\u53f7\u8fd0\u7b97\u8fdb\u884c\u6c42\u5bfc\uff0c\u80fd\u63d0\u4f9b\u7cbe\u786e\u7684\u89e3\u6790\u89e3\uff0c\u4f46\u5728\u5904\u7406\u590d\u6742\u51fd\u6570\u65f6\u53ef\u80fd\u6548\u7387\u8f83\u4f4e\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u81ea\u52a8\u5fae\u5206\u5e93<\/p>\n<\/p>\n<p><p>\u81ea\u52a8\u5fae\u5206\u5e93\u662f\u73b0\u4ee3\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff08\u5982TensorFlow\u3001PyTorch\uff09\u7684\u6838\u5fc3\uff0c\u5b83\u4eec\u80fd\u591f\u9ad8\u6548\u5730\u8ba1\u7b97\u68af\u5ea6\uff0c\u8fd9\u5bf9\u4e8e\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u6765\u8bf4\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002<\/p>\n<\/p>\n<ol>\n<li>TensorFlow\u4e2d\u7684\u68af\u5ea6\u8ba1\u7b97<\/li>\n<\/ol>\n<p><p>TensorFlow\u662f\u4e00\u4e2a\u5e7f\u6cdb\u4f7f\u7528\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u81ea\u52a8\u5fae\u5206\u529f\u80fd\u3002\u5229\u7528TensorFlow\u8ba1\u7b97\u68af\u5ea6\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u5b9a\u4e49\u8ba1\u7b97\u56fe\u3001\u6784\u5efa\u635f\u5931\u51fd\u6570\u4ee5\u53ca\u4f7f\u7528<code>tf.GradientTape<\/code>\u8ba1\u7b97\u68af\u5ea6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def f(x):<\/p>\n<p>    return x2 + 3*x + 2<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf<\/strong><\/h2>\n<p>x = tf.Variable(1.0)<\/p>\n<h2><strong>\u4f7f\u7528GradientTape\u8ba1\u7b97\u68af\u5ea6<\/strong><\/h2>\n<p>with tf.GradientTape() as tape:<\/p>\n<p>    y = f(x)<\/p>\n<h2><strong>\u8ba1\u7b97\u68af\u5ea6<\/strong><\/h2>\n<p>grad = tape.gradient(y, x)<\/p>\n<p>print(&quot;Gradient:&quot;, grad.numpy())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>PyTorch\u4e2d\u7684\u68af\u5ea6\u8ba1\u7b97<\/li>\n<\/ol>\n<p><p>PyTorch\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u63d0\u4f9b\u4e86\u52a8\u6001\u8ba1\u7b97\u56fe\u7684\u7279\u6027\uff0c\u4f7f\u5f97\u68af\u5ea6\u8ba1\u7b97\u66f4\u52a0\u7075\u6d3b\u3002\u8ba1\u7b97\u68af\u5ea6\u7684\u57fa\u672c\u6d41\u7a0b\u5305\u62ec\u5b9a\u4e49\u5f20\u91cf\u3001\u6784\u5efa\u635f\u5931\u51fd\u6570\u5e76\u8c03\u7528<code>backward()<\/code>\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def f(x):<\/p>\n<p>    return x2 + 3*x + 2<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5f20\u91cf\uff0c\u5e76\u542f\u7528\u68af\u5ea6\u8ba1\u7b97<\/strong><\/h2>\n<p>x = torch.tensor(1.0, requires_grad=True)<\/p>\n<h2><strong>\u8ba1\u7b97\u51fd\u6570\u503c<\/strong><\/h2>\n<p>y = f(x)<\/p>\n<h2><strong>\u53cd\u5411\u4f20\u64ad\u8ba1\u7b97\u68af\u5ea6<\/strong><\/h2>\n<p>y.backward()<\/p>\n<h2><strong>\u8f93\u51fa\u68af\u5ea6<\/strong><\/h2>\n<p>print(&quot;Gradient:&quot;, x.grad)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u6570\u503c\u5fae\u5206<\/p>\n<\/p>\n<p><p>\u6570\u503c\u5fae\u5206\u662f\u4e00\u79cd\u901a\u8fc7\u6709\u9650\u5dee\u5206\u8fd1\u4f3c\u5bfc\u6570\u7684\u65b9\u6cd5\u3002\u867d\u7136\u7cbe\u5ea6\u53ef\u80fd\u4e0d\u5982\u81ea\u52a8\u5fae\u5206\uff0c\u4f46\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u4ecd\u7136\u5f88\u6709\u7528\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6709\u9650\u5dee\u5206\u6cd5<\/li>\n<\/ol>\n<p><p>\u6709\u9650\u5dee\u5206\u6cd5\u662f\u4e00\u79cd\u7b80\u5355\u800c\u5b9e\u7528\u7684\u6570\u503c\u5fae\u5206\u65b9\u6cd5\uff0c\u901a\u8fc7\u8ba1\u7b97\u51fd\u6570\u5728\u4e24\u4e2a\u975e\u5e38\u63a5\u8fd1\u7684\u70b9\u7684\u503c\u6765\u8fd1\u4f3c\u5bfc\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def numerical_gradient(f, x, h=1e-5):<\/p>\n<p>    grad = (f(x + h) - f(x - h)) \/ (2 * h)<\/p>\n<p>    return grad<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570<\/strong><\/h2>\n<p>def f(x):<\/p>\n<p>    return x2 + 3*x + 2<\/p>\n<h2><strong>\u8ba1\u7b97\u68af\u5ea6<\/strong><\/h2>\n<p>x = 1.0<\/p>\n<p>grad = numerical_gradient(f, x)<\/p>\n<p>print(&quot;Numerical Gradient:&quot;, grad)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4e2d\u5fc3\u5dee\u5206\u6cd5<\/li>\n<\/ol>\n<p><p>\u4e2d\u5fc3\u5dee\u5206\u6cd5\u662f\u6709\u9650\u5dee\u5206\u6cd5\u7684\u4e00\u79cd\u6539\u8fdb\u5f62\u5f0f\uff0c\u901a\u8fc7\u5728\u4e24\u4e2a\u65b9\u5411\u4e0a\u53d6\u6837\uff0c\u80fd\u591f\u63d0\u4f9b\u66f4\u9ad8\u7cbe\u5ea6\u7684\u68af\u5ea6\u8fd1\u4f3c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def central_difference(f, x, h=1e-5):<\/p>\n<p>    return (f(x + h) - f(x - h)) \/ (2 * h)<\/p>\n<h2><strong>\u8ba1\u7b97\u68af\u5ea6<\/strong><\/h2>\n<p>grad = central_difference(f, x)<\/p>\n<p>print(&quot;Central Difference Gradient:&quot;, grad)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u7b26\u53f7\u5fae\u5206<\/p>\n<\/p>\n<p><p>\u7b26\u53f7\u5fae\u5206\u4f7f\u7528\u7b26\u53f7\u8fd0\u7b97\u6765\u6c42\u5bfc\u6570\uff0c\u53ef\u4ee5\u5f97\u5230\u7cbe\u786e\u7684\u89e3\u6790\u89e3\u3002SymPy\u662fPython\u4e2d\u7528\u4e8e\u7b26\u53f7\u8ba1\u7b97\u7684\u5e93\uff0c\u80fd\u591f\u8f7b\u677e\u5b9e\u73b0\u7b26\u53f7\u5fae\u5206\u3002<\/p>\n<\/p>\n<ol>\n<li>\u4f7f\u7528SymPy\u8fdb\u884c\u7b26\u53f7\u5fae\u5206<\/li>\n<\/ol>\n<p><p>SymPy\u63d0\u4f9b\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u7b26\u53f7\u8ba1\u7b97\u73af\u5883\uff0c\u53ef\u4ee5\u76f4\u63a5\u6c42\u89e3\u51fd\u6570\u7684\u5bfc\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sympy as sp<\/p>\n<h2><strong>\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf<\/strong><\/h2>\n<p>x = sp.symbols(&#39;x&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u7b26\u53f7\u51fd\u6570<\/strong><\/h2>\n<p>f = x2 + 3*x + 2<\/p>\n<h2><strong>\u8ba1\u7b97\u5bfc\u6570<\/strong><\/h2>\n<p>grad = sp.diff(f, x)<\/p>\n<p>print(&quot;Symbolic Gradient:&quot;, grad)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u591a\u53d8\u91cf\u51fd\u6570\u7684\u7b26\u53f7\u5fae\u5206<\/li>\n<\/ol>\n<p><p>\u5bf9\u4e8e\u591a\u53d8\u91cf\u51fd\u6570\uff0cSymPy\u4e5f\u80fd\u591f\u8f7b\u677e\u5904\u7406\uff0c\u901a\u8fc7\u5206\u522b\u5bf9\u6bcf\u4e2a\u53d8\u91cf\u6c42\u5bfc\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf<\/p>\n<p>x, y = sp.symbols(&#39;x y&#39;)<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u591a\u53d8\u91cf\u51fd\u6570<\/strong><\/h2>\n<p>f = x&lt;strong&gt;2 + y&lt;\/strong&gt;2 + 3*x*y + 2<\/p>\n<h2><strong>\u8ba1\u7b97\u5bf9x\u7684\u504f\u5bfc\u6570<\/strong><\/h2>\n<p>grad_x = sp.diff(f, x)<\/p>\n<h2><strong>\u8ba1\u7b97\u5bf9y\u7684\u504f\u5bfc\u6570<\/strong><\/h2>\n<p>grad_y = sp.diff(f, y)<\/p>\n<p>print(&quot;Symbolic Gradient wrt x:&quot;, grad_x)<\/p>\n<p>print(&quot;Symbolic Gradient wrt y:&quot;, grad_y)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u68af\u5ea6\u7684\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u68af\u5ea6\u7684\u8ba1\u7b97\u4e0d\u4ec5\u4ec5\u662f\u6570\u5b66\u4e0a\u7684\u64cd\u4f5c\uff0c\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u68af\u5ea6\u6709\u7740\u5e7f\u6cdb\u7684\u7528\u9014\u3002<\/p>\n<\/p>\n<ol>\n<li>\u68af\u5ea6\u4e0b\u964d\u6cd5<\/li>\n<\/ol>\n<p><p>\u68af\u5ea6\u4e0b\u964d\u6cd5\u662f\u4e00\u79cd\u4f18\u5316\u7b97\u6cd5\uff0c\u901a\u8fc7\u6cbf\u7740\u68af\u5ea6\u7684\u53cd\u65b9\u5411\u66f4\u65b0\u53c2\u6570\uff0c\u6765\u6700\u5c0f\u5316\u635f\u5931\u51fd\u6570\u3002\u5176\u6838\u5fc3\u601d\u60f3\u662f\u5229\u7528\u68af\u5ea6\u63d0\u4f9b\u7684\u65b9\u5411\u4fe1\u606f\u6765\u627e\u5230\u51fd\u6570\u7684\u6700\u4f18\u503c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def gradient_descent(f, grad_f, x_init, learning_rate=0.01, steps=100):<\/p>\n<p>    x = x_init<\/p>\n<p>    for _ in range(steps):<\/p>\n<p>        x -= learning_rate * grad_f(x)<\/p>\n<p>    return x<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u51fd\u6570\u548c\u5176\u68af\u5ea6<\/strong><\/h2>\n<p>def f(x):<\/p>\n<p>    return x2 + 3*x + 2<\/p>\n<p>def grad_f(x):<\/p>\n<p>    return 2*x + 3<\/p>\n<h2><strong>\u4f7f\u7528\u68af\u5ea6\u4e0b\u964d\u6cd5\u6700\u5c0f\u5316\u51fd\u6570<\/strong><\/h2>\n<p>x_min = gradient_descent(f, grad_f, x_init=0.0)<\/p>\n<p>print(&quot;Minimum x:&quot;, x_min)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u795e\u7ecf\u7f51\u7edc\u4e2d\u7684\u53cd\u5411\u4f20\u64ad<\/li>\n<\/ol>\n<p><p>\u5728\u795e\u7ecf\u7f51\u7edc\u4e2d\uff0c\u68af\u5ea6\u7528\u4e8e\u53cd\u5411\u4f20\u64ad\u7b97\u6cd5\uff0c\u8fd9\u662f\u8bad\u7ec3\u795e\u7ecf\u7f51\u7edc\u7684\u6838\u5fc3\u3002\u901a\u8fc7\u8ba1\u7b97\u635f\u5931\u51fd\u6570\u5173\u4e8e\u6bcf\u4e2a\u53c2\u6570\u7684\u68af\u5ea6\uff0c\u4f18\u5316\u7b97\u6cd5\u80fd\u591f\u66f4\u65b0\u53c2\u6570\u4ee5\u63d0\u9ad8\u6a21\u578b\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import torch<\/p>\n<p>import torch.nn as nn<\/p>\n<p>import torch.optim as optim<\/p>\n<h2><strong>\u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684\u7ebf\u6027\u6a21\u578b<\/strong><\/h2>\n<p>model = nn.Linear(1, 1)<\/p>\n<h2><strong>\u5b9a\u4e49\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668<\/strong><\/h2>\n<p>criterion = nn.MSELoss()<\/p>\n<p>optimizer = optim.SGD(model.parameters(), lr=0.01)<\/p>\n<h2><strong>\u8f93\u5165\u548c\u76ee\u6807\u8f93\u51fa<\/strong><\/h2>\n<p>input = torch.tensor([[1.0]])<\/p>\n<p>target = torch.tensor([[2.0]])<\/p>\n<h2><strong>\u524d\u5411\u4f20\u64ad<\/strong><\/h2>\n<p>output = model(input)<\/p>\n<p>loss = criterion(output, target)<\/p>\n<h2><strong>\u53cd\u5411\u4f20\u64ad<\/strong><\/h2>\n<p>optimizer.zero_grad()<\/p>\n<p>loss.backward()<\/p>\n<h2><strong>\u66f4\u65b0\u53c2\u6570<\/strong><\/h2>\n<p>optimizer.step()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u68af\u5ea6\u8ba1\u7b97\u7684\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p>\u5728\u8ba1\u7b97\u68af\u5ea6\u7684\u8fc7\u7a0b\u4e2d\uff0c\u6709\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\u548c\u6ce8\u610f\u4e8b\u9879\u9700\u8981\u4e86\u89e3\u3002<\/p>\n<\/p>\n<ol>\n<li>\u68af\u5ea6\u7206\u70b8\u4e0e\u68af\u5ea6\u6d88\u5931<\/li>\n<\/ol>\n<p><p>\u5728\u6df1\u5ea6\u795e\u7ecf\u7f51\u7edc\u4e2d\uff0c\u68af\u5ea6\u7206\u70b8\u548c\u68af\u5ea6\u6d88\u5931\u662f\u4e24\u4e2a\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u6df1\u5c42\u7f51\u7edc\u65f6\u3002\u68af\u5ea6\u7206\u70b8\u4f1a\u5bfc\u81f4\u68af\u5ea6\u503c\u8fc7\u5927\uff0c\u4f7f\u5f97\u6a21\u578b\u65e0\u6cd5\u6536\u655b\uff1b\u800c\u68af\u5ea6\u6d88\u5931\u5219\u4f7f\u5f97\u68af\u5ea6\u503c\u8d8b\u8fd1\u4e8e\u96f6\uff0c\u5bfc\u81f4\u8bad\u7ec3\u901f\u5ea6\u53d8\u6162\u751a\u81f3\u505c\u6b62\u3002<\/p>\n<\/p>\n<p><p>\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u68af\u5ea6\u88c1\u526a\u3001\u9009\u62e9\u5408\u9002\u7684\u6fc0\u6d3b\u51fd\u6570\uff08\u5982ReLU\uff09\u3001\u4ee5\u53ca\u4f7f\u7528\u66f4\u597d\u7684\u6743\u91cd\u521d\u59cb\u5316\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<ol start=\"2\">\n<li>\u6570\u503c\u7cbe\u5ea6\u95ee\u9898<\/li>\n<\/ol>\n<p><p>\u5728\u8fdb\u884c\u6570\u503c\u5fae\u5206\u65f6\uff0c\u9009\u62e9\u5408\u9002\u7684\u6b65\u957f\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u6b65\u957f\u8fc7\u5927\u4f1a\u5bfc\u81f4\u8fd1\u4f3c\u8bef\u5dee\uff0c\u800c\u6b65\u957f\u8fc7\u5c0f\u5219\u53ef\u80fd\u5bfc\u81f4\u6570\u503c\u7cbe\u5ea6\u95ee\u9898\u3002\u901a\u5e38\uff0c\u9009\u62e9\u4e00\u4e2a\u9002\u4e2d\u7684\u6b65\u957f\uff08\u59821e-5\uff09\u80fd\u591f\u5728\u7cbe\u5ea6\u548c\u8ba1\u7b97\u6548\u7387\u4e4b\u95f4\u53d6\u5f97\u5e73\u8861\u3002<\/p>\n<\/p>\n<ol start=\"3\">\n<li>\u786e\u4fdd\u6b63\u786e\u7684\u8ba1\u7b97\u56fe<\/li>\n<\/ol>\n<p><p>\u5728\u4f7f\u7528\u81ea\u52a8\u5fae\u5206\u5e93\u65f6\uff0c\u786e\u4fdd\u8ba1\u7b97\u56fe\u7684\u6b63\u786e\u6027\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u9519\u8bef\u7684\u8ba1\u7b97\u56fe\u53ef\u80fd\u5bfc\u81f4\u68af\u5ea6\u8ba1\u7b97\u9519\u8bef\uff0c\u4ece\u800c\u5f71\u54cd\u6a21\u578b\u7684\u8bad\u7ec3\u6548\u679c\u3002\u4f7f\u7528<code>torch.no_grad()<\/code>\u6216<code>tf.stop_gradient()<\/code>\u53ef\u4ee5\u9632\u6b62\u4e0d\u5fc5\u8981\u7684\u8ba1\u7b97\u56fe\u8ddf\u8e2a\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u7528Python\u8ba1\u7b97\u68af\u5ea6\u662f\u673a\u5668\u5b66\u4e60\u548c\u6df1\u5ea6\u5b66\u4e60\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u4efb\u52a1\uff0c\u65e0\u8bba\u662f\u901a\u8fc7\u81ea\u52a8\u5fae\u5206\u5e93\u3001\u6570\u503c\u5fae\u5206\u8fd8\u662f\u7b26\u53f7\u5fae\u5206\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u80fd\u591f\u6709\u6548\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u548c\u7ed3\u679c\u7684\u51c6\u786e\u6027\u3002\u65e0\u8bba\u662f\u4f18\u5316\u7b97\u6cd5\u8fd8\u662f\u795e\u7ecf\u7f51\u7edc\u8bad\u7ec3\uff0c\u68af\u5ea6\u7684\u8ba1\u7b97\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u4e00\u90e8\u5206\u3002\u901a\u8fc7\u6df1\u5165\u7406\u89e3\u548c\u7075\u6d3b\u5e94\u7528\u8fd9\u4e9b\u5de5\u5177\u548c\u6280\u672f\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u5e76\u63a8\u52a8\u6280\u672f\u8fdb\u6b65\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8ba1\u7b97\u68af\u5ea6\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5728Python\u4e2d\u8ba1\u7b97\u68af\u5ea6\u901a\u5e38\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528NumPy\u5e93\u8fdb\u884c\u5411\u91cf\u5316\u8fd0\u7b97\uff0c\u6216\u8005\u4f7f\u7528\u81ea\u52a8\u5fae\u5206\u5e93\u5982TensorFlow\u6216PyTorch\u3002\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\uff0c\u4f7f\u7528\u6570\u503c\u65b9\u6cd5\uff08\u5982\u6709\u9650\u5dee\u5206\u6cd5\uff09\u6216\u7b26\u53f7\u8ba1\u7b97\u6765\u83b7\u5f97\u5176\u5bfc\u6570\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u673a\u5668\u5b66\u4e60\u6a21\u578b\uff0c\u63a8\u8350\u4f7f\u7528\u81ea\u52a8\u5fae\u5206\u529f\u80fd\uff0c\u8fd9\u6837\u53ef\u4ee5\u66f4\u9ad8\u6548\u5730\u8ba1\u7b97\u68af\u5ea6\u5e76\u907f\u514d\u624b\u52a8\u63a8\u5bfc\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u53ef\u4ee5\u5e2e\u52a9\u6211\u8ba1\u7b97\u68af\u5ea6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u8ba1\u7b97\u68af\u5ea6\u3002NumPy\u662f\u57fa\u7840\u7684\u79d1\u5b66\u8ba1\u7b97\u5e93\uff0c\u9002\u5408\u8fdb\u884c\u7b80\u5355\u7684\u6570\u503c\u68af\u5ea6\u8ba1\u7b97\u3002\u5bf9\u4e8e\u6df1\u5ea6\u5b66\u4e60\u5e94\u7528\uff0cTensorFlow\u548cPyTorch\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u81ea\u52a8\u5fae\u5206\u529f\u80fd\uff0c\u80fd\u591f\u81ea\u52a8\u8ba1\u7b97\u6a21\u578b\u53c2\u6570\u7684\u68af\u5ea6\u3002\u6b64\u5916\uff0cSymPy\u5219\u662f\u4e00\u4e2a\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u7684\u5e93\uff0c\u9002\u5408\u9700\u8981\u89e3\u6790\u89e3\u7684\u573a\u666f\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u673a\u5668\u5b66\u4e60\u6a21\u578b\u4e2d\u5e94\u7528\u68af\u5ea6\u8ba1\u7b97\uff1f<\/strong><br \/>\u5728\u673a\u5668\u5b66\u4e60\u6a21\u578b\u4e2d\uff0c\u68af\u5ea6\u8ba1\u7b97\u901a\u5e38\u7528\u4e8e\u4f18\u5316\u7b97\u6cd5\uff0c\u6bd4\u5982\u68af\u5ea6\u4e0b\u964d\u6cd5\u3002\u901a\u8fc7\u8ba1\u7b97\u635f\u5931\u51fd\u6570\u76f8\u5bf9\u4e8e\u6a21\u578b\u53c2\u6570\u7684\u68af\u5ea6\uff0c\u53ef\u4ee5\u66f4\u65b0\u53c2\u6570\u4ee5\u51cf\u5c11\u9884\u6d4b\u8bef\u5dee\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u63d0\u5230\u7684\u5e93\u6765\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\uff0c\u786e\u4fdd\u6a21\u578b\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u4e0d\u65ad\u8c03\u6574\uff0c\u4ee5\u63d0\u9ad8\u6027\u80fd\u548c\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5f00\u5934\u6bb5\u843d\uff1a\u7528Python\u8ba1\u7b97\u68af\u5ea6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5305\u62ec\u4f7f\u7528\u81ea\u52a8\u5fae\u5206\u5e93\uff08\u5982TensorFlow\u3001PyTorch\uff09\u3001\u6570 [&hellip;]","protected":false},"author":3,"featured_media":973282,"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\/973274"}],"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=973274"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973274\/revisions"}],"predecessor-version":[{"id":973285,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/973274\/revisions\/973285"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/973282"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=973274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=973274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=973274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}