{"id":186,"date":"2025-06-30T11:02:08","date_gmt":"2025-06-30T03:02:08","guid":{"rendered":"https:\/\/mianka.xyz\/?p=186"},"modified":"2025-06-30T11:18:01","modified_gmt":"2025-06-30T03:18:01","slug":"pytorchdataset","status":"publish","type":"post","link":"https:\/\/www.mianka.xyz\/?p=186","title":{"rendered":"PyTorch\u81ea\u5b9a\u4e49Dataset\u5168\u89e3\u6790\uff1a\u4ece\u7406\u8bba\u5230\u5b9e\u6218\u7684\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e00\u3001Dataset\u7684\u6838\u5fc3\u673a\u5236<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">PyTorch\u7684\u6570\u636e\u52a0\u8f7d\u4f53\u7cfb\u57fa\u4e8e\u4e24\u5927\u6838\u5fc3\u7ec4\u4ef6\uff1a<\/p>\n<ol style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">Dataset<\/span>\uff1a\u5b9a\u4e49\u6570\u636e\u96c6\u7684\u62bd\u8c61\u63a5\u53e3\uff0c\u8d1f\u8d23\u7d22\u5f15\u5230\u6837\u672c\u7684\u6620\u5c04\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">DataLoader<\/span>\uff1a\u5c01\u88c5Dataset\uff0c\u63d0\u4f9b\u6279\u91cf\u52a0\u8f7d\u3001\u591a\u7ebf\u7a0b\u52a0\u901f\u7b49\u529f\u80fd\u3002<\/p>\n<\/li>\n<\/ol>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">1.1 \u6570\u636e\u4f20\u9012\u6d41\u7a0b<\/h3>\n<pre style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin-top: 8px; margin-bottom: 8px; scrollbar-width: none; font-size: 15px; overflow: auto; position: relative; color: rgb(6, 7, 31); background-color: rgb(253, 253, 254);\" class=\"prism-highlight prism-language-python\">#&nbsp;\u5178\u578b\u6d41\u7a0b\u793a\u4f8b\ndataset&nbsp;=&nbsp;CustomDataset(...)&nbsp;&nbsp;#&nbsp;\u521b\u5efa\u81ea\u5b9a\u4e49\u6570\u636e\u96c6\ndataloader&nbsp;=&nbsp;DataLoader(dataset,&nbsp;batch_size=32,&nbsp;shuffle=True)&nbsp;&nbsp;#&nbsp;\u5305\u88c5\u4e3aDataLoader\nfor&nbsp;batch&nbsp;in&nbsp;dataloader:&nbsp;&nbsp;#&nbsp;\u8fed\u4ee3\u83b7\u53d6\u6279\u91cf\u6570\u636e\n&nbsp;&nbsp;&nbsp;&nbsp;inputs,&nbsp;labels&nbsp;=&nbsp;batch<\/pre>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">1.2 Map\u5f0f vs Iterable\u5f0f\u6570\u636e\u96c6<\/h3>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">Map\u5f0f\u6570\u636e\u96c6<\/span>\uff08\u5e38\u7528\uff09\uff1a\u901a\u8fc7<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">__getitem__<\/code>\u5b9e\u73b0\u7d22\u5f15\u8bbf\u95ee\uff0c\u652f\u6301\u968f\u673a\u6253\u4e71\uff08shuffle\uff09\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">Iterable\u5f0f\u6570\u636e\u96c6<\/span>\uff1a\u9002\u7528\u4e8e\u6d41\u5f0f\u6570\u636e\uff08\u5982\u5b9e\u65f6\u4f20\u611f\u5668\u6570\u636e\uff09\uff0c\u6309\u987a\u5e8f\u8fed\u4ee3\u3002<\/p>\n<\/li>\n<\/ul>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e8c\u3001\u81ea\u5b9a\u4e49Dataset\u7684\u5b9e\u73b0\u8303\u5f0f<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u7ee7\u627f<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">torch.utils.data.Dataset<\/code>\u9700\u5b9e\u73b0\u4e09\u4e2a\u6838\u5fc3\u65b9\u6cd5\uff1a<\/p>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2.1 \u57fa\u7840\u6a21\u677f<\/h3>\n<pre class=\"prism-highlight prism-language-python\">from&nbsp;torch.utils.data&nbsp;import&nbsp;Dataset\n\nclass&nbsp;CustomDataset(Dataset):\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self,&nbsp;data_path,&nbsp;transform=None):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.data_path&nbsp;=&nbsp;data_path\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.transform&nbsp;=&nbsp;transform\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.samples&nbsp;=&nbsp;self._load_data()&nbsp;&nbsp;#&nbsp;\u52a0\u8f7d\u6570\u636e\u5217\u8868\n\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__len__(self):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;len(self.samples)\n\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__getitem__(self,&nbsp;index):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;1.&nbsp;\u8bfb\u53d6\u539f\u59cb\u6570\u636e\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sample&nbsp;=&nbsp;self._read_sample(index)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;2.&nbsp;\u5e94\u7528\u9884\u5904\u7406\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;self.transform:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sample&nbsp;=&nbsp;self.transform(sample)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;3.&nbsp;\u8fd4\u56de\u6837\u672c\uff08\u5982\u56fe\u50cf+\u6807\u7b7e\uff09\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;sample<\/pre>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">2.2 \u5173\u952e\u65b9\u6cd5\u8be6\u89e3<\/h3>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\"><code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">__init__<\/code><\/span>\uff1a\u521d\u59cb\u5316\u6570\u636e\u8def\u5f84\u3001\u9884\u5904\u7406\u53d8\u6362\uff0c\u5e76\u52a0\u8f7d\u6570\u636e\u5143\u4fe1\u606f\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\"><code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">__len__<\/code><\/span>\uff1a\u8fd4\u56de\u6570\u636e\u96c6\u5927\u5c0f\uff0c\u7528\u4e8eDataLoader\u7684\u8fdb\u5ea6\u63a7\u5236\u3002<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\"><code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">__getitem__<\/code><\/span>\uff1a\u6838\u5fc3\u65b9\u6cd5\uff0c\u9700\u5b8c\u6210\u6570\u636e\u8bfb\u53d6\u3001\u9884\u5904\u7406\u548c\u8fd4\u56de\u3002<\/p>\n<\/li>\n<\/ul>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e09\u3001\u5b9e\u6218\u6848\u4f8b\uff1a\u56fe\u50cf\u5206\u7c7b\u6570\u636e\u96c6<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4ee5Kaggle\u7684Dogs vs Cats\u6570\u636e\u96c6\u4e3a\u4f8b\uff0c\u5b9e\u73b0\u81ea\u5b9a\u4e49Dataset\uff1a<\/p>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">3.1 \u6570\u636e\u51c6\u5907<\/h3>\n<pre style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin-top: 8px; margin-bottom: 8px; scrollbar-width: none; font-size: 15px; overflow: auto; position: relative; color: rgb(6, 7, 31); background-color: rgb(253, 253, 254);\" class=\"prism-highlight prism-language-python\">dataset\/\n\u251c\u2500\u2500&nbsp;train\/\n\u2502&nbsp;&nbsp;&nbsp;\u251c\u2500\u2500&nbsp;cat.0.jpg\n\u2502&nbsp;&nbsp;&nbsp;\u251c\u2500\u2500&nbsp;dog.0.jpg\n\u2502&nbsp;&nbsp;&nbsp;\u2514\u2500\u2500&nbsp;...\n\u2514\u2500\u2500&nbsp;annotations.txt&nbsp;&nbsp;#&nbsp;\u683c\u5f0f\uff1afilename&nbsp;label<\/pre>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">3.2 \u5b8c\u6574\u5b9e\u73b0<\/h3>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;os\nfrom&nbsp;PIL&nbsp;import&nbsp;Image\nfrom&nbsp;torch.utils.data&nbsp;import&nbsp;Dataset\nfrom&nbsp;torchvision&nbsp;import&nbsp;transforms\n\nclass&nbsp;DogCatDataset(Dataset):\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self,&nbsp;root_dir,&nbsp;ann_file,&nbsp;transform=None):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.root_dir&nbsp;=&nbsp;root_dir\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.transform&nbsp;=&nbsp;transform\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.samples&nbsp;=&nbsp;self._load_annotations(ann_file)\n\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;_load_annotations(self,&nbsp;ann_file):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;samples&nbsp;=&nbsp;[]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;open(ann_file)&nbsp;as&nbsp;f:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;line&nbsp;in&nbsp;f:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename,&nbsp;label&nbsp;=&nbsp;line.strip().split()\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;samples.append((filename,&nbsp;int(label)))\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;samples\n\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__len__(self):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;len(self.samples)\n\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__getitem__(self,&nbsp;index):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename,&nbsp;label&nbsp;=&nbsp;self.samples[index]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;img_path&nbsp;=&nbsp;os.path.join(self.root_dir,&nbsp;filename)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image&nbsp;=&nbsp;Image.open(img_path)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;self.transform:\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image&nbsp;=&nbsp;self.transform(image)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;image,&nbsp;label\n\n#&nbsp;\u4f7f\u7528\u793a\u4f8b\ntransform&nbsp;=&nbsp;transforms.Compose([\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.Resize(256),\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.CenterCrop(224),\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.ToTensor(),\n&nbsp;&nbsp;&nbsp;&nbsp;transforms.Normalize(mean=[0.485,&nbsp;0.456,&nbsp;0.406],&nbsp;std=[0.229,&nbsp;0.224,&nbsp;0.225])\n])\n\ndataset&nbsp;=&nbsp;DogCatDataset(\n&nbsp;&nbsp;&nbsp;&nbsp;root_dir=&#39;dataset\/train&#39;,\n&nbsp;&nbsp;&nbsp;&nbsp;ann_file=&#39;dataset\/annotations.txt&#39;,\n&nbsp;&nbsp;&nbsp;&nbsp;transform=transform\n)\n\ndataloader&nbsp;=&nbsp;DataLoader(dataset,&nbsp;batch_size=32,&nbsp;shuffle=True,&nbsp;num_workers=4)<\/pre>\n<p><span style=\"color: rgba(0, 0, 0, 0.85); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Helvetica Neue&quot;, Arial, &quot;Noto Sans&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Noto Color Emoji&quot;; white-space-collapse: preserve;\"><\/span><\/p>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u56db\u3001\u9ad8\u7ea7\u6280\u5de7\u4e0e\u4f18\u5316<\/h2>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">4.1 \u591a\u7ebf\u7a0b\u52a0\u901f<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u901a\u8fc7<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">num_workers<\/code>\u53c2\u6570\u542f\u7528\u591a\u8fdb\u7a0b\u52a0\u8f7d\uff1a<\/p>\n<pre style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin-top: 8px; margin-bottom: 8px; scrollbar-width: none; font-size: 15px; overflow: auto; position: relative; color: rgb(6, 7, 31); background-color: rgb(253, 253, 254);\">pythondataloader&nbsp;=&nbsp;DataLoader(dataset,&nbsp;batch_size=64,&nbsp;num_workers=8)<\/pre>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">4.2 \u81ea\u5b9a\u4e49Collate\u51fd\u6570<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u5904\u7406\u53d8\u957f\u6570\u636e\uff08\u5982NLP\u5e8f\u5217\uff09\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">def&nbsp;collate_fn(batch):\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;batch:&nbsp;List[Tuple(image,&nbsp;label)]\n&nbsp;&nbsp;&nbsp;&nbsp;images&nbsp;=&nbsp;[item[0]&nbsp;for&nbsp;item&nbsp;in&nbsp;batch]\n&nbsp;&nbsp;&nbsp;&nbsp;labels&nbsp;=&nbsp;[item[1]&nbsp;for&nbsp;item&nbsp;in&nbsp;batch]\n&nbsp;&nbsp;&nbsp;&nbsp;#&nbsp;\u5806\u53e0\u56fe\u50cf\u5e76\u8f6c\u6362\u4e3aTensor\n&nbsp;&nbsp;&nbsp;&nbsp;images&nbsp;=&nbsp;torch.stack(images,&nbsp;dim=0)\n&nbsp;&nbsp;&nbsp;&nbsp;labels&nbsp;=&nbsp;torch.LongTensor(labels)\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;images,&nbsp;labels\n\ndataloader&nbsp;=&nbsp;DataLoader(dataset,&nbsp;batch_size=32,&nbsp;collate_fn=collate_fn)<\/pre>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><span style=\"color: rgba(0, 0, 0, 0.85); font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, &quot;Helvetica Neue&quot;, Arial, &quot;Noto Sans&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;, &quot;Segoe UI Symbol&quot;, &quot;Noto Color Emoji&quot;; white-space-collapse: preserve;\"><\/span><\/p>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">4.3 \u5185\u5b58\u6620\u5c04\u4f18\u5316<\/h3>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u5bf9\u4e8e\u5927\u578b\u6570\u636e\u96c6\uff0c\u53ef\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff08\u5982HDF5\uff09\u51cf\u5c11I\/O\u5f00\u9500\uff1a<\/p>\n<pre class=\"prism-highlight prism-language-python\">import&nbsp;h5py\n\nclass&nbsp;HDF5Dataset(Dataset):\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__init__(self,&nbsp;h5_path):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.h5_file&nbsp;=&nbsp;h5py.File(h5_path,&nbsp;&#39;r&#39;)\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.length&nbsp;=&nbsp;len(self.h5_file[&#39;images&#39;])\n\n&nbsp;&nbsp;&nbsp;&nbsp;def&nbsp;__getitem__(self,&nbsp;index):\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;image&nbsp;=&nbsp;self.h5_file[&#39;images&#39;][index]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;label&nbsp;=&nbsp;self.h5_file[&#39;labels&#39;][index]\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;image,&nbsp;label<\/pre>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u4e0e\u89e3\u51b3\u65b9\u6848<\/h2>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">5.1 \u6570\u636e\u8def\u5f84\u9519\u8bef<\/h3>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u95ee\u9898<\/span>\uff1a<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">FileNotFoundError<\/code><\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u89e3\u51b3<\/span>\uff1a\u4f7f\u7528<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">os.path.join<\/code>\u6784\u5efa\u8de8\u5e73\u53f0\u8def\u5f84\uff0c\u68c0\u67e5\u6587\u4ef6\u6743\u9650\u3002<\/p>\n<\/li>\n<\/ul>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">5.2 \u5185\u5b58\u4e0d\u8db3<\/h3>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u95ee\u9898<\/span>\uff1a\u52a0\u8f7d\u5927\u578b\u6570\u636e\u96c6\u65f6OOM<\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u89e3\u51b3<\/span>\uff1a<\/p>\n<\/li>\n<ul style=\"list-style-type: square;\" class=\" list-paddingleft-2\">\n<li>\n<p>\u4f7f\u7528\u751f\u6210\u5668\uff08IterableDataset\uff09<\/p>\n<\/li>\n<li>\n<p>\u5206\u6279\u52a0\u8f7d\u6570\u636e<\/p>\n<\/li>\n<li>\n<p>\u964d\u4f4e<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">batch_size<\/code><\/p>\n<\/li>\n<\/ul>\n<\/ul>\n<h3 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 20px; border: none; line-height: 1.7; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">5.3 \u6570\u636e\u7c7b\u578b\u4e0d\u5339\u914d<\/h3>\n<ul style=\"box-sizing: border-box; padding: 0px 0px 0px 30px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style-position: outside; list-style-image: none; margin-top: 14px; margin-bottom: 1em; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\" class=\" list-paddingleft-2\">\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u95ee\u9898<\/span>\uff1a<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)<\/code><\/p>\n<\/li>\n<li>\n<p><span style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: -apple-system, BlinkMacSystemFont, &quot;Segoe UI&quot;, Roboto, Ubuntu, &quot;Helvetica Neue&quot;, Helvetica, Arial, &quot;PingFang SC&quot;, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei UI&quot;, &quot;Microsoft YaHei&quot;, &quot;Source Han Sans CN&quot;, sans-serif, &quot;Apple Color Emoji&quot;, &quot;Segoe UI Emoji&quot;; list-style: none; margin: 0px; scrollbar-width: none; font-weight: 600;\">\u89e3\u51b3<\/span>\uff1a\u663e\u5f0f\u8c03\u7528<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">.to(device)<\/code>\u6216\u4f7f\u7528<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">pin_memory=True<\/code>\u52a0\u901fGPU\u4f20\u8f93\u3002<\/p>\n<\/li>\n<\/ul>\n<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u516d\u3001\u603b\u7ed3<\/h2>\n<p id=\"\" style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFang-SC-Regular; list-style: none; margin-top: 0px; margin-bottom: 0px; scrollbar-width: none; font-size: 15px; line-height: 1.7; color: rgb(6, 7, 31); text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\">\u81ea\u5b9a\u4e49Dataset\u662fPyTorch\u6570\u636e\u6d41\u6c34\u7ebf\u7684\u6838\u5fc3\u6280\u80fd\uff0c\u901a\u8fc7\u7ee7\u627f<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">Dataset<\/code>\u7c7b\u5e76\u5b9e\u73b0<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">__len__<\/code>\u548c<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">__getitem__<\/code>\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u7075\u6d3b\u5904\u7406\u5404\u7c7b\u6570\u636e\u683c\u5f0f\u3002\u7ed3\u5408<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">DataLoader<\/code>\u7684\u591a\u7ebf\u7a0b\u52a0\u901f\u548c\u81ea\u5b9a\u4e49<code class=\" inline\" style=\"box-sizing: border-box; padding: 1px 4px; -webkit-font-smoothing: antialiased; font-family: SFMono-Regular, Consolas, &quot;Liberation Mono&quot;, Menlo, Courier, monospace; list-style: none; margin: 0px 2px; scrollbar-width: none; font-size: 12.75px; tab-size: 4; background-color: rgba(27, 31, 35, 0.05); border-radius: 3px;\">collate_fn<\/code>\uff0c\u53ef\u6784\u5efa\u9ad8\u6548\u7684\u6570\u636e\u52a0\u8f7d\u7ba1\u9053\u3002\u5b9e\u9645\u5e94\u7528\u4e2d\u9700\u6ce8\u610f\u8def\u5f84\u5904\u7406\u3001\u5185\u5b58\u4f18\u5316\u548c\u8bbe\u5907\u517c\u5bb9\u6027\uff0c\u4ee5\u786e\u4fdd\u8bad\u7ec3\u8fc7\u7a0b\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<h2 style=\"box-sizing: border-box; padding: 0px; -webkit-font-smoothing: antialiased; font-family: PingFangSC-Semibold; list-style: none; margin: 14px 0px 8px; scrollbar-width: none; color: rgb(5, 7, 59); font-size: 24px; border: none; line-height: 1.6; text-wrap-mode: wrap; background-color: rgb(253, 253, 254);\"><!--autointro--><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-186","post","type-post","status-publish","format-standard","hentry","category-pythonbiji"],"_links":{"self":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=186"}],"version-history":[{"count":0,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mianka.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}