{"id":41583,"date":"2025-06-03T23:27:24","date_gmt":"2025-06-03T15:27:24","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/41583.html"},"modified":"2025-06-03T23:27:24","modified_gmt":"2025-06-03T15:27:24","slug":"%e3%80%90python%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0%e3%80%91day-42-grad-cam%e4%b8%8ehook%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/41583.html","title":{"rendered":"\u3010python\u6df1\u5ea6\u5b66\u4e60\u3011Day 42 Grad-CAM\u4e0eHook\u51fd\u6570"},"content":{"rendered":"<p> <span style=\"color:#333333\">\u77e5\u8bc6\u70b9\u56de\u987e<\/span> <\/p>\n<li style=\"text-align:justify\"><span style=\"color:#333333\">\u56de\u8c03\u51fd\u6570<\/span><\/li>\n<li style=\"text-align:justify\"><span style=\"color:#333333\">lambda\u51fd\u6570<\/span><\/li>\n<li style=\"text-align:justify\"><span style=\"color:#333333\">hook\u51fd\u6570\u7684\u6a21\u5757\u94a9\u5b50\u548c\u5f20\u91cf\u94a9\u5b50<\/span><\/li>\n<li style=\"text-align:justify\"><span style=\"color:#333333\">Grad-CAM\u7684\u793a\u4f8b<\/span><\/li>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"background-color:#ffff00\"><span style=\"color:#333333\">\u4f5c\u4e1a&#xff1a;<\/span><\/span><span style=\"color:#333333\">\u7406\u89e3\u4e0b\u4eca\u5929\u7684\u4ee3\u7801\u5373\u53ef<\/span><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p style=\"margin-left:0;margin-right:0;text-align:justify\"><span style=\"color:#333333\">\u4e00\u3001\u56de\u8c03\u51fd\u6570<\/span><\/p>\n<p style=\"margin-left:0;margin-right:0;text-align:justify\">\n<p>\u56de\u8c03\u51fd\u6570\u662f\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u5176\u4ed6\u51fd\u6570\u7684\u51fd\u6570&#xff0c;\u5176\u76ee\u7684\u662f\u5728\u67d0\u4e2a\u7279\u5b9a\u4e8b\u4ef6\u53d1\u751f\u65f6\u88ab\u8c03\u7528\u6267\u884c\u3002\u8fd9\u79cd\u673a\u5236\u5141\u8bb8\u4ee3\u7801\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u6307\u5b9a\u9700\u8981\u6267\u884c\u7684\u903b\u8f91&#xff0c;\u5b9e\u73b0\u4e86\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p>\u56de\u8c03\u51fd\u6570\u7684\u6838\u5fc3\u4ef7\u503c\u5728\u4e8e&#xff1a;<\/p>\n<\/p>\n<p>1. \u89e3\u8026\u903b\u8f91&#xff1a;\u5c06\u901a\u7528\u903b\u8f91\u4e0e\u7279\u5b9a\u5904\u7406\u903b\u8f91\u5206\u79bb&#xff0c;\u4f7f\u4ee3\u7801\u66f4\u6a21\u5757\u5316\u3002<\/p>\n<p>2. \u4e8b\u4ef6\u9a71\u52a8\u7f16\u7a0b&#xff1a;\u5728\u5f02\u6b65\u64cd\u4f5c\u3001\u4e8b\u4ef6\u76d1\u542c&#xff08;\u5982\u70b9\u51fb\u6309\u94ae\u3001\u7f51\u7edc\u8bf7\u6c42\u5b8c\u6210&#xff09;\u7b49\u573a\u666f\u4e2d\u5e7f\u6cdb\u5e94\u7528\u3002<\/p>\n<p>3. \u5ef6\u8fdf\u6267\u884c&#xff1a;\u5141\u8bb8\u5728\u672a\u6765\u67d0\u4e2a\u65f6\u95f4\u70b9\u6267\u884c\u7279\u5b9a\u4ee3\u7801&#xff0c;\u800c\u4e0d\u5fc5\u7acb\u5373\u6267\u884c\u3002<\/p>\n<\/p>\n<p>\u5176\u4e2d\u56de\u8c03\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165&#xff0c;\u6240\u4ee5\u5728\u5b9a\u4e49\u7684\u65f6\u5019\u4e00\u822c\u7528callback\u6765\u547d\u540d&#xff0c;\u5728 PyTorch \u7684 Hook API \u4e2d&#xff0c;\u56de\u8c03\u53c2\u6570\u901a\u5e38\u547d\u540d\u4e3a hook<\/p>\n<\/p>\n<p>\u4e8c\u3001lambda\u51fd\u6570<\/p>\n<p>\u5728hook\u4e2d\u5e38\u5e38\u7528\u5230lambda\u51fd\u6570&#xff0c;\u5b83\u662f\u4e00\u79cd\u533f\u540d\u51fd\u6570&#xff08;\u6ca1\u6709\u6b63\u5f0f\u540d\u79f0\u7684\u51fd\u6570&#xff09;&#xff0c;\u6700\u5927\u7279\u70b9\u662f\u7528\u5b8c\u5373\u5f03&#xff0c;\u65e0\u9700\u63d0\u524d\u547d\u540d\u548c\u5b9a\u4e49\u3002\u5b83\u7684\u8bed\u6cd5\u5f62\u5f0f\u975e\u5e38\u7b80\u7ea6&#xff0c;\u4ec5\u9700\u4e00\u884c\u5373\u53ef\u5b8c\u6210\u5b9a\u4e49&#xff0c;\u683c\u5f0f\u5982\u4e0b&#xff1a;<\/p>\n<p>lambda \u53c2\u6570\u5217\u8868: \u8868\u8fbe\u5f0f<\/p>\n<\/p>\n<p>&#8211; \u53c2\u6570\u5217\u8868&#xff1a;\u53ef\u4ee5\u662f\u5355\u4e2a\u53c2\u6570\u3001\u591a\u4e2a\u53c2\u6570\u6216\u65e0\u53c2\u6570\u3002<\/p>\n<p>&#8211; \u8868\u8fbe\u5f0f&#xff1a;\u51fd\u6570\u7684\u8fd4\u56de\u503c&#xff08;\u65e0\u9700 return \u8bed\u53e5&#xff0c;\u8868\u8fbe\u5f0f\u7ed3\u679c\u76f4\u63a5\u8fd4\u56de&#xff09;\u3002<\/p>\n<\/p>\n<p>\u4e09\u3001hook\u51fd\u6570<\/p>\n<p>Hook \u51fd\u6570\u662f\u4e00\u79cd\u56de\u8c03\u51fd\u6570&#xff0c;\u5b83\u53ef\u4ee5\u5728\u4e0d\u5e72\u6270\u6a21\u578b\u6b63\u5e38\u8ba1\u7b97\u6d41\u7a0b\u7684\u60c5\u51b5\u4e0b&#xff0c;\u63d2\u5165\u5230\u6a21\u578b\u7684\u7279\u5b9a\u4f4d\u7f6e&#xff0c;\u4ee5\u4fbf\u83b7\u53d6\u6216\u4fee\u6539\u4e2d\u95f4\u5c42\u7684\u8f93\u51fa\u6216\u68af\u5ea6\u3002PyTorch \u63d0\u4f9b\u4e86\u4e24\u79cd\u4e3b\u8981\u7684 hook&#xff1a;<\/p>\n<p>1. Module Hooks&#xff1a;\u7528\u4e8e\u76d1\u542c\u6574\u4e2a\u6a21\u5757\u7684\u8f93\u5165\u548c\u8f93\u51fa<\/p>\n<p>2. Tensor Hooks&#xff1a;\u7528\u4e8e\u76d1\u542c\u5f20\u91cf\u7684\u68af\u5ea6<\/p>\n<\/p>\n<p>\u56db\u3001Grad-CAM<\/p>\n<p>Grad-CAM (Gradient-weighted Class Activation Mapping) \u7b97\u6cd5\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u53ef\u89c6\u5316\u6280\u672f&#xff0c;\u7528\u4e8e\u89e3\u91ca\u5377\u79ef\u795e\u7ecf\u7f51\u7edc (CNN) \u7684\u51b3\u7b56\u8fc7\u7a0b\u3002\u5b83\u901a\u8fc7\u8ba1\u7b97\u7279\u5f81\u56fe\u7684\u68af\u5ea6\u6765\u751f\u6210\u7c7b\u6fc0\u6d3b\u6620\u5c04&#xff08;Class Activation Mapping&#xff0c;\u7b80\u79f0 CAM &#xff09;&#xff0c;\u76f4\u89c2\u5730\u663e\u793a\u56fe\u50cf\u4e2d\u54ea\u4e9b\u533a\u57df\u5bf9\u6a21\u578b\u7684\u7279\u5b9a\u9884\u6d4b\u8d21\u732e\u6700\u5927\u3002<\/p>\n<p>Grad-CAM \u7684\u6838\u5fc3\u601d\u60f3\u662f&#xff1a;\u901a\u8fc7\u53cd\u5411\u4f20\u64ad\u5f97\u5230\u7684\u68af\u5ea6\u4fe1\u606f&#xff0c;\u6765\u8861\u91cf\u6bcf\u4e2a\u7279\u5f81\u56fe\u5bf9\u76ee\u6807\u7c7b\u522b\u7684\u91cd\u8981\u6027\u3002<\/p>\n<p>1. \u68af\u5ea6\u4fe1\u606f&#xff1a;\u901a\u8fc7\u8ba1\u7b97\u76ee\u6807\u7c7b\u522b\u5bf9\u7279\u5f81\u56fe\u7684\u68af\u5ea6&#xff0c;\u5f97\u5230\u6bcf\u4e2a\u7279\u5f81\u56fe\u7684\u91cd\u8981\u6027\u6743\u91cd\u3002<\/p>\n<p>2. \u7279\u5f81\u52a0\u6743&#xff1a;\u7528\u8fd9\u4e9b\u6743\u91cd\u5bf9\u7279\u5f81\u56fe\u8fdb\u884c\u52a0\u6743\u6c42\u548c&#xff0c;\u5f97\u5230\u7c7b\u6fc0\u6d3b\u6620\u5c04\u3002<\/p>\n<p>3. \u53ef\u89c6\u5316&#xff1a;\u5c06\u6fc0\u6d3b\u6620\u5c04\u53e0\u52a0\u5230\u539f\u59cb\u56fe\u50cf\u4e0a&#xff0c;\u9ad8\u4eae\u663e\u793a\u5bf9\u9884\u6d4b\u6700\u5173\u952e\u7684\u533a\u57df\u3002<\/p>\n<p> import torch<br \/>\nimport torch.nn as nn<br \/>\nimport torch.nn.functional as F<br \/>\nimport torchvision<br \/>\nimport torchvision.transforms as transforms<br \/>\nimport numpy as np<br \/>\nimport matplotlib.pyplot as plt<br \/>\nfrom PIL import Image<\/p>\n<p># \u8bbe\u7f6e\u968f\u673a\u79cd\u5b50\u786e\u4fdd\u7ed3\u679c\u53ef\u590d\u73b0<br \/>\n# \u5728\u6df1\u5ea6\u5b66\u4e60\u4e2d&#xff0c;\u968f\u673a\u79cd\u5b50\u53ef\u4ee5\u8ba9\u6bcf\u6b21\u8fd0\u884c\u4ee3\u7801\u65f6&#xff0c;\u6a21\u578b\u521d\u59cb\u5316\u53c2\u6570\u3001\u6570\u636e\u6253\u4e71\u7b49\u968f\u673a\u64cd\u4f5c\u4fdd\u6301\u4e00\u81f4&#xff0c;\u65b9\u4fbf\u8c03\u8bd5\u548c\u5bf9\u6bd4\u5b9e\u9a8c\u7ed3\u679c<br \/>\ntorch.manual_seed(42)<br \/>\nnp.random.seed(42)<\/p>\n<p># \u52a0\u8f7dCIFAR-10\u6570\u636e\u96c6<br \/>\n# \u5b9a\u4e49\u6570\u636e\u9884\u5904\u7406\u6b65\u9aa4&#xff0c;\u5148\u5c06\u56fe\u50cf\u8f6c\u6362\u4e3a\u5f20\u91cf&#xff0c;\u518d\u8fdb\u884c\u5f52\u4e00\u5316\u64cd\u4f5c<br \/>\n# \u5f52\u4e00\u5316\u7684\u5747\u503c\u548c\u6807\u51c6\u5dee\u662f(0.5, 0.5, 0.5)&#xff0c;\u8fd9\u91cc\u7684\u5747\u503c\u548c\u6807\u51c6\u5dee\u662f\u5bf9CIFAR-10\u6570\u636e\u96c6\u7684\u7ecf\u9a8c\u503c&#xff0c;\u4f7f\u5f97\u6570\u636e\u5206\u5e03\u66f4\u6709\u5229\u4e8e\u6a21\u578b\u8bad\u7ec3<br \/>\ntransform &#061; transforms.Compose([<br \/>\n    transforms.ToTensor(),<br \/>\n    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))<br \/>\n])<\/p>\n<p># \u52a0\u8f7d\u6d4b\u8bd5\u96c6&#xff0c;\u6307\u5b9a\u6570\u636e\u96c6\u6839\u76ee\u5f55\u4e3a&#039;.\/data&#039;&#xff0c;\u8bbe\u7f6e\u4e3a\u6d4b\u8bd5\u96c6&#xff08;train&#061;False&#xff09;&#xff0c;\u5982\u679c\u6570\u636e\u4e0d\u5b58\u5728\u5219\u4e0b\u8f7d&#xff08;download&#061;True&#xff09;&#xff0c;\u5e76\u5e94\u7528\u4e0a\u8ff0\u5b9a\u4e49\u7684\u9884\u5904\u7406<br \/>\ntestset &#061; torchvision.datasets.CIFAR10(<br \/>\n    root&#061;&#039;.\/data&#039;,<br \/>\n    train&#061;False,<br \/>\n    download&#061;True,<br \/>\n    transform&#061;transform<br \/>\n)<\/p>\n<p># \u5b9a\u4e49\u7c7b\u522b\u540d\u79f0&#xff0c;CIFAR-10\u6570\u636e\u96c6\u5305\u542b\u8fd910\u4e2a\u7c7b\u522b<br \/>\nclasses &#061; (&#039;\u98de\u673a&#039;, &#039;\u6c7d\u8f66&#039;, &#039;\u9e1f&#039;, &#039;\u732b&#039;, &#039;\u9e7f&#039;, &#039;\u72d7&#039;, &#039;\u9752\u86d9&#039;, &#039;\u9a6c&#039;, &#039;\u8239&#039;, &#039;\u5361\u8f66&#039;)<\/p>\n<p># \u5b9a\u4e49\u4e00\u4e2a\u7b80\u5355\u7684CNN\u6a21\u578b<br \/>\nclass SimpleCNN(nn.Module):<br \/>\n    def __init__(self):<br \/>\n        super(SimpleCNN, self).__init__()<br \/>\n        # \u7b2c\u4e00\u4e2a\u5377\u79ef\u5c42&#xff0c;\u8f93\u5165\u901a\u9053\u4e3a3&#xff08;\u5f69\u8272\u56fe\u50cf&#xff09;&#xff0c;\u8f93\u51fa\u901a\u9053\u4e3a32&#xff0c;\u5377\u79ef\u6838\u5927\u5c0f\u4e3a3&#215;3&#xff0c;\u586b\u5145\u4e3a1\u4ee5\u4fdd\u6301\u56fe\u50cf\u5c3a\u5bf8\u4e0d\u53d8<br \/>\n        self.conv1 &#061; nn.Conv2d(3, 32, kernel_size&#061;3, padding&#061;1)<br \/>\n        # \u7b2c\u4e8c\u4e2a\u5377\u79ef\u5c42&#xff0c;\u8f93\u5165\u901a\u9053\u4e3a32&#xff0c;\u8f93\u51fa\u901a\u9053\u4e3a64&#xff0c;\u5377\u79ef\u6838\u5927\u5c0f\u4e3a3&#215;3&#xff0c;\u586b\u5145\u4e3a1<br \/>\n        self.conv2 &#061; nn.Conv2d(32, 64, kernel_size&#061;3, padding&#061;1)<br \/>\n        # \u7b2c\u4e09\u4e2a\u5377\u79ef\u5c42&#xff0c;\u8f93\u5165\u901a\u9053\u4e3a64&#xff0c;\u8f93\u51fa\u901a\u9053\u4e3a128&#xff0c;\u5377\u79ef\u6838\u5927\u5c0f\u4e3a3&#215;3&#xff0c;\u586b\u5145\u4e3a1<br \/>\n        self.conv3 &#061; nn.Conv2d(64, 128, kernel_size&#061;3, padding&#061;1)<br \/>\n        # \u6700\u5927\u6c60\u5316\u5c42&#xff0c;\u6c60\u5316\u6838\u5927\u5c0f\u4e3a2&#215;2&#xff0c;\u6b65\u957f\u4e3a2&#xff0c;\u7528\u4e8e\u4e0b\u91c7\u6837&#xff0c;\u51cf\u5c11\u6570\u636e\u91cf\u5e76\u63d0\u53d6\u4e3b\u8981\u7279\u5f81<br \/>\n        self.pool &#061; nn.MaxPool2d(2, 2)<br \/>\n        # \u7b2c\u4e00\u4e2a\u5168\u8fde\u63a5\u5c42&#xff0c;\u8f93\u5165\u7279\u5f81\u6570\u4e3a128 * 4 * 4&#xff08;\u7ecf\u8fc7\u524d\u9762\u5377\u79ef\u548c\u6c60\u5316\u540e\u7684\u7279\u5f81\u7ef4\u5ea6&#xff09;&#xff0c;\u8f93\u51fa\u4e3a512<br \/>\n        self.fc1 &#061; nn.Linear(128 * 4 * 4, 512)<br \/>\n        # \u7b2c\u4e8c\u4e2a\u5168\u8fde\u63a5\u5c42&#xff0c;\u8f93\u5165\u4e3a512&#xff0c;\u8f93\u51fa\u4e3a10&#xff08;\u5bf9\u5e94CIFAR-10\u768410\u4e2a\u7c7b\u522b&#xff09;<br \/>\n        self.fc2 &#061; nn.Linear(512, 10)<\/p>\n<p>    def forward(self, x):<br \/>\n        # \u7b2c\u4e00\u4e2a\u5377\u79ef\u5c42\u540e\u63a5ReLU\u6fc0\u6d3b\u51fd\u6570\u548c\u6700\u5927\u6c60\u5316\u64cd\u4f5c&#xff0c;\u7ecf\u8fc7\u6c60\u5316\u540e\u56fe\u50cf\u5c3a\u5bf8\u53d8\u4e3a\u539f\u6765\u7684\u4e00\u534a&#xff0c;\u8fd9\u91cc\u8f93\u51fa\u5c3a\u5bf8\u53d8\u4e3a16&#215;16<br \/>\n        x &#061; self.pool(F.relu(self.conv1(x)))<br \/>\n        # \u7b2c\u4e8c\u4e2a\u5377\u79ef\u5c42\u540e\u63a5ReLU\u6fc0\u6d3b\u51fd\u6570\u548c\u6700\u5927\u6c60\u5316\u64cd\u4f5c&#xff0c;\u8f93\u51fa\u5c3a\u5bf8\u53d8\u4e3a8&#215;8<br \/>\n        x &#061; self.pool(F.relu(self.conv2(x)))<br \/>\n        # \u7b2c\u4e09\u4e2a\u5377\u79ef\u5c42\u540e\u63a5ReLU\u6fc0\u6d3b\u51fd\u6570\u548c\u6700\u5927\u6c60\u5316\u64cd\u4f5c&#xff0c;\u8f93\u51fa\u5c3a\u5bf8\u53d8\u4e3a4&#215;4<br \/>\n        x &#061; self.pool(F.relu(self.conv3(x)))<br \/>\n        # \u5c06\u7279\u5f81\u56fe\u5c55\u5e73\u4e3a\u4e00\u7ef4\u5411\u91cf&#xff0c;\u4ee5\u4fbf\u8f93\u5165\u5230\u5168\u8fde\u63a5\u5c42<br \/>\n        x &#061; x.view(-1, 128 * 4 * 4)<br \/>\n        # \u7b2c\u4e00\u4e2a\u5168\u8fde\u63a5\u5c42\u540e\u63a5ReLU\u6fc0\u6d3b\u51fd\u6570<br \/>\n        x &#061; F.relu(self.fc1(x))<br \/>\n        # \u7b2c\u4e8c\u4e2a\u5168\u8fde\u63a5\u5c42\u8f93\u51fa\u5206\u7c7b\u7ed3\u679c<br \/>\n        x &#061; self.fc2(x)<br \/>\n        return x<\/p>\n<p># \u521d\u59cb\u5316\u6a21\u578b<br \/>\nmodel &#061; SimpleCNN()<br \/>\nprint(&#034;\u6a21\u578b\u5df2\u521b\u5efa&#034;)<\/p>\n<p># \u5982\u679c\u6709GPU\u5219\u4f7f\u7528GPU&#xff0c;\u5c06\u6a21\u578b\u8f6c\u79fb\u5230\u5bf9\u5e94\u7684\u8bbe\u5907\u4e0a<br \/>\ndevice &#061; torch.device(&#034;cuda:0&#034; if torch.cuda.is_available() else &#034;cpu&#034;)<br \/>\nmodel &#061; model.to(device)<\/p>\n<p># \u8bad\u7ec3\u6a21\u578b&#xff08;\u7b80\u5316\u7248&#xff0c;\u5b9e\u9645\u5e94\u7528\u4e2d\u5e94\u8be5\u8fdb\u884c\u5b8c\u6574\u8bad\u7ec3&#xff09;<br \/>\ndef train_model(model, epochs&#061;1):<br \/>\n    # \u52a0\u8f7d\u8bad\u7ec3\u96c6&#xff0c;\u6307\u5b9a\u6570\u636e\u96c6\u6839\u76ee\u5f55\u4e3a&#039;.\/data&#039;&#xff0c;\u8bbe\u7f6e\u4e3a\u8bad\u7ec3\u96c6&#xff08;train&#061;True&#xff09;&#xff0c;\u5982\u679c\u6570\u636e\u4e0d\u5b58\u5728\u5219\u4e0b\u8f7d&#xff08;download&#061;True&#xff09;&#xff0c;\u5e76\u5e94\u7528\u524d\u9762\u5b9a\u4e49\u7684\u9884\u5904\u7406<br \/>\n    trainset &#061; torchvision.datasets.CIFAR10(<br \/>\n        root&#061;&#039;.\/data&#039;,<br \/>\n        train&#061;True,<br \/>\n        download&#061;True,<br \/>\n        transform&#061;transform<br \/>\n    )<br \/>\n    # \u521b\u5efa\u6570\u636e\u52a0\u8f7d\u5668&#xff0c;\u8bbe\u7f6e\u6279\u91cf\u5927\u5c0f\u4e3a64&#xff0c;\u6253\u4e71\u6570\u636e\u987a\u5e8f&#xff08;shuffle&#061;True&#xff09;&#xff0c;\u4f7f\u75282\u4e2a\u7ebf\u7a0b\u52a0\u8f7d\u6570\u636e<br \/>\n    trainloader &#061; torch.utils.data.DataLoader(<br \/>\n        trainset,<br \/>\n        batch_size&#061;64,<br \/>\n        shuffle&#061;True,<br \/>\n        num_workers&#061;2<br \/>\n    )<\/p>\n<p>    # \u5b9a\u4e49\u635f\u5931\u51fd\u6570\u4e3a\u4ea4\u53c9\u71b5\u635f\u5931&#xff0c;\u7528\u4e8e\u5206\u7c7b\u4efb\u52a1<br \/>\n    criterion &#061; nn.CrossEntropyLoss()<br \/>\n    # \u5b9a\u4e49\u4f18\u5316\u5668\u4e3aAdam&#xff0c;\u7528\u4e8e\u66f4\u65b0\u6a21\u578b\u53c2\u6570&#xff0c;\u5b66\u4e60\u7387\u8bbe\u7f6e\u4e3a0.001<br \/>\n    optimizer &#061; torch.optim.Adam(model.parameters(), lr&#061;0.001)<\/p>\n<p>    for epoch in range(epochs):<br \/>\n        running_loss &#061; 0.0<br \/>\n        for i, data in enumerate(trainloader, 0):<br \/>\n            # \u4ece\u6570\u636e\u52a0\u8f7d\u5668\u4e2d\u83b7\u53d6\u56fe\u50cf\u548c\u6807\u7b7e<br \/>\n            inputs, labels &#061; data<br \/>\n            # \u5c06\u56fe\u50cf\u548c\u6807\u7b7e\u8f6c\u79fb\u5230\u5bf9\u5e94\u7684\u8bbe\u5907&#xff08;GPU\u6216CPU&#xff09;\u4e0a<br \/>\n            inputs, labels &#061; inputs.to(device), labels.to(device)<\/p>\n<p>            # \u6e05\u7a7a\u68af\u5ea6&#xff0c;\u907f\u514d\u68af\u5ea6\u7d2f\u52a0<br \/>\n            optimizer.zero_grad()<br \/>\n            # \u6a21\u578b\u524d\u5411\u4f20\u64ad\u5f97\u5230\u8f93\u51fa<br \/>\n            outputs &#061; model(inputs)<br \/>\n            # \u8ba1\u7b97\u635f\u5931<br \/>\n            loss &#061; criterion(outputs, labels)<br \/>\n            # \u53cd\u5411\u4f20\u64ad\u8ba1\u7b97\u68af\u5ea6<br \/>\n            loss.backward()<br \/>\n            # \u66f4\u65b0\u6a21\u578b\u53c2\u6570<br \/>\n            optimizer.step()<\/p>\n<p>            running_loss &#043;&#061; loss.item()<br \/>\n            if i % 100 &#061;&#061; 99:<br \/>\n                # \u6bcf100\u4e2a\u6279\u6b21\u6253\u5370\u4e00\u6b21\u5e73\u5747\u635f\u5931<br \/>\n                print(f&#039;[{epoch &#043; 1}, {i &#043; 1}] \u635f\u5931: {running_loss \/ 100:.3f}&#039;)<br \/>\n                running_loss &#061; 0.0<\/p>\n<p>    print(&#034;\u8bad\u7ec3\u5b8c\u6210&#034;)<\/p>\n<p># \u8bad\u7ec3\u6a21\u578b&#xff08;\u53ef\u9009&#xff0c;\u5982\u679c\u6709\u9884\u8bad\u7ec3\u6a21\u578b\u53ef\u4ee5\u52a0\u8f7d&#xff09;<br \/>\n# \u53d6\u6d88\u4e0b\u9762\u8fd9\u884c\u7684\u6ce8\u91ca\u6765\u8bad\u7ec3\u6a21\u578b<br \/>\n# train_model(model, epochs&#061;1)<\/p>\n<p># \u6216\u8005\u5c1d\u8bd5\u52a0\u8f7d\u9884\u8bad\u7ec3\u6a21\u578b&#xff08;\u5982\u679c\u5b58\u5728&#xff09;<br \/>\ntry:<br \/>\n    # \u5c1d\u8bd5\u52a0\u8f7d\u540d\u4e3a&#039;cifar10_cnn.pth&#039;\u7684\u6a21\u578b\u53c2\u6570<br \/>\n    model.load_state_dict(torch.load(&#039;cifar10_cnn.pth&#039;))<br \/>\n    print(&#034;\u5df2\u52a0\u8f7d\u9884\u8bad\u7ec3\u6a21\u578b&#034;)<br \/>\nexcept:<br \/>\n    print(&#034;\u65e0\u6cd5\u52a0\u8f7d\u9884\u8bad\u7ec3\u6a21\u578b&#xff0c;\u4f7f\u7528\u672a\u8bad\u7ec3\u6a21\u578b\u6216\u8bad\u7ec3\u65b0\u6a21\u578b&#034;)<br \/>\n    # \u5982\u679c\u6ca1\u6709\u9884\u8bad\u7ec3\u6a21\u578b&#xff0c;\u53ef\u4ee5\u5728\u8fd9\u91cc\u8c03\u7528train_model\u51fd\u6570<br \/>\n    train_model(model, epochs&#061;1)<br \/>\n    # \u4fdd\u5b58\u8bad\u7ec3\u540e\u7684\u6a21\u578b\u53c2\u6570<br \/>\n    torch.save(model.state_dict(), &#039;cifar10_cnn.pth&#039;)<\/p>\n<p># \u8bbe\u7f6e\u6a21\u578b\u4e3a\u8bc4\u4f30\u6a21\u5f0f&#xff0c;\u6b64\u65f6\u6a21\u578b\u4e2d\u7684\u4e00\u4e9b\u64cd\u4f5c&#xff08;\u5982dropout\u3001batchnorm\u7b49&#xff09;\u4f1a\u5207\u6362\u5230\u8bc4\u4f30\u72b6\u6001<br \/>\nmodel.eval()<\/p>\n<p># Grad-CAM\u5b9e\u73b0<br \/>\nclass GradCAM:<br \/>\n    def __init__(self, model, target_layer):<br \/>\n        self.model &#061; model<br \/>\n        self.target_layer &#061; target_layer<br \/>\n        self.gradients &#061; None<br \/>\n        self.activations &#061; None<\/p>\n<p>        # \u6ce8\u518c\u94a9\u5b50&#xff0c;\u7528\u4e8e\u83b7\u53d6\u76ee\u6807\u5c42\u7684\u524d\u5411\u4f20\u64ad\u8f93\u51fa\u548c\u53cd\u5411\u4f20\u64ad\u68af\u5ea6<br \/>\n        self.register_hooks()<\/p>\n<p>    def register_hooks(self):<br \/>\n        # \u524d\u5411\u94a9\u5b50\u51fd\u6570&#xff0c;\u5728\u76ee\u6807\u5c42\u524d\u5411\u4f20\u64ad\u540e\u88ab\u8c03\u7528&#xff0c;\u4fdd\u5b58\u76ee\u6807\u5c42\u7684\u8f93\u51fa&#xff08;\u6fc0\u6d3b\u503c&#xff09;<br \/>\n        def forward_hook(module, input, output):<br \/>\n            self.activations &#061; output.detach()<\/p>\n<p>        # \u53cd\u5411\u94a9\u5b50\u51fd\u6570&#xff0c;\u5728\u76ee\u6807\u5c42\u53cd\u5411\u4f20\u64ad\u540e\u88ab\u8c03\u7528&#xff0c;\u4fdd\u5b58\u76ee\u6807\u5c42\u7684\u68af\u5ea6<br \/>\n        def backward_hook(module, grad_input, grad_output):<br \/>\n            self.gradients &#061; grad_output[0].detach()<\/p>\n<p>        # \u5728\u76ee\u6807\u5c42\u6ce8\u518c\u524d\u5411\u94a9\u5b50\u548c\u53cd\u5411\u94a9\u5b50<br \/>\n        self.target_layer.register_forward_hook(forward_hook)<br \/>\n        self.target_layer.register_backward_hook(backward_hook)<\/p>\n<p>    def generate_cam(self, input_image, target_class&#061;None):<br \/>\n        # \u524d\u5411\u4f20\u64ad&#xff0c;\u5f97\u5230\u6a21\u578b\u8f93\u51fa<br \/>\n        model_output &#061; self.model(input_image)<\/p>\n<p>        if target_class is None:<br \/>\n            # \u5982\u679c\u672a\u6307\u5b9a\u76ee\u6807\u7c7b\u522b&#xff0c;\u5219\u53d6\u6a21\u578b\u9884\u6d4b\u6982\u7387\u6700\u5927\u7684\u7c7b\u522b\u4f5c\u4e3a\u76ee\u6807\u7c7b\u522b<br \/>\n            target_class &#061; torch.argmax(model_output, dim&#061;1).item()<\/p>\n<p>        # \u6e05\u9664\u6a21\u578b\u68af\u5ea6&#xff0c;\u907f\u514d\u4e4b\u524d\u7684\u68af\u5ea6\u5f71\u54cd<br \/>\n        self.model.zero_grad()<\/p>\n<p>        # \u53cd\u5411\u4f20\u64ad&#xff0c;\u6784\u9020one-hot\u5411\u91cf&#xff0c;\u4f7f\u5f97\u76ee\u6807\u7c7b\u522b\u5bf9\u5e94\u7684\u68af\u5ea6\u4e3a1&#xff0c;\u5176\u4f59\u4e3a0&#xff0c;\u7136\u540e\u8fdb\u884c\u53cd\u5411\u4f20\u64ad\u8ba1\u7b97\u68af\u5ea6<br \/>\n        one_hot &#061; torch.zeros_like(model_output)<br \/>\n        one_hot[0, target_class] &#061; 1<br \/>\n        model_output.backward(gradient&#061;one_hot)<\/p>\n<p>        # \u83b7\u53d6\u4e4b\u524d\u4fdd\u5b58\u7684\u76ee\u6807\u5c42\u7684\u68af\u5ea6\u548c\u6fc0\u6d3b\u503c<br \/>\n        gradients &#061; self.gradients<br \/>\n        activations &#061; self.activations<\/p>\n<p>        # \u5bf9\u68af\u5ea6\u8fdb\u884c\u5168\u5c40\u5e73\u5747\u6c60\u5316&#xff0c;\u5f97\u5230\u6bcf\u4e2a\u901a\u9053\u7684\u6743\u91cd&#xff0c;\u7528\u4e8e\u8861\u91cf\u6bcf\u4e2a\u901a\u9053\u7684\u91cd\u8981\u6027<br \/>\n        weights &#061; torch.mean(gradients, dim&#061;(2, 3), keepdim&#061;True)<\/p>\n<p>        # \u52a0\u6743\u6fc0\u6d3b\u6620\u5c04&#xff0c;\u5c06\u6743\u91cd\u4e0e\u6fc0\u6d3b\u503c\u76f8\u4e58\u5e76\u6c42\u548c&#xff0c;\u5f97\u5230\u7c7b\u6fc0\u6d3b\u6620\u5c04\u7684\u521d\u6b65\u7ed3\u679c<br \/>\n        cam &#061; torch.sum(weights * activations, dim&#061;1, keepdim&#061;True)<\/p>\n<p>        # ReLU\u6fc0\u6d3b&#xff0c;\u53ea\u4fdd\u7559\u5bf9\u76ee\u6807\u7c7b\u522b\u6709\u6b63\u8d21\u732e\u7684\u533a\u57df&#xff0c;\u53bb\u9664\u8d1f\u8d21\u732e\u7684\u5f71\u54cd<br \/>\n        cam &#061; F.relu(cam)<\/p>\n<p>        # \u8c03\u6574\u5927\u5c0f\u5e76\u5f52\u4e00\u5316&#xff0c;\u5c06\u7c7b\u6fc0\u6d3b\u6620\u5c04\u8c03\u6574\u4e3a\u4e0e\u8f93\u5165\u56fe\u50cf\u76f8\u540c\u7684\u5c3a\u5bf8&#xff08;32&#215;32&#xff09;&#xff0c;\u5e76\u5f52\u4e00\u5316\u5230[0, 1]\u8303\u56f4<br \/>\n        cam &#061; F.interpolate(cam, size&#061;(32, 32), mode&#061;&#039;bilinear&#039;, align_corners&#061;False)<br \/>\n        cam &#061; cam &#8211; cam.min()<br \/>\n        cam &#061; cam \/ cam.max() if cam.max() &gt; 0 else cam<\/p>\n<p>        return cam.cpu().squeeze().numpy(), target_class<br \/>\n import warnings<br \/>\nwarnings.filterwarnings(&#034;ignore&#034;)<br \/>\nimport matplotlib.pyplot as plt<br \/>\n# \u8bbe\u7f6e\u4e2d\u6587\u5b57\u4f53\u652f\u6301<br \/>\nplt.rcParams[&#034;font.family&#034;] &#061; [&#034;SimHei&#034;]<br \/>\nplt.rcParams[&#039;axes.unicode_minus&#039;] &#061; False  # \u89e3\u51b3\u8d1f\u53f7\u663e\u793a\u95ee\u9898<br \/>\n# \u9009\u62e9\u4e00\u4e2a\u968f\u673a\u56fe\u50cf<br \/>\n# idx &#061; np.random.randint(len(testset))<br \/>\nidx &#061; 102  # \u9009\u62e9\u6d4b\u8bd5\u96c6\u4e2d\u7684\u7b2c101\u5f20\u56fe\u7247 (\u7d22\u5f15\u4ece0\u5f00\u59cb)<br \/>\nimage, label &#061; testset[idx]<br \/>\nprint(f&#034;\u9009\u62e9\u7684\u56fe\u50cf\u7c7b\u522b: {classes[label]}&#034;)<\/p>\n<p># \u8f6c\u6362\u56fe\u50cf\u4ee5\u4fbf\u53ef\u89c6\u5316<br \/>\ndef tensor_to_np(tensor):<br \/>\n    img &#061; tensor.cpu().numpy().transpose(1, 2, 0)<br \/>\n    mean &#061; np.array([0.5, 0.5, 0.5])<br \/>\n    std &#061; np.array([0.5, 0.5, 0.5])<br \/>\n    img &#061; std * img &#043; mean<br \/>\n    img &#061; np.clip(img, 0, 1)<br \/>\n    return img<\/p>\n<p># \u6dfb\u52a0\u6279\u6b21\u7ef4\u5ea6\u5e76\u79fb\u52a8\u5230\u8bbe\u5907<br \/>\ninput_tensor &#061; image.unsqueeze(0).to(device)<\/p>\n<p># \u521d\u59cb\u5316Grad-CAM&#xff08;\u9009\u62e9\u6700\u540e\u4e00\u4e2a\u5377\u79ef\u5c42&#xff09;<br \/>\ngrad_cam &#061; GradCAM(model, model.conv3)<\/p>\n<p># \u751f\u6210\u70ed\u529b\u56fe<br \/>\nheatmap, pred_class &#061; grad_cam.generate_cam(input_tensor)<\/p>\n<p># \u53ef\u89c6\u5316<br \/>\nplt.figure(figsize&#061;(12, 4))<\/p>\n<p># \u539f\u59cb\u56fe\u50cf<br \/>\nplt.subplot(1, 3, 1)<br \/>\nplt.imshow(tensor_to_np(image))<br \/>\nplt.title(f&#034;\u539f\u59cb\u56fe\u50cf: {classes[label]}&#034;)<br \/>\nplt.axis(&#039;off&#039;)<\/p>\n<p># \u70ed\u529b\u56fe<br \/>\nplt.subplot(1, 3, 2)<br \/>\nplt.imshow(heatmap, cmap&#061;&#039;jet&#039;)<br \/>\nplt.title(f&#034;Grad-CAM\u70ed\u529b\u56fe: {classes[pred_class]}&#034;)<br \/>\nplt.axis(&#039;off&#039;)<\/p>\n<p># \u53e0\u52a0\u7684\u56fe\u50cf<br \/>\nplt.subplot(1, 3, 3)<br \/>\nimg &#061; tensor_to_np(image)<br \/>\nheatmap_resized &#061; np.uint8(255 * heatmap)<br \/>\nheatmap_colored &#061; plt.cm.jet(heatmap_resized)[:, :, :3]<br \/>\nsuperimposed_img &#061; heatmap_colored * 0.4 &#043; img * 0.6<br \/>\nplt.imshow(superimposed_img)<br \/>\nplt.title(&#034;\u53e0\u52a0\u70ed\u529b\u56fe&#034;)<br \/>\nplt.axis(&#039;off&#039;)<\/p>\n<p>plt.tight_layout()<br \/>\nplt.savefig(&#039;grad_cam_result.png&#039;)<br \/>\nplt.show()<\/p>\n<p># print(&#034;Grad-CAM\u53ef\u89c6\u5316\u5b8c\u6210\u3002\u5df2\u4fdd\u5b58\u4e3agrad_cam_result.png&#034;) <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb302\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u672c\u6587\u6458\u8981\u4ecb\u7ecd\u4e86\u6df1\u5ea6\u5b66\u4e60\u4e2d\u7684\u56de\u8c03\u673a\u5236\u4e0e\u53ef\u89c6\u5316\u6280\u672f\u3002\u4e3b\u8981\u5185\u5bb9\u5305\u62ec\uff1a1)\u56de\u8c03\u51fd\u6570\u7684\u4f5c\u7528\u53ca\u7279\u70b9\uff0c\u5f3a\u8c03\u5176\u89e3\u8026\u903b\u8f91\u548c\u4e8b\u4ef6\u9a71\u52a8\u7279\u6027\uff1b2)Lambda\u51fd\u6570\u7684\u533f\u540d\u7279\u6027\u53ca\u5176\u5728Hook\u4e2d\u7684\u8fd0\u7528\uff1b3)PyTorch\u7684\u4e24\u79cdHook\u673a\u5236\uff0c\u7528\u4e8e\u83b7\u53d6\u6a21\u578b\u4e2d\u95f4\u5c42\u4fe1\u606f\uff1b4)Grad-CAM\u7b97\u6cd5\u539f\u7406\uff0c\u901a\u8fc7\u68af\u5ea6\u52a0\u6743\u5b9e\u73b0CNN\u51b3\u7b56\u8fc7\u7a0b\u53ef\u89c6\u5316\u3002\u8fd9\u4e9b\u6280\u672f\u4e3a\u6a21\u578b\u8c03\u8bd5\u548c\u53ef\u89e3\u91ca\u6027\u63d0\u4f9b\u4e86\u91cd\u8981\u5de5\u5177\uff0c\u5176\u4e2d\u56de\u8c03\u673a\u5236\u5b9e\u73b0\u4e86\u7075\u6d3b\u63a7\u5236\uff0cGrad-CAM\u5219\u76f4\u89c2\u5448\u73b0\u5173\u952e\u7279\u5f81\u533a\u57df\u3002\u4f5c\u4e1a\u8981\u6c42\u7406\u89e3\u76f8\u5173\u4ee3\u7801\u5b9e\u73b0\u3002<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3824,81,190,86],"topic":[],"class_list":{"0":"post-41583","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-server","7":"tag-python","9":"tag-190","10":"tag-86"},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010python\u6df1\u5ea6\u5b66\u4e60\u3011Day 42 Grad-CAM\u4e0eHook\u51fd\u6570 - \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\/41583.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010python\u6df1\u5ea6\u5b66\u4e60\u3011Day 42 Grad-CAM\u4e0eHook\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb302\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u672c\u6587\u6458\u8981\u4ecb\u7ecd\u4e86\u6df1\u5ea6\u5b66\u4e60\u4e2d\u7684\u56de\u8c03\u673a\u5236\u4e0e\u53ef\u89c6\u5316\u6280\u672f\u3002\u4e3b\u8981\u5185\u5bb9\u5305\u62ec\uff1a1)\u56de\u8c03\u51fd\u6570\u7684\u4f5c\u7528\u53ca\u7279\u70b9\uff0c\u5f3a\u8c03\u5176\u89e3\u8026\u903b\u8f91\u548c\u4e8b\u4ef6\u9a71\u52a8\u7279\u6027\uff1b2)Lambda\u51fd\u6570\u7684\u533f\u540d\u7279\u6027\u53ca\u5176\u5728Hook\u4e2d\u7684\u8fd0\u7528\uff1b3)PyTorch\u7684\u4e24\u79cdHook\u673a\u5236\uff0c\u7528\u4e8e\u83b7\u53d6\u6a21\u578b\u4e2d\u95f4\u5c42\u4fe1\u606f\uff1b4)Grad-CAM\u7b97\u6cd5\u539f\u7406\uff0c\u901a\u8fc7\u68af\u5ea6\u52a0\u6743\u5b9e\u73b0CNN\u51b3\u7b56\u8fc7\u7a0b\u53ef\u89c6\u5316\u3002\u8fd9\u4e9b\u6280\u672f\u4e3a\u6a21\u578b\u8c03\u8bd5\u548c\u53ef\u89e3\u91ca\u6027\u63d0\u4f9b\u4e86\u91cd\u8981\u5de5\u5177\uff0c\u5176\u4e2d\u56de\u8c03\u673a\u5236\u5b9e\u73b0\u4e86\u7075\u6d3b\u63a7\u5236\uff0cGrad-CAM\u5219\u76f4\u89c2\u5448\u73b0\u5173\u952e\u7279\u5f81\u533a\u57df\u3002\u4f5c\u4e1a\u8981\u6c42\u7406\u89e3\u76f8\u5173\u4ee3\u7801\u5b9e\u73b0\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/41583.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-06-03T15:27:24+00:00\" \/>\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=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/41583.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/41583.html\",\"name\":\"\u3010python\u6df1\u5ea6\u5b66\u4e60\u3011Day 42 Grad-CAM\u4e0eHook\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-06-03T15:27:24+00:00\",\"dateModified\":\"2025-06-03T15:27:24+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/41583.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/41583.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/41583.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010python\u6df1\u5ea6\u5b66\u4e60\u3011Day 42 Grad-CAM\u4e0eHook\u51fd\u6570\"}]},{\"@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":"\u3010python\u6df1\u5ea6\u5b66\u4e60\u3011Day 42 Grad-CAM\u4e0eHook\u51fd\u6570 - \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\/41583.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010python\u6df1\u5ea6\u5b66\u4e60\u3011Day 42 Grad-CAM\u4e0eHook\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb302\u6b21\uff0c\u70b9\u8d5e3\u6b21\uff0c\u6536\u85cf2\u6b21\u3002\u672c\u6587\u6458\u8981\u4ecb\u7ecd\u4e86\u6df1\u5ea6\u5b66\u4e60\u4e2d\u7684\u56de\u8c03\u673a\u5236\u4e0e\u53ef\u89c6\u5316\u6280\u672f\u3002\u4e3b\u8981\u5185\u5bb9\u5305\u62ec\uff1a1)\u56de\u8c03\u51fd\u6570\u7684\u4f5c\u7528\u53ca\u7279\u70b9\uff0c\u5f3a\u8c03\u5176\u89e3\u8026\u903b\u8f91\u548c\u4e8b\u4ef6\u9a71\u52a8\u7279\u6027\uff1b2)Lambda\u51fd\u6570\u7684\u533f\u540d\u7279\u6027\u53ca\u5176\u5728Hook\u4e2d\u7684\u8fd0\u7528\uff1b3)PyTorch\u7684\u4e24\u79cdHook\u673a\u5236\uff0c\u7528\u4e8e\u83b7\u53d6\u6a21\u578b\u4e2d\u95f4\u5c42\u4fe1\u606f\uff1b4)Grad-CAM\u7b97\u6cd5\u539f\u7406\uff0c\u901a\u8fc7\u68af\u5ea6\u52a0\u6743\u5b9e\u73b0CNN\u51b3\u7b56\u8fc7\u7a0b\u53ef\u89c6\u5316\u3002\u8fd9\u4e9b\u6280\u672f\u4e3a\u6a21\u578b\u8c03\u8bd5\u548c\u53ef\u89e3\u91ca\u6027\u63d0\u4f9b\u4e86\u91cd\u8981\u5de5\u5177\uff0c\u5176\u4e2d\u56de\u8c03\u673a\u5236\u5b9e\u73b0\u4e86\u7075\u6d3b\u63a7\u5236\uff0cGrad-CAM\u5219\u76f4\u89c2\u5448\u73b0\u5173\u952e\u7279\u5f81\u533a\u57df\u3002\u4f5c\u4e1a\u8981\u6c42\u7406\u89e3\u76f8\u5173\u4ee3\u7801\u5b9e\u73b0\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/41583.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-06-03T15:27:24+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/41583.html","url":"https:\/\/www.wsisp.com\/helps\/41583.html","name":"\u3010python\u6df1\u5ea6\u5b66\u4e60\u3011Day 42 Grad-CAM\u4e0eHook\u51fd\u6570 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-06-03T15:27:24+00:00","dateModified":"2025-06-03T15:27:24+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/41583.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/41583.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/41583.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010python\u6df1\u5ea6\u5b66\u4e60\u3011Day 42 Grad-CAM\u4e0eHook\u51fd\u6570"}]},{"@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\/41583","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=41583"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/41583\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=41583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=41583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=41583"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=41583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}