{"id":40830,"date":"2025-05-30T11:16:13","date_gmt":"2025-05-30T03:16:13","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/40830.html"},"modified":"2025-05-30T11:16:13","modified_gmt":"2025-05-30T03:16:13","slug":"python%e6%89%93%e5%8d%a1day38","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/40830.html","title":{"rendered":"python\u6253\u5361day38"},"content":{"rendered":"<h2 style=\"margin-left:0pt;margin-right:0pt;text-align:justify\"><span style=\"color:#333333\">Dataset\u548cDataloader\u7c7b<\/span><\/h2>\n<li style=\"text-align:justify\"><span style=\"color:#333333\">Dataset\u7c7b\u7684__getitem__\u548c__len__\u65b9\u6cd5&#xff08;\u672c\u8d28\u662fpython\u7684\u7279\u6b8a\u65b9\u6cd5&#xff09;<\/span><\/li>\n<li style=\"text-align:justify\"><span style=\"color:#333333\">Dataloader\u7c7b<\/span><\/li>\n<li style=\"text-align:justify\"><span style=\"color:#333333\">minist\u624b\u5199\u6570\u636e\u96c6\u7684\u4e86\u89e3<\/span><\/li>\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\">\u4e86\u89e3\u4e0bcifar\u6570\u636e\u96c6&#xff0c;\u5c1d\u8bd5\u83b7\u53d6\u5176\u4e2d\u4e00\u5f20\u56fe\u7247<\/span><\/p>\n<p>     import torch<br \/>\nimport torchvision<br \/>\nfrom torch.utils.data import DataLoader , Dataset # DataLoader \u662f PyTorch \u4e2d\u7528\u4e8e\u52a0\u8f7d\u6570\u636e\u7684\u5de5\u5177<br \/>\nimport torchvision.transforms as transforms<br \/>\nimport matplotlib.pyplot as plt<br \/>\nimport numpy as np<\/p>\n<p># \u8bbe\u7f6e\u968f\u673a\u79cd\u5b50\u4ee5\u786e\u4fdd\u7ed3\u679c\u53ef\u91cd\u590d<br \/>\ntorch.manual_seed(42)<\/p>\n<p>transform &#061; transforms.Compose([<br \/>\n    transforms.ToTensor(),<br \/>\n    transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2470, 0.2435, 0.2616))  # CIFAR\u7684\u6807\u51c6\u5316\u53c2\u6570<br \/>\n])<br \/>\ntrain_dataset &#061; torchvision.datasets.CIFAR10(<br \/>\n    root&#061;&#039;.\/dataCIFAR&#039;,  # \u6570\u636e\u5b58\u653e\u7684\u8def\u5f84<br \/>\n    train&#061;True,     # \u4f7f\u7528\u8bad\u7ec3\u96c6<br \/>\n    download&#061;True,  # \u5982\u679c\u6ca1\u6709\u6570\u636e&#xff0c;\u5c31\u4e0b\u8f7d<br \/>\n    transform&#061;transform<br \/>\n)<\/p>\n<p># \u5b9a\u4e49\u7c7b\u522b<br \/>\nclasses &#061; (&#039;plane&#039;, &#039;car&#039;, &#039;bird&#039;, &#039;cat&#039;, &#039;deer&#039;,<br \/>\n           &#039;dog&#039;, &#039;frog&#039;, &#039;horse&#039;, &#039;ship&#039;, &#039;truck&#039;)<\/p>\n<p># \u968f\u673a\u9009\u62e9\u4e00\u5f20\u56fe\u7247<br \/>\nidx &#061; torch.randint(0, len(train_dataset), size&#061;(1,))<br \/>\nimg, label &#061; train_dataset[idx]<\/p>\n<p># \u53cd\u6807\u51c6\u5316\u51fd\u6570<br \/>\ndef denormalize(x):<br \/>\n    mean &#061; torch.tensor([0.4914, 0.4822, 0.4465])<br \/>\n    std &#061; torch.tensor([0.2470, 0.2435, 0.2616])<br \/>\n    # CIFAR-10\u662f\u5f69\u8272\u56fe\u50cf&#xff0c;\u9700\u8981\u5bf9\u6240\u6709\u901a\u9053\u8fdb\u884c\u53cd\u6807\u51c6\u5316<br \/>\n    return x * std[:, None, None] &#043; mean[:, None, None]<\/p>\n<p># \u663e\u793a\u56fe\u7247<br \/>\nplt.figure()<br \/>\nplt.imshow(denormalize(img).permute(1, 2, 0))  # \u8c03\u6574\u901a\u9053\u987a\u5e8f\u4ee5\u6b63\u786e\u663e\u793a\u5f69\u8272\u56fe\u50cf<br \/>\nplt.title(f&#039;Label: {classes[label]}&#039;)<br \/>\nplt.axis(&#039;off&#039;)<br \/>\nplt.show()<\/p>\n<p># 3. \u521b\u5efa\u6570\u636e\u52a0\u8f7d\u5668<br \/>\ntrain_loader &#061; DataLoader(<br \/>\n    train_dataset,<br \/>\n    batch_size&#061;64, # \u6bcf\u4e2a\u6279\u6b2164\u5f20\u56fe\u7247,\u4e00\u822c\u662f2\u7684\u5e42\u6b21\u65b9&#xff0c;\u8fd9\u4e0eGPU\u7684\u8ba1\u7b97\u6548\u7387\u6709\u5173<br \/>\n    shuffle&#061;True # \u968f\u673a\u6253\u4e71\u6570\u636e<br \/>\n)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"411\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530031612-683922fc14e69.png\" width=\"389\" \/><\/p>\n<p>&#064;\u6d59\u5927\u758f\u9526\u884c<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb423\u6b21\uff0c\u70b9\u8d5e7\u6b21\uff0c\u6536\u85cf4\u6b21\u3002Dataset\u7c7b\u7684__getitem__\u548c__len__\u65b9\u6cd5\uff08\u672c\u8d28\u662fpython\u7684\u7279\u6b8a\u65b9\u6cd5\uff09\u4e86\u89e3\u4e0bcifar\u6570\u636e\u96c6\uff0c\u5c1d\u8bd5\u83b7\u53d6\u5176\u4e2d\u4e00\u5f20\u56fe\u7247\u3002Dataset\u548cDataloader\u7c7b\u3002minist\u624b\u5199\u6570\u636e\u96c6\u7684\u4e86\u89e3\u3002Dataloader\u7c7b\u3002<\/p>\n","protected":false},"author":2,"featured_media":40829,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[81],"topic":[],"class_list":["post-40830","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>python\u6253\u5361day38 - \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\/40830.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"python\u6253\u5361day38 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb423\u6b21\uff0c\u70b9\u8d5e7\u6b21\uff0c\u6536\u85cf4\u6b21\u3002Dataset\u7c7b\u7684__getitem__\u548c__len__\u65b9\u6cd5\uff08\u672c\u8d28\u662fpython\u7684\u7279\u6b8a\u65b9\u6cd5\uff09\u4e86\u89e3\u4e0bcifar\u6570\u636e\u96c6\uff0c\u5c1d\u8bd5\u83b7\u53d6\u5176\u4e2d\u4e00\u5f20\u56fe\u7247\u3002Dataset\u548cDataloader\u7c7b\u3002minist\u624b\u5199\u6570\u636e\u96c6\u7684\u4e86\u89e3\u3002Dataloader\u7c7b\u3002\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/40830.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-30T03:16:13+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530031612-683922fc14e69.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=\"1 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/40830.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/40830.html\",\"name\":\"python\u6253\u5361day38 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-30T03:16:13+00:00\",\"dateModified\":\"2025-05-30T03:16:13+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/40830.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/40830.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/40830.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python\u6253\u5361day38\"}]},{\"@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":"python\u6253\u5361day38 - \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\/40830.html","og_locale":"zh_CN","og_type":"article","og_title":"python\u6253\u5361day38 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb423\u6b21\uff0c\u70b9\u8d5e7\u6b21\uff0c\u6536\u85cf4\u6b21\u3002Dataset\u7c7b\u7684__getitem__\u548c__len__\u65b9\u6cd5\uff08\u672c\u8d28\u662fpython\u7684\u7279\u6b8a\u65b9\u6cd5\uff09\u4e86\u89e3\u4e0bcifar\u6570\u636e\u96c6\uff0c\u5c1d\u8bd5\u83b7\u53d6\u5176\u4e2d\u4e00\u5f20\u56fe\u7247\u3002Dataset\u548cDataloader\u7c7b\u3002minist\u624b\u5199\u6570\u636e\u96c6\u7684\u4e86\u89e3\u3002Dataloader\u7c7b\u3002","og_url":"https:\/\/www.wsisp.com\/helps\/40830.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-30T03:16:13+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250530031612-683922fc14e69.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"1 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/40830.html","url":"https:\/\/www.wsisp.com\/helps\/40830.html","name":"python\u6253\u5361day38 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-30T03:16:13+00:00","dateModified":"2025-05-30T03:16:13+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/40830.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/40830.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/40830.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"python\u6253\u5361day38"}]},{"@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\/40830","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=40830"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/40830\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/40829"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=40830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=40830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=40830"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=40830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}