{"id":9352,"date":"2026-08-19T18:30:36","date_gmt":"2026-08-19T09:30:36","guid":{"rendered":"https:\/\/since2020.jp\/media\/?p=9352"},"modified":"2026-08-19T18:30:36","modified_gmt":"2026-08-19T09:30:36","slug":"building_a_diffusion_model_with_64x64_flower_images","status":"publish","type":"post","link":"https:\/\/since2020.jp\/media\/building_a_diffusion_model_with_64x64_flower_images\/","title":{"rendered":"\u3010Google Colab\u301164&#215;64\u306e\u82b1\u753b\u50cf\u3067\u4f5c\u308b\u62e1\u6563\u30e2\u30c7\u30eb"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">1. \u306f\u3058\u3081\u306b\uff1a\u300c\u82b1\u3092\u5fa9\u5143\u3059\u308bAI\u300d<\/h2>\n\n\n\n<p>\u307e\u305a\u306f\u3053\u308c\u3092\u898b\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img decoding=\"async\" width=\"332\" height=\"68\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-2.png\" alt=\"\" class=\"wp-image-9353\" style=\"aspect-ratio:4.8824268533476936;width:791px;height:auto\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-2.png 332w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-2-300x61.png 300w\" sizes=\"(max-width: 332px) 100vw, 332px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center\"><em>\u5b8c\u5168\u306a\u30e9\u30f3\u30c0\u30e0\u30ce\u30a4\u30ba\uff08\u7802\u5d50\uff09\u304b\u3089\u30011000\u30b9\u30c6\u30c3\u30d7\u304b\u3051\u3066\u82b1\u304c\u6d6e\u304b\u3073\u4e0a\u304c\u3063\u3066\u304f\u308b\u69d8\u5b50<\/em><\/p>\n\n\n\n<p>\u6700\u521d\u306e\u30d5\u30ec\u30fc\u30e0\u306f\u3001\u305f\u3060\u306e\u7802\u5d50\u3067\u3059\u3002\u610f\u5473\u306e\u3042\u308b\u60c5\u5831\u306f1\u30d3\u30c3\u30c8\u3082\u5165\u3063\u3066\u3044\u307e\u305b\u3093\u3002\u305d\u308c\u304c\u6570\u767e\u30b9\u30c6\u30c3\u30d7\u9032\u3080\u3068\u3001\u306a\u3093\u3068\u306a\u304f\u8272\u306e\u584a\u304c\u73fe\u308c\u3001\u8f2a\u90ed\u304c\u307e\u3068\u307e\u308a\u3001\u6700\u5f8c\u306b\u306f\u82b1\u306e\u3088\u3046\u306a\u7269\u4f53\u304c\u63cf\u304b\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3053\u306e\u753b\u50cf\u3092\u751f\u6210\u3057\u3066\u3044\u308b\u306e\u306f\u3001<strong>\u3042\u306a\u305f\u304c\u3053\u308c\u304b\u3089\u66f8\u304d\u4e0a\u3052\u308b\u30b3\u30fc\u30c9<\/strong>\u3067\u3059\u3002\u4e8b\u524d\u5b66\u7fd2\u6e08\u307f\u30e2\u30c7\u30eb\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u30ce\u30a4\u30ba\u30b9\u30b1\u30b8\u30e5\u30fc\u30eb\u306e\u5b9a\u7fa9\u304b\u3089\u5b66\u7fd2\u30eb\u30fc\u30d7\u3001\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u307e\u3067\u3001\u5168\u90e8\u81ea\u5206\u306e\u624b\u3067\u66f8\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u52d5\u4f5c\u74b0\u5883<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Google Colab\uff08\u7121\u6599\u67a0\u306eT4 GPU\u3067OK\uff09<\/li>\n\n\n\n<li>\u30e9\u30f3\u30bf\u30a4\u30e0 \u2192 \u30e9\u30f3\u30bf\u30a4\u30e0\u306e\u30bf\u30a4\u30d7\u3092\u5909\u66f4 \u2192 \u30cf\u30fc\u30c9\u30a6\u30a7\u30a2\u30a2\u30af\u30bb\u30e9\u30ec\u30fc\u30bf \u2192 <strong>T4 GPU<\/strong> \u3092\u5fd8\u308c\u305a\u306b\u8a2d\u5b9a\u3059\u308b\u3002<\/li>\n\n\n\n<li>\u4eca\u56de\u306f<strong>\u5b66\u7fd2\u3092\u8907\u6570\u56de\u306b\u5206\u3051\u3066\u518d\u958b\u3067\u304d\u308b<\/strong>\u69cb\u6210\u306b\u3057\u3066\u3044\u307e\u3059\u3002Colab\u306e\u30bb\u30c3\u30b7\u30e7\u30f3\u304c12\u6642\u9593\u3067\u5207\u308c\u3066\u3082\u3001\u7d9a\u304d\u304b\u3089\u5b66\u7fd2\u3092\u518d\u958b\u3067\u304d\u307e\u3059\uff08\u8a73\u3057\u304f\u306f6\u7ae0\uff09<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">2. \u62e1\u6563\u30e2\u30c7\u30eb\u306e\u5168\u4f53\u50cf<\/h2>\n\n\n\n<p>\u6570\u5f0f\u306e\u524d\u306b\u3001\u56f3\u3067\u7406\u89e3\u3057\u307e\u3057\u3087\u3046\u3002\u62e1\u6563\u30e2\u30c7\u30eb\u304c\u3084\u3063\u3066\u3044\u308b\u3053\u3068\u306f\u3001\u9a5a\u304f\u307b\u3069\u5358\u7d14\u306a<strong>2\u3064\u306e\u30d7\u30ed\u30bb\u30b9<\/strong>\u3060\u3051\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9806\u4f1d\u64ad\uff08Forward Process\uff09\uff1a\u82b1\u3092\u7802\u5d50\u306b\u3059\u308b<\/h3>\n\n\n\n<p>\u304d\u308c\u3044\u306a\u82b1\u306e\u753b\u50cf\u306b\u3001\u307b\u3093\u306e\u5c11\u3057\u3060\u3051\u30ce\u30a4\u30ba\u3092\u8db3\u3057\u307e\u3059\u3002\u307b\u3068\u3093\u3069\u5909\u5316\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u3082\u3046\u4e00\u5ea6\u8db3\u3057\u307e\u3059\u3002\u307e\u3060\u82b1\u3067\u3059\u3002\u3053\u308c\u30921000\u56de\u7e70\u308a\u8fd4\u3059\u3068\u3001\u5143\u304c\u4f55\u3060\u3063\u305f\u306e\u304b\u5b8c\u5168\u306b\u308f\u304b\u3089\u306a\u3044\u7802\u5d50\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"771\" height=\"90\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-3.png\" alt=\"\" class=\"wp-image-9354\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-3.png 771w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-3-300x35.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-3-768x90.png 768w\" sizes=\"(max-width: 771px) 100vw, 771px\" \/><\/figure>\n\n\n\n<p>\u3053\u3053\u3067\u91cd\u8981\u306a\u306e\u306f\u3001<strong>\u3053\u306e\u30d7\u30ed\u30bb\u30b9\u306b\u5b66\u7fd2\u306f\u4e00\u5207\u5fc5\u8981\u306a\u3044<\/strong>\u3068\u3044\u3046\u3053\u3068\u3067\u3059\u3002\u300c\u4e71\u6570\u3092\u8db3\u3059\u300d\u3060\u3051\u306a\u306e\u3067\u3001\u6a5f\u68b0\u7684\u306b\u5b9f\u884c\u3067\u304d\u307e\u3059\u3002\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u8a00\u3048\u3070 <code>image + random_noise<\/code> \u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9006\u4f1d\u64ad\uff08Reverse Process\uff09\uff1a\u7802\u5d50\u304b\u3089\u82b1\u3092\u53d6\u308a\u623b\u3059<\/h3>\n\n\n\n<p>\u6b21\u306f\u9006\u4f1d\u64ad\uff08\u30ce\u30a4\u30ba\u3092\u53d6\u308a\u9664\u304f\u4f5c\u696d\uff09\u3067\u3059\u3002\u7802\u5d50\u304b\u3089\u5c11\u3057\u305a\u3064\u30ce\u30a4\u30ba\u3092<strong>\u5f15\u304d\u7b97<\/strong>\u3057\u3066\u3044\u3051\u3070\u3001\u82b1\u306b\u623b\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"771\" height=\"91\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-4.png\" alt=\"\" class=\"wp-image-9355\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-4.png 771w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-4-300x35.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-4-768x91.png 768w\" sizes=\"(max-width: 771px) 100vw, 771px\" \/><\/figure>\n\n\n\n<p>\u4eba\u9593\u306b\u3068\u3063\u3066\u306f<strong>\u300c\u4f55\u3092\u5f15\u3051\u3070\u3044\u3044\u306e\u304b\u300d<\/strong>\u304c\u308f\u304b\u308a\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<p><strong>\u3053\u3053\u3067AI\u306e\u51fa\u756a\u3067\u3059\u3002<\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">AI\u304c\u5b66\u7fd2\u3059\u308b\u552f\u4e00\u306e\u3053\u3068<\/h3>\n\n\n\n<p>\u62e1\u6563\u30e2\u30c7\u30eb\u306e\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304c\u5b66\u7fd2\u3059\u308b\u306e\u306f\u3001\u6b21\u306e\uff11\u30bf\u30b9\u30af\u306e\u307f\u3067\u3059\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u300c\u30ce\u30a4\u30ba\u307e\u307f\u308c\u306e\u753b\u50cf\u304b\u3089\u3001\u305d\u3053\u306b\u4e57\u3063\u3066\u3044\u308b\u300e\u30ce\u30a4\u30ba\u6210\u5206\u300f\u3060\u3051\u3092\u5f53\u3066\u308b\u300d<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>\u82b1\u306e\u63cf\u304d\u65b9\u3092\u5b66\u3076\u308f\u3051\u3067\u306f\u306a\u304f\u3001\u30ce\u30a4\u30ba\u3092\u63a8\u5b9a\u3059\u308b\u3060\u3051\u3067\u3059\u3002\u305d\u3057\u3066\u63a8\u5b9a\u3057\u305f\u30ce\u30a4\u30ba\u3092\u5f15\u304d\u7b97\u3059\u308c\u3070\u3001\u7d50\u679c\u3068\u3057\u3066\u82b1\u304c\u6b8b\u308b\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3057\u304b\u3082\u3001\u3053\u306e\u5b66\u7fd2\u306b\u306f<strong>\u6b63\u89e3\u30e9\u30d9\u30eb\u3092\u6e96\u5099\u3059\u308b\u5fc5\u8981\u304c\u3042\u308a\u307e\u305b\u3093<\/strong>\u3002\u9806\u4f1d\u64ad\u3067\u81ea\u5206\u304c\u8db3\u3057\u305f\u30ce\u30a4\u30ba\u304c\u6b63\u89e3\u30c7\u30fc\u30bf\u306b\u306a\u308b\u304b\u3089\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4e00\u8a00\u3067\u307e\u3068\u3081\u308b\u3068\u3001\u62e1\u6563\u30e2\u30c7\u30eb\u306e\u5b66\u7fd2\u3068\u306f\uff1a<\/p>\n\n\n\n<p><strong>\u300c\u81ea\u5206\u3067\u4ed5\u8fbc\u3093\u3060\u30ce\u30a4\u30ba\u3092\u3001\u81ea\u5206\u3067\u5f53\u3066\u308b\u8a13\u7df4\u3092\u5ef6\u3005\u3068\u7e70\u308a\u8fd4\u3059\u300d<\/strong><\/p>\n\n\n\n<p>\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u4ee5\u964d\u306e\u7ae0\u3067\u306f\u3001\u4ee5\u4e0a\u306e\u6d41\u308c\u3092\u6570\u5f0f\u3068\u30b3\u30fc\u30c9\u3067\u8a18\u8ff0\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">3. \u5b9f\u88c5\u6e96\u5099\uff1aColab\u74b0\u5883\u3068\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u30e9\u30a4\u30d6\u30e9\u30ea\u306e\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-bash\" data-lang=\"Bash\"><code>!pip install -q diffusers datasets<\/code><\/pre><\/div>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import torch&lt;br&gt;import torch.nn.functional as F\nfrom torch.optim import Adam\nfrom diffusers import UNet2DModel\nfrom datasets import load_dataset\nfrom torchvision import transforms\nimport os\n\ndevice = &quot;cuda&quot; if torch.cuda.is_available() else &quot;cpu&quot;\nprint(device)  # cuda \u3068\u51fa\u308c\u3070OK<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u306e\u30ed\u30fc\u30c9<\/h3>\n\n\n\n<p>Hugging Face Hub\u304b\u3089\u82b1\u306e\u753b\u50cf\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3092\u53d6\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>dataset = load_dataset(&quot;huggan\/flowers-102-categories&quot;, split=&quot;train&quot;)\nprint(dataset)\nprint(dataset.column_names)  # \u753b\u50cf\u30ab\u30e9\u30e0\u306e\u540d\u524d\u3092\u78ba\u8a8d<\/code><\/pre><\/div>\n\n\n\n<p><code>huggan\/flowers-102-categories<\/code> \u306f\u3001102\u7a2e\u985e\u306e\u82b1\u3092\u96c6\u3081\u305f\u753b\u50cf\u30c7\u30fc\u30bf\u30bb\u30c3\u30c8\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u524d\u51e6\u7406\uff1a64<math data-latex=\"\\times\"><semantics><mo lspace=\"0em\" rspace=\"0em\">\u00d7<\/mo><annotation encoding=\"application\/x-tex\">\\times<\/annotation><\/semantics><\/math>64\u3078\u306e\u30ea\u30b5\u30a4\u30ba\u3068\u30c6\u30f3\u30bd\u30eb\u5316<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>preprocess = transforms.Compose([\n    transforms.Resize((64, 64)),\n    transforms.ToTensor(),                  # [0, 1] \u306e\u30c6\u30f3\u30bd\u30eb\u3078\n    transforms.Normalize([0.5], [0.5]),     # [-1, 1] \u3078\n])\n\ndef transform(examples):\n    return {&quot;image&quot;: [preprocess(img.convert(&quot;RGB&quot;)) for img in examples[&quot;image&quot;]]}\n\ndataset.set_transform(transform)\n\ndataloader = torch.utils.data.DataLoader(dataset, batch_size=16, shuffle=True)<\/code><\/pre><\/div>\n\n\n\n<p>\u3053\u3053\u3067<strong>\u5730\u5473\u3060\u304c\u6975\u3081\u3066\u91cd\u8981<\/strong>\u306a\u306e\u304c <code>Normalize([0.5], [0.5])<\/code> \u3067\u3059\u3002\u753b\u7d20\u5024\u3092 <code>[0, 1]<\/code> \u3067\u306f\u306a\u304f <strong><code>[-1, 1]<\/code> \u306e\u7bc4\u56f2\u306b\u5909\u63db<\/strong>\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u7406\u7531\u306f\u3001\u3053\u306e\u5f8c\u3067\u8db3\u3059\u6a19\u6e96\u6b63\u898f\u5206\u5e03\u306e\u30ce\u30a4\u30ba <math data-latex=\"\\mathcal{N}(0, 1)\"><semantics><mrow><mi class=\"mathcal\">\ud835\udca9<\/mi><mo form=\"prefix\" stretchy=\"false\">(<\/mo><mn>0<\/mn><mo separator=\"true\">,<\/mo><mn>1<\/mn><mo form=\"postfix\" stretchy=\"false\">)<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">\\mathcal{N}(0, 1)<\/annotation><\/semantics><\/math> \u304c\u3001\u5e73\u57470\u3067\u6b63\u8ca0\u306b\u5e83\u304c\u308b\u5024\u3060\u304b\u3089\u3067\u3059\u3002\u753b\u50cf\u5074\u304c <code>[0, 1]<\/code> \u306e\u307e\u307e\u3060\u3068\u3001\u30c7\u30fc\u30bf\u3068\u30ce\u30a4\u30ba\u306e\u30b9\u30b1\u30fc\u30eb\u304c\u565b\u307f\u5408\u308f\u305a\u3001\u5b66\u7fd2\u304c\u3046\u307e\u304f\u9032\u307f\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<p>\uff08<code>[0.5]<\/code> \u30681\u8981\u7d20\u3057\u304b\u6e21\u3057\u3066\u3044\u307e\u305b\u3093\u304c\u30013\u30c1\u30e3\u30f3\u30cd\u30eb\u306e\u753b\u50cf\u306b\u5bfe\u3057\u3066\u3082\u30d6\u30ed\u30fc\u30c9\u30ad\u30e3\u30b9\u30c8\u3055\u308c\u308b\u306e\u3067\u3001\u5b9f\u8cea <code>[0.5, 0.5, 0.5]<\/code> \u3068\u540c\u3058\u610f\u5473\u306b\u306a\u308a\u307e\u3059\u3002\uff09<\/p>\n\n\n\n<p><code>set_transform<\/code> \u3092\u4f7f\u3046\u3068\u3001<code>examples[\"image\"]<\/code> \u306e\u30ad\u30fc\u540d\u3067\u8f9e\u66f8\u3092\u8fd4\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002\u3042\u3068\u3067\u5b66\u7fd2\u30eb\u30fc\u30d7\u5074\u306f <code>batch[\"image\"]<\/code> \u3068\u3057\u3066\u3053\u308c\u3092\u53d7\u3051\u53d6\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e2d\u8eab\u306e\u78ba\u8a8d<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import matplotlib.pyplot as plt\n\nbatch = next(iter(dataloader))[&quot;image&quot;]\nprint(batch.shape)  # torch.Size([16, 3, 64, 64])\n\nfig, axes = plt.subplots(1, 8, figsize=(16, 2))\nfor ax, img in zip(axes, batch[:8]):\n\t\t# Matplotlib\u306eimshow\u306f(\u9ad8\u3055, \u5e45, \u30c1\u30e3\u30f3\u30cd\u30eb\u6570)\u306e\u9806\u5e8f\u3092\u671f\u5f85\u3059\u308b\u305f\u3081\u3001\n    # \u30c6\u30f3\u30bd\u30eb\u3092(\u30c1\u30e3\u30f3\u30cd\u30eb\u6570, \u9ad8\u3055, \u5e45)\u304b\u3089(\u9ad8\u3055, \u5e45, \u30c1\u30e3\u30f3\u30cd\u30eb\u6570)\u306b\u4e26\u3079\u66ff\u3048\u308b\u5fc5\u8981\u304c\u3042\u308b\u3002\n    # permute(1, 2, 0)\u306f\u3001\u5143\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b91(\u9ad8\u3055)\u3092\u65b0\u3057\u3044\u30a4\u30f3\u30c7\u30c3\u30af\u30b90\u306b\u3001\n    # \u5143\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b92(\u5e45)\u3092\u65b0\u3057\u3044\u30a4\u30f3\u30c7\u30c3\u30af\u30b91\u306b\u3001\n    # \u5143\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b90(\u30c1\u30e3\u30f3\u30cd\u30eb\u6570)\u3092\u65b0\u3057\u3044\u30a4\u30f3\u30c7\u30c3\u30af\u30b92\u306b\u79fb\u52d5\u3055\u305b\u308b\u3002\n    ax.imshow((img.permute(1, 2, 0) + 1) \/ 2)  # [-1,1] \u2192 [0,1] \u306b\u623b\u3057\u3066\u8868\u793a\n    ax.axis(&quot;off&quot;)\nplt.show()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"123\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-5-1024x123.png\" alt=\"\" class=\"wp-image-9356\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-5-1024x123.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-5-300x36.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-5-768x92.png 768w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-5.png 1260w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u82b1\u304c8\u8f2a\u4e26\u3079\u3070\u6e96\u5099\u5b8c\u4e86\u3067\u3059\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">4. \u9806\u4f1d\u64ad\uff1a\u753b\u50cf\u3092\u7802\u5d50\u306b\u3059\u308b\u6570\u5f0f\u3068\u30b3\u30fc\u30c9<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u6570\u5f0f\u306e\u5de5\u592b\uff1a1000\u56de\u306e\u30eb\u30fc\u30d7\u30921\u884c\u306b\u3059\u308b<\/h3>\n\n\n\n<p>\u7d20\u6734\u306b\u5b9f\u88c5\u3059\u308b\u306a\u3089\u3001\u300c\u30ce\u30a4\u30ba\u3092\u8db3\u3059\u300d\u3092 <math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math> \u56de\u30eb\u30fc\u30d7\u3059\u308c\u3070\u3001\u6642\u523b <math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math>\u306e\u30ce\u30a4\u30ba\u753b\u50cf <math data-latex=\"x_t\"><semantics><msub><mi>x<\/mi><mi>t<\/mi><\/msub><annotation encoding=\"application\/x-tex\">x_t<\/annotation><\/semantics><\/math> \u304c\u624b\u306b\u5165\u308a\u307e\u3059\u3002\u3057\u304b\u3057\u5b66\u7fd2\u4e2d\u306b\u6bce\u56de1000\u56de\u30eb\u30fc\u30d7\u3092\u56de\u3059\u306e\u306f\u8ad6\u5916\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u3053\u3067DDPM\u306e\u8ad6\u6587\u304c\u4f7f\u3046\u306e\u304c\u3001<strong>\u4efb\u610f\u306e\u6642\u523b <\/strong><math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math><strong> \u306e\u30ce\u30a4\u30ba\u753b\u50cf\u3092\u4e00\u767a\u3067\u4f5c\u308c\u308b<\/strong>\u3053\u306e\u5f0f\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-math\"><math display=\"block\"><semantics><mrow><mi>q<\/mi><mo form=\"prefix\" stretchy=\"false\">(<\/mo><msub><mi>x<\/mi><mi>t<\/mi><\/msub><mi>|<\/mi><msub><mi>x<\/mi><mn>0<\/mn><\/msub><mo form=\"postfix\" stretchy=\"false\">)<\/mo><mo>=<\/mo><mi class=\"mathcal\">\ud835\udca9<\/mi><mrow><mo fence=\"true\" form=\"prefix\">(<\/mo><msub><mi>x<\/mi><mi>t<\/mi><\/msub><mo separator=\"true\">;<\/mo><msqrt><msub><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/msub><\/msqrt><msub><mi>x<\/mi><mn>0<\/mn><\/msub><mo separator=\"true\">,<\/mo><mo form=\"prefix\" stretchy=\"false\">(<\/mo><mn>1<\/mn><mo>\u2212<\/mo><msub><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/msub><mo form=\"postfix\" stretchy=\"false\">)<\/mo><mi>\ud835\udc08<\/mi><mo fence=\"true\" form=\"postfix\">)<\/mo><\/mrow><\/mrow><annotation encoding=\"application\/x-tex\">q(x_t \\vert x_0) = \\mathcal{N}\\left(x_t; \\sqrt{\\bar{\\alpha}_t}x_0, (1-\\bar{\\alpha}_t)\\mathbf{I}\\right)<\/annotation><\/semantics><\/math><\/div>\n\n\n\n<p><math data-latex=\"\\mathcal{N}\"><semantics><mi class=\"mathcal\">\ud835\udca9<\/mi><annotation encoding=\"application\/x-tex\">\\mathcal{N}<\/annotation><\/semantics><\/math> \u3068\u3044\u3046\u8a18\u53f7\u306b\u3072\u308b\u307e\u306a\u3044\u3067\u304f\u3060\u3055\u3044\u3002\u3053\u308c\u306f\u300c\u6b63\u898f\u5206\u5e03\u300d\u3092\u8868\u3059\u3060\u3051\u3067\u3001\u610f\u5473\u3057\u3066\u3044\u308b\u306e\u306f\u6b21\u306e\u3053\u3068\u3067\u3059\u3002<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><math data-latex=\"x_t\"><semantics><msub><mi>x<\/mi><mi>t<\/mi><\/msub><annotation encoding=\"application\/x-tex\">x_t<\/annotation><\/semantics><\/math> \u306f\u3001<strong>\u5e73\u5747\u304c <\/strong><math data-latex=\"\\sqrt{\\bar{\\alpha}_t}x_0\"><semantics><mrow><msqrt><msub><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/msub><\/msqrt><msub><mi>x<\/mi><mn>0<\/mn><\/msub><\/mrow><annotation encoding=\"application\/x-tex\">\\sqrt{\\bar{\\alpha}_t}x_0<\/annotation><\/semantics><\/math><strong>\u3001\u5206\u6563\u304c <\/strong><math data-latex=\"1-\\bar{\\alpha}_t\"><semantics><mrow><mn>1<\/mn><mo>\u2212<\/mo><msub><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/msub><\/mrow><annotation encoding=\"application\/x-tex\">1-\\bar{\\alpha}_t<\/annotation><\/semantics><\/math><strong> \u306e\u6b63\u898f\u5206\u5e03<\/strong>\u304b\u3089\u53d6\u308a\u51fa\u3055\u308c\u308b<\/p>\n<\/blockquote>\n\n\n\n<p>\u305d\u3057\u3066\u6b63\u898f\u5206\u5e03\u304b\u3089\u306e\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u306f\u3001\u300c\u5e73\u5747 + \u6a19\u6e96\u504f\u5dee \u00d7 \u6a19\u6e96\u6b63\u898f\u4e71\u6570\u300d\u3067\u66f8\u3051\u307e\u3059\u3002\u3064\u307e\u308a\u4e0a\u306e\u5f0f\u306f\u3001\u5b9f\u8cea\u3053\u3046\u8aad\u307f\u66ff\u3048\u3089\u308c\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-math\"><math display=\"block\"><semantics><mrow><msub><mi>x<\/mi><mi>t<\/mi><\/msub><mo>=<\/mo><msqrt><msub><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/msub><\/msqrt><msub><mi>x<\/mi><mn>0<\/mn><\/msub><mo>+<\/mo><msqrt><mrow><mn>1<\/mn><mo>\u2212<\/mo><msub><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/msub><\/mrow><\/msqrt><mi>\u03f5<\/mi><mspace width=\"2em\"><\/mspace><mo form=\"prefix\" stretchy=\"false\">(<\/mo><mi>\u03f5<\/mi><mo>\u223c<\/mo><mi class=\"mathcal\">\ud835\udca9<\/mi><mo form=\"prefix\" stretchy=\"false\">(<\/mo><mn>0<\/mn><mo separator=\"true\">,<\/mo><mi>\ud835\udc08<\/mi><mo form=\"postfix\" stretchy=\"false\">)<\/mo><mo form=\"postfix\" stretchy=\"false\">)<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">x_t = \\sqrt{\\bar{\\alpha}_t}x_0 + \\sqrt{1-\\bar{\\alpha}_t}\\epsilon \\qquad (\\epsilon \\sim \\mathcal{N}(0, \\mathbf{I}))<\/annotation><\/semantics><\/math><\/div>\n\n\n\n<p>\u5143\u753b\u50cf <math data-latex=\"x_0\"><semantics><msub><mi>x<\/mi><mn>0<\/mn><\/msub><annotation encoding=\"application\/x-tex\">x_0<\/annotation><\/semantics><\/math> \u3068\u30ce\u30a4\u30ba <math data-latex=\"\\epsilon\"><semantics><mi>\u03f5<\/mi><annotation encoding=\"application\/x-tex\">\\epsilon<\/annotation><\/semantics><\/math> \u306e\u3001\u91cd\u307f\u4ed8\u304d\u8db3\u3057\u7b97\u3092\u8868\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><math data-latex=\"\\sqrt{\\bar{\\alpha}_t}\"><semantics><msqrt><msub><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/msub><\/msqrt><annotation encoding=\"application\/x-tex\">\\sqrt{\\bar{\\alpha}_t}<\/annotation><\/semantics><\/math>\uff1a\u5143\u753b\u50cf\u3092\u6b8b\u3059\u5272\u5408\uff08<math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math> \u304c\u9032\u3080\u307b\u30690\u306b\u8fd1\u3065\u304f\uff09<\/li>\n\n\n\n<li><math data-latex=\"\\sqrt{1-\\bar{\\alpha}_t}\"><semantics><msqrt><mrow><mn>1<\/mn><mo>\u2212<\/mo><msub><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/msub><\/mrow><\/msqrt><annotation encoding=\"application\/x-tex\">\\sqrt{1-\\bar{\\alpha}_t}<\/annotation><\/semantics><\/math> \uff1a\u30ce\u30a4\u30ba\u3092\u6df7\u305c\u308b\u5272\u5408\uff08<math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math> \u304c\u9032\u3080\u307b\u30691\u306b\u8fd1\u3065\u304f\uff09<\/li>\n<\/ul>\n\n\n\n<p><math data-latex=\"t=0\"><semantics><mrow><mi>t<\/mi><mo>=<\/mo><mn>0<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">t=0<\/annotation><\/semantics><\/math> \u3067\u306f\u753b\u50cf100%\u3001<math data-latex=\"t=1000\"><semantics><mrow><mi>t<\/mi><mo>=<\/mo><mn>1000<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">t=1000<\/annotation><\/semantics><\/math> \u3067\u306f\u30ce\u30a4\u30ba100%\u306b\u306a\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><math data-latex=\"\\beta\"><semantics><mi>\u03b2<\/mi><annotation encoding=\"application\/x-tex\">\\beta<\/annotation><\/semantics><\/math>\u3001<math data-latex=\"\\alpha\"><semantics><mi>\u03b1<\/mi><annotation encoding=\"application\/x-tex\">\\alpha<\/annotation><\/semantics><\/math>\u3001<math data-latex=\"\\bar{\\alpha}\"><semantics><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><annotation encoding=\"application\/x-tex\">\\bar{\\alpha}<\/annotation><\/semantics><\/math> \u306e\u95a2\u4fc2<\/h3>\n\n\n\n<p>\u767b\u5834\u3059\u308b\u8a18\u53f7\u306f3\u3064\u3060\u3051\u3067\u3001\u5f8c\u308d2\u3064\u306f <math data-latex=\"\\beta\"><semantics><mi>\u03b2<\/mi><annotation encoding=\"application\/x-tex\">\\beta<\/annotation><\/semantics><\/math> \u304b\u3089\u6a5f\u68b0\u7684\u306b\u6c7a\u307e\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u8a18\u53f7<\/th><th>\u5b9a\u7fa9<\/th><th>\u610f\u5473<\/th><\/tr><\/thead><tbody><tr><td><math data-latex=\"\\beta\"><semantics><mi>\u03b2<\/mi><annotation encoding=\"application\/x-tex\">\\beta<\/annotation><\/semantics><\/math><\/td><td>\u81ea\u5206\u3067\u6c7a\u3081\u308b\uff08\u30b9\u30b1\u30b8\u30e5\u30fc\u30eb\uff09<\/td><td>\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u8db3\u3059\u30ce\u30a4\u30ba\u306e\u5f37\u3055<\/td><\/tr><tr><td><math data-latex=\"\\alpha_t\"><semantics><msub><mi>\u03b1<\/mi><mi>t<\/mi><\/msub><annotation encoding=\"application\/x-tex\">\\alpha_t<\/annotation><\/semantics><\/math><\/td><td><math data-latex=\"1 - \\beta_t\"><semantics><mrow><mn>1<\/mn><mo>\u2212<\/mo><msub><mi>\u03b2<\/mi><mi>t<\/mi><\/msub><\/mrow><annotation encoding=\"application\/x-tex\">1 &#8211; \\beta_t<\/annotation><\/semantics><\/math><\/td><td>\u5404\u30b9\u30c6\u30c3\u30d7\u3067\u6b8b\u308b\u753b\u50cf\u306e\u5272\u5408<\/td><\/tr><tr><td><math data-latex=\"\\bar{\\alpha}\"><semantics><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><annotation encoding=\"application\/x-tex\">\\bar{\\alpha}<\/annotation><\/semantics><\/math><\/td><td><math data-latex=\"\\alpha_1 \\alpha_2 \\cdots \\alpha_t\"><semantics><mrow><msub><mi>\u03b1<\/mi><mn>1<\/mn><\/msub><msub><mi>\u03b1<\/mi><mn>2<\/mn><\/msub><mo>\u22ef<\/mo><msub><mi>\u03b1<\/mi><mi>t<\/mi><\/msub><\/mrow><annotation encoding=\"application\/x-tex\">\\alpha_1 \\alpha_2 \\cdots \\alpha_t<\/annotation><\/semantics><\/math><\/td><td>\u30b9\u30c6\u30c3\u30d70\u304b\u3089 <math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math> \u307e\u3067\u6b8b\u3063\u305f\u753b\u50cf\u306e\u5272\u5408\uff08\u7d2f\u7a4d\u7a4d\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><math data-latex=\"\\bar{\\alpha}\"><semantics><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><annotation encoding=\"application\/x-tex\">\\bar{\\alpha}<\/annotation><\/semantics><\/math> \u306f\u300c<math data-latex=\"\\alpha\"><semantics><mi>\u03b1<\/mi><annotation encoding=\"application\/x-tex\">\\alpha<\/annotation><\/semantics><\/math> \u306e\u7d2f\u7a4d\u7a4d\u300d\u3068\u3044\u3046\u3060\u3051\u3067\u3001PyTorch\u3067\u306f <code>torch.cumprod<\/code> \u306b\u3088\u308a\u5b9f\u88c5\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PyTorch\u5b9f\u88c5<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>T = 1000  # \u7dcf\u30b9\u30c6\u30c3\u30d7\u6570\n\n# \u7dda\u5f62\u30ce\u30a4\u30ba\u30b9\u30b1\u30b8\u30e5\u30fc\u30eb\uff08DDPM\u8ad6\u6587\u306e\u8a2d\u5b9a\uff09\nbeta = torch.linspace(0.0001, 0.02, T).to(device)\nalpha = 1.0 - beta\nalpha_cumprod = torch.cumprod(alpha, axis=0).to(device)  # \u2190 \u3053\u308c\u304c \u1fb1_t<\/code><\/pre><\/div>\n\n\n\n<p>\u305d\u3057\u3066\u3001\u9806\u4f1d\u64ad\u306e\u672c\u4f53\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>def q_sample(x_0, t, noise=None):\n    &quot;&quot;&quot;x_0 \u306b\u6642\u523b t \u76f8\u5f53\u306e\u30ce\u30a4\u30ba\u3092\u8db3\u3059&quot;&quot;&quot;\n    if noise is None:\n        noise = torch.randn_like(x_0)\n\n    sqrt_alpha_cumprod_t = torch.sqrt(alpha_cumprod[t])[:, None, None, None]\n    sqrt_one_minus_alpha_cumprod_t = torch.sqrt(1.0 - alpha_cumprod[t])[:, None, None, None]\n\n    return sqrt_alpha_cumprod_t * x_0 + sqrt_one_minus_alpha_cumprod_t * noise<\/code><\/pre><\/div>\n\n\n\n<p><code>alpha_cumprod[t]<\/code> \u306f\u5f62\u304c <code>(batch_size,)<\/code> \u306e\u30d9\u30af\u30c8\u30eb\u3067\u3059\u304c\u3001\u753b\u50cf\u306f <code>(batch_size, 3, 64, 64)<\/code> \u3068\u3044\u30464\u6b21\u5143\u30c6\u30f3\u30bd\u30eb\u3067\u3059\u3002<code>[:, None, None, None]<\/code> \u3067\u672b\u5c3e\u306b3\u3064\u306e\u6b21\u5143\u3092\u8ffd\u52a0\u3057\u3001<code>(batch_size, 1, 1, 1)<\/code> \u306e\u5f62\u306b\u3057\u3066\u304b\u3089\u30d6\u30ed\u30fc\u30c9\u30ad\u30e3\u30b9\u30c8\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p><strong>\u6700\u5f8c\u306e1\u884c\u306b\u6ce8\u76ee\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/strong> \u4e0a\u3067\u7d39\u4ecb\u3057\u305f\u6570\u5f0f<\/p>\n\n\n\n<div class=\"wp-block-math\"><math display=\"block\"><semantics><mrow><msub><mi>x<\/mi><mi>t<\/mi><\/msub><mo>=<\/mo><msqrt><msub><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/msub><\/msqrt><msub><mi>x<\/mi><mn>0<\/mn><\/msub><mo>+<\/mo><msqrt><mrow><mn>1<\/mn><mo>\u2212<\/mo><msub><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/msub><\/mrow><\/msqrt><mi>\u03f5<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">x_t = \\sqrt{\\bar{\\alpha}_t}x_0 + \\sqrt{1-\\bar{\\alpha}_t}\\epsilon<\/annotation><\/semantics><\/math><\/div>\n\n\n\n<p>\u304c\u3001\u305d\u306e\u307e\u307e <code>sqrt_alpha_cumprod_t * x_0 + sqrt_one_minus_alpha_cumprod_t * noise<\/code> \u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u8a18\u53f7\u304cPyTorch\u306e\u5909\u6570\u540d\u306b\u7f6e\u304d\u63db\u308f\u3063\u305f\u3060\u3051\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9f\u969b\u306b\u82b1\u306b\u30ce\u30a4\u30ba\u3092\u52a0\u3048\u3066\u307f\u308b<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>x_0 = batch[:1].to(device)  # \u82b1\u30921\u8f2a\nsteps = [0, 50, 100, 200, 400, 700, 999]\n\nfig, axes = plt.subplots(1, len(steps), figsize=(16, 2.5))\nfor ax, step in zip(axes, steps):\n    t = torch.tensor([step], device=device)\n    x_t = q_sample(x_0, t)\n    img = (x_t[0].cpu().permute(1, 2, 0) + 1) \/ 2\n    ax.imshow(img.clamp(0, 1))\n    ax.set_title(f&quot;t={step}&quot;)\n    ax.axis(&quot;off&quot;)\nplt.show()<\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1024\" height=\"157\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-6-1024x157.png\" alt=\"\" class=\"wp-image-9361\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-6-1024x157.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-6-300x46.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-6-768x118.png 768w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-6.png 1260w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u51fa\u529b\u3092\u898b\u308b\u3068\u3001<code>t=50<\/code> \u3042\u305f\u308a\u3067\u306f\u307e\u3060\u82b1\u3089\u3057\u3055\u304c\u6b8b\u3063\u3066\u3044\u3066\u3001<code>t=200<\/code> \u3067\u3056\u3089\u3064\u304d\u304c\u76ee\u7acb\u3061\u3001<code>t=400<\/code> \u3067\u8f2a\u90ed\u304c\u307c\u3084\u3051\u3001<code>t=999<\/code> \u3067\u306f\u5b8c\u5168\u306a\u7802\u5d50\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">5. \u30e2\u30c7\u30eb\u5b9a\u7fa9\uff1a\u30ce\u30a4\u30ba\u3092\u4e88\u6e2c\u3059\u308b\u300cU-Net\u300d<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">U-Net\u306e\u5f79\u5272\u306f\u300c\u753b\u50cf \u2192 \u753b\u50cf\u300d<\/h3>\n\n\n\n<p>\u62e1\u6563\u30e2\u30c7\u30eb\u306e\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304c\u3084\u308b\u3053\u3068\u306f\u3001\u975e\u5e38\u306b\u30b7\u30f3\u30d7\u30eb\u3067\u3059\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5165\u529b<\/strong>\uff1a\u30ce\u30a4\u30ba\u307e\u307f\u308c\u306e\u753b\u50cf <math data-latex=\"x_t\"><semantics><msub><mi>x<\/mi><mi>t<\/mi><\/msub><annotation encoding=\"application\/x-tex\">x_t<\/annotation><\/semantics><\/math>\uff08<math data-latex=\"3 \\times 64 \\times 64\"><semantics><mrow><mn>3<\/mn><mo>\u00d7<\/mo><mn>64<\/mn><mo>\u00d7<\/mo><mn>64<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">3 \\times 64 \\times 64<\/annotation><\/semantics><\/math>\uff09\u3068\u3001\u6642\u523b <math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math>\uff08\u6574\u65701\u3064\uff09<\/li>\n\n\n\n<li><strong>\u51fa\u529b<\/strong>\uff1a\u305d\u3053\u306b\u4e57\u3063\u3066\u3044\u308b\u30ce\u30a4\u30ba <math data-latex=\"\\epsilon\"><semantics><mi>\u03f5<\/mi><annotation encoding=\"application\/x-tex\">\\epsilon<\/annotation><\/semantics><\/math> \u306e\u4e88\u6e2c\u5024\uff08<math data-latex=\"3 \\times 64 \\times 64\"><semantics><mrow><mn>3<\/mn><mo>\u00d7<\/mo><mn>64<\/mn><mo>\u00d7<\/mo><mn>64<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">3 \\times 64 \\times 64<\/annotation><\/semantics><\/math>\uff09<\/li>\n<\/ul>\n\n\n\n<p>\u5165\u529b\u3068\u51fa\u529b\u304c\u540c\u3058\u30b5\u30a4\u30ba\u3067\u3059\u3002\u5206\u985e\u306e\u3088\u3046\u306b\u6b21\u5143\u3092\u6f70\u3057\u3066\u3044\u304f\u5fc5\u8981\u306f\u306a\u304f\u3001<strong>\u753b\u50cf\u3092\u5165\u308c\u305f\u3089\u753b\u50cf\u304c\u51fa\u3066\u304f\u308b<\/strong>\u69cb\u9020\u304c\u5fc5\u8981\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u3053\u3067U-Net\u3067\u3059\u3002\u540d\u524d\u306e\u7531\u6765\u3067\u3042\u308b\u300cU\u300d\u306e\u5b57\u306f\u3001\u3053\u3046\u3044\u3046\u5f62\u3092\u3057\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"771\" height=\"450\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-7.png\" alt=\"\" class=\"wp-image-9362\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-7.png 771w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-7-300x175.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-7-768x448.png 768w\" sizes=\"(max-width: 771px) 100vw, 771px\" \/><\/figure>\n\n\n\n<p>\u524d\u534a\uff08Encoder\uff09\u3067\u89e3\u50cf\u5ea6\u3092\u843d\u3068\u3057\u306a\u304c\u3089\u300c\u5168\u4f53\u306e\u69cb\u9020\u300d\u3092\u6349\u3048\u3001\u5f8c\u534a\uff08Decoder\uff09\u3067\u89e3\u50cf\u5ea6\u3092\u623b\u3057\u307e\u3059\u3002\u3053\u306e\u3068\u304d\u3001<strong>\u540c\u3058\u89e3\u50cf\u5ea6\u306e\u5c64\u3069\u3046\u3057\u3092\u6a2a\u306b\u76f4\u7d50\u3059\u308b\u300c\u30b9\u30ad\u30c3\u30d7\u63a5\u7d9a\u300d<\/strong>\u304c\u3042\u308b\u306e\u304cU-Net\u306e\u809d\u3067\u3059\u3002\u30ce\u30a4\u30ba\u4e88\u6e2c\u306f\u300c1\u30d4\u30af\u30bb\u30eb\u5358\u4f4d\u306e\u7d30\u304b\u3044\u5024\u300d\u3092\u63a8\u5b9a\u3059\u308b\u30bf\u30b9\u30af\u3067\u3059\u3002<math data-latex=\"8\\times 8\"><semantics><mrow><mn>8<\/mn><mo>\u00d7<\/mo><mn>8<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">8\\times 8<\/annotation><\/semantics><\/math>\u307e\u3067\u6f70\u3057\u305f\u60c5\u5831\u3060\u3051\u304b\u3089<math data-latex=\"64\\times 64\"><semantics><mrow><mn>64<\/mn><mo>\u00d7<\/mo><mn>64<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">64\\times 64<\/annotation><\/semantics><\/math>\u3092\u5fa9\u5143\u3057\u3088\u3046\u3068\u3059\u308b\u3068\u3001\u7d30\u90e8\u304c\u5b8c\u5168\u306b\u5931\u308f\u308c\u307e\u3059\u3002\u30b9\u30ad\u30c3\u30d7\u63a5\u7d9a\u306f\u3001Encoder\u304c\u6301\u3063\u3066\u3044\u305f\u9ad8\u89e3\u50cf\u5ea6\u306e\u60c5\u5831\u3092\u3001Decoder\u306b\u76f4\u63a5\u624b\u6e21\u3059\u629c\u3051\u9053\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Time Embedding<\/h3>\n\n\n\n<p><strong>\u540c\u3058\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u304c\u3001<\/strong><math data-latex=\"t=10\"><semantics><mrow><mi>t<\/mi><mo>=<\/mo><mn>10<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">t=10<\/annotation><\/semantics><\/math><strong> \u306e\u3068\u304d\u3068 <\/strong><math data-latex=\"t=900\"><semantics><mrow><mi>t<\/mi><mo>=<\/mo><mn>900<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">t=900<\/annotation><\/semantics><\/math><strong> \u306e\u3068\u304d\u3067\u3001\u307e\u3063\u305f\u304f\u9055\u3046\u632f\u308b\u821e\u3044\u3092\u3057\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002<\/strong><\/p>\n\n\n\n<p><math data-latex=\"t=10\"><semantics><mrow><mi>t<\/mi><mo>=<\/mo><mn>10<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">t=10<\/annotation><\/semantics><\/math> \u306e\u753b\u50cf\u306f\u307b\u307c\u5143\u753b\u50cf\u3067\u3001\u4e57\u3063\u3066\u3044\u308b\u30ce\u30a4\u30ba\u306f\u3054\u304f\u308f\u305a\u304b\u3002<math data-latex=\"t=900\"><semantics><mrow><mi>t<\/mi><mo>=<\/mo><mn>900<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">t=900<\/annotation><\/semantics><\/math> \u306e\u753b\u50cf\u306f\u307b\u307c\u7802\u5d50\u3067\u3001\u307b\u3068\u3093\u3069\u304c\u30ce\u30a4\u30ba\u3002\u540c\u3058\u300c\u30ce\u30a4\u30ba\u3092\u5f53\u3066\u308d\u300d\u3068\u3044\u3046\u4ed5\u4e8b\u3067\u3082\u3001\u8981\u6c42\u3055\u308c\u308b\u51fa\u529b\u306e\u30b9\u30b1\u30fc\u30eb\u3082\u6027\u8cea\u3082\u307e\u308b\u3067\u9055\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u3060\u304b\u3089\u30e2\u30c7\u30eb\u306b\u306f\u3001\u300c<strong>\u4eca\u304c\u4f55\u30b9\u30c6\u30c3\u30d7\u76ee\u306a\u306e\u304b<\/strong>\u300d\u3092\u5fc5\u305a\u6559\u3048\u306a\u3051\u308c\u3070\u306a\u308a\u307e\u305b\u3093\u3002\u6574\u6570 <math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math> \u3092\u305d\u306e\u307e\u307e\u6e21\u3057\u3066\u3082\u3001\u30cb\u30e5\u30fc\u30e9\u30eb\u30cd\u30c3\u30c8\u30ef\u30fc\u30af\u306f\u5927\u304d\u306a\u30b9\u30ab\u30e9\u30fc1\u500b\u304b\u3089\u7d30\u304b\u3044\u9055\u3044\u3092\u8aad\u307f\u53d6\u308b\u306e\u304c\u82e6\u624b\u3067\u3059\u3002900\u3068901\u306e\u5dee\u3082\u300110\u3068900\u306e\u5dee\u3082\u3001\u6271\u3044\u304c\u96d1\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<p>\u305d\u3053\u3067\u4f7f\u308f\u308c\u308b\u306e\u304c\u3001Transformer\u3067\u304a\u306a\u3058\u307f\u306e<strong>Sinusoidal Position Embeddings\uff08\u6b63\u5f26\u6ce2\u4f4d\u7f6e\u30a8\u30f3\u30b3\u30fc\u30c7\u30a3\u30f3\u30b0\uff09<\/strong>\u3067\u3059\u3002\u6574\u6570 <math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math> \u3092\u3001\u5468\u6ce2\u6570\u306e\u7570\u306a\u308bsin\/cos\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u591a\u6b21\u5143\u306e\u30d9\u30af\u30c8\u30eb\u306b\u5c55\u958b\u3057\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u30b3\u30fc\u30c9\u306e\u5de5\u592b\uff1aU-Net\u306f<code>diffusers<\/code>\u304b\u3089<\/h3>\n\n\n\n<p><strong>U-Net\u306e\u4e2d\u8eab\u306f\u65e2\u88fd\u54c1\u3092\u4f7f\u3044\u307e\u3059<\/strong>\u3002Hugging Face\u306e <code>diffusers<\/code> \u306b\u3042\u308b <code>UNet2DModel<\/code> \u304c\u3001\u307e\u3055\u306b\u3053\u306e\u7528\u9014\u306e\u305f\u3081\u306b\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>model = UNet2DModel(\n    sample_size=64,\n    in_channels=3,\n    out_channels=3,\n    layers_per_block=2,\n    block_out_channels=(64, 128, 128, 256),\n    down_block_types=(\n        &quot;DownBlock2D&quot;,      # 64x64 \u2192 32x32\n        &quot;DownBlock2D&quot;,      # 32x32 \u2192 16x16\n        &quot;AttnDownBlock2D&quot;,  # 16x16 \u2192 8x8\uff08\u3053\u3053\u3067Attention\uff09\n        &quot;DownBlock2D&quot;,\n    ),\n    up_block_types=(\n        &quot;UpBlock2D&quot;,\n        &quot;AttnUpBlock2D&quot;,    # 16x16\u3067Attention\n        &quot;UpBlock2D&quot;,\n        &quot;UpBlock2D&quot;,\n    ),\n)\n\nmodel.to(device)\n\nn_params = sum(p.numel() for p in model.parameters())\nprint(f&quot;\u30d1\u30e9\u30e1\u30fc\u30bf\u6570: {n_params \/ 1e6:.1f}M&quot;)<\/code><\/pre><\/div>\n\n\n\n<p>Attention\u3092<math data-latex=\"16\\times 16\"><semantics><mrow><mn>16<\/mn><mo>\u00d7<\/mo><mn>16<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">16\\times 16<\/annotation><\/semantics><\/math>\u306e\u89e3\u50cf\u5ea6\u306b\u3060\u3051\u5165\u308c\u3066\u3044\u308b\u306e\u306f\u3001\u8a08\u7b97\u91cf\u3068\u306e\u30c8\u30ec\u30fc\u30c9\u30aa\u30d5\u3067\u3059\u3002Attention\u306f\u89e3\u50cf\u5ea6\u306e2\u4e57\u3067\u30b3\u30b9\u30c8\u304c\u5897\u3048\u308b\u305f\u3081\u3001<math data-latex=\"64\\times 64\"><semantics><mrow><mn>64<\/mn><mo>\u00d7<\/mo><mn>64<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">64\\times 64<\/annotation><\/semantics><\/math> \u306b\u5165\u308c\u308b\u3068\u5b66\u7fd2\u306b\u6642\u9593\u304c\u304b\u304b\u308a\u3059\u304e\u307e\u3059\u3002\u4e00\u65b9\u3001\u4f4e\u89e3\u50cf\u5ea6\u5074\u306b\u5165\u308c\u308b\u3068\u300c\u82b1\u3073\u3089\u3069\u3046\u3057\u306e\u4f4d\u7f6e\u95a2\u4fc2\u300d\u306e\u3088\u3046\u306a\u5927\u57df\u7684\u306a\u6574\u5408\u6027\u3092\u53d6\u308b\u306e\u306b\u52b9\u679c\u7684\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u547c\u3073\u51fa\u3057\u65b9\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>t = torch.randint(0, T, (16,), device=device).long()\nx_t = q_sample(batch.to(device), t)\n\npred_noise = model(x_t, t).sample   # \u2190 .sample \u3092\u5fd8\u308c\u305a\u306b\nprint(pred_noise.shape)             # torch.Size([16, 3, 64, 64])<\/code><\/pre><\/div>\n\n\n\n<p><code>diffusers<\/code> \u306e\u30e2\u30c7\u30eb\u306f\u51fa\u529b\u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3067\u8fd4\u3059\u306e\u3067\u3001\u30c6\u30f3\u30bd\u30eb\u3092\u53d6\u308a\u51fa\u3059\u306b\u306f <code>.sample<\/code> \u304c\u5fc5\u8981\u3067\u3059\u3002\u3053\u3053\u306f\u5730\u5473\u306b\u30cf\u30de\u308b\u30dd\u30a4\u30f3\u30c8\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u306a\u304a\u3001\u6642\u523b\u306e\u57cb\u3081\u8fbc\u307f\u306f <code>UNet2DModel<\/code> \u306e\u5185\u90e8\u3067\u81ea\u52d5\u7684\u306b\u51e6\u7406\u3055\u308c\u307e\u3059\u3002\u524d\u7ae0\u3067\u8aac\u660e\u3057\u305fSinusoidal Position Embeddings\u3068\u672c\u8cea\u7684\u306b\u540c\u3058\u3082\u306e\u304c\u4e2d\u3067\u52d5\u3044\u3066\u3044\u308b\u3001\u3068\u601d\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">6. \u5b66\u7fd2\u30eb\u30fc\u30d7\uff1a\u30ce\u30a4\u30ba\u63a8\u5b9a<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u640d\u5931\u95a2\u6570<\/h3>\n\n\n\n<p>\u62e1\u6563\u30e2\u30c7\u30eb\u306e\u8ad6\u6587\u306b\u306f\u3001\u5909\u5206\u4e0b\u754c\u3060\u306eKL\u30c0\u30a4\u30d0\u30fc\u30b8\u30a7\u30f3\u30b9\u3060\u306e\u304c\u6570\u30da\u30fc\u30b8\u306b\u308f\u305f\u3063\u3066\u5c55\u958b\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3057\u304b\u3057\u3001\u305d\u308c\u3092\u5168\u90e8\u6574\u7406\u3057\u3066\u7c21\u7565\u5316\u3057\u305f\u7d50\u679c\u3001\u5b9f\u88c5\u3067\u4f7f\u3046\u640d\u5931\u95a2\u6570\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"wp-block-math\"><math display=\"block\"><semantics><mrow><mi>L<\/mi><mo>=<\/mo><msub><mi>\ud835\udd3c<\/mi><mrow><mi>t<\/mi><mo separator=\"true\">,<\/mo><msub><mi>x<\/mi><mn>0<\/mn><\/msub><mo separator=\"true\">,<\/mo><mi>\u03f5<\/mi><\/mrow><\/msub><mrow><mo fence=\"true\" form=\"prefix\">[<\/mo><msup><mrow><mo fence=\"true\" form=\"prefix\">\u2016<\/mo><mi>\u03f5<\/mi><mo>\u2212<\/mo><msub><mi>\u03f5<\/mi><mi>\u03b8<\/mi><\/msub><mo form=\"prefix\" stretchy=\"false\">(<\/mo><msub><mi>x<\/mi><mi>t<\/mi><\/msub><mo separator=\"true\">,<\/mo><mi>t<\/mi><mo form=\"postfix\" stretchy=\"false\">)<\/mo><mo fence=\"true\" form=\"postfix\">\u2016<\/mo><\/mrow><mn>2<\/mn><\/msup><mo fence=\"true\" form=\"postfix\">]<\/mo><\/mrow><\/mrow><annotation encoding=\"application\/x-tex\">L = \\mathbb{E}_{t, x_0, \\epsilon} \\left[ \\left\\Vert \\epsilon &#8211; \\epsilon_\\theta(x_t, t) \\right\\Vert^2 \\right]<\/annotation><\/semantics><\/math><\/div>\n\n\n\n<p><math data-latex=\"\\mathbb{E}\"><semantics><mi>\ud835\udd3c<\/mi><annotation encoding=\"application\/x-tex\">\\mathbb{E}<\/annotation><\/semantics><\/math> \u306f\u300c\u5e73\u5747\u300d\u3001<math data-latex=\"\\Vert \\cdot \\Vert^2 \"><semantics><mrow><mi>\u2016<\/mi><mo>\u22c5<\/mo><msup><mi>\u2016<\/mi><mn>2<\/mn><\/msup><\/mrow><annotation encoding=\"application\/x-tex\">\\Vert \\cdot \\Vert^2 <\/annotation><\/semantics><\/math>\u306f\u300c\u4e8c\u4e57\u300d\u3002\u3064\u307e\u308a\u4e2d\u8eab\u306f<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u672c\u7269\u306e\u30ce\u30a4\u30ba <\/strong><math data-latex=\"\\epsilon\"><semantics><mi>\u03f5<\/mi><annotation encoding=\"application\/x-tex\">\\epsilon<\/annotation><\/semantics><\/math><strong> \u3068\u3001\u30e2\u30c7\u30eb\u304c\u4e88\u6e2c\u3057\u305f\u30ce\u30a4\u30ba <\/strong><math data-latex=\"\\epsilon_\\theta\"><semantics><msub><mi>\u03f5<\/mi><mi>\u03b8<\/mi><\/msub><annotation encoding=\"application\/x-tex\">\\epsilon_\\theta<\/annotation><\/semantics><\/math><strong> \u306e\u3001\u4e8c\u4e57\u8aa4\u5dee\u306e\u5e73\u5747<\/strong><\/p>\n<\/blockquote>\n\n\n\n<p>\u3057\u305f\u304c\u3063\u3066\u3053\u308c\u306f<strong>\u305f\u3060\u306e\u5e73\u5747\u4e8c\u4e57\u8aa4\u5dee\uff08MSE\uff09<\/strong>\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>loss = F.mse_loss(noise_pred, noise)<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u30b3\u30fc\u30c9\u306e\u5de5\u592b\uff1a\u30c1\u30a7\u30c3\u30af\u30dd\u30a4\u30f3\u30c8\u304b\u3089\u5b66\u7fd2\u3092\u518d\u958b\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b<\/h3>\n\n\n\n<p>Colab\u306e\u7121\u6599GPU\u306b\u306f\u3001<strong>\u30bb\u30c3\u30b7\u30e7\u30f3\u304c\u4e00\u5b9a\u6642\u9593\u3067\u5207\u308c\u308b<\/strong>\u3068\u3044\u3046\u5236\u7d04\u304c\u3042\u308a\u307e\u3059\u3002\u82b1\u306e\u3088\u3046\u306a\u88ab\u5199\u4f53\u306f\u3001\u305d\u308c\u306a\u308a\u306e\u30a8\u30dd\u30c3\u30af\u6570\u3092\u7a4d\u307e\u306a\u3044\u3068\u54c1\u8cea\u304c\u51fa\u307e\u305b\u3093\u3002\u305d\u3053\u3067\u4eca\u56de\u306f\u3001<strong>\u300c\u9014\u4e2d\u307e\u3067\u5b66\u7fd2\u3057\u305f\u30e2\u30c7\u30eb\u3092\u4fdd\u5b58\u3057\u3001\u6b21\u306e\u30bb\u30c3\u30b7\u30e7\u30f3\u3067\u305d\u3053\u304b\u3089\u7d9a\u304d\u3092\u5b66\u7fd2\u3059\u308b\u300d<\/strong>\u3068\u3044\u3046\u904b\u7528\u3092\u7d44\u307f\u8fbc\u307f\u307e\u3059<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u5b66\u7fd2\u958b\u59cb\u6642\u306b\u3001\u3082\u3057\u4fdd\u5b58\u6e08\u307f\u306e\u91cd\u307f\uff08<code>model.pth<\/code>\uff09\u304c\u3042\u308c\u3070\u8aad\u307f\u8fbc\u3080<\/li>\n\n\n\n<li>\u8ffd\u52a0\u3067\u5b66\u7fd2\u3092\u56de\u3059<\/li>\n\n\n\n<li>\u4e00\u5b9a\u30a8\u30dd\u30c3\u30af\u3054\u3068\u306b\u65b0\u3057\u3044\u91cd\u307f\uff08<code>model.pth<\/code>\uff09\u3068\u3057\u3066\u4fdd\u5b58\u3057\u76f4\u3059<\/li>\n<\/ol>\n\n\n\n<p><strong>\u300c\u540c\u3058\u30e2\u30c7\u30eb\u306e\u5b66\u7fd2\u3092\u3001\u30bb\u30c3\u30b7\u30e7\u30f3\u3092\u307e\u305f\u3044\u3067\u7d99\u7d9a\u3059\u308b\u300d<\/strong>\u3068\u3044\u3063\u305f\u64cd\u4f5c\u3067\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>model = UNet2DModel(\n    sample_size=64,\n    in_channels=3,\n    out_channels=3,\n    layers_per_block=2,\n    block_out_channels=(64, 128, 128, 256),\n    down_block_types=(&quot;DownBlock2D&quot;, &quot;DownBlock2D&quot;, &quot;AttnDownBlock2D&quot;, &quot;DownBlock2D&quot;),\n    up_block_types=(&quot;UpBlock2D&quot;, &quot;AttnUpBlock2D&quot;, &quot;UpBlock2D&quot;, &quot;UpBlock2D&quot;),\n)\n\n# \u524d\u56de\u306e\u30bb\u30c3\u30b7\u30e7\u30f3\u3067\u4fdd\u5b58\u3057\u305f\u91cd\u307f\u304c\u3042\u308c\u3070\u8aad\u307f\u8fbc\u3080\nif os.path.exists(&#039;model.pth&#039;):\n    model.load_state_dict(torch.load(&#039;model.pth&#039;, map_location=device))\n    print(&quot;Successfully reloaded model.pth (Original Structure)&quot;)\n\noptimizer = Adam(model.parameters(), lr=5e-5)\nepochs = 50<\/code><\/pre><\/div>\n\n\n\n<p><strong>\u3053\u3053\u3067\u6ce8\u610f\u3057\u305f\u3044\u306e\u304c\u5b66\u7fd2\u7387\u3067\u3059\u3002<\/strong> \u30bc\u30ed\u304b\u3089\u5b66\u7fd2\u3059\u308b\u3068\u304d\u306e <code>1e-4<\/code> \u306b\u5bfe\u3057\u3066\u3001\u518d\u958b\u5f8c\u306e\u5b66\u7fd2\u7387\u306f <code>5e-5<\/code> \u3068\u534a\u5206\u306b\u843d\u3068\u3057\u3066\u3044\u307e\u3059\u3002\u3053\u308c\u306f\u3001\u3059\u3067\u306b\u3042\u308b\u7a0b\u5ea6\u5b66\u7fd2\u304c\u9032\u3093\u3060\u30e2\u30c7\u30eb\u306b\u5bfe\u3057\u3066\u5927\u304d\u3059\u304e\u308b\u5b66\u7fd2\u7387\u3092\u304b\u3051\u308b\u3068\u3001\u305b\u3063\u304b\u304f\u5b66\u3093\u3060\u91cd\u307f\u3092\u4e00\u6c17\u306b\u58ca\u3057\u3066\u3057\u307e\u3046\u3053\u3068\u304c\u3042\u308b\u305f\u3081\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b66\u7fd2\u30eb\u30fc\u30d7\u672c\u4f53<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>print(f&quot;Fine-tuning started (Saving to model.pth)...&quot;)\ntry:\n    for epoch in range(epochs):\n        loss_sum = 0\n        for step, batch in enumerate(dataloader):\n            clean_images = batch[&quot;image&quot;].to(device)\n            t = torch.randint(0, T, (clean_images.shape[0],), device=device).long()\n            noise = torch.randn_like(clean_images).to(device)\n            noisy_images = q_sample(clean_images, t, noise=noise)\n\n            noise_pred = model(noisy_images, t).sample\n            loss = F.mse_loss(noise_pred, noise)\n\n            optimizer.zero_grad()\n            loss.backward()\n            optimizer.step()\n            loss_sum += loss.item()\n\n        if (epoch + 1) % 5 == 0:\n            avg_loss = loss_sum \/ len(dataloader)\n            print(f&quot;Epoch {epoch+1}\/{epochs} | Loss: {avg_loss:.4f}&quot;)\n            torch.save(model.state_dict(), &#039;model.pth&#039;)\n\n    torch.save(model.state_dict(), &#039;model.pth&#039;)\n    print(&quot;Final model saved as model.pth&quot;)\nexcept Exception as e:\n    print(f&quot;Error: {e}&quot;)<\/code><\/pre><\/div>\n\n\n\n<p>\u30eb\u30fc\u30d7\u306e\u4e2d\u6838\u306f\u3001\u5b9f\u8cea<strong>4\u30b9\u30c6\u30c3\u30d7\u3060\u3051<\/strong>\u3067\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u30e9\u30f3\u30c0\u30e0\u5024\u306e\u5272\u308a\u632f\u308a<\/strong>\uff1a\u5404\u753b\u50cf\u306b\u30e9\u30f3\u30c0\u30e0\u306a <math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math> \u3092\u5272\u308a\u5f53\u3066\u308b<\/li>\n\n\n\n<li><strong>\u30ce\u30a4\u30ba\u5408\u6210<\/strong>\uff1a\u30ce\u30a4\u30ba\u3092\u751f\u6210\u3057\u3066\u753b\u50cf\u306b\u6df7\u305c\u308b<\/li>\n\n\n\n<li><strong>\u7b54\u3048\u5408\u308f\u305b<\/strong>\uff1a\u30e2\u30c7\u30eb\u306e\u4e88\u6e2c\u3068\u672c\u7269\u306e\u30ce\u30a4\u30ba\u3092\u6bd4\u8f03<\/li>\n\n\n\n<li><strong>5\u30a8\u30dd\u30c3\u30af\u3054\u3068\u306b\u4fdd\u5b58<\/strong>\uff1a<code>model.pth<\/code> \u3068\u3057\u3066\u91cd\u307f\u3092\u66f4\u65b0\u3057\u3066\u3044\u304f<\/li>\n<\/ol>\n\n\n\n<p><code>try\/except<\/code> \u3067\u56f2\u3063\u3066\u3044\u308b\u306e\u306f\u3001Colab\u306e\u30bb\u30c3\u30b7\u30e7\u30f3\u5207\u65ad\u3084GPU\u30e1\u30e2\u30ea\u4e0d\u8db3\u3067\u5b66\u7fd2\u304c\u9014\u4e2d\u3067\u6b62\u307e\u3063\u3066\u3057\u307e\u3063\u3066\u3082\u3001<strong>\u305d\u308c\u307e\u3067\u306b\u4fdd\u5b58\u3055\u308c\u305f <code>model.pth<\/code> \u306f\u6b8b\u308b<\/strong>\u3088\u3046\u306b\u3059\u308b\u305f\u3081\u3067\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u640d\u5931\u306e\u5024\u306e\u8aad\u307f\u65b9<\/h3>\n\n\n\n<p><strong>\u300closs\u304c\u4e0b\u304c\u308a\u304d\u3063\u305f\u3088\u3046\u306b\u898b\u3048\u3066\u3082\u3001\u3059\u3050\u306b\u53ce\u675f\u3057\u305f\u3068\u5224\u65ad\u3059\u308b\u306e\u306f\u65e9\u8a08\u3067\u3059\u3002<\/strong> \u62e1\u6563\u30e2\u30c7\u30eb\u306eloss\u306f\u5168\u6642\u523b\u306e\u5e73\u5747\u5024\u3067\u3042\u308a\u3001\u305d\u306e\u5927\u90e8\u5206\u3092\u300c\u7c21\u5358\u306a\u554f\u984c\u300d\u304c\u5360\u3081\u3066\u3044\u307e\u3059\u3002<math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math> \u304c\u5927\u304d\u3044\u9818\u57df\uff08\u307b\u307c\u7802\u5d50\uff09\u3067\u306f\u3001\u300c\u5165\u529b\u3068\u307b\u307c\u540c\u3058\u3082\u306e\u3092\u751f\u6210\u3059\u308b\u300d\u306e\u3067AI\u306f\u305d\u3053\u305d\u3053\u306b\u6b63\u89e3\u3067\u304d\u3066\u3057\u307e\u3046\u304b\u3089\u3067\u3059\u3002<\/p>\n\n\n\n<p>\u4e00\u65b9\u3001\u751f\u6210\u54c1\u8cea\u3092\u6c7a\u3081\u308b\u306e\u306f <math data-latex=\"t\"><semantics><mi>t<\/mi><annotation encoding=\"application\/x-tex\">t<\/annotation><\/semantics><\/math> \u304c\u5c0f\u3055\u3044\u9818\u57df\u306e\u3001\u7d30\u304b\u3044\u7cbe\u5ea6\u3067\u3059\u3002\u3053\u306e\u90e8\u5206\u306e\u6539\u5584\u306b\u3064\u3044\u3066\u306f\u3001loss\u306e\u975e\u5e38\u306b\u5c0f\u3055\u3044\u30aa\u30fc\u30c0\u30fc\u306b\u3057\u304b\u73fe\u308c\u307e\u305b\u3093\u3002\u3057\u305f\u304c\u3063\u3066<strong>loss\u306e\u6570\u5024\u3067\u306f\u306a\u304f\u3001\u5b9f\u969b\u306b\u751f\u6210\u3057\u305f\u753b\u50cf\u3092\u898b\u3066\u5224\u65ad\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b<\/strong>\u3068\u8a00\u3048\u307e\u3059\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">7. \u751f\u6210\uff1a \u7802\u5d50\u304b\u3089\u82b1\u3092\u932c\u6210\u3059\u308b\uff08\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\uff09<\/h2>\n\n\n\n<p>\u5b66\u7fd2\u304c\u7d42\u308f\u308b\u3068\u3044\u3088\u3044\u3088\u7802\u5d50\u304b\u3089\u82b1\u3092\u53d6\u308a\u51fa\u3059\u6bb5\u968e\u3067\u3059\u3002\u3053\u3053\u304b\u3089\u306f\u5b66\u7fd2\u30b9\u30af\u30ea\u30d7\u30c8\u3068\u306f\u5225\u306e\u30bb\u30eb\u3068\u3057\u3066\u3001\u751f\u6210\u5c02\u7528\u306e\u30b3\u30fc\u30c9\u3092\u7d44\u307f\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u9006\u30d7\u30ed\u30bb\u30b9\u306e\u6570\u5f0f<\/h3>\n\n\n\n<div class=\"wp-block-math\"><math display=\"block\"><semantics><mrow><msub><mi>x<\/mi><mrow><mi>t<\/mi><mo>\u2212<\/mo><mn>1<\/mn><\/mrow><\/msub><mo>=<\/mo><mfrac><mn>1<\/mn><msqrt><msub><mi>\u03b1<\/mi><mi>t<\/mi><\/msub><\/msqrt><\/mfrac><mrow><mo fence=\"true\" form=\"prefix\">(<\/mo><msub><mi>x<\/mi><mi>t<\/mi><\/msub><mo>\u2212<\/mo><mfrac><mrow><mn>1<\/mn><mo>\u2212<\/mo><msub><mi>\u03b1<\/mi><mi>t<\/mi><\/msub><\/mrow><msqrt><mrow><mn>1<\/mn><mo>\u2212<\/mo><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/mrow><\/msqrt><\/mfrac><msub><mi>\u03f5<\/mi><mi>\u03b8<\/mi><\/msub><mo form=\"prefix\" stretchy=\"false\">(<\/mo><msub><mi>x<\/mi><mi>t<\/mi><\/msub><mo separator=\"true\">,<\/mo><mi>t<\/mi><mo form=\"postfix\" stretchy=\"false\">)<\/mo><mo fence=\"true\" form=\"postfix\">)<\/mo><\/mrow><mo>+<\/mo><msub><mi>\u03c3<\/mi><mi>t<\/mi><\/msub><mi>z<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">x_{t-1} = \\frac{1}{\\sqrt{\\alpha_t}} \\left( x_t &#8211; \\frac{1 &#8211; \\alpha_t}{\\sqrt{1 &#8211; \\bar{\\alpha}t}} \\epsilon_\\theta(x_t, t) \\right) + \\sigma_t z<\/annotation><\/semantics><\/math><\/div>\n\n\n\n<p>\u3084\u3063\u3066\u3044\u308b\u3053\u3068\u306f<strong>3\u624b\u9806<\/strong>\u3067\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u62ec\u5f27\u306e\u4e2d<\/strong>\uff1a\u73fe\u5728\u306e\u753b\u50cf <math data-latex=\"x_t \"><semantics><msub><mi>x<\/mi><mi>t<\/mi><\/msub><annotation encoding=\"application\/x-tex\">x_t <\/annotation><\/semantics><\/math> \u304b\u3089\u3001\u30e2\u30c7\u30eb\u304c\u4e88\u6e2c\u3057\u305f\u30ce\u30a4\u30ba <math data-latex=\"\\epsilon_\\theta\"><semantics><msub><mi>\u03f5<\/mi><mi>\u03b8<\/mi><\/msub><annotation encoding=\"application\/x-tex\">\\epsilon_\\theta<\/annotation><\/semantics><\/math> \u3092\u3001\u4fc2\u6570\u3092\u304b\u3051\u3066<strong>\u5f15\u304f<\/strong><\/li>\n\n\n\n<li><math data-latex=\"1\/\\sqrt{\\alpha_t}\"><semantics><mrow><mn>1<\/mn><mi>\/<\/mi><msqrt><msub><mi>\u03b1<\/mi><mi>t<\/mi><\/msub><\/msqrt><\/mrow><annotation encoding=\"application\/x-tex\">1\/\\sqrt{\\alpha_t}<\/annotation><\/semantics><\/math>\uff1a\u5f15\u304d\u7b97\u3067\u7e2e\u3093\u3060\u5206\u306e\u30b9\u30b1\u30fc\u30eb\u3092<strong>\u623b\u3059<\/strong><\/li>\n\n\n\n<li><math data-latex=\"+ \\sigma_t z\"><semantics><mrow><mo>+<\/mo><msub><mi>\u03c3<\/mi><mi>t<\/mi><\/msub><mi>z<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">+ \\sigma_t z<\/annotation><\/semantics><\/math>\uff1a\u65b0\u3057\u3044\u30ce\u30a4\u30ba\u3092<strong>\u5c11\u3057\u3060\u3051\u8db3\u3057\u623b\u3059<\/strong><\/li>\n<\/ol>\n\n\n\n<p>1\u30682\u306f\u7d0d\u5f97\u3067\u304d\u308b\u3068\u3057\u3066\u3001<strong>3\u304c\u5947\u5999\u306b\u898b\u3048\u308b\u306f\u305a\u3067\u3059\u3002\u30ce\u30a4\u30ba\u3092\u6d88\u3057\u305f\u3044\u306e\u306b\u3001\u306a\u305c\u8db3\u3059\u306e\u3067\u3057\u3087\u3046\u304b\u3002<\/strong><\/p>\n\n\n\n<p>\u7406\u7531\u306f\u3001\u9006\u30d7\u30ed\u30bb\u30b9\u304c\u672c\u8cea\u7684\u306b<strong>\u78ba\u7387\u7684<\/strong>\u3060\u304b\u3089\u3067\u3059\u3002\u3042\u308b\u7802\u5d50\u304b\u3089\u5fa9\u5143\u3057\u3046\u308b\u82b1\u306f1\u8f2a\u3067\u306f\u306a\u304f\u3001\u7121\u6570\u306b\u3042\u308a\u307e\u3059\u3002\u6bce\u30b9\u30c6\u30c3\u30d7\u3092\u30ce\u30a4\u30ba\u3092\u542b\u307e\u306a\u3044\u6700\u3082\u3089\u3057\u3044\u3082\u306e\u306b\u6c7a\u3081\u6253\u3061\u3057\u3066\u3057\u307e\u3046\u3068\u3001\u751f\u6210\u3055\u308c\u308b\u753b\u50cf\u306f\u3044\u305a\u308c\u3082\u5747\u4e00\u7684\u306a\u753b\u50cf\u306b\u53ce\u675f\u3057\u307e\u3059\u3002\u5c11\u3057\u306e\u4e71\u6570\u3092\u6b8b\u3059\u3053\u3068\u3067\u3001\u30e2\u30c7\u30eb\u306f\u6bce\u56de\u9055\u3046\u82b1\u3092\u3001\u306f\u3063\u304d\u308a\u3057\u305f\u7279\u5fb4\u3068\u3068\u3082\u306b\u63cf\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u305f\u3060\u3057\u6700\u7d42\u30b9\u30c6\u30c3\u30d7\uff08<math data-latex=\"t=0\"><semantics><mrow><mi>t<\/mi><mo>=<\/mo><mn>0<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">t=0<\/annotation><\/semantics><\/math>\uff09\u3067\u306f\u3001\u51fa\u529b\u3092\u78ba\u5b9a\u3055\u305b\u308b\u305f\u3081\u306b\u30ce\u30a4\u30ba\u3092\u8db3\u3057\u307e\u305b\u3093\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u518d\u73fe\u6027\u306e\u78ba\u4fdd\uff1a\u30b7\u30fc\u30c9\u56fa\u5b9a<\/h3>\n\n\n\n<p>\u751f\u6210\u7d50\u679c\u3092\u4eba\u306b\u898b\u305b\u305f\u308a\u3001\u3042\u3068\u3067\u898b\u6bd4\u3079\u305f\u308a\u3059\u308b\u3068\u304d\u306f\u3001<strong>\u4e71\u6570\u306e\u30b7\u30fc\u30c9\u3092\u56fa\u5b9a\u3057\u3066\u304a\u304f<\/strong>\u3068\u4fbf\u5229\u3067\u3059\u3002\u540c\u3058\u30b7\u30fc\u30c9\u306a\u3089\u540c\u3058\u7802\u5d50\u304b\u3089\u59cb\u307e\u308b\u306e\u3067\u3001\u30e2\u30c7\u30eb\u306e\u91cd\u307f\u3092\u5dee\u3057\u66ff\u3048\u305f\u3068\u304d\u306e\u54c1\u8cea\u306e\u9055\u3044\u3092\u30d5\u30a7\u30a2\u306b\u6bd4\u8f03\u3067\u304d\u307e\u3059\u3002<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>import random\nimport numpy as np\n\ndef set_seed(seed=42):\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    random.seed(seed)\n    np.random.seed(seed)\n\nset_seed(43)<\/code><\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9f\u88c5<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>from tqdm.auto import tqdm\n\nmodel_path = &#039;model.pth&#039;\nif not os.path.exists(model_path):\n    model_path = &#039;model.pth&#039;\n\nmodel.load_state_dict(torch.load(model_path, map_location=device))\nmodel.to(device)\nmodel.eval()\n\nimg = torch.randn(1, 3, 64, 64).to(device)\nstep_images = []\n\nwith torch.no_grad():\n    for i in tqdm(reversed(range(T)), total=T):\n        t = torch.tensor([i], device=device).long()\n        predicted_noise = model(img, t).sample\n\n        a_t, a_cp_t = alpha[i], alpha_cumprod[i]\n        img = (1 \/ torch.sqrt(a_t)) * (\n            img - ((1 - a_t) \/ torch.sqrt(1 - a_cp_t)) * predicted_noise\n        )\n\n        if i &gt; 0:\n            b_t = beta[i]\n            img = img + torch.sqrt(b_t) * torch.randn_like(img)\n\n        img = torch.clamp(img, -1.0, 1.0)\n\n        if i % 200 == 0 or i == 0:\n            step_images.append(img.detach().cpu())<\/code><\/pre><\/div>\n\n\n\n<p>\u6570\u5f0f\u3068\u306e\u5bfe\u5fdc\u3092\u78ba\u8a8d\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6570\u5f0f<\/th><th>\u30b3\u30fc\u30c9<\/th><\/tr><\/thead><tbody><tr><td><math data-latex=\"\\epsilon_\\theta(x_t, t)\"><semantics><mrow><msub><mi>\u03f5<\/mi><mi>\u03b8<\/mi><\/msub><mo form=\"prefix\" stretchy=\"false\">(<\/mo><msub><mi>x<\/mi><mi>t<\/mi><\/msub><mo separator=\"true\">,<\/mo><mi>t<\/mi><mo form=\"postfix\" stretchy=\"false\">)<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">\\epsilon_\\theta(x_t, t)<\/annotation><\/semantics><\/math><\/td><td><code>predicted_noise<\/code><\/td><\/tr><tr><td><math data-latex=\"x_t - \\frac{1-\\alpha_t}{\\sqrt{1-\\bar{\\alpha}t}} \\epsilon_\\theta\"><semantics><mrow><msub><mi>x<\/mi><mi>t<\/mi><\/msub><mo>\u2212<\/mo><mfrac><mrow><mn>1<\/mn><mo>\u2212<\/mo><msub><mi>\u03b1<\/mi><mi>t<\/mi><\/msub><\/mrow><msqrt><mrow><mn>1<\/mn><mo>\u2212<\/mo><mover><mi>\u03b1<\/mi><mo stretchy=\"false\" class=\"tml-xshift\">\u203e<\/mo><\/mover><mi>t<\/mi><\/mrow><\/msqrt><\/mfrac><msub><mi>\u03f5<\/mi><mi>\u03b8<\/mi><\/msub><\/mrow><annotation encoding=\"application\/x-tex\">x_t &#8211; \\frac{1-\\alpha_t}{\\sqrt{1-\\bar{\\alpha}t}} \\epsilon_\\theta<\/annotation><\/semantics><\/math><\/td><td><code>img - ((1 - a_t) \/ torch.sqrt(1 - a_cp_t)) * predicted_noise<\/code><\/td><\/tr><tr><td><math data-latex=\"\\frac{1}{\\sqrt{\\alpha_t}}(\\cdots)\"><semantics><mrow><mfrac><mn>1<\/mn><msqrt><msub><mi>\u03b1<\/mi><mi>t<\/mi><\/msub><\/msqrt><\/mfrac><mo form=\"prefix\" stretchy=\"false\">(<\/mo><mo>\u22ef<\/mo><mspace width=\"0.1667em\"><\/mspace><mo form=\"postfix\" stretchy=\"false\">)<\/mo><\/mrow><annotation encoding=\"application\/x-tex\">\\frac{1}{\\sqrt{\\alpha_t}}(\\cdots)<\/annotation><\/semantics><\/math><\/td><td><code>(1 \/ torch.sqrt(a_t)) * (...)<\/code><\/td><\/tr><tr><td><math data-latex=\"+\\sigma_t z\"><semantics><mrow><mo>+<\/mo><msub><mi>\u03c3<\/mi><mi>t<\/mi><\/msub><mi>z<\/mi><\/mrow><annotation encoding=\"application\/x-tex\">+\\sigma_t z<\/annotation><\/semantics><\/math><\/td><td><code>+ torch.sqrt(b_t) * torch.randn_like(img)<\/code>\uff08<math data-latex=\"\\sigma_t = \\sqrt{\\beta_t}\"><semantics><mrow><msub><mi>\u03c3<\/mi><mi>t<\/mi><\/msub><mo>=<\/mo><msqrt><msub><mi>\u03b2<\/mi><mi>t<\/mi><\/msub><\/msqrt><\/mrow><annotation encoding=\"application\/x-tex\">\\sigma_t = \\sqrt{\\beta_t}<\/annotation><\/semantics><\/math>\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong><code>torch.clamp(img, -1.0, 1.0)<\/code><\/strong> \u306b\u3064\u3044\u3066\u88dc\u8db3\u3059\u308b\u3068\u3001\u7406\u8ad6\u4e0a\u3001\u753b\u50cf\u306f <code>[-1, 1]<\/code> \u306e\u7bc4\u56f2\u306b\u53ce\u307e\u3063\u3066\u3044\u308b\u306f\u305a\u3067\u3059\u304c\u3001\u5b9f\u969b\u306b\u306f\u30b9\u30c6\u30c3\u30d7\u3092\u91cd\u306d\u308b\u4e2d\u3067\u30e2\u30c7\u30eb\u306e\u4e88\u6e2c\u8aa4\u5dee\u304c\u84c4\u7a4d\u3057\u3001\u5024\u304c\u305d\u306e\u7bc4\u56f2\u3092\u308f\u305a\u304b\u306b\u306f\u307f\u51fa\u3059\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002\u6bce\u30b9\u30c6\u30c3\u30d7\u306e\u7d42\u308f\u308a\u306b\u30af\u30e9\u30f3\u30d7\uff08\u5024\u306e\u5207\u308a\u8a70\u3081\uff09\u3092\u5165\u308c\u3066\u304a\u304f\u3053\u3068\u3067\u3001\u8aa4\u5dee\u304c\u6b21\u306e\u30b9\u30c6\u30c3\u30d7\u306b\u5897\u5e45\u3057\u3066\u4f1d\u308f\u308b\u306e\u3092\u9632\u304e\u3001\u751f\u6210\u3092\u5b89\u5b9a\u3055\u305b\u3066\u3044\u307e\u3059\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u5b9f\u884c\u3068\u4fdd\u5b58<\/h3>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-python\" data-lang=\"Python\"><code>final_img_tensor = step_images[-1].squeeze().permute(1, 2, 0)\nfinal_img_np = ((final_img_tensor.numpy() + 1.0) \/ 2.0 * 255).astype(np.uint8)\n\nfrom PIL import Image\npil_img = Image.fromarray(final_img_np)\npil_img.save(&quot;generated_output.png&quot;)\nprint(&quot;\u2705 \u753b\u50cf\u3092 generated_output.png \u3068\u3057\u3066\u4fdd\u5b58\u3057\u307e\u3057\u305f\u3002&quot;)\n\nfig, axes = plt.subplots(1, len(step_images), figsize=(15, 3))\nfor ax, img_tensor in zip(axes, step_images):\n    display_img = (img_tensor.squeeze().permute(1, 2, 0).numpy() + 1.0) \/ 2.0\n    ax.imshow(np.clip(display_img, 0, 1))\n    ax.axis(&quot;off&quot;)\nplt.show()<\/code><\/pre><\/div>\n\n\n\n<p><code>i % 200 == 0<\/code> \u306e\u9593\u9694\u3067\u4fdd\u5b58\u3057\u3066\u3044\u308b\u306e\u3067\u3001<code>step_images<\/code> \u306b\u306f <math data-latex=\"t=800, 600, 400, 200, 0\"><semantics><mrow><mi>t<\/mi><mo>=<\/mo><mn>800<\/mn><mo separator=\"true\">,<\/mo><mn>600<\/mn><mo separator=\"true\">,<\/mo><mn>400<\/mn><mo separator=\"true\">,<\/mo><mn>200<\/mn><mo separator=\"true\">,<\/mo><mn>0<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">t=800, 600, 400, 200, 0<\/annotation><\/semantics><\/math> \u76f8\u5f53\u306e5\u679a\u304c\u4e26\u3073\u307e\u3059\u3002\u7802\u5d50\u304b\u3089\u82b1\u304c\u59ff\u3092\u73fe\u3059\u307e\u3067\u3092\u30015\u30b3\u30de\u306e\u9077\u79fb\u56f3\u3068\u3057\u3066\u78ba\u8a8d\u3067\u304d\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img decoding=\"async\" width=\"1024\" height=\"191\" src=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-8-1024x191.png\" alt=\"\" class=\"wp-image-9363\" style=\"aspect-ratio:5.36165191740413;width:808px;height:auto\" srcset=\"https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-8-1024x191.png 1024w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-8-300x56.png 300w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-8-768x143.png 768w, https:\/\/since2020.jp\/media\/wp-content\/uploads\/2026\/08\/image-8.png 1182w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\" \/>\n\n\n\n<h2 class=\"wp-block-heading\">8. \u307e\u3068\u3081\u3068\u6ce8\u610f\u70b9<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f55\u304c\u3067\u304d\u305f\u304b<\/h3>\n\n\n\n<p><math data-latex=\"64\\times 64\"><semantics><mrow><mn>64<\/mn><mo>\u00d7<\/mo><mn>64<\/mn><\/mrow><annotation encoding=\"application\/x-tex\">64\\times 64<\/annotation><\/semantics><\/math>\u3068\u3044\u3046\u4f4e\u89e3\u50cf\u5ea6\u3001\u5c0f\u3055\u306a\u30e2\u30c7\u30eb\u3001\u7121\u6599\u306eT4 GPU\u3067\u5b66\u7fd2\u3002\u3053\u306e\u6761\u4ef6\u3067\u3082\u3001<strong>\u4e71\u6570\u304b\u3089\u82b1\u304c\u751f\u6210\u3055\u308c\u308b<\/strong>\u3068\u3053\u308d\u307e\u3067\u78ba\u8a8d\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u751f\u6210\u7d50\u679c\u304c\u30a4\u30de\u30a4\u30c1\u3060\u3063\u305f\u3089<\/h3>\n\n\n\n<p>\u6539\u5584\u306e\u52b9\u304d\u3084\u3059\u3044\u9806\u306b\u6319\u3052\u307e\u3059\u3002<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u8ffd\u52a0\u5b66\u7fd2\u3092\u7e70\u308a\u8fd4\u3059<\/strong> \u2014 <code>model.pth<\/code> \u3092\u6b21\u56de\u4ee5\u964d\u306e <code>model.pth<\/code> \u3068\u3057\u3066\u4f7f\u3044\u56de\u3057\u3001\u3055\u3089\u306b\u30a8\u30dd\u30c3\u30af\u3092\u91cd\u306d\u308b\u3002<\/li>\n\n\n\n<li><strong>EMA\uff08\u6307\u6570\u79fb\u52d5\u5e73\u5747\uff09\u3092\u5c0e\u5165\u3059\u308b<\/strong> \u2014 \u30e2\u30c7\u30eb\u306e\u91cd\u307f\u306e\u79fb\u52d5\u5e73\u5747\u3067\u30b5\u30f3\u30d7\u30ea\u30f3\u30b0\u3059\u308b\u3068\u3001\u751f\u6210\u304c\u5b89\u5b9a\u3059\u308b\u3002<\/li>\n\n\n\n<li><strong>\u30e2\u30c7\u30eb\u3092\u5927\u304d\u304f\u3059\u308b<\/strong> \u2014 <code>block_out_channels<\/code> \u3092 <code>(128, 128, 256, 256)<\/code> \u306a\u3069\u306b<\/li>\n\n\n\n<li><strong>\u30d0\u30c3\u30c1\u30b5\u30a4\u30ba\u3092\u4e0a\u3052\u308b<\/strong> \u2014 \u82b1\u306e\u3088\u3046\u306b\u88ab\u5199\u4f53\u306e\u3070\u3089\u3064\u304d\u304c\u5927\u304d\u3044\u30c7\u30fc\u30bf\u3067\u306f\u3001<code>batch_size=16<\/code> \u306f\u3084\u3084\u5c0f\u3055\u3081\u3067\u3059\u3002GPU\u30e1\u30e2\u30ea\u306b\u4f59\u88d5\u304c\u3042\u308c\u307032\u301c64\u306b\u5897\u3084\u3059\u3002<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>1. \u306f\u3058\u3081\u306b\uff1a\u300c\u82b1\u3092\u5fa9\u5143\u3059\u308bAI\u300d \u307e\u305a\u306f\u3053\u308c\u3092\u898b\u3066\u304f\u3060\u3055\u3044\u3002 \u5b8c\u5168\u306a\u30e9\u30f3\u30c0\u30e0\u30ce\u30a4\u30ba\uff08\u7802\u5d50\uff09\u304b\u3089\u30011000\u30b9\u30c6\u30c3\u30d7\u304b\u3051\u3066\u82b1\u304c\u6d6e\u304b\u3073\u4e0a\u304c\u3063\u3066\u304f\u308b\u69d8\u5b50 \u6700\u521d\u306e\u30d5\u30ec\u30fc\u30e0\u306f\u3001\u305f\u3060\u306e\u7802\u5d50\u3067\u3059\u3002\u610f\u5473\u306e\u3042\u308b\u60c5\u5831\u306f1\u30d3\u30c3\u30c8\u3082\u5165\u3063\u3066\u3044\u307e [&hellip;]<\/p>\n","protected":false},"author":100,"featured_media":9276,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","swell_btn_cv_data":"","footnotes":"","_wp_rev_ctl_limit":""},"categories":[1249],"tags":[96,331,1385,286],"class_list":["post-9352","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-knowledge","tag-ai","tag-python","tag-1385","tag-286"],"_links":{"self":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/9352","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/users\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/comments?post=9352"}],"version-history":[{"count":2,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/9352\/revisions"}],"predecessor-version":[{"id":9365,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/posts\/9352\/revisions\/9365"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media\/9276"}],"wp:attachment":[{"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/media?parent=9352"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/categories?post=9352"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/since2020.jp\/media\/wp-json\/wp\/v2\/tags?post=9352"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}