{"id":285,"date":"2024-08-31T23:37:47","date_gmt":"2024-08-31T14:37:47","guid":{"rendered":"http:\/\/kkaneko-lab.com\/?p=285"},"modified":"2024-09-03T20:28:08","modified_gmt":"2024-09-03T11:28:08","slug":"c%e3%81%a7%e6%a9%9f%e6%a2%b0%e5%ad%a6%e7%bf%92torchsharp%e3%80%80%e6%89%8b%e6%9b%b8%e3%81%8d%e6%96%87%e5%ad%97%e8%aa%8d%e8%ad%98%e3%82%a2%e3%83%97%e3%83%aa%e3%81%ae%e5%ae%9f%e8%a3%85","status":"publish","type":"post","link":"https:\/\/kkaneko-lab.com\/?p=285","title":{"rendered":"\u3010TorchSharp\u3011C#\u3067\u6a5f\u68b0\u5b66\u7fd2\u3000\u624b\u66f8\u304d\u6587\u5b57\u8a8d\u8b58\u30a2\u30d7\u30ea\u306e\u5b9f\u88c5\uff08\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u4ed8\u304d\uff09"},"content":{"rendered":"\n<p>\u6a5f\u68b0\u5b66\u7fd2\u306b\u95a2\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\uff0c\u57fa\u672c\u7684\u306bpython\u306b\u3088\u308a\u5b9f\u88c5\u3055\u308c\u307e\u3059\uff0e\u3057\u304b\u3057C#(.Net)\u3067\u4f5c\u6210\u6e08\u307f\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u6a5f\u68b0\u5b66\u7fd2\u306e\u6a5f\u80fd\u3092\u8ffd\u52a0\u3057\u305f\u3044\u5834\u5408\u3084\uff0cGUI\u3082\u4f5c\u6210\u3057\u305f\u3044\u5834\u5408\u306a\u3069\uff0cC#(.Net)\u3067\u6a5f\u68b0\u5b66\u7fd2\u306e\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u306a\u3051\u308c\u3070\u306a\u3089\u306a\u3044\u72b6\u6cc1\u3082\u3042\u308b\u3068\u601d\u3044\u307e\u3059\uff0eC#\u7528\u306e\u6a5f\u68b0\u5b66\u7fd2\u30e9\u30a4\u30d6\u30e9\u30ea\u306f\u3044\u304f\u3064\u304b\u3042\u308a\u307e\u3059\u304c\uff0c.NET Foundation\u306b\u7d44\u307f\u8fbc\u307e\u308c\u3066\u3044\u308bTorchSharp\u304c\u7121\u96e3\u306a\u9078\u629e\u80a2\u3060\u3068\u601d\u3044\u307e\u3059\uff0ePytorch\u30d9\u30fc\u30b9\u306a\u306e\u3067\uff0cPytorch\u306b\u7cbe\u901a\u3057\u3066\u3044\u308b\u65b9\u306fTorchSharp\u3082\u4f7f\u3044\u3053\u306a\u305b\u308b\u3068\u601d\u3044\u307e\u3059\uff0e\u3057\u304b\u3057\uff0cTorchSharp\u306b\u95a2\u3059\u308b\u60c5\u5831\u304c\u5c11\u306a\u304b\u3063\u305f\u305f\u3081\uff08Qiita\u3067\u306f2\u4ef6\u306e\u307f\uff09\uff0c\u672c\u7a3f\u3067\u306f<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">TorchSharp + C#(.Net 8.0)\u306b\u3088\u308b\u6559\u5e2b\u3042\u308a\u5b66\u7fd2\u306e\u5b9f\u88c5\u4f8b\u3068\u3057\u3066\uff0c\u624b\u66f8\u304d\u6570\u5b57\u306e\u30af\u30e9\u30b9\u5206\u985e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092GUI\u4ed8\u3067\u4f5c\u6210<\/mark><\/strong>\u3057\u307e\u3059\uff0e<\/p>\n\n\n\n<p>\u6559\u5e2b\u30c7\u30fc\u30bf\u306e\u5b66\u7fd2\u306b\u52a0\u3048\u3066\uff0c\u4ee5\u4e0b\u306e\u3088\u3046\u306bGUI\u306b\u30ad\u30e3\u30f3\u30d0\u30b9\u3092\u8a2d\u7f6e\u3057\u3066\uff0c\u63cf\u304b\u308c\u305f\u6570\u5b57\u3092\u8a8d\u8b58\u3059\u308b\u6a5f\u80fd\u3092\u6301\u3064\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u307e\u3059\uff0e\u5b9f\u969b\u306b\u4f5c\u6210\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306f\u4ee5\u4e0b\u304b\u3089\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u53ef\u80fd\u3067\u3059\uff0e<\/p>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\uff1a <a href=\"https:\/\/github.com\/kkaneko1090\/TorchSharpSupervisedLearning\">https:\/\/github.com\/kkaneko1090\/TorchSharpSupervisedLearning<\/a><\/mark><\/strong><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"295\" height=\"493\" src=\"https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-3.png\" alt=\"\" class=\"wp-image-306\" srcset=\"https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-3.png 295w, https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-3-180x300.png 180w\" sizes=\"auto, (max-width: 295px) 100vw, 295px\" \/><\/figure>\n<\/div>\n\n\n<p>\u5b66\u7fd2\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306f\uff0c\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u5185\u306eTrainningData\u30d5\u30a9\u30eb\u30c0\u306b\u683c\u7d0d\u3057\u3066\u3044\u307e\u3059\uff0e\u624b\u66f8\u304d\u306e\u201d0\u201d\uff0c\u201d1\u201d\uff0c\u201d2\u201d\u306e3\u7a2e\u985e\u309221\u679a\u305a\u3064\u7528\u610f\u3057\u305f\u306e\u3067\uff0c\u4eca\u56de\u306f3\u30af\u30e9\u30b9\u306e\u5206\u985e\u3068\u306a\u308a\u307e\u3059\uff0e\u8a73\u3057\u304f\u306fTrainningData\u30d5\u30a9\u30eb\u30c0\u306e\u4e2d\u8eab\u3092\u898b\u3066\u304f\u3060\u3055\u3044\uff0e<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"335\" height=\"128\" src=\"https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-5.png\" alt=\"\" class=\"wp-image-333\" style=\"width:396px;height:auto\" srcset=\"https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-5.png 335w, https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-5-300x115.png 300w\" sizes=\"auto, (max-width: 335px) 100vw, 335px\" \/><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306e\u4f5c\u6210<\/strong><\/h2>\n\n\n\n<p>\u4eca\u56de\u306fWPF\u3067GUI\u3082\u4f5c\u6210\u3057\u305f\u3044\u306e\u3067\uff0c.Net WPF\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u9078\u629e\u3057\u307e\u3059\uff0e<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1014\" height=\"675\" src=\"https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image.png\" alt=\"\" class=\"wp-image-299\" srcset=\"https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image.png 1014w, https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-300x200.png 300w, https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-768x511.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>\u73fe\u6642\u70b9\u3067\u306e\u6700\u65b0\u7248\u306eTorchSharp(v0.103)\u306f.Net 6.0 \u3092\u5bfe\u8c61\u3068\u3057\u3066\u3044\u307e\u3059\u304c\uff0c.Net 8.0\u3067\u3082\u52d5\u4f5c\u3059\u308b\u3053\u3068\u3092\u78ba\u8a8d\u3057\u3066\u3044\u308b\u305f\u3081\uff0c\u4eca\u56de\u306f.Net8.0\u3067\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u3092\u4f5c\u6210\u3057\u307e\u3059\uff0e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2.\u3000\u30d1\u30c3\u30b1\u30fc\u30b8\u306e\u6e96\u5099<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"250\" src=\"https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-2.png\" alt=\"\" class=\"wp-image-303\" srcset=\"https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-2.png 750w, https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-2-300x100.png 300w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/figure>\n\n\n\n<p>\u6700\u65b0\u306eTorchSharp\u3092Nuget\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\uff0eGPU\u3092\u4f7f\u3063\u3066\u5b66\u7fd2\u3092\u884c\u3044\u305f\u3044\u306e\u3067\uff0c\u540c\u3058\u30d0\u30fc\u30b8\u30e7\u30f3\u306eTorchSharp-cuda-windows\u3082\u540c\u69d8\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u3059\uff0e\u307e\u305f\uff0c\u672c\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u306fBitmap\u3092\u6271\u3046\u305f\u3081\uff0cSystem.Drawing.Common\u3082\u8ffd\u52a0\u3057\u307e\u3057\u305f\uff0e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3.\u3000\u6a5f\u68b0\u5b66\u7fd2\u30e2\u30c7\u30eb\u306e\u5b9f\u88c5<\/strong><\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\uff0c\u6a5f\u68b0\u5b66\u7fd2\u30e2\u30c7\u30eb\u306e\u30af\u30e9\u30b9 \u201dMLModel\u201d \u3092\u5b9f\u88c5\u3057\u307e\u3057\u305f\uff0e\u30e1\u30f3\u30d0\u5909\u6570\u3068\u3057\u3066\u30e2\u30c7\u30eb\u3092\u69cb\u6210\u3059\u308b\u5404\u5c64\u3092\u30e1\u30f3\u30d0\u5909\u6570\u3068\u3057\u3066\u8a18\u8ff0\u3057\u307e\u3059\uff0e\u5404\u5c64\u306e\u6b21\u5143\u306f\uff0c\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u3067\u521d\u671f\u5316\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\uff0e\u5168\u7d50\u5408\u5c64\u306e\u5165\u529b\u6b21\u5143\u3092\u628a\u63e1\u3059\u308b\u305f\u3081\u306b\uff0c\u30c0\u30df\u30fc\u306e\u5165\u529b\u30c7\u30fc\u30bf\u3092\u7528\u3044\u3066\uff0c\u7573\u307f\u8fbc\u307f\u5c64\u306e\u51fa\u529b\u201ddammyConvOutput\u201d\u3092\u8a08\u7b97\u3057\u3066\u3044\u307e\u3059\uff0e<\/p>\n\n\n\n<p>if (torch.cuda.is_available()) _device = CUDA;\u3067\u306f\uff0cGPU\u304c\u4f7f\u7528\u53ef\u80fd\u304b\u3092\u5224\u5b9a\u3057\uff0c\u4f7f\u7528\u3067\u304d\u308b\u5834\u5408\u306fGPU\u3067\u5b66\u7fd2\u3092\u5b9f\u884c\u3059\u308b\u3088\u3046\u306b\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\u3057\u307e\u3057\u305f\uff0e_device = CUDA\u3067\u3042\u308c\u3070\uff0cthis.to(_device);\u3067\u30e2\u30c7\u30eb\u304cGPU\u306b\u8ee2\u9001\u3055\u308c\u307e\u3059\uff0e<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>using TorchSharp;\nusing TorchSharp.Modules;\nusing static TorchSharp.torch;\nusing static TorchSharp.torch.nn;\nusing static TorchSharp.torch.nn.functional;\n\nnamespace TorchSharpSupervisedLearning\n{\n    public class MLModel : Module&lt;Tensor, Tensor&gt;\n    {\n        #region \u30e1\u30f3\u30d0\u5909\u6570\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u7573\u307f\u8fbc\u307f\u5c641\n        \/\/\/ &lt;\/summary&gt;\n        private Conv2d _conv1;\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u7573\u307f\u8fbc\u307f\u5c642\n        \/\/\/ &lt;\/summary&gt;\n        private Conv2d _conv2;\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u5168\u7d50\u5408\u5c641\n        \/\/\/ &lt;\/summary&gt;\n        private Linear _linear1;\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u5168\u7d50\u5408\u5c642\n        \/\/\/ &lt;\/summary&gt;\n        private Linear _linear2;\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u96a0\u308c\u5c64\u306e\u30b5\u30a4\u30ba\n        \/\/\/ &lt;\/summary&gt;\n        private int _hiddenLayerSize = 32;\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u30c7\u30d0\u30a4\u30b9\uff08CPU or GPU\uff09\n        \/\/\/ &lt;\/summary&gt;\n        private Device _device = CPU;\n        #endregion\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=&quot;inputSize&quot;&gt;\u5165\u529b\u3059\u308b\u753b\u50cf\u306e\u30b5\u30a4\u30ba&lt;\/param&gt;\n        \/\/\/ &lt;param name=&quot;outputSize&quot;&gt;\u51fa\u529b\u3059\u308b\u30d9\u30af\u30c8\u30eb\u306e\u30b5\u30a4\u30ba&lt;\/param&gt;\n        public MLModel(int[] inputSize, int outputSize) : base(&quot;CNN&quot;)\n        {\n            \/\/\u30c0\u30df\u30fc\u306e\u5165\u529b\u30c7\u30fc\u30bf\u3092\u4f5c\u6210\uff08\u5404\u5c64\u306e\u6b21\u5143\u306e\u521d\u671f\u5316\u306b\u4f7f\u7528\uff09\n            Tensor dammyInput = zeros([inputSize[0], inputSize[1]]).unsqueeze(0).unsqueeze(0);\n            \/\/\u7573\u307f\u8fbc\u307f\u5c64\u306e\u521d\u671f\u5316\n            _conv1 = Conv2d(in_channels: 1, out_channels: 16, kernelSize: 8, stride: 2);\n            _conv2 = Conv2d(in_channels: 16, out_channels: 16, kernelSize: 8, stride: 2);\n            \/\/\u30c0\u30df\u30fc\u306e\u7573\u307f\u8fbc\u307f\u5c64\u306e\u51fa\u529b\n            Tensor dammyConvOutput = _conv1.forward(dammyInput); \/\/\u7573\u307f\u8fbc\u307f\u5c641\n            dammyConvOutput = _conv2.forward(dammyConvOutput); \/\/\u7573\u307f\u8fbc\u307f\u5c642\n            dammyConvOutput = flatten(dammyConvOutput, start_dim: 1); \/\/\u5e73\u6ed1\u5316\n            \/\/\u5168\u7d50\u5408\u5c64\u306e\u521d\u671f\u5316\n            _linear1 = Linear(inputSize: dammyConvOutput.shape[1], outputSize: _hiddenLayerSize);\n            _linear2 = Linear(inputSize: _hiddenLayerSize, outputSize: outputSize);\n\n            \/\/\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u306e\u767b\u9332\n            RegisterComponents();\n\n            \/\/GPU\u3092\u4f7f\u7528\u3067\u304d\u308b\u304b\n            if (torch.cuda.is_available()) _device = CUDA; \/\/GPU\u3092\u6d3b\u7528\n            \/\/\u30c7\u30d0\u30a4\u30b9\u306b\u8ee2\u9001\n            this.to(_device); \n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u9806\u4f1d\u64ad\u51e6\u7406\u306e\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=&quot;input&quot;&gt;\u5165\u529b\u30c7\u30fc\u30bf&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        public override Tensor forward(Tensor input)\n        {\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\/\/\u5f8c\u8ff0\u3059\u308b\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u30d0\u30c3\u30c1\u5b66\u7fd2\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=&quot;dataset&quot;&gt;\u6559\u5e2b\u30c7\u30fc\u30bf\u306e\u30ea\u30b9\u30c8&lt;\/param&gt;\n        \/\/\/ &lt;param name=&quot;epochCount&quot;&gt;\u30a8\u30dd\u30c3\u30af\u6570&lt;\/param&gt;\n        \/\/\/ &lt;param name=&quot;batchSize&quot;&gt;\u30d0\u30c3\u30c1\u30b5\u30a4\u30ba&lt;\/param&gt;\n        public void TrainOnBatch(List&lt;(Tensor input, Tensor output)&gt; dataset, int epochCount, int batchSize)\n        {\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\/\/\u5f8c\u8ff0\u3059\u308b\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u63a8\u8ad6\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=&quot;input&quot;&gt;\u5358\u4e00\u306e\u5165\u529b\u30c7\u30fc\u30bf&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        public (int index, float probability) Predict(Tensor input) \n        {\n            \u3000\u3000\/\/\u5f8c\u8ff0\u3059\u308b\n        }\n    }\n}\n<\/code><\/pre><\/div>\n\n\n\n<p>\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306b\u52a0\u3048\u3066\uff0c\u9806\u4f1d\u64ad\u306e\u95a2\u6570\u201dforward\u201d\uff0c\u30d0\u30c3\u30c1\u5b66\u7fd2\u3092\u884c\u3046\u201dTrainOnBatch\u201d\uff0c\u5b66\u7fd2\u5f8c\u306b\u63a8\u8ad6\u3092\u884c\u3046\u305f\u3081\u306e\u201dPredict\u201d\u306e3\u3064\u306e\u95a2\u6570\u3082\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\uff0e\u305d\u308c\u305e\u308c\u306e\u95a2\u6570\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\uff0e<\/p>\n\n\n\n<p>\u201dforward\u201d\u306f\u89aa\u30af\u30e9\u30b9\u306e\u95a2\u6570\u3092override\u3057\u3066\u3044\u307e\u3059\uff0e\u5358\u7d14\u306b\u5404\u5c64\u306e\u51fa\u529b\u3092\u9806\u756a\u306b\u8a08\u7b97\u3057\u3066\u3044\u304f\u3060\u3051\u3067\u3059\uff0e\u4eca\u56de\u306f\u30af\u30e9\u30b9\u5206\u985e\u3092\u884c\u3046\u306e\u3067\uff0c\u51fa\u529b\u5c64\u306e\u6d3b\u6027\u5316\u95a2\u6570\u306fSoftmax\u3068\u3057\u3066\u304a\u308a\u307e\u3059\uff0e<\/p>\n\n\n\n<p>\u201dTrainOnBatch\u201d\u3067\u306f\u5b66\u7fd2\u30c7\u30fc\u30bf\u3092\u30d0\u30c3\u30c1\u306b\u5206\u5272\u3057\u3066\uff0c\u307e\u305avar predicted = this.forward(input);\u3067\u9806\u4f1d\u64ad\u3057\u307e\u3059\uff0e\u305d\u306e\u5f8c\u6559\u5e2b\u30c7\u30fc\u30bf\u3068\u306e\u5dee\u5206\u3092var error = loss.forward(predicted, output);\u3067\u8a08\u7b97\u3059\u308b\u3053\u3068\u3067\uff0c\u8a08\u7b97\u30b0\u30e9\u30d5\u304c\u69cb\u7bc9\u3055\u308c\u308b\u306e\u3067\uff0cerror.backward();\u3067\u30b0\u30e9\u30d5\u3092\u8fbf\u3063\u3066\u9006\u4f1d\u64ad\u3057\u307e\u3059\uff0e<\/p>\n\n\n\n<p>\u201dPredict\u201d\u306f\u5b66\u7fd2\u5f8c\u306b\u547c\u3073\u51fa\u3059\u63a8\u8ad6\u7528\u306e\u95a2\u6570\u3067\u3059\uff0eTensor\u5316\u3057\u305f\u753b\u50cf\u304c\u3069\u306e\u30af\u30e9\u30b9\u306b\u5206\u985e\u3055\u308c\u308b\u304b\u4e88\u6e2c\u3057\u307e\u3059\uff0e\u623b\u308a\u5024\u306f\u4e88\u6e2c\u3055\u308c\u308b\u30af\u30e9\u30b9\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3068\u78ba\u7387\u3067\u3059\uff0e<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-csharp\" data-lang=\"C#\"><code>        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u9806\u4f1d\u64ad\u51e6\u7406\u306e\u30aa\u30fc\u30d0\u30fc\u30e9\u30a4\u30c9\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=&quot;input&quot;&gt;\u5165\u529b\u30c7\u30fc\u30bf&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        public override Tensor forward(Tensor input)\n        {\n            \/\/\u7573\u307f\u8fbc\u307f\n            var x = relu(_conv1.forward(input));\u3000\/\/\u6d3b\u6027\u5316\u95a2\u6570\u306fReLU\u3092\u4f7f\u7528\n            x = relu(_conv2.forward(x));\n            \/\/\u5e73\u5766\u5316\n            x = torch.flatten(x, start_dim: 1);\n            \/\/\u5168\u7d50\u5408\u5c64\n            x = relu(_linear1.forward(x));\n            x = softmax(_linear2.forward(x), dim:1); \/\/\u30af\u30e9\u30b9\u5206\u985e\u306e\u305f\u3081Softmax\u95a2\u6570\n            return x;\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u30d0\u30c3\u30c1\u5b66\u7fd2\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=&quot;dataset&quot;&gt;\u6559\u5e2b\u30c7\u30fc\u30bf\u306e\u30ea\u30b9\u30c8&lt;\/param&gt;\n        \/\/\/ &lt;param name=&quot;epochCount&quot;&gt;\u30a8\u30dd\u30c3\u30af\u6570&lt;\/param&gt;\n        \/\/\/ &lt;param name=&quot;batchSize&quot;&gt;\u30d0\u30c3\u30c1\u30b5\u30a4\u30ba&lt;\/param&gt;\n        public void TrainOnBatch(List&lt;(Tensor input, Tensor output)&gt; dataset, int epochCount, int batchSize)\n        {\n            \/\/\u30aa\u30d7\u30c6\u30a3\u30de\u30b6\u306e\u521d\u671f\u5316\n            var optimizer = optim.Adam(parameters: this.parameters(), lr: 0.001); \/\/\u5b66\u7fd2\u7387\u30920.001\u306b\u8a2d\u5b9a\n            \/\/\u640d\u5931\u95a2\u6570\n            var loss = CrossEntropyLoss();\n\n            \/\/\u30a8\u30dd\u30c3\u30af\u6570\u3060\u3051\u7e70\u308a\u8fd4\u3057\n            for (int epoch = 0; epoch &lt; epochCount; epoch++)\n            {\n                \/\/\u30d0\u30c3\u30c1\u53d6\u308a\u51fa\u3057\n                var batcheArray = Utility.GetBatch(dataset, batchSize);\n\n                \/\/\u30d0\u30c3\u30c1\u306e\u7e70\u308a\u8fd4\u3057\n                for (int batch = 0; batch &lt; batcheArray.Length; batch++)\n                {\n                    \/\/\u5165\u529b\u30c7\u30fc\u30bf\n                    var input = batcheArray[batch].input.to(_device);\n                    \/\/\u51fa\u529b\u30c7\u30fc\u30bf\n                    var output = batcheArray[batch].output.to(_device);\n                    \n                    \/\/\u30aa\u30d7\u30c6\u30a3\u30de\u30a4\u30b6\u306e\u52fe\u914d\u3092\u521d\u671f\u5316\n                    optimizer.zero_grad();\n                    \/\/\u63a8\u8ad6\n                    var predicted = this.forward(input);\n                    \/\/\u6b8b\u5dee\n                    var error = loss.forward(predicted, output);\n                    \/\/\u9006\u4f1d\u64ad\n                    error.backward();\n                    optimizer.step();\n\n                    Console.WriteLine(error.ToSingle());\n                }\n\n                \/\/\u30e1\u30e2\u30ea\u89e3\u653e\n                GC.Collect();\n            }\n        }\n\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ \u63a8\u8ad6\n        \/\/\/ &lt;\/summary&gt;\n        \/\/\/ &lt;param name=&quot;input&quot;&gt;\u5358\u4e00\u306e\u5165\u529b\u30c7\u30fc\u30bf&lt;\/param&gt;\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\n        public (int index, float probability) Predict(Tensor input) \n        {\n            \/\/\u9806\u4f1d\u64ad\n            Tensor output =  this.forward(input.unsqueeze(0).to(_device)).squeeze(0);\n            \/\/\u914d\u5217\u306b\u5909\u63db\n            float[] array = new float[output.shape[0]];\n            for(int i = 0; i &lt; array.Length; i++) array[i] = output[i].ToSingle();\n            \/\/\u6700\u5927\u5024\u3092\u3068\u308b\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u3092\u53d6\u5f97\n            int maxIndex = Array.IndexOf(array, array.Max());\n            return (maxIndex, array[maxIndex]);\n        }<\/code><\/pre><\/div>\n\n\n\n<p>TorchSharp\u306e\u201dDataLoader\u201d\u3092\u4f7f\u3048\u3070\u30d0\u30c3\u30c1\u5b66\u7fd2\u3092\u3088\u308a\u30b9\u30de\u30fc\u30c8\u306b\u5b9f\u88c5\u3067\u304d\u305d\u3046\u3067\u3059\u304c\uff0c\u4eca\u56de\u306fUtility.cs\u306b\u30d0\u30c3\u30c1\u751f\u6210\u7528\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u88c5\u3057\u307e\u3057\u305f\uff0eUtility.cs\u306b\u306f\uff0cBitmap\u2192Tensor\u306e\u5909\u63db\u306a\u3069\u3082\u8a18\u8f09\u3057\u3066\u304a\u308a\u307e\u3059\u304c\uff0c\u8a73\u7d30\u306fGitHub\u306b\u516c\u958b\u3057\u305f\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u3092\u53c2\u7167\u3057\u3066\u304f\u3060\u3055\u3044\uff0e<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4.\u3000GUI\u306e\u4f5c\u6210<\/strong><\/h2>\n\n\n\n<p>\u624b\u66f8\u304d\u306e\u6570\u5b57\u306b\u3064\u3044\u3066\uff0c\u5b66\u7fd2\u3068\u63a8\u8ad6\u306e\u4e21\u65b9\u306e\u6a5f\u80fd\u3092\u5099\u3048\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u3092\u4f5c\u6210\u3057\u305f\u3044\u306e\u3067\uff0c\u4ee5\u4e0b\u306e\u3088\u3046\u306aGUI\u3092\u4f5c\u6210\u3057\u307e\u3057\u305f\uff0e<\/p>\n\n\n\n<p>\u307e\u305a\u5b66\u7fd2\u306e\u305f\u3081\u306b\u201dTrain\u201d\u30dc\u30bf\u30f3\u3092\u914d\u7f6e\u3057\u307e\u3057\u305f\uff0e\u3053\u306e\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u30d5\u30a9\u30eb\u30c0\u9078\u629e\u30c0\u30a4\u30a2\u30ed\u30b0\u304c\u7acb\u3061\u4e0a\u304c\u308b\u306e\u3067\uff0c\u5b66\u7fd2\u7528\u306e\u753b\u50cf\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\uff08\u30bd\u30fc\u30b9\u30b3\u30fc\u30c9\u306b\u4ed8\u5c5e\u306eTrainningData\u30d5\u30a9\u30eb\u30c0\uff09\u3092\u9078\u629e\u3059\u308b\u3068\u5b66\u7fd2\u304c\u958b\u59cb\u3057\u307e\u3059\uff0e<\/p>\n\n\n\n<p>GUI\u306e\u4e0a\u90e8\u306e\u767d\u3044\u6b63\u65b9\u5f62\u9818\u57df\u306fInkCanvas\u3067\uff0c\u3053\u3053\u306b\u30de\u30a6\u30b9\u3067\u30c9\u30e9\u30c3\u30b0\u3057\u3066\u6570\u5b57\u3092\u63cf\u304d\uff0c\u201dPredict\u201d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068GUI\u4e0a\u306b\u63a8\u8ad6\u7d50\u679c\u304c\u8868\u793a\u3055\u308c\u307e\u3059\uff0e\u307e\u305f\u201dReset\u201d\u30dc\u30bf\u30f3\u3067InkCanvas\u3092\u767d\u7d19\u306b\u623b\u305b\u307e\u3059\uff0e<\/p>\n\n\n\n<p>\u5b66\u7fd2\u4e2d\u306eLoss\u306e\u63a8\u79fb\u306f\uff0cGUI\u3068\u540c\u6642\u306b\u7acb\u3061\u4e0a\u304c\u308b\u30b3\u30f3\u30bd\u30fc\u30eb\u30a6\u30a3\u30f3\u30c9\u30a6\u306b\u8868\u793a\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3057\u305f\uff0e<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"875\" height=\"506\" src=\"https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-4.png\" alt=\"\" class=\"wp-image-310\" srcset=\"https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-4.png 875w, https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-4-300x173.png 300w, https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/image-4-768x444.png 768w\" sizes=\"auto, (max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/figure>\n\n\n\n<p>XAML\u306f\u4ee5\u4e0b\u306e\u901a\u308a\uff0e<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-html\" data-lang=\"HTML\"><code>&lt;Window x:Class=&quot;TorchSharpSupervisedLearning.MainWindow&quot;\n        xmlns=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml\/presentation&quot;\n        xmlns:x=&quot;http:\/\/schemas.microsoft.com\/winfx\/2006\/xaml&quot;\n        xmlns:d=&quot;http:\/\/schemas.microsoft.com\/expression\/blend\/2008&quot;\n        xmlns:mc=&quot;http:\/\/schemas.openxmlformats.org\/markup-compatibility\/2006&quot;\n        xmlns:local=&quot;clr-namespace:TorchSharpSupervisedLearning&quot;\n        mc:Ignorable=&quot;d&quot;\n        Title=&quot;MainWindow&quot; Height=&quot;500&quot; Width=&quot;309&quot;\n        Background=&quot;Gray&quot;&gt;\n    &lt;Grid&gt;\n        &lt;StackPanel Orientation=&quot;Vertical&quot;&gt;\n            &lt;Canvas Background=&quot;Transparent&quot; Margin=&quot;10&quot; Width=&quot;257&quot; Height=&quot;257&quot;&gt;\n                &lt;InkCanvas x:Name=&quot;cnvDrawingArea&quot; Width=&quot;256&quot; Height=&quot;256&quot; Margin=&quot;0&quot; Background=&quot;White&quot;\/&gt;\n            &lt;\/Canvas&gt;\n            &lt;TextBlock x:Name=&quot;txtPredicted&quot; Text=&quot;null&quot; Height=&quot;32&quot; Width=&quot;250&quot; Foreground=&quot;Yellow&quot; TextAlignment=&quot;Center&quot; FontSize=&quot;18&quot; FontStyle=&quot;Normal&quot;\/&gt;\n            &lt;Button x:Name=&quot;btnReset&quot; Content=&quot;Reset&quot; Height=&quot;32&quot; Width=&quot;150&quot; Margin=&quot;5&quot; Click=&quot;btnReset_Click&quot;\/&gt;\n            &lt;Button x:Name=&quot;btnPredict&quot; Content=&quot;Predict&quot; Height=&quot;32&quot; Width=&quot;150&quot; Margin=&quot;5&quot; Click=&quot;btnPredict_Click&quot;\/&gt;\n            &lt;Button x:Name=&quot;btnTrain&quot; Content=&quot;Train&quot; Height=&quot;32&quot; Width=&quot;150&quot; Margin=&quot;5&quot; Click=&quot;btnTrain_Click&quot;\/&gt;\n        &lt;\/StackPanel&gt;\n    &lt;\/Grid&gt;\n&lt;\/Window&gt;\n\n<\/code><\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5.\u3000\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u5b9f\u884c<\/strong><\/h2>\n\n\n\n<p>\u4ee5\u4e0b\u306e\u52d5\u753b\u306e\u3088\u3046\u306b\u64cd\u4f5c\u3059\u308b\u3053\u3068\u3067\uff0c\u2460\u624b\u66f8\u304d\u6570\u5b57\u306e\u5b66\u7fd2\uff0c\u2461Canvas\u306e\u30ea\u30bb\u30c3\u30c8\uff0c\u2462\u624b\u66f8\u304d\u6587\u5b57\u306e\u63a8\u8ad6\u3092\u884c\u3048\u307e\u3059\uff0e\u52d5\u753b\u3067\u306f\uff0c\u624b\u66f8\u304d\u306e\u6570\u5b57\u3092\u7cbe\u5ea6\u3088\u304f\u63a8\u8ad6\u3067\u304d\u3066\u3044\u308b\u3053\u3068\u304c\u308f\u304b\u308a\u307e\u3059\uff0e\u3053\u306e\u3088\u3046\u306bTorchSharp\u3092\u7528\u3044\u308b\u3053\u3068\u3067\uff0cC#\u3067\u3082\u5341\u5206\u306b\u6a5f\u68b0\u5b66\u7fd2\u304c\u884c\u3048\u307e\u3059\uff0e<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"498\" style=\"aspect-ratio: 848 \/ 498;\" width=\"848\" autoplay controls loop muted src=\"https:\/\/kkaneko-lab.com\/wp-content\/uploads\/2024\/08\/20240901-0252-14.6706465.mp4\"><\/video><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u6a5f\u68b0\u5b66\u7fd2\u306b\u95a2\u3059\u308b\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\uff0c\u57fa\u672c\u7684\u306bpython\u306b\u3088\u308a\u5b9f\u88c5\u3055\u308c\u307e\u3059\uff0e\u3057\u304b\u3057C#(.Net)\u3067\u4f5c\u6210\u6e08\u307f\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306b\u6a5f\u68b0\u5b66\u7fd2\u306e\u6a5f\u80fd\u3092\u8ffd\u52a0\u3057\u305f\u3044\u5834\u5408\u3084\uff0cGUI\u3082\u4f5c\u6210\u3057\u305f\u3044\u5834\u5408\u306a\u3069\uff0cC#(.Net)\u3067\u6a5f\u68b0\u5b66\u7fd2\u306e\u51e6\u7406\u3092\u5b9f\u88c5\u3057\u306a &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/kkaneko-lab.com\/?p=285\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u3010TorchSharp\u3011C#\u3067\u6a5f\u68b0\u5b66\u7fd2\u3000\u624b\u66f8\u304d\u6587\u5b57\u8a8d\u8b58\u30a2\u30d7\u30ea\u306e\u5b9f\u88c5\uff08\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u4ed8\u304d\uff09&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_locale":"ja","_original_post":"http:\/\/kkaneko-lab.com\/?p=285","footnotes":""},"categories":[5],"tags":[13,16,15,17],"class_list":["post-285","post","type-post","status-publish","format-standard","hentry","category-5","tag-c","tag-torchsharp","tag-wpf","tag-17","ja"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/kkaneko-lab.com\/index.php?rest_route=\/wp\/v2\/posts\/285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kkaneko-lab.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kkaneko-lab.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kkaneko-lab.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kkaneko-lab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=285"}],"version-history":[{"count":34,"href":"https:\/\/kkaneko-lab.com\/index.php?rest_route=\/wp\/v2\/posts\/285\/revisions"}],"predecessor-version":[{"id":400,"href":"https:\/\/kkaneko-lab.com\/index.php?rest_route=\/wp\/v2\/posts\/285\/revisions\/400"}],"wp:attachment":[{"href":"https:\/\/kkaneko-lab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kkaneko-lab.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kkaneko-lab.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}