{"id":1634,"date":"2025-02-02T13:41:00","date_gmt":"2025-02-02T05:41:00","guid":{"rendered":"https:\/\/www.forillusion.com\/?p=1634"},"modified":"2025-02-14T11:39:02","modified_gmt":"2025-02-14T03:39:02","slug":"3-6-softmax-regression-scratch","status":"publish","type":"post","link":"https:\/\/www.forillusion.com\/index.php\/3-6-softmax-regression-scratch\/","title":{"rendered":"3.6 softmax\u56de\u5f52\u7684\u4ece\u96f6\u5f00\u59cb\u5b9e\u73b0"},"content":{"rendered":"\n<p><div class=\"has-toc have-toc\"><\/div><\/p>\n\n\n\n<p id=\"20250202191029-7pvvq8k\">\u5bfc\u5165\u672c\u8282\u5b9e\u73b0\u6240\u9700\u7684\u5305\u6216\u6a21\u5757<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">import numpy as np\nimport sys\nimport torch\nimport torchvision\nfrom torch.utils import data\nfrom torchvision import transforms\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u83b7\u53d6\u548c\u8bfb\u53d6\u6570\u636e\">\u83b7\u53d6\u548c\u8bfb\u53d6\u6570\u636e<\/h2>\n\n\n\n<p id=\"20250124205300-v69zy24\">\u5148\u83b7\u53d6Fashion-MNIST\u6570\u636e\u96c6\uff0c\u5e76\u8bbe\u7f6e\u6279\u91cf\u5927\u5c0f\u4e3a256\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">def get_dataloader_workers():\n    if sys.platform.startswith('win'):\n        num_workers = 0  # 0\u8868\u793a\u4e0d\u7528\u989d\u5916\u7684\u8fdb\u7a0b\u6765\u52a0\u901f\u8bfb\u53d6\u6570\u636e\n    else:\n        num_workers = 4\n\ndef load_data_fashion_mnist(batch_size, resize=None): \n    # \u4e0b\u8f7dFashion-MNIST\u6570\u636e\u96c6\uff0c\u7136\u540e\u5c06\u5176\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff0c\u8fd4\u56de\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6\u7684\u6570\u636e\u8fed\u4ee3\u5668\n    mnist_train = torchvision.datasets.FashionMNIST(root=\"data\", train=True, transform=transforms.ToTensor(), download=True)\n    mnist_test = torchvision.datasets.FashionMNIST(root=\"data\", train=False, transform=transforms.ToTensor(), download=True)\n    return (data.DataLoader(mnist_train, batch_size, shuffle=True, num_workers=get_dataloader_workers()),\n            data.DataLoader(mnist_test, batch_size, shuffle=False, num_workers=get_dataloader_workers()))\n\nbatch_size = 256\ntrain_iter, test_iter = load_data_fashion_mnist(batch_size)\n<\/code><\/pre>\n\n\n\n<p id=\"20250124192908-w47z5z9\">\u4f7f\u7528\u5411\u91cf\u8868\u793a\u6bcf\u4e2a\u6837\u672c\u3002\u5df2\u77e5\u6bcf\u4e2a\u6837\u672c\u8f93\u5165\u662f\u9ad8\u548c\u5bbd\u5747\u4e3a28\u50cf\u7d20\u7684\u56fe\u50cf\u3002\u6a21\u578b\u7684\u8f93\u5165\u5411\u91cf\u7684\u957f\u5ea6\u662f \uff1a\u8be5\u5411\u91cf\u7684\u6bcf\u4e2a\u5143\u7d20\u5bf9\u5e94\u56fe\u50cf\u4e2d\u6bcf\u4e2a\u50cf\u7d20\u3002\u7531\u4e8e\u56fe\u50cf\u670910\u4e2a\u7c7b\u522b\uff0c\u5355\u5c42\u795e\u7ecf\u7f51\u7edc\u8f93\u51fa\u5c42\u7684\u8f93\u51fa\u4e2a\u6570\u4e3a10\uff0c\u56e0\u6b64softmax\u56de\u5f52\u7684\u6743\u91cd\u548c\u504f\u5dee\u53c2\u6570\u5206\u522b\u4e3a\u548c\u7684\u77e9\u9635\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u521d\u59cb\u5316\u6a21\u578b\u53c2\u6570\">\u521d\u59cb\u5316\u6a21\u578b\u53c2\u6570<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">num_inputs = 784\nnum_outputs = 10\n\nW = torch.tensor(np.random.normal(0, 0.01, (num_inputs, num_outputs)), dtype=torch.float) \n# np.random.normal \u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\uff0c\u5747\u503c\u4e3a0\uff0c\u6807\u51c6\u5dee\u4e3a0.01\uff0c\u5f62\u72b6\u4e3a\uff08num_inputs, num_outputs\uff09\uff0ctorch.tensor \u5c06\u751f\u6210\u7684\u968f\u673a\u6570\u8f6c\u6362\u4e3a\u5f20\u91cf\nb = torch.zeros(num_outputs, dtype=torch.float) # \u504f\u7f6eb\u521d\u59cb\u5316\u4e3a0\n\nW.requires_grad_(requires_grad=True) # \u8bbe\u7f6e\u53c2\u6570W\u9700\u8981\u6c42\u68af\u5ea6\nb.requires_grad_(requires_grad=True) # \u8bbe\u7f6e\u53c2\u6570b\u9700\u8981\u6c42\u68af\u5ea6\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5b9e\u73b0softmax\u8fd0\u7b97\">\u5b9e\u73b0softmax\u8fd0\u7b97<\/h2>\n\n\n\n<p id=\"20250124193400-jgqwj20\">\u5148\u63cf\u8ff0\u4e00\u4e0b\u5bf9\u5982\u4f55\u5bf9\u591a\u7ef4Tensor\u200b\u6309\u7ef4\u5ea6\u64cd\u4f5c\u3002\u5728\u4e0b\u9762\u7684\u4f8b\u5b50\u4e2d\uff0c\u7ed9\u5b9a\u4e00\u4e2aTensor\u200b\u77e9\u9635X\u200b\u3002\u6211\u4eec\u53ef\u4ee5\u53ea\u5bf9\u5176\u4e2d\u540c\u4e00\u5217\uff08dim=0\u200b\uff09\u6216\u540c\u4e00\u884c\uff08dim=1\u200b\uff09\u7684\u5143\u7d20\u6c42\u548c\uff0c\u5e76\u5728\u7ed3\u679c\u4e2d\u4fdd\u7559\u884c\u548c\u5217\u8fd9\u4e24\u4e2a\u7ef4\u5ea6\uff08keepdim=True\u200b\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">X = torch.tensor(&#91;&#91;1, 2, 3], &#91;4, 5, 6]])\nprint(X.sum(dim=0, keepdim=True))\nprint(X.sum(dim=1, keepdim=True))\n\n# output:\n# tensor(&#91;&#91;5, 7, 9]])\n# tensor(&#91;&#91; 6],\n#         &#91;15]])\n<\/code><\/pre>\n\n\n\n<p id=\"20250124193814-pcc6ms8\">\u5728\u4e0b\u9762\u7684\u51fd\u6570\u4e2d\uff0c\u77e9\u9635X\u200b\u7684\u884c\u6570\u662f\u6837\u672c\u6570\uff0c\u5217\u6570\u662f\u8f93\u51fa\u4e2a\u6570\u3002\u4e3a\u4e86\u8868\u8fbe\u6837\u672c\u9884\u6d4b\u5404\u4e2a\u8f93\u51fa\u7684\u6982\u7387\uff0csoftmax\u8fd0\u7b97\u4f1a\u5148\u901a\u8fc7exp\u200b\u51fd\u6570\u5bf9\u6bcf\u4e2a\u5143\u7d20\u505a\u6307\u6570\u8fd0\u7b97\uff0c\u518d\u5bf9exp\u200b\u77e9\u9635\u540c\u884c\u5143\u7d20\u6c42\u548c\uff0c\u6700\u540e\u4ee4\u77e9\u9635\u6bcf\u884c\u5404\u5143\u7d20\u4e0e\u8be5\u884c\u5143\u7d20\u4e4b\u548c\u76f8\u9664\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u6700\u7ec8\u5f97\u5230\u7684\u77e9\u9635\u6bcf\u884c\u5143\u7d20\u548c\u4e3a1\u4e14\u975e\u8d1f\u3002\u56e0\u6b64\uff0c\u8be5\u77e9\u9635\u6bcf\u884c\u90fd\u662f\u5408\u6cd5\u7684\u6982\u7387\u5206\u5e03\u3002softmax\u8fd0\u7b97\u7684\u8f93\u51fa\u77e9\u9635\u4e2d\u7684\u4efb\u610f\u4e00\u884c\u5143\u7d20\u4ee3\u8868\u4e86\u4e00\u4e2a\u6837\u672c\u5728\u5404\u4e2a\u8f93\u51fa\u7c7b\u522b\u4e0a\u7684\u9884\u6d4b\u6982\u7387\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">def softmax(X):\n    X_exp = X.exp()\n    partition = X_exp.sum(dim=1, keepdim=True)\n    return X_exp \/ partition  # \u8fd9\u91cc\u5e94\u7528\u4e86\u5e7f\u64ad\u673a\u5236\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5b9a\u4e49\u6a21\u578b\">\u5b9a\u4e49\u6a21\u578b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">def net(X):\n    return softmax(torch.mm(X.view((-1, num_inputs)), W) + b) \n    # X.view((-1, num_inputs))\u5c06X\u53d8\u5f62\u4e3a(batch_size, num_inputs)\u7684\u5f62\u72b6\uff0c\u4e5f\u5c31\u662f\u5c06\u6bcf\u4e2a28*28\u7684\u56fe\u50cf\u90fd\u8f6c\u6362\u4e3a\u957f\u5ea6\u4e3anum_inputs\uff08784\uff09\u7684\u5411\u91cf\uff0c\u4e00\u884c\u4ee3\u8868\u4e00\u4e2a\u6837\u672c\uff08\u56fe\u50cf\uff09\uff0c\u4e00\u5171batch_size\u884c\n    # torch.mm(X.view((-1, num_inputs)), W)\u5b9e\u73b0X\u548cW\u7684\u77e9\u9635\u4e58\u6cd5\uff0c\u7136\u540e\u52a0\u4e0a\u504f\u7f6eb\uff0c\u6700\u540e\u5c06\u7ed3\u679c\u8f93\u5165softmax\u51fd\u6570\uff0c\u5f97\u5230\u9884\u6d4b\u6982\u7387\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u5b9a\u4e49\u635f\u5931\u51fd\u6570\">\u5b9a\u4e49\u635f\u5931\u51fd\u6570<\/h2>\n\n\n\n<p id=\"20250124195610-8vst1rr\">\u4f7f\u7528gather\u200b\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u901a\u8fc7\u7d22\u5f15\u83b7\u53d6\u6570\u636e\uff0c\u4e5f\u5c31\u662f\u53ef\u4ee5\u5f97\u5230\u6807\u7b7e\u6240\u5bf9\u5e94\u7684\u9884\u6d4b\u6982\u7387\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\"># \ny_hat = torch.tensor(&#91;&#91;0.1, 0.3, 0.6], &#91;0.3, 0.2, 0.5]])  # \u5047\u8bbe\u6709\u4e24\u4e2a\u6837\u672c\uff0c\u6bcf\u4e2a\u6837\u672c\u6709\u4e09\u4e2a\u7c7b\u522b\u7684\u9884\u6d4b\u6982\u7387\ny = torch.LongTensor(&#91;0, 2])  # \u4e24\u4e2a\u6837\u672c\u7684\u6807\u7b7e\u5206\u522b\u4e3a0\u548c2\ny_hat.gather(1, y.view(-1, 1))   \n# y.view(-1, 1)\u5c06y\u53d8\u5f62\u4e3a\u5217\u5411\u91cf\uff0c\u7136\u540e\u4f7f\u7528gather\u51fd\u6570\uff0c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u7ef4\u5ea6\uff0c\u7b2c\u4e8c\u4e2a\u53c2\u6570\u662f\u7d22\u5f15\uff0c\u8868\u793a\u5728\u7b2c\u4e00\u4e2a\u7ef4\u5ea6\u4e0a\uff0c\u53d6\u7d22\u5f15\u4e3ay\u7684\u5143\u7d20\n# \u7ef4\u5ea6\uff1a0\u8868\u793a\u6309\u884c\u53d6\u503c\uff0c1\u8868\u793a\u6309\u5217\u53d6\u503c\n<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">def cross_entropy(y_hat, y):\n    return - torch.log(y_hat.gather(1, y.view(-1, 1))) \n    # gather\u51fd\u6570\u53d6\u51fa\u6807\u7b7ey\u5bf9\u5e94\u7684\u9884\u6d4b\u6982\u7387\uff0c\u7136\u540e\u53d6\u5bf9\u6570\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u8ba1\u7b97\u5206\u7c7b\u51c6\u786e\u7387\">\u8ba1\u7b97\u5206\u7c7b\u51c6\u786e\u7387<\/h2>\n\n\n\n<p id=\"20250124194828-kuv0z1l\">\u7ed9\u5b9a\u4e00\u4e2a\u7c7b\u522b\u7684\u9884\u6d4b\u6982\u7387\u5206\u5e03y_hat\u200b\uff0c\u6211\u4eec\u628a\u9884\u6d4b\u6982\u7387\u6700\u5927\u7684\u7c7b\u522b\u4f5c\u4e3a\u8f93\u51fa\u7c7b\u522b\u3002\u5982\u679c\u5b83\u4e0e\u771f\u5b9e\u7c7b\u522by\u200b\u4e00\u81f4\uff0c\u8bf4\u660e\u8fd9\u6b21\u9884\u6d4b\u662f\u6b63\u786e\u7684\u3002\u5206\u7c7b\u51c6\u786e\u7387\u5373\u6b63\u786e\u9884\u6d4b\u6570\u91cf\u4e0e\u603b\u9884\u6d4b\u6570\u91cf\u4e4b\u6bd4\u3002<\/p>\n\n\n\n<p id=\"20250124203241-zm7njaq\">\u5b9a\u4e49\u4e00\u4e2a\u51c6\u786e\u7387\u51fd\u6570accuracy\u200b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">def accuracy(y_hat, y):\n    return (y_hat.argmax(dim=1) == y).float().mean().item()\n    # y_hat.argmax(dim=1)\u8fd4\u56de\u77e9\u9635y_hat\u6bcf\u884c\u4e2d\u6700\u5927\u5143\u7d20\u7684\u7d22\u5f15\uff0c\u4e14\u8fd4\u56de\u7ed3\u679c\u4e0e\u53d8\u91cfy\u5f62\u72b6\u76f8\u540c\u3002\n    # \u76f8\u7b49\u6761\u4ef6\u5224\u65ad\u5f0f(y_hat.argmax(dim=1) == y)\u662f\u4e00\u4e2a\u7c7b\u578b\u4e3aByteTensor\u7684Tensor\uff0c\u7528float()\u5c06\u5176\u8f6c\u6362\u4e3a\u503c\u4e3a0\uff08\u76f8\u7b49\u4e3a\u5047\uff09\u62161\uff08\u76f8\u7b49\u4e3a\u771f\uff09\u7684\u6d6e\u70b9\u578bTensor\u3002\n    # \u6700\u540e\u7528mean()\u6c42\u5f97\u5e73\u5747\u503c\u5373\u4e3a\u6b63\u786e\u7387\u3002\n    # item()\u5c06\u53ea\u542b\u4e00\u4e2a\u5143\u7d20\u7684tensor\u8f6c\u6362\u4e3a\u6807\u91cf\n<\/code><\/pre>\n\n\n\n<p id=\"20250124205000-hk7rr2e\">\u8bc4\u4ef7\u6a21\u578bnet\u200b\u5728\u6570\u636e\u96c6data_iter\u200b\u4e0a\u7684\u51c6\u786e\u7387\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">def evaluate_accuracy(data_iter, net):\n    acc_sum, n = 0.0, 0\n    for X, y in data_iter: # X\u662f\u56fe\u50cf\uff0cy\u662f\u6807\u7b7e\uff0c\u6570\u91cf\u4e3abatch_size\n        acc_sum += (net(X).argmax(dim=1) == y).float().sum().item() # net(X) \u8fd4\u56de\u9884\u6d4b\u6982\u7387\uff0cargmax(dim=1)\u8fd4\u56de\u6982\u7387\u6700\u5927\u7684\u7c7b\u522b\uff0c\u4e0e\u6807\u7b7ey\u6bd4\u8f83\n        n += y.shape&#91;0] # y.shape&#91;0]\u662fy\u7684\u884c\u6570\uff0c\u4e5f\u5c31\u662fbatch_size\n    return acc_sum \/ n # \u8fd4\u56de\u6b63\u786e\u7387\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u8bad\u7ec3\u6a21\u578b\">\u8bad\u7ec3\u6a21\u578b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\"># 3.2.6 \u5b9a\u4e49\u4f18\u5316\u7b97\u6cd5\ndef sgd(params,lr,batch_size):  #\u5b9a\u4e49\u4f18\u5316\u7b97\u6cd5,params:\u5f85\u4f18\u5316\u53c2\u6570,lr:\u5b66\u4e60\u7387,batch_size:\u6279\u91cf\u5927\u5c0f\n    for param in params:\n        param.data-=lr*param.grad\/batch_size  #\u6ce8\u610f\u8fd9\u91cc\u66f4\u6539param\u65f6\u7528\u7684param.data\n\n\ndef train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params=None, lr=None, optimizer=None): \n    # net: \u7f51\u7edc\uff0c\u5373\u7ebf\u6027\u56de\u5f52\u6a21\u578b\n    # train_iter: \u8bad\u7ec3\u6570\u636e\u96c6\uff0ctest_iter: \u6d4b\u8bd5\u6570\u636e\u96c6\n    # loss: \u635f\u5931\u51fd\u6570\uff0cnum_epochs: \u8bad\u7ec3\u7684\u8f6e\u6570\uff0cbatch_size: \u6279\u91cf\u5927\u5c0f\n    # params: \u6a21\u578b\u53c2\u6570\uff0c\u5373W\u548cb\uff0clr: \u5b66\u4e60\u7387\uff0coptimizer: \u4f18\u5316\u7b97\u6cd5\uff0c\u5982SGD\n    for epoch in range(num_epochs):  # \u8bad\u7ec3\u6a21\u578b\u4e00\u5171\u9700\u8981num_epochs\u4e2a\u8fed\u4ee3\u5468\u671f\n        train_l_sum, train_acc_sum, n = 0.0, 0.0, 0 # \u8bad\u7ec3\u635f\u5931\u603b\u548c\uff0c\u8bad\u7ec3\u51c6\u786e\u5ea6\u603b\u548c\uff0c\u6837\u672c\u6570\n        for X, y in train_iter: # X\u662f\u56fe\u50cf\uff0cy\u662f\u6807\u7b7e\uff0c\u6570\u91cf\u4e3abatch_size\n            y_hat = net(X) # \u9884\u6d4b\u6982\u7387\n            l = loss(y_hat, y).sum() # \u8ba1\u7b97\u635f\u5931\uff0csum()\u5c06\u6240\u6709loss\u503c\u76f8\u52a0\u5f97\u5230\u4e00\u4e2a\u6807\u91cf\n\n            # \u68af\u5ea6\u6e05\u96f6\n            if optimizer is not None: # \u4f7f\u7528PyTorch\u5185\u7f6e\u7684\u4f18\u5316\u5668\u548c\u635f\u5931\u51fd\u6570\n                optimizer.zero_grad() # \u68af\u5ea6\u6e05\u96f6\n            elif params is not None and params&#91;0].grad is not None: # \u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u4f18\u5316\u5668\u548c\u635f\u5931\u51fd\u6570\n                for param in params: \n                    param.grad.data.zero_()\n\n            l.backward() # \u8ba1\u7b97\u68af\u5ea6\n            if optimizer is None: # \u4f7f\u7528PyTorch\u5185\u7f6e\u7684\u4f18\u5316\u5668\u548c\u635f\u5931\u51fd\u6570\n                sgd(params, lr, batch_size) # \u66f4\u65b0\u6a21\u578b\u53c2\u6570\n            else:\n                optimizer.step()  # \u201csoftmax\u56de\u5f52\u7684\u7b80\u6d01\u5b9e\u73b0\u201d\u4e00\u8282\u5c06\u7528\u5230\n\n\n            train_l_sum += l.item() # \u5c06\u5f53\u524d\u6279\u6b21loss\u503c\u76f8\u52a0\u5f97\u5230\u4e00\u4e2a\u603b\u7684loss\u503c\n            train_acc_sum += (y_hat.argmax(dim=1) == y).sum().item() # \u8ba1\u7b97\u603b\u51c6\u786e\u7387\n            n += y.shape&#91;0] # y.shape&#91;0]\u662fy\u7684\u884c\u6570\uff0c\u4e5f\u5c31\u662fbatch_size\uff0c\u8ba1\u7b97\u603b\u6837\u672c\u6570\n\n        test_acc = evaluate_accuracy(test_iter, net) # \u8ba1\u7b97\u6d4b\u8bd5\u96c6\u51c6\u786e\u7387\n        print('\u5468\u671f %d, \u635f\u5931 %.4f, \u6570\u636e\u96c6\u51c6\u786e\u7387 %.3f, \u6d4b\u8bd5\u96c6\u51c6\u786e\u7387 %.3f'\n              % (epoch + 1, train_l_sum \/ n, train_acc_sum \/ n, test_acc))\n  \nif __name__ == '__main__':\n    batch_size = 256\n    train_iter, test_iter = load_data_fashion_mnist(batch_size)\n\n\n    num_inputs = 28*28\n    num_outputs = 10\n\n    W = torch.tensor(np.random.normal(0, 0.01, (num_inputs, num_outputs)), dtype=torch.float) \n    # np.random.normal \u751f\u6210\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\uff0c\u5747\u503c\u4e3a0\uff0c\u6807\u51c6\u5dee\u4e3a0.01\uff0c\u5f62\u72b6\u4e3a\uff08num_inputs, num_outputs\uff09\uff0ctorch.tensor \u5c06\u751f\u6210\u7684\u968f\u673a\u6570\u8f6c\u6362\u4e3a\u5f20\u91cf\n    b = torch.zeros(num_outputs, dtype=torch.float) # \u504f\u7f6eb\u521d\u59cb\u5316\u4e3a0\n\n    W.requires_grad_(requires_grad=True) # \u8bbe\u7f6e\u53c2\u6570W\u9700\u8981\u6c42\u68af\u5ea6\n    b.requires_grad_(requires_grad=True) # \u8bbe\u7f6e\u53c2\u6570b\u9700\u8981\u6c42\u68af\u5ea6\n    num_epochs, lr = 5, 0.1\n\n    train_ch3(net, train_iter, test_iter, cross_entropy, num_epochs, batch_size, &#91;W, b], lr)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"\u9884\u6d4b\">\u9884\u6d4b<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"python\">import matplotlib.pyplot as plt\n\ndef show_images_in_new_window(imgs, num_rows, num_cols, titles=None, scale=1.5):\n    figsize = (num_cols * scale, num_rows * scale)\n    fig, axes = plt.subplots(num_rows, num_cols, figsize=figsize)\n    axes = axes.flatten()\n    for i, (ax, img) in enumerate(zip(axes, imgs)):\n        if torch.is_tensor(img):\n            ax.imshow(img.numpy())\n        else:\n            ax.imshow(img)\n        ax.axes.get_xaxis().set_visible(False)\n        ax.axes.get_yaxis().set_visible(False)\n        if titles:\n            ax.set_title(titles&#91;i])\n    plt.show()\n\ndef get_fashion_mnist_labels(labels):  #@save  \n    \"\"\"\u8fd4\u56deFashion-MNIST\u6570\u636e\u96c6\u7684\u6587\u672c\u6807\u7b7e\"\"\"\n    text_labels = &#91;'t-shirt', 'trouser', 'pullover', 'dress', 'coat', 'sandal', 'shirt', 'sneaker', 'bag', 'ankle boot']\n    return &#91;text_labels&#91;int(i)] for i in labels]\n\n\nfor X, y in test_iter:\n    y_hat = net(X)\n    show_images_in_new_window(X&#91;0:9].reshape(9, 28, 28), 3, 3, titles=get_fashion_mnist_labels(y_hat&#91;0:9].argmax(dim=1)))\n    break\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5bfc\u5165\u672c\u8282\u5b9e\u73b0\u6240\u9700\u7684\u5305\u6216\u6a21\u5757 \u83b7\u53d6\u548c\u8bfb\u53d6\u6570\u636e \u5148\u83b7\u53d6Fashion-MNIST\u6570\u636e\u96c6\uff0c\u5e76\u8bbe\u7f6e\u6279\u91cf\u5927\u5c0f\u4e3a256\u3002 \u4f7f\u7528\u5411\u91cf\u8868\u793a\u6bcf\u4e2a\u6837\u672c &#8230;<\/p>","protected":false},"author":1,"featured_media":1635,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46,3],"tags":[45,44,12,22],"class_list":["post-1634","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-46","category-3","tag-45","tag-44","tag-12","tag-22"],"_links":{"self":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/comments?post=1634"}],"version-history":[{"count":1,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1634\/revisions"}],"predecessor-version":[{"id":1716,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/posts\/1634\/revisions\/1716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media\/1635"}],"wp:attachment":[{"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/media?parent=1634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/categories?post=1634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.forillusion.com\/index.php\/wp-json\/wp\/v2\/tags?post=1634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}