{"id":5343,"date":"2021-11-06T08:06:58","date_gmt":"2021-11-05T23:06:58","guid":{"rendered":"http:\/\/hituji1012.xsrv.jp\/?p=869"},"modified":"2026-08-01T13:51:03","modified_gmt":"2026-08-01T04:51:03","slug":"92","status":"publish","type":"post","link":"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/","title":{"rendered":"\u3010Pytorch\u3011\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u306b\u3064\u3044\u3066"},"content":{"rendered":"\n<p>\u4eca\u56de\u306fpytorch\u306b\u3064\u3044\u3066\u3001\u7fd2\u5f97\u3092\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>pytorch\u306f\u753b\u50cf\u3084\u81ea\u7136\u8a00\u8a9e\u306a\u3069\u306e\u6a5f\u68b0\u5b66\u7fd2\u958b\u767a\u304c\u3067\u304d\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002<\/p>\n<p>\u4ed6\u306b\u3082\u3001Tensorflow\u3001Keras\u306a\u3069\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u6700\u8fd1\u306fpytorch\u304c\u4eba\u6c17\u3067\u3059\u3002<\/p>\n<p>\u672c\u8a18\u4e8b\u306e\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9<\/p>\n<p><a href=\"http:\/\/hituji1012.xsrv.jp\/wp-content\/uploads\/2021\/01\/ml8.zip\">>>\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9(jupyter notebook)<\/a><\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_38 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u76ee\u6b21<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" style=\"display: none;\"><label for=\"item\" aria-label=\"Table of Content\"><span style=\"display: flex;align-items: center;width: 35px;height: 30px;justify-content: center;\"><svg style=\"fill: #444444;color:#444444\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #444444;color:#444444\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/label><input type=\"checkbox\" id=\"item\"><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#pytorch%E3%81%A8%E3%81%AF\" title=\"pytorch\u3068\u306f?\">pytorch\u3068\u306f?<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB\" title=\"\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\">\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#pytorch%E3%81%AE%E4%BD%BF%E3%81%84%E6%96%B9\" title=\"pytorch\u306e\u4f7f\u3044\u65b9\">pytorch\u306e\u4f7f\u3044\u65b9<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%81%AE%E8%AA%AD%E3%81%BF%E8%BE%BC%E3%81%BF\" title=\"\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u8aad\u307f\u8fbc\u307f\">\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u8aad\u307f\u8fbc\u307f<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E3%83%86%E3%83%B3%E3%82%BD%E3%83%AB%E3%81%AE%E4%BD%9C%E6%88%90\" title=\"\u30c6\u30f3\u30bd\u30eb\u306e\u4f5c\u6210\">\u30c6\u30f3\u30bd\u30eb\u306e\u4f5c\u6210<\/a><ul class='ez-toc-list-level-4'><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E4%B9%B1%E6%95%B001%E3%81%AE%E4%BD%9C%E6%88%90\" title=\"\u4e71\u6570,0,1,\u306e\u4f5c\u6210\">\u4e71\u6570,0,1,\u306e\u4f5c\u6210<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#list%E3%81%8B%E3%82%89%E4%BD%9C%E6%88%90\" title=\"list\u304b\u3089\u4f5c\u6210\">list\u304b\u3089\u4f5c\u6210<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#numpy%E3%81%8B%E3%82%89%E4%BD%9C%E6%88%90\" title=\"numpy\u304b\u3089\u4f5c\u6210\">numpy\u304b\u3089\u4f5c\u6210<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#tensor%E3%81%8B%E3%82%89numpy%E3%81%AE%E5%A4%89%E6%8F%9B\" title=\"tensor\u304b\u3089numpy\u306e\u5909\u63db\">tensor\u304b\u3089numpy\u306e\u5909\u63db<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9%E3%81%AE%E8%A8%AD%E5%AE%9A\" title=\"\u30c7\u30d0\u30a4\u30b9\u306e\u8a2d\u5b9a\">\u30c7\u30d0\u30a4\u30b9\u306e\u8a2d\u5b9a<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E3%83%86%E3%83%B3%E3%82%BD%E3%83%AB%E3%81%AE%E7%A2%BA%E8%AA%8D\" title=\"\u30c6\u30f3\u30bd\u30eb\u306e\u78ba\u8a8d\">\u30c6\u30f3\u30bd\u30eb\u306e\u78ba\u8a8d<\/a><ul class='ez-toc-list-level-4'><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E5%BD%A2\" title=\"\u5f62\">\u5f62<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E5%9E%8B%E3%81%AE%E3%82%BF%E3%82%A4%E3%83%97\" title=\"\u578b\u306e\u30bf\u30a4\u30d7\">\u578b\u306e\u30bf\u30a4\u30d7<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9\" title=\"\u30c7\u30d0\u30a4\u30b9\">\u30c7\u30d0\u30a4\u30b9<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E3%83%86%E3%83%B3%E3%82%BD%E3%83%AB%E3%81%8B%E3%82%89%E3%83%87%E3%83%BC%E3%82%BF%E3%82%92%E5%8F%96%E3%82%8A%E5%87%BA%E3%81%99\" title=\"\u30c6\u30f3\u30bd\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u308a\u51fa\u3059\">\u30c6\u30f3\u30bd\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u308a\u51fa\u3059<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E3%83%86%E3%83%B3%E3%82%BD%E3%83%AB%E3%81%AE%E8%A8%88%E7%AE%97\" title=\"\u30c6\u30f3\u30bd\u30eb\u306e\u8a08\u7b97\">\u30c6\u30f3\u30bd\u30eb\u306e\u8a08\u7b97<\/a><ul class='ez-toc-list-level-4'><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E5%9B%9B%E5%89%87%E6%BC%94%E7%AE%97\" title=\"\u56db\u5247\u6f14\u7b97\">\u56db\u5247\u6f14\u7b97<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E8%A1%8C%E5%88%97%E3%81%AE%E7%A9%8D\" title=\"\u884c\u5217\u306e\u7a4d\">\u884c\u5217\u306e\u7a4d<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E8%BB%A2%E7%BD%AE\" title=\"\u8ee2\u7f6e\">\u8ee2\u7f6e<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E6%AC%A1%E5%85%83%E3%83%87%E3%83%BC%E3%82%BF%E6%95%B0%E3%81%AE%E5%85%A5%E3%82%8C%E6%9B%BF%E3%81%88\" title=\"\u6b21\u5143\u30c7\u30fc\u30bf\u6570\u306e\u5165\u308c\u66ff\u3048\">\u6b21\u5143\u30c7\u30fc\u30bf\u6570\u306e\u5165\u308c\u66ff\u3048<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/#%E3%81%BE%E3%81%A8%E3%82%81\" title=\"\u307e\u3068\u3081\">\u307e\u3068\u3081<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"pytorch%E3%81%A8%E3%81%AF\"><\/span>pytorch\u3068\u306f?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>pytorch\u306fFacebook\u306b\u958b\u767a\u3055\u308c\u305f\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3001\u753b\u50cf\u3084\u81ea\u7136\u8a00\u8a9e\u306a\u3069\u306e\u6a5f\u68b0\u5b66\u7fd2\u958b\u767a\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>Tensorflow\u306fGoogle\u306b\u958b\u767a\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3069\u3061\u3089\u3092\u4f7f\u3063\u3066\u3082\u3001\u958b\u767a\u304c\u3067\u304d\u307e\u3059\u304c\u3001\u6700\u8fd1\u306fpytorch\u304c\u4eba\u6c17\u3067\u3059\u3002<\/p>\n<p>Google\u30c8\u30ec\u30f3\u30c9\u898b\u3066\u3082\u3001\u305d\u306e\u50be\u5411\u304c\u308f\u304b\u308a\u307e\u3059\u306d\u3002<\/p>\n<p><a href=\"https:\/\/tetsumag.com\/wp-content\/uploads\/2021\/01\/pytorch-1.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/tetsumag.com\/wp-content\/uploads\/2021\/01\/pytorch-1.png\" alt=\"\" width=\"400\" height=\"195\" class=\"aligncenter size-full wp-image-7114\" srcset=\"https:\/\/tetsumag.com\/blog\/wp-content\/uploads\/2021\/01\/pytorch-1.png 400w, https:\/\/tetsumag.com\/blog\/wp-content\/uploads\/2021\/01\/pytorch-1-300x146.png 300w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>\u3061\u306a\u307f\u306b\u3001Keras\u306f\u30d9\u30fc\u30b9\u306bTensorflow\u3092\u4f7f\u3063\u3066\u3044\u3066\u3001\u3088\u308a\u958b\u767a\u304c\u3057\u3084\u3059\u304f\u3055\u308c\u3066\u3044\u308b\u3082\u306e\u3067\u3059\u3002<\/p>\n<p>\u3069\u308c\u3082\u3001numpy\u306e\u3088\u3046\u306a\u884c\u5217\u6f14\u7b97\u3092\u3059\u308b\u3082\u306e\u3067\u3001\u884c\u5217\u30c7\u30fc\u30bf\uff08\u914d\u5217\uff09\u3092Tensor\uff08\u30c6\u30f3\u30bd\u30eb\uff09\u3068\u8a00\u3044\u307e\u3059\u3002<\/p>\n<p>\u3069\u3061\u3089\u3082\u3001\u4eba\u6c17\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306a\u306e\u3067\u3001\u60c5\u5831\u306b\u4e0d\u8db3\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<p>\u9055\u3044\u306f\u3001pytorch\u306fnumpy\u3068\u540c\u3058\u611f\u899a\u3067\u4f7f\u3048\u308b\u3068\u3044\u3046\u70b9\u3067\u3059\u3002<\/p>\n<p>Tensorflow\u306f\u30e2\u30c7\u30eb\u3092\u6c7a\u3081\u305f\u3089\u3001\u305d\u306e\u30e2\u30c7\u30eb\u304c\u5b9f\u884c\u3055\u308c\u308b\u306e\u3067\u3059\u304c\u3001\u8a08\u7b97\u51e6\u7406\u3092\u30c7\u30d0\u30c3\u30b0\u3057\u306a\u304c\u3089\u8ffd\u3044\u306b\u304f\u3044\u306a\u3069\u521d\u5b66\u8005\u306b\u306f\u4f7f\u3044\u65b9\u304c\u308f\u304b\u308a\u306b\u304f\u3044\u3067\u3059\u3002<\/p>\n<p>pytorch\u306fnumpy\u3068\u540c\u3058\u3088\u3046\u306b\u3001\u5b9f\u884c\u3057\u305f\u3089\u3001\u305d\u306e\u7d50\u679c\u3092\u53d7\u3051\u53d6\u3063\u3066\u3001\u307e\u305f\u6b21\u306e\u8a08\u7b97\u3068\u9032\u3093\u3067\u3044\u304f\u306e\u3067\u3001\u611f\u899a\u7684\u306b\u3082\u99b4\u67d3\u307f\u3084\u3059\u3044\u3067\u3059\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E3%82%A4%E3%83%B3%E3%82%B9%E3%83%88%E3%83%BC%E3%83%AB\"><\/span>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>pytorch\u306f\u4e0b\u8a18\u306e\u516c\u5f0f\u3088\u308a\u3001OS\u306a\u3069\u3092\u9078\u629e\u3057\u3066\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\u3002<\/p>\n<p>NVIDIA\u306eGPU\u3092\u642d\u8f09\u3057\u3066\u306a\u3044\u30d1\u30bd\u30b3\u30f3\u3067\u4f7f\u3046\u65b9\u306f\u3001CUDA\u3092None\u3068\u3057\u307e\u3059\u3002<\/p>\n<p><strong><a href=\"https:\/\/pytorch.org\" target=\"_blank\" rel=\"noopener\">&gt;&gt; pytorch\u516c\u5f0f<\/a><\/strong><\/p>\n<p><a href=\"https:\/\/tetsumag.com\/wp-content\/uploads\/2021\/01\/pytorch-2.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/tetsumag.com\/wp-content\/uploads\/2021\/01\/pytorch-2.png\" alt=\"\" width=\"400\" height=\"148\" class=\"aligncenter size-full wp-image-7113\" srcset=\"https:\/\/tetsumag.com\/blog\/wp-content\/uploads\/2021\/01\/pytorch-2.png 400w, https:\/\/tetsumag.com\/blog\/wp-content\/uploads\/2021\/01\/pytorch-2-300x111.png 300w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>Run this Command\u3092\u30b3\u30d4\u30fc\u3057\u3066\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3057\u3087\u3046\u3002<\/p>\n<h2><span class=\"ez-toc-section\" id=\"pytorch%E3%81%AE%E4%BD%BF%E3%81%84%E6%96%B9\"><\/span>pytorch\u306e\u4f7f\u3044\u65b9<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>pytorch\u306fnumpy\u3068\u4f3c\u3066\u3044\u308b\u306e\u304c\u7279\u5fb4\u3067\u3059\u304c\u3001pytorch\u306eTensor\u304b\u3089numpy(ndarray)\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u3082\u591a\u3044\u3067\u3059\u3002<\/p>\n<p>\u6a5f\u68b0\u5b66\u7fd2\u306e\u30e2\u30c7\u30eb\u306f\u81ea\u5206\u3067\u4f5c\u3089\u306a\u304f\u3066\u3082\u3001\u3059\u3067\u306b\u4f5c\u3063\u3066\u3042\u308b\u3082\u306e\u3092\u8aad\u307f\u8fbc\u3080\u3053\u3068\u304c\u591a\u3044\u3067\u3059\u304c\u3001\u6700\u7d42\u7684\u306b\u51fa\u529b\u3055\u308c\u305f\u7d50\u679c\u306fTensor\u3067\u51fa\u3066\u304f\u308b\u306e\u3067\u3001\u3053\u306eTensor\u304c\u64cd\u4f5c\u304c\u3067\u304d\u306a\u3051\u308c\u3070\u3044\u3051\u307e\u305b\u3093\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u306b\u3082\u3001\u3053\u3053\u3067Tensor\u306e\u4f7f\u3044\u65b9\u3092\u7406\u89e3\u3057\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E3%83%A9%E3%82%A4%E3%83%96%E3%83%A9%E3%83%AA%E3%81%AE%E8%AA%AD%E3%81%BF%E8%BE%BC%E3%81%BF\"><\/span>\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u8aad\u307f\u8fbc\u307f<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u8aad\u307f\u8fbc\u307f\u306f\u6b21\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"\">\r\n\r\nimport torch\r\n\r\n<\/pre>\n<p>numpy\u3082\u4f7f\u3046\u306e\u3067\u3001numpy\u3082\u4e00\u7dd2\u306b\u8aad\u307f\u8fbc\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">import torch\r\nimport numpy as np<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E3%83%86%E3%83%B3%E3%82%BD%E3%83%AB%E3%81%AE%E4%BD%9C%E6%88%90\"><\/span>\u30c6\u30f3\u30bd\u30eb\u306e\u4f5c\u6210<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u57fa\u672c\u7684\u306b\u3001numpy\u3068\u540c\u3058\u3088\u3046\u306a\u65b9\u6cd5\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E4%B9%B1%E6%95%B001%E3%81%AE%E4%BD%9C%E6%88%90\"><\/span>\u4e71\u6570,0,1,\u306e\u4f5c\u6210<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u307e\u305a\u3001\u4e71\u6570\u30010\u30011\u306e\u30c6\u30f3\u30bd\u30eb\u3092\u4f5c\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<table style=\"border-collapse: collapse; width: 62.663755458515276%; height: 115px;\">\n<tbody>\n<tr style=\"height: 18px;\">\n<td style=\"width: 33.10104529616724%; background-color: #8c8c8c; height: 18px;\"><strong>\u30e1\u30bd\u30c3\u30c9<\/strong><\/td>\n<td style=\"width: 66.89895470383276%; background-color: #8c8c8c; height: 18px;\"><strong>\u6a5f\u80fd<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 33.10104529616724%; height: 18px;\">rand<\/td>\n<td style=\"width: 66.89895470383276%; height: 18px;\">0\u4ee5\u4e0a\u30011\u672a\u6e80\u306e\u4e71\u6570\u306e\u30c6\u30f3\u30bd\u30eb<\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 33.10104529616724%; height: 18px;\">ones<\/td>\n<td style=\"width: 66.89895470383276%; height: 18px;\">1\u306e\u30c6\u30f3\u30bd\u30eb<\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 33.10104529616724%; height: 18px;\">zeros<\/td>\n<td style=\"width: 66.89895470383276%; height: 18px;\">0\u306e\u30c6\u30f3\u30bd\u30eb<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5f15\u6570\u306b\u6b21\u5143\u6570\u3084\u30c7\u30fc\u30bf\u6570\u3092\u6c7a\u3081\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u30012\u6b21\u5143\u306e2\u00d73\u306e\u30c7\u30fc\u30bf\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">shape = (2,3)\r\nrand_tensor = torch.rand(shape)\r\nones_tensor = torch.ones(shape)\r\nzeros_tensor = torch.zeros(shape)<\/pre>\n<p>\u4e71\u6570\u306e\u7d50\u679c\u3092\u898b\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">print(rand_tensor)<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">tensor([[0.0263, 0.9624, 0.6261],\r\n        [0.6330, 0.5714, 0.0918]])<\/pre>\n<p>0-1\u306e\u4e71\u6570\u304c\u3067\u304d\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u6700\u521d\u306e\u65b9\u306btensor\u3068\u3042\u308a\u307e\u3059\u306d\u3002tensor\u3067\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u3068\u304c\u308f\u304b\u308a\u307e\u3059\u3002<\/p>\n<p>\u4ed6\u306e\u3001ones_tensor\u3082zeros_tensor\u3082\u305d\u308c\u305e\u308c1\u30680\u304c\u5165\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h4><span class=\"ez-toc-section\" id=\"list%E3%81%8B%E3%82%89%E4%BD%9C%E6%88%90\"><\/span>list\u304b\u3089\u4f5c\u6210<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>list\u304b\u3089\u30c6\u30f3\u30bd\u30eb\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>\u4f5c\u6210\u3059\u308b\u30c7\u30fc\u30bf\u306f\u30011\u304b\u30896\u306e2\u6b21\u5143\u306e2\u00d73\u3068\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">data = [[1, 2, 3],[4, 5, 6]]\r\nx_data = torch.tensor(data)\r\nprint(x_data)<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">tensor([[1, 2, 3],\r\n        [4, 5, 6]])<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"numpy%E3%81%8B%E3%82%89%E4%BD%9C%E6%88%90\"><\/span>numpy\u304b\u3089\u4f5c\u6210<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>list\u3060\u3051\u3067\u306a\u304f\u3001numpy\u304b\u3089\u3082\u30c6\u30f3\u30bd\u30eb\u304c\u4f5c\u6210\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">data = [[1, 2, 3],[4, 5, 6]]\r\nnp_array = np.array(data)\r\nx_data = torch.tensor(np_array)\r\nprint(x_data)<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">tensor([[1, 2],\r\n        [3, 4]], dtype=torch.int32)<\/pre>\n<p>dtype=torch.int32\u3068\u306a\u3063\u3066\u3044\u308b\u306e\u306f\u3001pytorch\u306e\u578b\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>int32\u306f\u6574\u6570\u578b\u3067\u300132\u306f32\u30d3\u30c3\u30c8\u3092\u8868\u3057\u3066\u3044\u3066\u3001\u3053\u306e\u6570\u5b57\u3067\u8868\u73fe\u3067\u304d\u308b\u6570\u5024\u306e\u5927\u304d\u3055\u304c\u6c7a\u307e\u308a\u307e\u3059\u3002<\/p>\n<p><strong><a href=\"https:\/\/pytorch.org\/docs\/stable\/tensor_attributes.html\" target=\"_blank\" rel=\"noopener\">&gt;&gt; pytorch\u578b\u306e\u4e00\u89a7<\/a><\/strong><\/p>\n<h4><span class=\"ez-toc-section\" id=\"tensor%E3%81%8B%E3%82%89numpy%E3%81%AE%E5%A4%89%E6%8F%9B\"><\/span>tensor\u304b\u3089numpy\u306e\u5909\u63db<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>numpy\u304b\u3089tensor\u3092\u4f5c\u308b\u65b9\u6cd5\u3092\u898b\u307e\u3057\u305f\u304c\u3001\u9006\u306e\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">x_data = torch.tensor([[1, 2, 3],[4, 5, 6]])\r\nprint('\u5909\u63db\u524d', x_data)\r\nnp_array = x_data.to('cpu').detach().numpy().copy()\r\nprint('\u5909\u63db\u5f8c', np_array)<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">\u5909\u63db\u524d tensor([[1, 2, 3],\r\n        [4, 5, 6]])\r\n\u5909\u63db\u5f8c [[1 2 3]\r\n [4 5 6]]<\/pre>\n<p>\u6a5f\u68b0\u5b66\u7fd2\u3067\u63a8\u8ad6\u3057\u305f\u3042\u3068\u306f\u3001numpy\u306b\u53d6\u308a\u51fa\u3057\u3066\u3001\u51e6\u7406\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>to(&#8216;cpu&#8217;)\u3068\u306a\u3063\u3066\u3044\u308b\u3068\u3053\u308d\u306f\u3001\u6b21\u3067\u7d39\u4ecb\u3057\u307e\u3059\u304c\u3001numpy\u306b\u3059\u308b\u3068\u304d\u306f\u5fc5\u305a\u5165\u308c\u3066\u304a\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9%E3%81%AE%E8%A8%AD%E5%AE%9A\"><\/span>\u30c7\u30d0\u30a4\u30b9\u306e\u8a2d\u5b9a<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u30c6\u30f3\u30bd\u30eb\u306b\u306f\u30c7\u30d0\u30a4\u30b9\u306e\u8a2d\u5b9a\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u7c21\u5358\u306b\u8a00\u3048\u3070\u3001\u30c6\u30f3\u30bd\u30eb\u304cCPU\u3068GPU\u3069\u3061\u3089\u3092\u4f7f\u3063\u3066\u8a08\u7b97\u3059\u308b\u304b\u306e\u8a2d\u5b9a\u3067\u3059\u3002<\/p>\n<p>GPU\u304c\u4f7f\u3048\u308b\u304b\u306f<strong>torch.cuda.is_available()<\/strong>\u304cTrue \/False\u3067\u8fd4\u3057\u3066\u304f\u308b\u306e\u3067\u3001\u6b21\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3067\u8a2d\u5b9a\u3059\u308b\u30c7\u30d0\u30a4\u30b9\u3092\u6c7a\u3081\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\r\nprint(device)<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">cuda<\/pre>\n<p>&nbsp;<\/p>\n<p>NVIDIA\u306eGPU\u306e\u30d1\u30bd\u30b3\u30f3\u3067\u3001cuda\u3084cudnn\u306e\u8a2d\u5b9a\u304c\u3057\u3066\u3042\u308c\u3070\u3001cuda\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u308c\u4ee5\u5916\u306f\u3001cpu\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<p>\u8a2d\u5b9a\u3059\u308b\u5834\u5408\u306f<strong>to<\/strong>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">x_data = x_data.to(device)<\/pre>\n<p>\u610f\u56f3\u7684\u306bCPU\u3092\u4f7f\u3044\u305f\u3044\u5834\u5408\u306f\u3001to(&#8216;cpu&#8217;)\u3068\u3059\u308c\u3070OK\u3067\u3059\u3002<\/p>\n<p>GPU\u304c\u8907\u6570\u4f7f\u3048\u308b\u5834\u5408\u3001&#8217;cuda&#8217;\u3068\u3057\u3066\u3044\u307e\u3059\u304c\u3001&#8217;cuda:\u30c7\u30d0\u30a4\u30b9No&#8217;\u3068\u3059\u308b\u3053\u3068\u3067\u3001GPU\u306e\u30c7\u30d0\u30a4\u30b9\u3092\u6307\u5b9a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u30c7\u30d0\u30a4\u30b9No\u306f0\u304b\u3089\u59cb\u307e\u308b\u6570\u5b57\u3067\u3059\u3002<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E3%83%86%E3%83%B3%E3%82%BD%E3%83%AB%E3%81%AE%E7%A2%BA%E8%AA%8D\"><\/span>\u30c6\u30f3\u30bd\u30eb\u306e\u78ba\u8a8d<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u6b21\u306b\u30c6\u30f3\u30bd\u30eb\u306e\u60c5\u5831\u306b\u3064\u3044\u3066\u307f\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306e\u30c7\u30fc\u30bf\u306b\u3064\u3044\u3066\u307f\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">data = [[1, 2, 3],[4, 5, 6]]\r\nx_data = torch.tensor(data)<\/pre>\n<table style=\"border-collapse: collapse; width: 56.98689956331877%; height: 117px;\">\n<tbody>\n<tr style=\"height: 18px;\">\n<td style=\"width: 35.24904214559387%; background-color: #8c8c8c; height: 18px;\"><strong>\u5c5e\u6027<\/strong><\/td>\n<td style=\"width: 64.75095785440612%; background-color: #8c8c8c; height: 18px;\"><strong>\u6a5f\u80fd<\/strong><\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 35.24904214559387%; height: 18px;\">shape<\/td>\n<td style=\"width: 64.75095785440612%; height: 18px;\">\u6b21\u5143\u3068\u30c7\u30fc\u30bf\u6570\u3092\u78ba\u8a8d<\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 35.24904214559387%; height: 18px;\">dtype<\/td>\n<td style=\"width: 64.75095785440612%; height: 18px;\">\u578b\u306e\u78ba\u8a8d<\/td>\n<\/tr>\n<tr style=\"height: 18px;\">\n<td style=\"width: 35.24904214559387%; height: 18px;\">device<\/td>\n<td style=\"width: 64.75095785440612%; height: 18px;\">\u30c7\u30d0\u30a4\u30b9\u306e\u78ba\u8a8d<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4><span class=\"ez-toc-section\" id=\"%E5%BD%A2\"><\/span>\u5f62<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u5f62\u306f\u6b21\u5143\u6570\u3001\u30c7\u30fc\u30bf\u6570\u3067\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">print(x_data.shape)<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">torch.Size([2, 3])<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"%E5%9E%8B%E3%81%AE%E3%82%BF%E3%82%A4%E3%83%97\"><\/span>\u578b\u306e\u30bf\u30a4\u30d7<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u30c6\u30f3\u30bd\u30eb\u306e\u30c7\u30fc\u30bf\u306e\u578b\u306e\u30bf\u30a4\u30d7\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<p><strong><a href=\"https:\/\/pytorch.org\/docs\/stable\/tensor_attributes.html\" target=\"_blank\" rel=\"noopener\">&gt;&gt; pytorch\u578b\u306e\u4e00\u89a7<\/a><\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">print(x_data.dtype)<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">torch.int64<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"%E3%83%87%E3%83%90%E3%82%A4%E3%82%B9\"><\/span>\u30c7\u30d0\u30a4\u30b9<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u30c6\u30f3\u30bd\u30eb\u306b\u8a2d\u5b9a\u3055\u308c\u3066\u3044\u308b\u30c7\u30d0\u30a4\u30b9\u3092\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">print(x_data.device)<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">cpu<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E3%83%86%E3%83%B3%E3%82%BD%E3%83%AB%E3%81%8B%E3%82%89%E3%83%87%E3%83%BC%E3%82%BF%E3%82%92%E5%8F%96%E3%82%8A%E5%87%BA%E3%81%99\"><\/span>\u30c6\u30f3\u30bd\u30eb\u304b\u3089\u30c7\u30fc\u30bf\u3092\u53d6\u308a\u51fa\u3059<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>tensor\u306fnumpy\u3068\u540c\u3058\u3088\u3046\u306b\u5ea7\u6a19\u3092\u6307\u5b9a\u3057\u305f\u308a\u3001\u958b\u59cbNo:\u7d42\u4e86No\u3092\u4f7f\u3063\u3066\u7bc4\u56f2\u3092\u6307\u5b9a\u3057\u305f\u53d6\u5f97\u3082\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>numpy\u3068\u540c\u3058\u306a\u306e\u3067\u3001\u7c21\u5358\u306b\u3044\u304f\u3064\u304b\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<p>1\u304b\u30899\u306e3\u00d73\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">data = [[1, 2, 3],\r\n        [4, 5, 6],\r\n        [7, 8, 9]]\r\nx_data = torch.tensor(data)\r\n\r\nprint('\u5ea7\u6a19\u6307\u5b9a            ', x_data[1,2])\r\nprint('\u7bc4\u56f2\u6307\u5b9a(\u5168\u884c,2-3\u5217) ', x_data[:, 1:3])\r\nprint('\u7bc4\u56f2\u6307\u5b9a(1-2\u884c:\u5168\u5217) ', x_data[0:2, :])\r\nprint('\u7bc4\u56f2\u6307\u5b9a(1-2\u884c,2-3\u5217)', x_data[0:2, 1:3])<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">\u5ea7\u6a19\u6307\u5b9a             tensor(6)\r\n\u7bc4\u56f2\u6307\u5b9a(\u5168\u884c,2-3\u5217)  tensor([[2, 3],\r\n        [5, 6],\r\n        [8, 9]])\r\n\u7bc4\u56f2\u6307\u5b9a(1-2\u884c:\u5168\u5217)  tensor([[1, 2, 3],\r\n        [4, 5, 6]])\r\n\u7bc4\u56f2\u6307\u5b9a(1-2\u884c,2-3\u5217) tensor([[2, 3],\r\n        [5, 6]])<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"%E3%83%86%E3%83%B3%E3%82%BD%E3%83%AB%E3%81%AE%E8%A8%88%E7%AE%97\"><\/span>\u30c6\u30f3\u30bd\u30eb\u306e\u8a08\u7b97<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u6b21\u306f\u30c6\u30f3\u30bd\u30eb\u540c\u58eb\u306e\u8a08\u7b97\u3092\u7d39\u4ecb\u3057\u307e\u3059\u3002<\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E5%9B%9B%E5%89%87%E6%BC%94%E7%AE%97\"><\/span>\u56db\u5247\u6f14\u7b97<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u307e\u305a\u3001\u56db\u5247\u6f14\u7b97\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002<\/p>\n<p>\u4f7f\u3046\u306e\u306f\u6b21\u306e2\u3064\u306e\u30c7\u30fc\u30bf\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">data1 = [[2, 4, 6],\r\n         [8, 10, 12]]\r\nx_data1 = torch.tensor(data1)\r\ndata2 = [[1, 2, 3],\r\n         [4, 5, 6]]\r\nx_data2 = torch.tensor(data2)<\/pre>\n<p>\u8a08\u7b97\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">print('\u8db3\u3057\u7b97', x_data1+x_data2)\r\nprint('\u5f15\u304d\u7b97', x_data1-x_data2)\r\nprint('\u639b\u3051\u7b97', x_data1*x_data2)\r\nprint('\u5272\u308a\u7b97', x_data1\/\/x_data2)<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">\u8db3\u3057\u7b97 tensor([[ 3,  6,  9],\r\n        [12, 15, 18]])\r\n\u5f15\u304d\u7b97 tensor([[1, 2, 3],\r\n        [4, 5, 6]])\r\n\u639b\u3051\u7b97 tensor([[ 2,  8, 18],\r\n        [32, 50, 72]])\r\n\u5272\u308a\u7b97 tensor([[2, 2, 2],\r\n        [2, 2, 2]])<\/pre>\n<p>pytorch\u3067\u3082\u30d6\u30ed\u30fc\u30c9\u30ad\u30e3\u30b9\u30c8\u304c\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">print('\u8db3\u3057\u7b97+\u30d6\u30ed\u30fc\u30c9\u30ad\u30e3\u30b9\u30c8', x_data1+2)<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">\u8db3\u3057\u7b97+\u30d6\u30ed\u30fc\u30c9\u30ad\u30e3\u30b9\u30c8 tensor([[ 4,  6,  8],\r\n        [10, 12, 14]])<\/pre>\n<p>\u8db3\u3057\u305f\u306e\u306f2\u30921\u3064\u3060\u3051\u3067\u3059\u304c\u3001x_data\u306e\u5168\u3066\u306b\u8db3\u3055\u308c\u307e\u3059\u3002<\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E8%A1%8C%E5%88%97%E3%81%AE%E7%A9%8D\"><\/span>\u884c\u5217\u306e\u7a4d<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u884c\u5217\u306e\u7a4d\u306b\u306f<strong>matmul<\/strong>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u6b21\u306e2\u00d73\u30683\u00d72\u306e\u7a4d\u3092\u8a08\u7b97\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">data1 = [[2, 4, 6],\r\n         [8, 10, 12]]\r\nx_data1 = torch.tensor(data1)\r\ndata2 = [[1, 2], \r\n         [3, 4],\r\n         [5, 6]]\r\nx_data2 = torch.tensor(data2)\r\nprint(torch.matmul(x_data1, x_data2))<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">tensor([[ 44,  56],\r\n        [ 98, 128]])<\/pre>\n<h4><span class=\"ez-toc-section\" id=\"%E8%BB%A2%E7%BD%AE\"><\/span>\u8ee2\u7f6e<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u30c6\u30f3\u30bd\u30eb\u306e\u8ee2\u7f6e\u306f<strong>t<\/strong>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">data1 = [[2, 4, 6],\r\n         [8, 10, 12]]\r\nx_data1 = torch.tensor(data1)\r\nprint(x_data1.t())<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">tensor([[ 2,  8],\r\n        [ 4, 10],\r\n        [ 6, 12]])<\/pre>\n<p>\u8ee2\u7f6e\u306fWiki\u306b\u308f\u304b\u308a\u3084\u3059\u3044\u56f3\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p><strong><a href=\"https:\/\/ja.wikipedia.org\/wiki\/\u8ee2\u7f6e\u884c\u5217\" target=\"_blank\" rel=\"noopener\">&gt;&gt; \u8ee2\u7f6e\u306b\u3064\u3044\u3066<\/a><\/strong><\/p>\n<h4><span class=\"ez-toc-section\" id=\"%E6%AC%A1%E5%85%83%E3%83%87%E3%83%BC%E3%82%BF%E6%95%B0%E3%81%AE%E5%85%A5%E3%82%8C%E6%9B%BF%E3%81%88\"><\/span>\u6b21\u5143\u30c7\u30fc\u30bf\u6570\u306e\u5165\u308c\u66ff\u3048<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>\u753b\u50cf\u30c7\u30fc\u30bf\u3092\u53d6\u308a\u6271\u3046\u3068\u3001\u9ad8\u3055\u00d7\u5e45\u00d7ch\u306e3\u6b21\u5143\u3067\u53d6\u5f97\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3092ch\u00d7\u9ad8\u3055\u00d7\u5e45\u3068\u5165\u308c\u66ff\u308b\u6642\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u6642\u306f<strong>transpose(\u6b21\u5143No<\/strong><strong>, \u6b21\u5143No)<\/strong>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<p>\u6b21\u5143No\u306b\u306f\u3001\u5165\u308c\u66ff\u3048\u308b\u6b21\u5143\u30922\u3064\u5165\u308c\u307e\u3059\u3002<\/p>\n<p>64\u00d732\u00d73\u306e\u30c7\u30fc\u30bf\u3067\u78ba\u8a8d\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" title=\"In\">x_data = torch.rand(64, 32, 3)\r\nprint('\u5909\u66f4\u524d', x_data.shape)\r\n\r\n# 1\u6b21\u5143\u30683\u6b21\u5143\u76ee\u3092\u5165\u308c\u66ff\u3048\r\nx_data = x_data.transpose(0, 2)\r\nprint('\u5909\u66f4\u5f8c1', x_data.shape)\r\n# 2\u6b21\u5143\u30683\u6b21\u5143\u76ee\u3092\u5165\u308c\u66ff\u3048\r\nx_data = x_data.transpose(1, 2)\r\nprint('\u5909\u66f4\u5f8c2', x_data.shape)<\/pre>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\" title=\"Out\">\u5909\u66f4\u524d torch.Size([64, 32, 3])\r\n\u5909\u66f4\u5f8c1 torch.Size([3, 32, 64])\r\n\u5909\u66f4\u5f8c2 torch.Size([3, 64, 32])<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"%E3%81%BE%E3%81%A8%E3%82%81\"><\/span>\u307e\u3068\u3081<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u4ee5\u4e0a\u304cpytorch\u306e\u4f7f\u3044\u65b9\u3067\u3059\u3002<\/p>\n<p>numpy\u3068\u307b\u3068\u3093\u3069\u540c\u3058\u3060\u3063\u305f\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u6700\u521d\u306e\u65b9\u306f\u3001pytorch\u306e\u30e9\u30a4\u30d6\u30e9\u30ea\u306b\u5165\u3063\u3066\u3044\u308b\u30e2\u30c7\u30eb\u3092\u305d\u306e\u307e\u307e\u4f7f\u3063\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<p>\u6700\u521d\u306e\u6559\u5e2b\u30c7\u30fc\u30bf\u306e\u8aad\u307f\u8fbc\u307f\u3068\u63a8\u8ad6\u7d50\u679c\u306e\u51e6\u7406\u3092\u3059\u308b\u3053\u3068\u304c\u591a\u3044\u306e\u3067\u3001\u3053\u3053\u3089\u8fba\u306e\u77e5\u8b58\u3092\u3057\u3063\u304b\u308a\u6291\u3048\u3066\u304a\u304f\u3068\u3088\u3044\u3067\u3059\u3002<\/p>\n<blockquote class=\"wp-embedded-content\" data-secret=\"HTbAx3PI0r\"><p><a href=\"https:\/\/www.tetsumag.com\/2021\/11\/05\/104\/\">numpy\u306e\u4f7f\u3044\u65b9  \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u304b\u3089reshape,array\u306e\u4f7f\u3044\u65b9\u307e\u3067<\/a><\/p><\/blockquote>\n<p><iframe class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;numpy\u306e\u4f7f\u3044\u65b9  \u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u304b\u3089reshape,array\u306e\u4f7f\u3044\u65b9\u307e\u3067&#8221; &#8212; \u3066\u3064\u307e\u3050\" src=\"https:\/\/www.tetsumag.com\/2021\/11\/05\/104\/embed\/#?secret=hdURePJ5fK#?secret=HTbAx3PI0r\" data-secret=\"HTbAx3PI0r\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u56de\u306fpytorch\u306b\u3064\u3044\u3066\u3001\u7fd2\u5f97\u3092\u3057\u3066\u3044\u304d\u307e\u3059\u3002 pytorch\u306f\u753b\u50cf\u3084\u81ea\u7136\u8a00\u8a9e\u306a\u3069\u306e\u6a5f\u68b0\u5b66\u7fd2\u958b\u767a\u304c\u3067\u304d\u308b\u30e9\u30a4\u30d6\u30e9\u30ea\u3067\u3059\u3002 \u4ed6\u306b\u3082\u3001Tensorflow\u3001Keras\u306a\u3069\u304c\u3042\u308a\u307e\u3059\u304c\u3001\u6700\u8fd1\u306fpytorch\u304c\u4eba\u6c17\u3067\u3059\u3002 \u672c &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/tetsumag.com\/blog\/2021\/11\/06\/92\/\"> <span class=\"screen-reader-text\">\u3010Pytorch\u3011\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u65b9\u6cd5\u306b\u3064\u3044\u3066<\/span> \u3082\u3063\u3068\u8aad\u3080 &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":12875,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","_themeisle_gutenberg_block_has_review":false},"categories":[127],"tags":[95],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/tetsumag.com\/blog\/wp-json\/wp\/v2\/posts\/5343"}],"collection":[{"href":"https:\/\/tetsumag.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tetsumag.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tetsumag.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tetsumag.com\/blog\/wp-json\/wp\/v2\/comments?post=5343"}],"version-history":[{"count":3,"href":"https:\/\/tetsumag.com\/blog\/wp-json\/wp\/v2\/posts\/5343\/revisions"}],"predecessor-version":[{"id":12874,"href":"https:\/\/tetsumag.com\/blog\/wp-json\/wp\/v2\/posts\/5343\/revisions\/12874"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tetsumag.com\/blog\/wp-json\/wp\/v2\/media\/12875"}],"wp:attachment":[{"href":"https:\/\/tetsumag.com\/blog\/wp-json\/wp\/v2\/media?parent=5343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tetsumag.com\/blog\/wp-json\/wp\/v2\/categories?post=5343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tetsumag.com\/blog\/wp-json\/wp\/v2\/tags?post=5343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}