{"id":76019,"date":"2026-02-13T19:27:15","date_gmt":"2026-02-13T11:27:15","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/76019.html"},"modified":"2026-02-13T19:27:15","modified_gmt":"2026-02-13T11:27:15","slug":"%e6%9d%8e%e5%93%a5%e8%80%83%e7%a0%94-%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%e7%ac%ac%e4%ba%8c%e8%8a%82","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/76019.html","title":{"rendered":"\u674e\u54e5\u8003\u7814-\u6df1\u5ea6\u5b66\u4e60\u7b2c\u4e8c\u8282"},"content":{"rendered":"<p>import torch<br \/>\nimport matplotlib.pyplot as plt   # \u5bfc\u5165\u7ed8\u56fe\u5e93&#xff0c;\u7528\u4e8e\u53ef\u89c6\u5316<br \/>\nimport random                     # \u5bfc\u5165\u968f\u673a\u6a21\u5757&#xff0c;\u7528\u4e8e\u6253\u4e71\u6570\u636e<\/p>\n<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; \u6570\u636e\u751f\u6210 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\ndef create_data(w, b, data_num):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u751f\u6210\u7ebf\u6027\u56de\u5f52\u7684\u5408\u6210\u6570\u636e\u3002<br \/>\n    \u53c2\u6570:<br \/>\n        w (Tensor): \u771f\u5b9e\u7684\u6743\u91cd\u5411\u91cf&#xff0c;\u5f62\u72b6\u4e3a (\u7279\u5f81\u6570,)<br \/>\n        b (Tensor): \u771f\u5b9e\u7684\u504f\u7f6e\u6807\u91cf<br \/>\n        data_num (int): \u6837\u672c\u6570\u91cf<br \/>\n    \u8fd4\u56de:<br \/>\n        x (Tensor): \u7279\u5f81\u77e9\u9635&#xff0c;\u5f62\u72b6 (data_num, \u7279\u5f81\u6570)<br \/>\n        y (Tensor): \u6807\u7b7e\u5411\u91cf&#xff0c;\u5f62\u72b6 (data_num,)<br \/>\n    &#034;&#034;&#034;<br \/>\n    # \u751f\u6210\u7279\u5f81&#xff1a;\u4ece\u6807\u51c6\u6b63\u6001\u5206\u5e03 N(0,1) \u4e2d\u91c7\u6837&#xff0c;\u5f62\u72b6 (data_num, len(w))<br \/>\n    x &#061; torch.normal(0, 1, (data_num, len(w)))<br \/>\n    # \u8ba1\u7b97\u7ebf\u6027\u90e8\u5206&#xff1a;y &#061; X * w &#043; b&#xff0c;matmul \u5b9e\u73b0\u77e9\u9635\u4e58\u6cd5<br \/>\n    y &#061; torch.matmul(x, w) &#043; b<br \/>\n    # \u6dfb\u52a0\u566a\u58f0&#xff1a;\u4ece N(0,0.01) \u91c7\u6837&#xff0c;\u5f62\u72b6\u4e0e y \u76f8\u540c<br \/>\n    noise &#061; torch.normal(0, 0.01, y.shape)<br \/>\n    y &#043;&#061; noise<br \/>\n    return x, y<\/p>\n<p># \u8bbe\u7f6e\u6837\u672c\u6570\u91cf<br \/>\nnum &#061; 500<\/p>\n<p># \u5b9a\u4e49\u771f\u5b9e\u7684\u6a21\u578b\u53c2\u6570&#xff1a;\u6743\u91cd\u548c\u504f\u7f6e<br \/>\ntrue_w &#061; torch.tensor([8.1, 2, 2, 4])   # 4\u4e2a\u7279\u5f81\u7684\u6743\u91cd<br \/>\ntrue_b &#061; torch.tensor(1.1)              # \u504f\u7f6e<\/p>\n<p># \u751f\u6210\u5408\u6210\u6570\u636e\u96c6<br \/>\nX, Y &#061; create_data(true_w, true_b, num)<\/p>\n<p># \u53ef\u89c6\u5316&#xff1a;\u53ea\u53d6\u7b2c4\u4e2a\u7279\u5f81&#xff08;\u7d22\u5f153&#xff09;\u4e0e\u6807\u7b7e\u7684\u6563\u70b9\u56fe<br \/>\nplt.scatter(X[:, 3], Y, 1)   # \u70b9\u5927\u5c0f\u4e3a1<br \/>\nplt.show()<\/p>\n<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; \u6570\u636e\u8fed\u4ee3\u5668 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\ndef data_provider(data, label, batchsize):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u6279\u91cf\u6570\u636e\u751f\u6210\u5668&#xff08;\u6bcf\u6b21\u8fd4\u56de\u4e00\u4e2a\u6279\u6b21\u7684\u6570\u636e&#xff09;\u3002<br \/>\n    \u53c2\u6570:<br \/>\n        data (Tensor): \u7279\u5f81\u77e9\u9635<br \/>\n        label (Tensor): \u6807\u7b7e\u5411\u91cf<br \/>\n        batchsize (int): \u6279\u6b21\u5927\u5c0f<br \/>\n    \u751f\u6210:<br \/>\n        (get_data, get_label): \u4e00\u4e2a\u6279\u6b21\u7684\u7279\u5f81\u548c\u6807\u7b7e<br \/>\n    &#034;&#034;&#034;<br \/>\n    length &#061; len(label)<br \/>\n    # \u751f\u6210\u6240\u6709\u6837\u672c\u7684\u7d22\u5f15\u5217\u8868<br \/>\n    indices &#061; list(range(length))<br \/>\n    # \u968f\u673a\u6253\u4e71\u7d22\u5f15&#xff0c;\u4fdd\u8bc1\u6bcf\u4e2a epoch \u7684\u6570\u636e\u987a\u5e8f\u4e0d\u540c<br \/>\n    random.shuffle(indices)<br \/>\n    # \u6309 batchsize \u904d\u5386\u7d22\u5f15<br \/>\n    for each in range(0, length, batchsize):<br \/>\n        get_indices &#061; indices[each: each &#043; batchsize]   # \u5f53\u524d\u6279\u6b21\u7684\u7d22\u5f15<br \/>\n        get_data &#061; data[get_indices]                    # \u6839\u636e\u7d22\u5f15\u53d6\u7279\u5f81<br \/>\n        get_label &#061; label[get_indices]                 # \u6839\u636e\u7d22\u5f15\u53d6\u6807\u7b7e<br \/>\n        yield get_data, get_label                      # \u751f\u6210\u5668\u8fd4\u56de\u5f53\u524d\u6279\u6b21<\/p>\n<p># \u8bbe\u7f6e\u6279\u6b21\u5927\u5c0f<br \/>\nbatchsize &#061; 16<br \/>\n# &#xff08;\u6ce8\u91ca\u6389\u7684\u6d4b\u8bd5\u4ee3\u7801&#xff0c;\u53ef\u7528\u4e8e\u9a8c\u8bc1\u751f\u6210\u5668&#xff09;<br \/>\n# for batch_x, batch_y in data_provider(X, Y, batchsize):<br \/>\n#     print(batch_x, batch_y)<br \/>\n#     break<\/p>\n<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; \u6a21\u578b\u5b9a\u4e49 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\ndef fun(x, w, b):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u7ebf\u6027\u56de\u5f52\u6a21\u578b\u7684\u524d\u5411\u8ba1\u7b97\u3002<br \/>\n    \u53c2\u6570:<br \/>\n        x (Tensor): \u8f93\u5165\u7279\u5f81\u77e9\u9635<br \/>\n        w (Tensor): \u6743\u91cd<br \/>\n        b (Tensor): \u504f\u7f6e<br \/>\n    \u8fd4\u56de:<br \/>\n        pred_y (Tensor): \u9884\u6d4b\u503c<br \/>\n    &#034;&#034;&#034;<br \/>\n    pred_y &#061; torch.matmul(x, w) &#043; b<br \/>\n    return pred_y<\/p>\n<p>def maeLoss(pre_y, y):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u5e73\u5747\u7edd\u5bf9\u8bef\u5dee\u635f\u5931\u51fd\u6570\u3002<br \/>\n    \u53c2\u6570:<br \/>\n        pre_y (Tensor): \u9884\u6d4b\u503c<br \/>\n        y (Tensor): \u771f\u5b9e\u503c<br \/>\n    \u8fd4\u56de:<br \/>\n        \u6807\u91cf\u635f\u5931\u503c<br \/>\n    &#034;&#034;&#034;<br \/>\n    return torch.sum(abs(pre_y &#8211; y)) \/ len(y)<\/p>\n<p>def sgd(paras, lr):<br \/>\n    &#034;&#034;&#034;<br \/>\n    \u968f\u673a\u68af\u5ea6\u4e0b\u964d\u66f4\u65b0\u53c2\u6570\u3002<br \/>\n    \u53c2\u6570:<br \/>\n        paras (list): \u9700\u8981\u66f4\u65b0\u7684\u53c2\u6570\u5217\u8868&#xff08;\u5305\u542b w \u548c b&#xff09;<br \/>\n        lr (float): \u5b66\u4e60\u7387<br \/>\n    &#034;&#034;&#034;<br \/>\n    with torch.no_grad():   # \u5728\u6b64\u4e0a\u4e0b\u6587\u4e2d&#xff0c;\u4e0d\u8ffd\u8e2a\u68af\u5ea6&#xff0c;\u8282\u7701\u5185\u5b58<br \/>\n        for para in paras:<br \/>\n            # \u53c2\u6570\u66f4\u65b0&#xff1a;\u53c2\u6570 &#061; \u53c2\u6570 &#8211; \u68af\u5ea6 * \u5b66\u4e60\u7387<br \/>\n            # \u6ce8\u610f&#xff1a;\u4e0d\u80fd\u5199\u6210 para &#061; para &#8211; para.grad*lr&#xff0c;\u56e0\u4e3a\u8fd9\u4f1a\u521b\u5efa\u65b0\u53d8\u91cf&#xff0c;<br \/>\n            # \u800c\u539f\u5730\u51cf\u6cd5 para -&#061; para.grad*lr \u4f1a\u76f4\u63a5\u4fee\u6539 para \u6307\u5411\u7684\u5f20\u91cf\u3002<br \/>\n            para -&#061; para.grad * lr<br \/>\n            # \u68af\u5ea6\u6e05\u96f6&#xff1a;\u907f\u514d\u68af\u5ea6\u7d2f\u79ef&#xff0c;\u5f71\u54cd\u4e0b\u4e00\u6b21\u8ba1\u7b97<br \/>\n            para.grad.zero_()<\/p>\n<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; \u53c2\u6570\u521d\u59cb\u5316 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nlr &#061; 0.03   # \u5b66\u4e60\u7387<br \/>\n# \u521d\u59cb\u5316\u6743\u91cd&#xff1a;\u4ece N(0,0.01) \u91c7\u6837&#xff0c;\u5f62\u72b6\u4e0e\u771f\u5b9e\u6743\u91cd\u76f8\u540c&#xff0c;\u5e76\u8bbe\u7f6e requires_grad&#061;True \u4ee5\u4fbf\u8ba1\u7b97\u68af\u5ea6<br \/>\nw_0 &#061; torch.normal(0, 0.01, true_w.shape, requires_grad&#061;True)<br \/>\n# \u521d\u59cb\u5316\u504f\u7f6e&#xff1a;\u6807\u91cf 0.01&#xff0c;\u8bbe\u7f6e requires_grad&#061;True<br \/>\nb_0 &#061; torch.tensor(0.01, requires_grad&#061;True)<br \/>\nprint(&#034;\u521d\u59cb\u6743\u91cd:&#034;, w_0, &#034;\u521d\u59cb\u504f\u7f6e:&#034;, b_0)<\/p>\n<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; \u8bad\u7ec3\u5faa\u73af &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nepochs &#061; 50   # \u8bad\u7ec3\u8f6e\u6570<\/p>\n<p>for epoch in range(epochs):<br \/>\n    data_loss &#061; 0   # \u7d2f\u8ba1\u5f53\u524d epoch \u7684\u603b\u635f\u5931<br \/>\n    # \u904d\u5386\u6bcf\u4e2a\u6279\u6b21\u7684\u6570\u636e<br \/>\n    for batch_x, batch_y in data_provider(X, Y, batchsize):<br \/>\n        # 1. \u524d\u5411\u4f20\u64ad&#xff1a;\u8ba1\u7b97\u5f53\u524d\u6279\u6b21\u7684\u9884\u6d4b\u503c<br \/>\n        pred_y &#061; fun(batch_x, w_0, b_0)<br \/>\n        # 2. \u8ba1\u7b97\u635f\u5931<br \/>\n        loss &#061; maeLoss(pred_y, batch_y)<br \/>\n        # 3. \u53cd\u5411\u4f20\u64ad&#xff1a;\u81ea\u52a8\u8ba1\u7b97\u635f\u5931\u5173\u4e8e w_0 \u548c b_0 \u7684\u68af\u5ea6<br \/>\n        loss.backward()<br \/>\n        # 4. \u4f7f\u7528 SGD \u66f4\u65b0\u53c2\u6570&#xff0c;\u5e76\u6e05\u96f6\u68af\u5ea6<br \/>\n        sgd([w_0, b_0], lr)<br \/>\n        # 5. \u7d2f\u52a0\u635f\u5931&#xff08;\u6ce8\u610f&#xff1a;loss \u662f\u6807\u91cf\u5f20\u91cf&#xff0c;\u7d2f\u52a0\u540e\u4ecd\u7136\u662f\u5f20\u91cf&#xff09;<br \/>\n        data_loss &#043;&#061; loss<br \/>\n    # \u6253\u5370\u6bcf\u4e2a epoch \u7684\u635f\u5931<br \/>\n    print(&#034;epoch %03d: loss: %.6f&#034; % (epoch, data_loss))<\/p>\n<p># \u6253\u5370\u771f\u5b9e\u53c2\u6570\u4e0e\u5b66\u4e60\u5230\u7684\u53c2\u6570\u5bf9\u6bd4<br \/>\nprint(&#034;\u771f\u5b9e\u7684\u51fd\u6570\u503c\u662f&#034;, true_w, true_b)<br \/>\nprint(&#034;\u8bad\u7ec3\u5f97\u5230\u7684\u53c2\u6570\u503c\u662f&#034;, w_0, b_0)<\/p>\n<p># &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; \u7ed3\u679c\u53ef\u89c6\u5316 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nidx &#061; 3   # \u9009\u62e9\u7b2c4\u4e2a\u7279\u5f81\u8fdb\u884c\u53ef\u89c6\u5316<br \/>\n# \u7ed8\u5236\u62df\u5408\u76f4\u7ebf&#xff1a;\u4f7f\u7528 detach() \u4ece\u8ba1\u7b97\u56fe\u4e2d\u5206\u79bb&#xff0c;\u8f6c\u6362\u4e3a numpy \u6570\u7ec4\u8fdb\u884c\u7ed8\u56fe<br \/>\n# \u76f4\u7ebf\u65b9\u7a0b&#xff1a;y &#061; x * w_0[idx] &#043; b_0<br \/>\nplt.plot(X[:, idx].detach().numpy(),<br \/>\n         X[:, idx].detach().numpy() * w_0[idx].detach().numpy() &#043; b_0.detach().numpy())<br \/>\n# \u7ed8\u5236\u539f\u59cb\u6570\u636e\u6563\u70b9\u56fe<br \/>\nplt.scatter(X[:, idx], Y, 1)<br \/>\nplt.show()<\/p>\n<h4>1.\u00a0\u6570\u636e\u51c6\u5907<\/h4>\n<ul>\n<li>\n<p>\u771f\u5b9e\u53c2\u6570&#xff1a;\u8bbe\u5b9a\u771f\u5b9e\u6743\u91cd\u00a0true_w &#061; [8.1, 2, 2, 4]&#xff0c;\u771f\u5b9e\u504f\u7f6e\u00a0true_b &#061; 1.1\u3002<\/p>\n<\/li>\n<li>\n<p>\u751f\u6210\u5408\u6210\u6570\u636e&#xff1a;\u8c03\u7528\u00a0create_data()&#xff0c;\u6839\u636e\u771f\u5b9e\u53c2\u6570\u751f\u6210 500 \u4e2a\u6837\u672c&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u7279\u5f81\u00a0X\u00a0\u4ece\u6807\u51c6\u6b63\u6001\u5206\u5e03\u00a0N(0,1)\u00a0\u91c7\u6837\u3002<\/p>\n<\/li>\n<li>\n<p>\u6807\u7b7e\u00a0Y\u00a0\u7531\u7ebf\u6027\u516c\u5f0f\u00a0X\u00b7w &#043; b\u00a0\u8ba1\u7b97&#xff0c;\u5e76\u6dfb\u52a0\u5fae\u5c0f\u9ad8\u65af\u566a\u58f0\u00a0N(0,0.01)\u3002<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p>\u53ef\u89c6\u5316&#xff1a;\u7ed8\u5236\u7b2c 4 \u4e2a\u7279\u5f81\u4e0e\u6807\u7b7e\u7684\u6563\u70b9\u56fe&#xff0c;\u89c2\u5bdf\u6570\u636e\u5206\u5e03\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4>2.\u00a0\u6a21\u578b\u4e0e\u8d85\u53c2\u6570\u5b9a\u4e49<\/h4>\n<ul>\n<li>\n<p>\u6a21\u578b&#xff1a;\u7ebf\u6027\u56de\u5f52\u00a0fun(x, w, b) &#061; x\u00b7w &#043; b\u3002<\/p>\n<\/li>\n<li>\n<p>\u635f\u5931\u51fd\u6570&#xff1a;\u5e73\u5747\u7edd\u5bf9\u8bef\u5dee&#xff08;MAE&#xff09;\u00a0maeLoss()\u3002<\/p>\n<\/li>\n<li>\n<p>\u4f18\u5316\u5668&#xff1a;\u624b\u52a8\u5b9e\u73b0\u968f\u673a\u68af\u5ea6\u4e0b\u964d\u00a0sgd()&#xff0c;\u901a\u8fc7\u00a0with torch.no_grad()\u00a0\u539f\u5730\u66f4\u65b0\u53c2\u6570\u5e76\u6e05\u96f6\u68af\u5ea6\u3002<\/p>\n<\/li>\n<li>\n<p>\u8d85\u53c2\u6570&#xff1a;<\/p>\n<ul>\n<li>\n<p>\u5b66\u4e60\u7387\u00a0lr &#061; 0.03<\/p>\n<\/li>\n<li>\n<p>\u6279\u6b21\u5927\u5c0f\u00a0batchsize &#061; 16<\/p>\n<\/li>\n<li>\n<p>\u8bad\u7ec3\u8f6e\u6570\u00a0epochs &#061; 50<\/p>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<hr \/>\n<h4>3.\u00a0\u53c2\u6570\u521d\u59cb\u5316<\/h4>\n<ul>\n<li>\n<p>\u6743\u91cd\u00a0w_0\u00a0\u4ece\u00a0N(0,0.01)\u00a0\u91c7\u6837&#xff0c;\u504f\u7f6e\u00a0b_0\u00a0\u521d\u59cb\u5316\u4e3a\u00a00.01&#xff0c;\u5747\u8bbe\u7f6e\u00a0requires_grad&#061;True\u00a0\u4ee5\u8ba1\u7b97\u68af\u5ea6\u3002<\/p>\n<\/li>\n<\/ul>\n<hr \/>\n<h4>4.\u00a0\u8bad\u7ec3\u5faa\u73af<\/h4>\n<p>\u6bcf\u4e2a epoch \u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c&#xff1a;<\/p>\n<li>\n<p>\u6570\u636e\u8fed\u4ee3&#xff1a;\u901a\u8fc7\u81ea\u5b9a\u4e49\u751f\u6210\u5668\u00a0data_provider()\u00a0\u6253\u4e71\u6570\u636e\u7d22\u5f15&#xff0c;\u6bcf\u6b21\u8fd4\u56de\u4e00\u4e2a\u6279\u6b21\u00a0(batch_x, batch_y)\u3002<\/p>\n<\/li>\n<li>\n<p>\u524d\u5411\u4f20\u64ad&#xff1a;\u8ba1\u7b97\u5f53\u524d\u6279\u6b21\u7684\u9884\u6d4b\u503c\u00a0pred_y &#061; fun(batch_x, w_0, b_0)\u3002<\/p>\n<\/li>\n<li>\n<p>\u635f\u5931\u8ba1\u7b97&#xff1a;\u8ba1\u7b97\u9884\u6d4b\u503c\u4e0e\u771f\u5b9e\u503c\u7684 MAE \u635f\u5931\u3002<\/p>\n<\/li>\n<li>\n<p>\u53cd\u5411\u4f20\u64ad&#xff1a;\u8c03\u7528\u00a0loss.backward()&#xff0c;\u81ea\u52a8\u8ba1\u7b97\u635f\u5931\u5bf9\u00a0w_0\u00a0\u548c\u00a0b_0\u00a0\u7684\u68af\u5ea6\u3002<\/p>\n<\/li>\n<li>\n<p>\u53c2\u6570\u66f4\u65b0&#xff1a;\u8c03\u7528\u00a0sgd([w_0, b_0], lr)&#xff0c;\u6267\u884c\u00a0para -&#061; para.grad * lr\u00a0\u5e76\u6e05\u96f6\u68af\u5ea6\u3002<\/p>\n<\/li>\n<li>\n<p>\u635f\u5931\u7d2f\u8ba1&#xff1a;\u5c06\u5f53\u524d\u6279\u6b21\u635f\u5931\u7d2f\u52a0\u81f3\u00a0data_loss\u3002<\/p>\n<\/li>\n<p>\u6bcf\u4e2a epoch \u7ed3\u675f\u540e\u6253\u5370\u5f53\u524d\u635f\u5931\u503c\u3002<\/p>\n<hr \/>\n<h4>5.\u00a0\u7ed3\u679c\u8f93\u51fa\u4e0e\u53ef\u89c6\u5316<\/h4>\n<ul>\n<li>\n<p>\u6253\u5370\u8bad\u7ec3\u5f97\u5230\u7684\u53c2\u6570\u00a0w_0\u00a0\u548c\u00a0b_0&#xff0c;\u4e0e\u771f\u5b9e\u53c2\u6570\u5bf9\u6bd4\u3002<\/p>\n<\/li>\n<li>\n<p>\u518d\u6b21\u7ed8\u5236\u7b2c 4 \u4e2a\u7279\u5f81\u7684\u6563\u70b9\u56fe&#xff0c;\u5e76\u53e0\u52a0\u62df\u5408\u7684\u76f4\u7ebf\u00a0x * w_0[3] &#043; b_0&#xff0c;\u76f4\u89c2\u5c55\u793a\u6a21\u578b\u62df\u5408\u6548\u679c\u3002<\/p>\n<\/li>\n<li>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"350\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260213112713-698f0a91d05a8.png\" width=\"437\" \/><\/p>\n<\/li>\n<li>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"262\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260213112714-698f0a923bcd7.png\" width=\"1337\" \/><\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>import torch<br \/>\nimport matplotlib.pyplot as plt   # \u5bfc\u5165\u7ed8\u56fe\u5e93&#xff0c;\u7528\u4e8e\u53ef\u89c6\u5316<br \/>\nimport random                     # \u5bfc\u5165\u968f\u673a\u6a21\u5757&#xff0c;\u7528\u4e8e\u6253\u4e71\u6570\u636e# &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; \u6570\u636e\u751f\u6210 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\ndef create_data(w, b, data_num):\\&#8221;\\&#8221;\\&#8221;\u751f\u6210\u7ebf\u6027\u56de\u5f52\u7684\u5408\u6210\u6570\u636e\u3002\u53c2\u6570:w (Tensor): \u771f\u5b9e\u7684\u6743\u91cd<\/p>\n","protected":false},"author":2,"featured_media":76017,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[50,86],"topic":[],"class_list":["post-76019","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-50","tag-86"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u674e\u54e5\u8003\u7814-\u6df1\u5ea6\u5b66\u4e60\u7b2c\u4e8c\u8282 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/76019.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u674e\u54e5\u8003\u7814-\u6df1\u5ea6\u5b66\u4e60\u7b2c\u4e8c\u8282 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"import torch import matplotlib.pyplot as plt  # \u5bfc\u5165\u7ed8\u56fe\u5e93&#xff0c;\u7528\u4e8e\u53ef\u89c6\u5316 import random           # \u5bfc\u5165\u968f\u673a\u6a21\u5757&#xff0c;\u7528\u4e8e\u6253\u4e71\u6570\u636e# -------------------- \u6570\u636e\u751f\u6210 -------------------- def create_data(w, b, data_num):&quot;&quot;&quot;\u751f\u6210\u7ebf\u6027\u56de\u5f52\u7684\u5408\u6210\u6570\u636e\u3002\u53c2\u6570:w (Tensor): \u771f\u5b9e\u7684\u6743\u91cd\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/76019.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-13T11:27:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260213112713-698f0a91d05a8.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/76019.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/76019.html\",\"name\":\"\u674e\u54e5\u8003\u7814-\u6df1\u5ea6\u5b66\u4e60\u7b2c\u4e8c\u8282 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-13T11:27:15+00:00\",\"dateModified\":\"2026-02-13T11:27:15+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/76019.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/76019.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/76019.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u674e\u54e5\u8003\u7814-\u6df1\u5ea6\u5b66\u4e60\u7b2c\u4e8c\u8282\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u674e\u54e5\u8003\u7814-\u6df1\u5ea6\u5b66\u4e60\u7b2c\u4e8c\u8282 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/76019.html","og_locale":"zh_CN","og_type":"article","og_title":"\u674e\u54e5\u8003\u7814-\u6df1\u5ea6\u5b66\u4e60\u7b2c\u4e8c\u8282 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"import torch import matplotlib.pyplot as plt  # \u5bfc\u5165\u7ed8\u56fe\u5e93&#xff0c;\u7528\u4e8e\u53ef\u89c6\u5316 import random           # \u5bfc\u5165\u968f\u673a\u6a21\u5757&#xff0c;\u7528\u4e8e\u6253\u4e71\u6570\u636e# -------------------- \u6570\u636e\u751f\u6210 -------------------- def create_data(w, b, data_num):\"\"\"\u751f\u6210\u7ebf\u6027\u56de\u5f52\u7684\u5408\u6210\u6570\u636e\u3002\u53c2\u6570:w (Tensor): \u771f\u5b9e\u7684\u6743\u91cd","og_url":"https:\/\/www.wsisp.com\/helps\/76019.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-13T11:27:15+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/02\/20260213112713-698f0a91d05a8.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/76019.html","url":"https:\/\/www.wsisp.com\/helps\/76019.html","name":"\u674e\u54e5\u8003\u7814-\u6df1\u5ea6\u5b66\u4e60\u7b2c\u4e8c\u8282 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-13T11:27:15+00:00","dateModified":"2026-02-13T11:27:15+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/76019.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/76019.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/76019.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u674e\u54e5\u8003\u7814-\u6df1\u5ea6\u5b66\u4e60\u7b2c\u4e8c\u8282"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/76019","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=76019"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/76019\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/76017"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=76019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=76019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=76019"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=76019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}