{"id":8309,"date":"2022-10-04T10:58:40","date_gmt":"2022-10-04T02:58:40","guid":{"rendered":"http:\/\/139.9.1.231\/?p=8309"},"modified":"2022-10-04T10:58:42","modified_gmt":"2022-10-04T02:58:42","slug":"pytorch_dataloader","status":"publish","type":"post","link":"http:\/\/139.9.1.231\/index.php\/2022\/10\/04\/pytorch_dataloader\/","title":{"rendered":"pytorch\u5982\u4f55\u52a0\u8f7d\u4e0d\u540c\u5c3a\u5bf8\u7684\u56fe\u7247\u6570\u636e"},"content":{"rendered":"\n<p>     \u5982\u4f55\u4f7f\u7528dataloader\u52a0\u8f7d\u76f8\u540c\u7ef4\u5ea6\u4f46\u662f\u4e0d\u540c\u5c3a\u5bf8\u7684\u6570\u636e\u96c6\uff08\u56fe\u7247\uff09\uff0c\u4e0d\u4f7f\u7528resize\uff0ccrop\u7b49\u6539\u53d8\u6a21\u578b\u8f93\u5165\u7684shape\u3002<\/p>\n\n\n\n<p class=\"has-light-gray-background-color has-background\">\u77e5\u4e4e\uff1a<a href=\"https:\/\/www.zhihu.com\/question\/395888465\">https:\/\/www.zhihu.com\/question\/395888465<\/a><\/p>\n\n\n\n<p>\u5982\u679c\u52a0\u8f7d\u7684\u6570\u636e\u7684\u7ef4\u5ea6\u5c3a\u5bf8\u4e0d\u76f8\u540c\u7684\u8bdd\uff0c\u5728\u8fed\u4ee3\u5668\u4e2d\u4f1a\u7206\u51fa\u5982\u4e0b\u7684\u9519\u8bef<\/p>\n\n\n\n<h1>RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0.<\/h1>\n\n\n\n<p class=\"has-bright-blue-background-color has-background\">1\u3001pytorch\u7684dataloader\u9ed8\u8ba4\u7684collate_fn\u4f1a\u4f7f\u7528torch.stack\u5408\u5e76\u591a\u5f20\u56fe\u7247\u6210\u4e3abatch<\/p>\n\n\n\n<p>\u8981\u4e48\u53e6\u5916\u5199\u4e00\u4e2acollate_fn<\/p>\n\n\n\n<p>\u8981\u4e48\u5728dataset\u7c7b\u4e2d\u5bf9\u56fe\u7247\u505apadding\uff0c\u4f7f\u5f97\u56fe\u7247\u7684size\u4e00\u6837\uff0c\u53ef\u4ee5\u76f4\u63a5stack<\/p>\n\n\n\n<p class=\"has-bright-blue-background-color has-background\">2\u3001\u5173\u4e8ecollate_fn:<\/p>\n\n\n\n<p>\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/pytorch.org\/docs\/stable\/data.html#working-with-collate-fn\" target=\"_blank\">https:\/\/pytorch.org\/docs\/stable\/data.html#working-with-collate-fn<\/a><\/p>\n\n\n\n<p>The use of&nbsp;<code>collate_fn<\/code>&nbsp;is slightly different when automatic batching is enabled or disabled.<\/p>\n\n\n\n<ul><li><strong>When automatic batching is disabled<\/strong>,&nbsp;<code>collate_fn<\/code>&nbsp;is called with each individual data sample, and the output is yielded from the data loader iterator. In this case, the default&nbsp;<code>collate_fn<\/code>&nbsp;simply converts NumPy arrays in PyTorch tensors.<\/li><li><strong>When automatic batching is enabled<\/strong>,&nbsp;<code>collate_fn<\/code>&nbsp;is called with a list of data samples at each time. It is expected to collate the input samples into a batch for yielding from the data loader iterator. The rest of this section describes behavior of the default&nbsp;<code>collate_fn<\/code>&nbsp;in this case.<\/li><\/ul>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u4f60\u53ef\u4ee5\u8003\u8651\u5173\u95ed\u81ea\u52a8\u6253\u5305\uff0c\u8fd9\u6837collate_fn\u5904\u7406\u7684\u5c31\u662f\u72ec\u7acb\u7684\u6837\u672c\u3002\u4e5f\u53ef\u4ee5\u6253\u5f00\u81ea\u52a8\u6253\u5305\uff0c\u8fd9\u6837\u8fd9\u4e2a\u51fd\u6570\u5c31\u4f1a\u88ab\u8f93\u5165\u4e00\u4e2abatch\u5217\u8868\u7684\u6570\u636e\u3002\u6ce8\u610f\uff0c\u8fd9\u4e2a\u5217\u8868\u7684\u6570\u636e\u53ef\u4ee5\u4e0d\u540c\u5927\u5c0f\u54e6\uff0c\u77e5\u8bc6\u8fd9\u6837\u4f60\u5c31\u6ca1\u529e\u6cd5\u5c06\u5176<code>stack<\/code>\u6210\u4e00\u4e2a\u5b8c\u6574\u7684batch\u3002\u6240\u4ee5\uff0c\u5b9e\u9645\u4e0a\u4f60\u7684\u62a5\u9519\uff0c\u5e94\u8be5\u662f\u8fd9\u4e2a\u4f4d\u7f6e\u51fa\u7684\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u53ef\u4ee5\u8003\u8651\u4ee5\u4e0b\u51e0\u79cd\u7b56\u7565\uff1a<\/p>\n\n\n\n<ul><li><strong>\u5355\u4e2a\u6837\u672c\u8f93\u5165\uff0c\u8fd9\u6837\u540c\u4e00\u4e2abatch\u7ec4\u5408\u7684\u65f6\u5019\u5c31\u4e0d\u9700\u8981\u62c5\u5fc3\u4e86<\/strong><\/li><li><strong>\u5bf9\u8f93\u5165\u6837\u672cpadding\u6210\u6700\u5927\u7684\u5f62\u72b6\uff0c\u7ec4\u5408\u6210batch\uff0c\u4e4b\u540e\u9001\u5165\u7f51\u7edc\u7684\u65f6\u5019\uff0c\u4f60\u53ef\u4ee5\u628a\u6570\u636e\u62c6\u5206\u5f00\uff0c\u6309\u4f60\u60f3\u8981\u7684\u5c06\u5176\u53bb\u6389padding\u6216\u8005\u5176\u4ed6\u64cd\u4f5c<\/strong><\/li><li><strong>\u6b63\u5e38\u8bfb\u53d6\uff0c\u4e4b\u540e\u518d\u81ea\u5b9a\u4e49\u7684collate_fn\u4e2d\u5c06\u6570\u636e\u62c6\u5f00\u8fd4\u56de\uff0c\u8fd9\u6837\u53ef\u4ee5\u8fd4\u56de\u76f8\u540c\u7ed3\u6784\u7684\u6570\u636e<\/strong><\/li><\/ul>\n\n\n\n<p>\u5bf9\u4e8e\u6700\u540e\u4e00\u70b9\uff0c\u7ed9\u4e2a\u5c0fdemo\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class OurDataset(Dataset):\n    def __init__(self, *tensors):\n        self.tensors = tensors\n    def __getitem__(self, index):\n        return self.tensors&#91;index]\n    def __len__(self):\n        return len(self.tensors)\n\ndef collate_wrapper(batch):\n#\u51fd\u6570\u5c31\u4f1a\u8f93\u5165\u4e00\u4e2abatch\u7684\u5217\u8868\u7684\u6570\u636e\uff08\u6ce8\u610f\u662fbatch\u662f\u4e00\u4e2a\u5217\u8868\uff0c\u6240\u4ee5\u91cc\u9762\u7684\u6570\u636e\u53ef\u4ee5\u4e0d\u540c\u5927\u5c0f\uff09\n    a, b = batch\n    return a, b\n\na = torch.randn(3, 2, 3)\nb = torch.randn(3, 3, 4)\ndataset = OurDataset(a, b)\n\nloader = DataLoader(dataset, batch_size=2, collate_fn=collate_wrapper)\n\nfor sample in loader:\n    print(&#91;x.size() for x in sample])\n\n<em># Out: &#91;torch.Size(&#91;1, 3, 2, 3]), torch.Size(&#91;1, 3, 3, 4])]<\/em><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u4f7f\u7528dataloader\u52a0\u8f7d\u76f8\u540c\u7ef4\u5ea6\u4f46\u662f\u4e0d\u540c\u5c3a\u5bf8\u7684\u6570\u636e\u96c6\uff08\u56fe\u7247\uff09\uff0c\u4e0d\u4f7f\u7528resize\uff0ccrop\u7b49\u6539\u53d8\u6a21\u578b\u8f93 &hellip; <a href=\"http:\/\/139.9.1.231\/index.php\/2022\/10\/04\/pytorch_dataloader\/\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">pytorch\u5982\u4f55\u52a0\u8f7d\u4e0d\u540c\u5c3a\u5bf8\u7684\u56fe\u7247\u6570\u636e<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11],"tags":[],"_links":{"self":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8309"}],"collection":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/comments?post=8309"}],"version-history":[{"count":20,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8309\/revisions"}],"predecessor-version":[{"id":8789,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/posts\/8309\/revisions\/8789"}],"wp:attachment":[{"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/media?parent=8309"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/categories?post=8309"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/139.9.1.231\/index.php\/wp-json\/wp\/v2\/tags?post=8309"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}