{"id":1112928,"date":"2025-01-08T17:43:03","date_gmt":"2025-01-08T09:43:03","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1112928.html"},"modified":"2025-01-08T17:43:06","modified_gmt":"2025-01-08T09:43:06","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e6%8a%8a%e8%ae%ad%e7%bb%83%e6%95%b0%e6%8d%ae%e5%ae%9e%e6%97%b6%e6%98%be%e7%a4%ba","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1112928.html","title":{"rendered":"\u5982\u4f55\u7528python\u628a\u8bad\u7ec3\u6570\u636e\u5b9e\u65f6\u663e\u793a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25074733\/7d0e4831-3211-4995-ab6c-b9285d2bb4df.webp\" alt=\"\u5982\u4f55\u7528python\u628a\u8bad\u7ec3\u6570\u636e\u5b9e\u65f6\u663e\u793a\" \/><\/p>\n<p><p> <strong>\u8981\u5b9e\u73b0\u7528Python\u628a\u8bad\u7ec3\u6570\u636e\u5b9e\u65f6\u663e\u793a\uff0c\u53ef\u4ee5\u4f7f\u7528\u53ef\u89c6\u5316\u5de5\u5177\u5982Matplotlib\u3001Seaborn\u3001Plotly\u7b49\uff0c\u5229\u7528\u8fd9\u4e9b\u5de5\u5177\u63d0\u4f9b\u7684\u52a8\u6001\u7ed8\u56fe\u529f\u80fd\uff0c\u5b9e\u65f6\u5c55\u793a\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u7684\u6570\u636e\u53d8\u5316\u3002<\/strong> \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Matplotlib\u548cPlotly\u6765\u5b9e\u73b0\u8fd9\u4e00\u76ee\u6807\uff0c\u5e76\u6df1\u5165\u63a2\u8ba8\u5982\u4f55\u4f18\u5316\u53ef\u89c6\u5316\u6548\u679c\u548c\u4ee3\u7801\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><p><strong>\u4e00\u3001MATPLOTLIB\u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u5c55\u793a<\/strong><\/p>\n<\/p>\n<p><p>Matplotlib\u662fPython\u4e2d\u6700\u5e38\u7528\u7684\u7ed8\u56fe\u5e93\u4e4b\u4e00\uff0c\u652f\u6301\u9759\u6001\u3001\u52a8\u6001\u548c\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u3002\u5229\u7528Matplotlib\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u5c55\u793a\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5Matplotlib<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Matplotlib\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install matplotlib<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u5c55\u793a<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Matplotlib\u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u5c55\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<p>import time<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u7a97\u53e3<\/strong><\/h2>\n<p>plt.ion()  # \u5f00\u542f\u4ea4\u4e92\u6a21\u5f0f<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>x_data = []<\/p>\n<p>y_data = []<\/p>\n<p>line, = ax.plot(x_data, y_data)<\/p>\n<h2><strong>\u6a21\u62df\u6570\u636e\u751f\u6210\u8fc7\u7a0b<\/strong><\/h2>\n<p>for i in range(100):<\/p>\n<p>    x_data.append(i)<\/p>\n<p>    y_data.append(np.sin(i * 0.1))<\/p>\n<p>    line.set_xdata(x_data)<\/p>\n<p>    line.set_ydata(y_data)<\/p>\n<p>    ax.relim()<\/p>\n<p>    ax.autoscale_view()<\/p>\n<p>    fig.canvas.draw()<\/p>\n<p>    fig.canvas.flush_events()<\/p>\n<p>    time.sleep(0.1)  # \u6a21\u62df\u6570\u636e\u751f\u6210\u7684\u65f6\u95f4\u95f4\u9694<\/p>\n<p>plt.ioff()  # \u5173\u95ed\u4ea4\u4e92\u6a21\u5f0f<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>plt.ion()<\/code>\u5f00\u542f\u4ea4\u4e92\u6a21\u5f0f\uff0c\u4f7f\u5f97\u7ed8\u56fe\u7a97\u53e3\u80fd\u591f\u5b9e\u65f6\u66f4\u65b0\u3002\u7136\u540e\uff0c\u901a\u8fc7\u5faa\u73af\u4e0d\u65ad\u751f\u6210\u6570\u636e\uff0c\u5e76\u66f4\u65b0\u7ed8\u56fe\u5185\u5bb9\u3002\u6700\u540e\uff0c\u901a\u8fc7<code>plt.ioff()<\/code>\u5173\u95ed\u4ea4\u4e92\u6a21\u5f0f\uff0c\u5e76\u663e\u793a\u6700\u7ec8\u7684\u7ed8\u56fe\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><h3>3. \u4f18\u5316\u5b9e\u65f6\u663e\u793a\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5982\u679c\u6570\u636e\u91cf\u8f83\u5927\uff0c\u7ed8\u56fe\u6027\u80fd\u53ef\u80fd\u4f1a\u6210\u4e3a\u74f6\u9888\u3002\u4e3a\u4e86\u4f18\u5316\u6027\u80fd\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u51cf\u5c11\u7ed8\u56fe\u6b21\u6570<\/strong>\uff1a\u5728\u6570\u636e\u91cf\u8f83\u5927\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u7ed8\u56fe\u66f4\u65b0\u9891\u7387\uff0c\u4f8b\u5982\u6bcf\u751f\u621010\u4e2a\u6570\u636e\u70b9\u66f4\u65b0\u4e00\u6b21\u7ed8\u56fe\u3002<\/li>\n<li><strong>\u7b80\u5316\u7ed8\u56fe\u5185\u5bb9<\/strong>\uff1a\u51cf\u5c11\u7ed8\u56fe\u5185\u5bb9\u7684\u590d\u6742\u5ea6\uff0c\u4f8b\u5982\u53bb\u6389\u4e0d\u5fc5\u8981\u7684\u7ed8\u56fe\u5143\u7d20\uff08\u6807\u9898\u3001\u7f51\u683c\u7ebf\u7b49\uff09\uff0c\u4ee5\u63d0\u9ad8\u7ed8\u56fe\u901f\u5ea6\u3002<\/li>\n<li><strong>\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7ed8\u56fe\u5e93<\/strong>\uff1a\u5728\u6570\u636e\u91cf\u975e\u5e38\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7ed8\u56fe\u5e93\uff0c\u5982Plotly\u3002<\/li>\n<\/ul>\n<p><p><strong>\u4e8c\u3001PLOTLY\u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u5c55\u793a<\/strong><\/p>\n<\/p>\n<p><p>Plotly\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u4ea4\u4e92\u5f0f\u7ed8\u56fe\u5e93\uff0c\u652f\u6301\u5728\u6d4f\u89c8\u5668\u4e2d\u663e\u793a\u5b9e\u65f6\u6570\u636e\u3002\u76f8\u6bd4\u4e8eMatplotlib\uff0cPlotly\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u6027\u80fd\u66f4\u597d\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5Plotly<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86Plotly\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install plotly<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u5c55\u793a<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Plotly\u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u5c55\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import plotly.graph_objs as go<\/p>\n<p>from plotly.subplots import make_subplots<\/p>\n<p>import numpy as np<\/p>\n<p>import time<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u7a97\u53e3<\/strong><\/h2>\n<p>fig = make_subplots(rows=1, cols=1)<\/p>\n<p>x_data = []<\/p>\n<p>y_data = []<\/p>\n<p>scatter = go.Scatter(x=x_data, y=y_data, mode=&#39;lines&#39;)<\/p>\n<p>fig.add_trace(scatter)<\/p>\n<h2><strong>\u663e\u793a\u7ed8\u56fe\u7a97\u53e3<\/strong><\/h2>\n<p>fig.show()<\/p>\n<h2><strong>\u6a21\u62df\u6570\u636e\u751f\u6210\u8fc7\u7a0b<\/strong><\/h2>\n<p>for i in range(100):<\/p>\n<p>    x_data.append(i)<\/p>\n<p>    y_data.append(np.sin(i * 0.1))<\/p>\n<p>    with fig.batch_update():<\/p>\n<p>        scatter.x = x_data<\/p>\n<p>        scatter.y = y_data<\/p>\n<p>    time.sleep(0.1)  # \u6a21\u62df\u6570\u636e\u751f\u6210\u7684\u65f6\u95f4\u95f4\u9694<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>fig.show()<\/code>\u663e\u793a\u7ed8\u56fe\u7a97\u53e3\uff0c\u5e76\u901a\u8fc7<code>fig.batch_update()<\/code>\u5b9e\u73b0\u6570\u636e\u7684\u6279\u91cf\u66f4\u65b0\uff0c\u4ece\u800c\u63d0\u9ad8\u7ed8\u56fe\u6027\u80fd\u3002<\/p>\n<\/p>\n<p><h3>3. \u4f18\u5316\u5b9e\u65f6\u663e\u793a\u6027\u80fd<\/h3>\n<\/p>\n<p><p>\u4e0eMatplotlib\u7c7b\u4f3c\uff0c\u5728\u4f7f\u7528Plotly\u8fdb\u884c\u5b9e\u65f6\u6570\u636e\u5c55\u793a\u65f6\uff0c\u4e5f\u9700\u8981\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\u4ee5\u4f18\u5316\u6027\u80fd\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u51cf\u5c11\u7ed8\u56fe\u6b21\u6570<\/strong>\uff1a\u8bbe\u7f6e\u4e00\u4e2a\u7ed8\u56fe\u66f4\u65b0\u9891\u7387\uff0c\u4f8b\u5982\u6bcf\u751f\u621010\u4e2a\u6570\u636e\u70b9\u66f4\u65b0\u4e00\u6b21\u7ed8\u56fe\u3002<\/li>\n<li><strong>\u7b80\u5316\u7ed8\u56fe\u5185\u5bb9<\/strong>\uff1a\u51cf\u5c11\u7ed8\u56fe\u5185\u5bb9\u7684\u590d\u6742\u5ea6\uff0c\u4ee5\u63d0\u9ad8\u7ed8\u56fe\u901f\u5ea6\u3002<\/li>\n<li><strong>\u4f7f\u7528\u6279\u91cf\u66f4\u65b0<\/strong>\uff1a\u5229\u7528Plotly\u7684<code>batch_update()<\/code>\u65b9\u6cd5\uff0c\u5b9e\u73b0\u6570\u636e\u7684\u6279\u91cf\u66f4\u65b0\uff0c\u63d0\u9ad8\u7ed8\u56fe\u6027\u80fd\u3002<\/li>\n<\/ul>\n<p><p><strong>\u4e09\u3001\u7ed3\u5408PyTorch\u8fdb\u884c\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a<\/strong><\/p>\n<\/p>\n<p><p>\u5728<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6df1\u5ea6\u5b66\u4e60\u4e2d\uff0c\u5b9e\u65f6\u5c55\u793a\u8bad\u7ec3\u6570\u636e\u548c\u6307\u6807\uff08\u5982\u635f\u5931\u51fd\u6570\u503c\u3001\u51c6\u786e\u7387\u7b49\uff09\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u7ed3\u5408PyTorch\u548cMatplotlib\uff0c\u6f14\u793a\u5982\u4f55\u5b9e\u73b0\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5PyTorch<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86PyTorch\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u64cd\u4f5c\u7cfb\u7edf\u548c\u786c\u4ef6\u914d\u7f6e\uff0c\u53c2\u8003PyTorch\u5b98\u7f51\u7684\u5b89\u88c5\u6307\u5357\u8fdb\u884c\u5b89\u88c5\u3002<\/p>\n<\/p>\n<p><h3>2. \u5b9e\u73b0\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u7ed3\u5408PyTorch\u548cMatplotlib\u5b9e\u73b0\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a\uff1a<\/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<p>import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b<\/strong><\/h2>\n<p>class SimpleModel(nn.Module):<\/p>\n<p>    def __init__(self):<\/p>\n<p>        super(SimpleModel, self).__init__()<\/p>\n<p>        self.fc = nn.Linear(1, 1)<\/p>\n<p>    def forward(self, x):<\/p>\n<p>        return self.fc(x)<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x_tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n = torch.linspace(-1, 1, 100).view(-1, 1)<\/p>\n<p>y_train = x_train.pow(2) + 0.2 * torch.rand(x_train.size())<\/p>\n<h2><strong>\u5b9e\u4f8b\u5316\u6a21\u578b\u3001\u5b9a\u4e49\u635f\u5931\u51fd\u6570\u548c\u4f18\u5316\u5668<\/strong><\/h2>\n<p>model = SimpleModel()<\/p>\n<p>criterion = nn.MSELoss()<\/p>\n<p>optimizer = optim.SGD(model.parameters(), lr=0.1)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u7a97\u53e3<\/strong><\/h2>\n<p>plt.ion()<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>loss_data = []<\/p>\n<p>line, = ax.plot(loss_data)<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>for epoch in range(100):<\/p>\n<p>    model.train()<\/p>\n<p>    optimizer.zero_grad()<\/p>\n<p>    outputs = model(x_train)<\/p>\n<p>    loss = criterion(outputs, y_train)<\/p>\n<p>    loss.backward()<\/p>\n<p>    optimizer.step()<\/p>\n<p>    # \u66f4\u65b0\u7ed8\u56fe\u6570\u636e<\/p>\n<p>    loss_data.append(loss.item())<\/p>\n<p>    line.set_xdata(np.arange(len(loss_data)))<\/p>\n<p>    line.set_ydata(loss_data)<\/p>\n<p>    ax.relim()<\/p>\n<p>    ax.autoscale_view()<\/p>\n<p>    fig.canvas.draw()<\/p>\n<p>    fig.canvas.flush_events()<\/p>\n<p>plt.ioff()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7<code>plt.ion()<\/code>\u5f00\u542f\u4ea4\u4e92\u6a21\u5f0f\uff0c\u4f7f\u5f97\u7ed8\u56fe\u7a97\u53e3\u80fd\u591f\u5b9e\u65f6\u66f4\u65b0\u3002\u5728\u6bcf\u4e2a\u8bad\u7ec3\u5468\u671f\u4e2d\uff0c\u8ba1\u7b97\u635f\u5931\u503c\u5e76\u66f4\u65b0\u7ed8\u56fe\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>3. \u4f18\u5316\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f18\u5316\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a\uff0c\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u51cf\u5c11\u7ed8\u56fe\u6b21\u6570<\/strong>\uff1a\u5728\u6bcf\u4e2a\u8bad\u7ec3\u5468\u671f\u4e2d\uff0c\u53ea\u5728\u7279\u5b9a\u5468\u671f\uff08\u5982\u6bcf10\u4e2a\u5468\u671f\uff09\u66f4\u65b0\u4e00\u6b21\u7ed8\u56fe\u3002<\/li>\n<li><strong>\u7b80\u5316\u7ed8\u56fe\u5185\u5bb9<\/strong>\uff1a\u53bb\u6389\u4e0d\u5fc5\u8981\u7684\u7ed8\u56fe\u5143\u7d20\uff08\u6807\u9898\u3001\u7f51\u683c\u7ebf\u7b49\uff09\uff0c\u4ee5\u63d0\u9ad8\u7ed8\u56fe\u901f\u5ea6\u3002<\/li>\n<li><strong>\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7ed8\u56fe\u5e93<\/strong>\uff1a\u5728\u6570\u636e\u91cf\u975e\u5e38\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7ed8\u56fe\u5e93\uff0c\u5982Plotly\u3002<\/li>\n<\/ul>\n<p><p><strong>\u56db\u3001\u7ed3\u5408TensorFlow\u8fdb\u884c\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a<\/strong><\/p>\n<\/p>\n<p><p>TensorFlow\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684\u6df1\u5ea6\u5b66\u4e60\u6846\u67b6\uff0c\u4e0b\u9762\u6211\u4eec\u5c06\u7ed3\u5408TensorFlow\u548cMatplotlib\uff0c\u6f14\u793a\u5982\u4f55\u5b9e\u73b0\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>1. \u5b89\u88c5TensorFlow<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86TensorFlow\u5e93\u3002\u5982\u679c\u6ca1\u6709\u5b89\u88c5\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install tensorflow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u5b9e\u73b0\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u7ed3\u5408TensorFlow\u548cMatplotlib\u5b9e\u73b0\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tensorflow as tf<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<p>import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>x_train = np.linspace(-1, 1, 100).reshape(-1, 1)<\/p>\n<p>y_train = x_train  2 + 0.2 * np.random.rand(x_train.shape[0], x_train.shape[1])<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b<\/strong><\/h2>\n<p>model = tf.keras.Sequential([<\/p>\n<p>    tf.keras.layers.Dense(1, input_shape=(1,))<\/p>\n<p>])<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;sgd&#39;, loss=&#39;mean_squared_error&#39;)<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7ed8\u56fe\u7a97\u53e3<\/strong><\/h2>\n<p>plt.ion()<\/p>\n<p>fig, ax = plt.subplots()<\/p>\n<p>loss_data = []<\/p>\n<p>line, = ax.plot(loss_data)<\/p>\n<h2><strong>\u81ea\u5b9a\u4e49\u56de\u8c03\u51fd\u6570<\/strong><\/h2>\n<p>class RealTimePlot(tf.keras.callbacks.Callback):<\/p>\n<p>    def on_epoch_end(self, epoch, logs=None):<\/p>\n<p>        loss_data.append(logs[&#39;loss&#39;])<\/p>\n<p>        line.set_xdata(np.arange(len(loss_data)))<\/p>\n<p>        line.set_ydata(loss_data)<\/p>\n<p>        ax.relim()<\/p>\n<p>        ax.autoscale_view()<\/p>\n<p>        fig.canvas.draw()<\/p>\n<p>        fig.canvas.flush_events()<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(x_train, y_train, epochs=100, callbacks=[RealTimePlot()])<\/p>\n<p>plt.ioff()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u901a\u8fc7\u81ea\u5b9a\u4e49\u56de\u8c03\u51fd\u6570<code>RealTimePlot<\/code>\uff0c\u5728\u6bcf\u4e2a\u8bad\u7ec3\u5468\u671f\u7ed3\u675f\u65f6\u66f4\u65b0\u7ed8\u56fe\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a\u3002<\/p>\n<\/p>\n<p><h3>3. \u4f18\u5316\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a<\/h3>\n<\/p>\n<p><p>\u4e0ePyTorch\u7c7b\u4f3c\uff0c\u4f18\u5316TensorFlow\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>\u51cf\u5c11\u7ed8\u56fe\u6b21\u6570<\/strong>\uff1a\u5728\u6bcf\u4e2a\u8bad\u7ec3\u5468\u671f\u4e2d\uff0c\u53ea\u5728\u7279\u5b9a\u5468\u671f\uff08\u5982\u6bcf10\u4e2a\u5468\u671f\uff09\u66f4\u65b0\u4e00\u6b21\u7ed8\u56fe\u3002<\/li>\n<li><strong>\u7b80\u5316\u7ed8\u56fe\u5185\u5bb9<\/strong>\uff1a\u53bb\u6389\u4e0d\u5fc5\u8981\u7684\u7ed8\u56fe\u5143\u7d20\uff08\u6807\u9898\u3001\u7f51\u683c\u7ebf\u7b49\uff09\uff0c\u4ee5\u63d0\u9ad8\u7ed8\u56fe\u901f\u5ea6\u3002<\/li>\n<li><strong>\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7ed8\u56fe\u5e93<\/strong>\uff1a\u5728\u6570\u636e\u91cf\u975e\u5e38\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7ed8\u56fe\u5e93\uff0c\u5982Plotly\u3002<\/li>\n<\/ul>\n<p><p><strong>\u4e94\u3001\u603b\u7ed3<\/strong><\/p>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u7528Python\u5b9e\u73b0\u8bad\u7ec3\u6570\u636e\u7684\u5b9e\u65f6\u663e\u793a\uff0c\u5206\u522b\u4f7f\u7528\u4e86Matplotlib\u548cPlotly\u4e24\u79cd\u7ed8\u56fe\u5e93\uff0c\u5e76\u7ed3\u5408PyTorch\u548cTensorFlow\u8fdb\u884c\u4e86\u793a\u4f8b\u6f14\u793a\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u7ed8\u56fe\u5e93\u548c\u4f18\u5316\u65b9\u6848\uff0c\u4ee5\u5b9e\u73b0\u9ad8\u6548\u3001\u5b9e\u65f6\u7684\u8bad\u7ec3\u6570\u636e\u5c55\u793a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5b9e\u65f6\u663e\u793a\u8bad\u7ec3\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528Matplotlib\u7b49\u53ef\u89c6\u5316\u5e93\u6765\u52a8\u6001\u5c55\u793a\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u7684\u6570\u636e\u3002\u901a\u8fc7\u5728\u8bad\u7ec3\u5faa\u73af\u4e2d\u5b9a\u671f\u66f4\u65b0\u56fe\u8868\uff0c\u60a8\u53ef\u4ee5\u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u7684\u53ef\u89c6\u5316\u3002\u5177\u4f53\u6b65\u9aa4\u5305\u62ec\u521b\u5efa\u4e00\u4e2a\u56fe\u5f62\u7a97\u53e3\uff0c\u7ed8\u5236\u521d\u59cb\u6570\u636e\uff0c\u5e76\u5728\u6bcf\u4e2a\u8bad\u7ec3\u8fed\u4ee3\u4e2d\u8c03\u7528\u66f4\u65b0\u51fd\u6570\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5de5\u5177\u53ef\u4ee5\u5e2e\u52a9\u6211\u5b9e\u73b0\u5b9e\u65f6\u6570\u636e\u53ef\u89c6\u5316\uff1f<\/strong><br \/>\u9664\u4e86Matplotlib\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6\u5e93\u5982Plotly\u3001Bokeh\u6216Dash\u3002\u8fd9\u4e9b\u5de5\u5177\u63d0\u4f9b\u4e86\u66f4\u4e3a\u4e30\u5bcc\u7684\u4ea4\u4e92\u6027\u548c\u52a8\u6001\u56fe\u8868\u529f\u80fd\uff0c\u4f7f\u5f97\u5b9e\u65f6\u6570\u636e\u5c55\u793a\u66f4\u52a0\u751f\u52a8\u548c\u76f4\u89c2\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f18\u5316\u5b9e\u65f6\u663e\u793a\u7684\u6027\u80fd\uff1f<\/strong><br \/>\u4e3a\u4e86\u786e\u4fdd\u5b9e\u65f6\u663e\u793a\u7684\u6d41\u7545\u6027\uff0c\u53ef\u4ee5\u8003\u8651\u51cf\u5c11\u6bcf\u6b21\u66f4\u65b0\u7684\u6570\u636e\u91cf\uff0c\u6216\u8005\u4f7f\u7528\u591a\u7ebf\u7a0b\u6765\u5904\u7406\u6570\u636e\u7684\u66f4\u65b0\u548c\u56fe\u5f62\u7684\u7ed8\u5236\u3002\u6b64\u5916\uff0c\u964d\u4f4e\u56fe\u5f62\u7684\u5206\u8fa8\u7387\u6216\u4f7f\u7528\u66f4\u9ad8\u6548\u7684\u7ed8\u56fe\u65b9\u6cd5\u4e5f\u80fd\u5e2e\u52a9\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<p><strong>\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\u5982\u4f55\u907f\u514d\u663e\u793a\u6570\u636e\u7684\u5e72\u6270\uff1f<\/strong><br \/>\u5728\u8bad\u7ec3\u8fc7\u7a0b\u4e2d\uff0c\u4f7f\u7528\u5408\u9002\u7684\u66f4\u65b0\u9891\u7387\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u4e2a\u65f6\u95f4\u95f4\u9694\u6216\u6bcf\u9694\u4e00\u5b9a\u6b65\u6570\u66f4\u65b0\u4e00\u6b21\u56fe\u8868\uff0c\u4ee5\u907f\u514d\u8fc7\u4e8e\u9891\u7e41\u7684\u66f4\u65b0\u5f71\u54cd\u8bad\u7ec3\u6027\u80fd\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u7ed8\u56fe\u548c\u8bad\u7ec3\u8fc7\u7a0b\u7684\u5206\u79bb\uff0c\u4f7f\u7528\u5f02\u6b65\u65b9\u5f0f\u53ef\u4ee5\u6709\u6548\u51cf\u5c11\u76f8\u4e92\u5e72\u6270\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5b9e\u73b0\u7528Python\u628a\u8bad\u7ec3\u6570\u636e\u5b9e\u65f6\u663e\u793a\uff0c\u53ef\u4ee5\u4f7f\u7528\u53ef\u89c6\u5316\u5de5\u5177\u5982Matplotlib\u3001Seaborn\u3001Plotly [&hellip;]","protected":false},"author":3,"featured_media":1112935,"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\/1112928"}],"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=1112928"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112928\/revisions"}],"predecessor-version":[{"id":1112939,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1112928\/revisions\/1112939"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1112935"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1112928"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1112928"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1112928"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}