CINXE.COM
ゼロから作るDeep Learning ❺ - O'Reilly Japan
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <title>ゼロから作るDeep Learning ❺ - O'Reilly Japan</title> <link rel="stylesheet" type="text/css" href="/assets/css/font-face.css"> <link rel="stylesheet" type="text/css" href="/assets/css/layout.css"> <link rel="stylesheet" type="text/css" href="/assets/css/layout-books.css"> <link rel="icon" type="image/png" href="https://www.oreilly.com/favicon.ico"> <meta name="format-detection" content="telephone=no"> <meta property="og:title" content="ゼロから作るDeep Learning ❺" /> <meta property="og:type" content="article" /> <meta property="og:image" content="https://www.oreilly.co.jp/books/images/picture_large978-4-8144-0059-1.jpeg" /> <meta property="og:image:secure_url" content="https://www.oreilly.co.jp/books/images/picture_large978-4-8144-0059-1.jpeg" /> <meta property="og:url" content="https://www.oreilly.co.jp/books/9784814400591/" /> <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:site" content="O'Reilly Japan" /> <meta name="twitter:creator" content="oreilly_japan" /> <link href="http://www.oreilly.co.jp/sales/atom.xml" type="application/atom+xml" rel="alternate" title="Sales Information" /> <meta property="og:description" content="人気シリーズの第5弾。今回のテーマは「生成モデル」です。本書では「正規分布」から「拡散モデル」に至るまでの技術を繋がりのあるストーリーとして展開します。読者は小さな学びを積み重ねながら、ステップバイステップで実装し、最終的には「Stable Diffusion」のような画像生成AIを完成させます。技術の面白さは細部にあります——ゼロから作る、数式に挑む。" /> <link rel="canonical" href="https://www.oreilly.co.jp//books/9784814400591/" /> <link rel="amphtml" href="https://www.oreilly.co.jp//books/9784814400591/mobile.html"> <!-- Google Tag Manager --> <script>(function (w, d, s, l, i) { w[l] = w[l] || []; w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' }); var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : ''; j.async = true; j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl; f.parentNode.insertBefore(j, f); })(window, document, 'script', 'dataLayer', 'GTM-KR9JFVC');</script> <!-- End Google Tag Manager --> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-M7QE72H4KS"></script> <script> window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', 'G-M7QE72H4KS'); </script> <style> .collapsible { background-color: white; border: 0; border-bottom: 1px solid rgb(246, 246, 246); margin: 0; padding: 0; cursor: pointer; width: 100%; text-align: left; outline: none; font-size: 1.5rem; font-weight: 700; line-height: 1.5em; } .collapsible:after { content: '\02795'; /* Unicode character for "plus" sign (+) */ font-size: 1.0rem; margin-left: 5px; float: right; } .collapsible-active:after { content: "\2796"; /* Unicode character for "minus" sign (-) */ } .collapsible:hover { box-shadow: 2px0 4px 8px rgb(0, 0, 0, .1); transition: box-shadow .2s; cursor: pointer; } .collapsible+section { padding: 0; margin-bottom: 30px; background-color: white; max-height: 0; overflow: hidden; transition: max-height 0.05s ease-out; } </style> </head> <body> <!-- Google Tag Manager (noscript) --> <noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-KR9JFVC" height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript> <!-- End Google Tag Manager (noscript) --> <div id="skipToMain" class="nav__SkipToMain"><a href="#maincontent">Skip to main content</a></div> <header role="banner" class="nav__GlboalHeader"> <div class="pageWidth"> <nav role="navigation" aria-label="site sections" class="nav__GlobalNav"> <a aria-current="page" title="Go to home page" class="nav__Logo" href="/"><img src="/assets/images/logo_oreilly_japan.svg" alt="O'Reilly logo"></a> <div class="nav__NavItems" id="nav__NavItems"> <div class="nav__NavGroup"> <a class="nav__Link" href="/blog/">Blog</a> <a class="nav__Link active" href="/books/">Books</a> <a class="nav__Link" href="/ebook/">Ebooks</a> <a class="nav__Link" href="/online-learning/">Learning platform</a> <a class="nav__Link" href="/order/">Order</a> <a class="nav__Link" href="/company/">About</a> </div> <div class="nav__CTA"> <a href="/ebook/cart" class="nav__Cart">Cart</a> </div> </div> <button id="nav__MobileBtn" aria-expanded="false" aria-controls="menuList" class="nav__MobileBtn"><span><span></span></span></button> </nav> </div> </header> <main id="maincontent"> <div class="pageWidth"> <h1 itemprop="name"> ゼロから作るDeep Learning ❺ </h1> <p class='sub_title'> ―生成モデル編</p> <span itemprop="publisher" content="オライリー・ジャパン"></span> <div class="contributers"> <span itemprop="author"> 斎藤 康毅 著 </span> </div> <section class="detail" itemscope itemtype="http://schema.org/Book"> <div class="detail-cover"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-8144-0059-1.jpeg" class="cover-photo" alt="[cover photo]" /> </div> <dl class="item-props"> <dt>TOPICS</dt> <dd> <a href="/books/datascience/"> Data Science </a> , <a href="/books/python/"> Python </a> </dd> <dt>発行年月日</dt> <dd itemprop="datePublished" content="2024-04-10"> 2024年04月 </dd> <dt>PRINT LENGTH</dt> <dd itemprop="numberOfPages"> 336 </dd> <dt>ISBN</dt> <dd itemprop="isbn"> 978-4-8144-0059-1 </dd> <dt>FORMAT</dt> <dd> Print PDF EPUB <div class="buying-options"> <div class="option-item"> <div class="option-name">Ebook</div> <div> 3,960円 </div> <a href="/ebook/cart?id=978-4-8144-0059-1&mode=add" class="update-button secondary-button"> Ebookを購入する </a> </div> <div class="option-item"> <div class="option-name">Print</div> <div> 3,960円 </div> <div class="link-book-order">書籍のご注文は<a href="https://www.ohmsha.co.jp/book/9784814400591/">オーム社サイト</a>へ </div> </div> </div> </dd> </dl> <div id="detail" class="content"> <p itemprop="description"> 人気シリーズの第5弾。今回のテーマは「生成モデル」です。本書では「正規分布」から「拡散モデル」に至るまでの技術を繋がりのあるストーリーとして展開します。読者は小さな学びを積み重ねながら、ステップバイステップで実装し、最終的には「Stable Diffusion」のような画像生成AIを完成させます。技術の面白さは細部にあります——ゼロから作る、数式に挑む。 </p> </div> <h2 class="collapsible">関連ファイル</h2> <section> <ul> <li><a href="https://github.com/oreilly-japan/deep-learning-from-scratch-5"> サンプルコード </a></li> </ul> </section> <h2 class="collapsible">正誤表</h2> <section> <p> ここで紹介する正誤表には、書籍発行後に気づいた誤植や更新された情報を掲載しています。以下のリストに記載の年月は、正誤表を作成し、増刷書籍を印刷した月です。お手持ちの書籍では、すでに修正が施されている場合がありますので、書籍最終ページの奥付でお手持ちの書籍の刷版、刷り年月日をご確認の上、ご利用ください。 </p> <h3> 正誤表 </h3> <div class="errata"> <a href="https://github.com/oreilly-japan/deep-learning-from-scratch-5/wiki/errata">https://github.com/oreilly-japan/deep-learning-from-scratch-5/wiki/errata</a><br> </div> </section> <h2 class="collapsible">目次</h2> <section> <div id="toc"> <pre>まえがき ステップ1 正規分布 1.1 確率の基礎 1.1.1 確率変数と確率分布 1.1.2 確率分布の種類 1.1.3 期待値と分散 1.2 正規分布 1.2.1 正規分布の確率密度関数 1.2.2 正規分布のコード 1.2.3 パラメータの役割 1.3 中心極限定理 1.3.1 中心極限定理とは 1.3.2 中心極限定理の実験 1.4 サンプル和の確率分布 1.4.1 サンプル和の期待値と分散 1.4.2 コードで確かめる 1.4.3 一様分布の平均と分散 1.5 身の回りにある正規分布 ステップ2 最尤推定 2.1 生成モデルの概要 2.1.1 生成モデルとは 2.1.2 母集団とサンプル 2.2 実データを使った生成モデルの実装 2.2.1 身長データセットの読み込み 2.2.2 正規分布による生成モデル 2.3 最尤推定の理論 2.3.1 尤度の最大化 2.3.2 微分を使って最大値を探す 2.3.3 正規分布の最尤推定 2.4 生成モデルの用途 2.4.1 新しいデータの生成 2.4.2 確率の計算 ステップ3 多次元正規分布 3.1 NumPyと多次元配列 3.1.1 多次元配列 3.1.2 NumPyの多次元配列 3.1.3 要素ごとの演算 3.1.4 ベクトルの内積と行列積 3.2 多次元正規分布 3.2.1 多次元正規分布の式 3.2.2 多次元正規分布の実装 3.3 2次元正規分布の可視化 3.3.1 3Dグラフの描画方法 3.3.2 等高線の描画 3.3.3 2次元正規分布のグラフ 3.4 多次元正規分布の最尤推定 3.4.1 最尤推定を行う 3.4.2 最尤推定の実装 3.4.3 実データを使う ステップ4 混合ガウスモデル 4.1 身の回りにある多峰性分布 4.1.1 多峰性分布のデータセット 4.2 混合ガウスモデルのデータ生成 4.2.1 GMMによるデータ生成 4.2.2 データ生成を行うコード 4.3 混合ガウスモデルの式 4.3.1 確率の復習 4.3.2 GMMの式 4.3.3 GMMの実装 4.4 パラメータ推定の難所 4.4.1 GMMのパラメータ推定 ステップ5 EMアルゴリズム 5.1 KLダイバージェンス 5.1.1 数式の表記について 5.1.2 KLダイバージェンスの定義式 5.1.3 KLダイバージェンスと最尤推定の関係 5.2 EMアルゴリズムの導出① 5.2.1 潜在変数を持つモデル 5.2.2 任意の確率分布q(z) 5.3 EMアルゴリズムの導出② 5.3.1 ELBO(エビデンスの下界) 5.3.2 EMアルゴリズムへ 5.3.3 複数データへの拡張 5.3.4 log p(x; θnew) ≥ log p(x; θold)の証明 5.4 GMMと EMアルゴリズム 5.4.1 EMアルゴリズムのEステップ 5.4.2 EMアルゴリズムのMステップ 5.5 EMアルゴリズムの実装 5.5.1 データセットとGMMのコード 5.5.2 EステップとMステップの実装 5.5.3 データ生成 ステップ6 ニューラルネットワーク 6.1 PyTorchと勾配法 6.1.1 PyTorchのインストール 6.1.2 テンソルの計算 6.1.3 勾配法 6.2 線形回帰 6.2.1 トイ・データセット 6.2.2 線形回帰の理論 6.2.3 線形回帰の実装 6.3 パラメータとオプティマイザ 6.3.1 ParameterとModuleクラス 6.3.2 オプティマイザ 6.4 ニューラルネットワークの実装 6.4.1 非線形なデータセット 6.4.2 線形変換と活性化関数 6.4.3 ニューラルネットワークの実装 6.5 torchvisionとデータセット 6.5.1 torchvisionのインストール 6.5.2 MNISTデータセット 6.5.3 前処理 6.5.4 データローダ ステップ7 変分オートエンコーダ(VAE) 7.1 VAEとデコーダ 7.1.1 1つの正規分布 7.1.2 混合ガウスモデル(GMM) 7.1.3 VAE 7.1.4 EMアルゴリズムの問題点 7.2 VAEとエンコーダ 7.2.1 EMアルゴリズムからVAEへ 7.2.2 データセット全体への適用 7.3 ELBOの最適化 7.3.1 ELBOの評価 7.3.2 変数変換トリック 7.4 VAEの実装 7.4.1 実装の方針 7.4.2 VAEのコード 7.4.3 学習を行うコード 7.4.4 新しい画像の生成 ステップ8 拡散モデルの理論 8.1 VAEから拡散モデルへ 8.1.1 VAEの復習 8.1.2 潜在変数の階層化 8.1.3 拡散モデルへ 8.2 拡散過程と逆拡散過程 8.2.1 拡散過程 8.2.2 逆拡散過程 8.3 ELBOの計算① 8.3.1 拡散モデルのELBO 8.3.2 ELBOの式展開 8.4 ELBOの計算② 8.4.1 q(xt | x0)の式 8.4.2 ELBOの近似解 8.4.3 q(xt | x0)の導出 8.5 ELBOの計算③ 8.5.1 q(xt−1 | xt, x0)の式 8.5.2 ELBOの近似解 8.5.3 q(xt−1 | xt, x0)の導出 8.6 拡散モデルの学習 8.6.1 ニューラルネットワークは何を予測するか? 8.6.2 元データを復元するニューラルネットワーク 8.6.3 ノイズを予測するニューラルネットワーク 8.6.4 新しいデータのサンプリング ステップ9 拡散モデルの実装 9.1 U-Net 9.1.1 U-Netとは 9.1.2 U-Netの実装 9.2 正弦波位置エンコーディング 9.2.1 正弦波位置エンコーディングとは 9.2.2 正弦波位置エンコーディングの実装 9.2.3 U-Netに組み込む 9.3 拡散過程 9.3.1 q(xt | xt−1)からのサンプリング 9.3.2 画像に対する拡散過程 9.3.3 q(xt | x0)からのサンプリング 9.3.4 Diffuserクラスの実装 9.4 データ生成 9.4.1 ワンステップのデノイズ処理 9.4.2 データ生成の実装 9.5 拡散モデルの学習(実装編) 9.5.1 拡散モデルの学習コード 9.5.2 学習結果 ステップ10 拡散モデルの応用 10.1 条件付き拡散モデル 10.1.1 拡散モデルに条件を追加する 10.1.2 条件付き拡散モデルの実装 10.2 スコア関数 10.2.1 スコア関数とは 10.2.2 式(10.1)の証明 10.3 分類器ガイダンス 10.3.1 分類器とは 10.3.2 分類器ガイダンスの導出 10.4 分類器なしガイダンス 10.4.1 分類器なしガイダンスの理論 10.4.2 分類器なしガイダンスの実装 10.5 Stable Diffusion 10.5.1 Stable Diffusionの仕組み 10.5.2 Diffusersライブラリ 付録A 多次元正規分布の最尤推定の導出 A.1 μの最尤推定 A.2 2次形式の微分(式(A.4)の証明) A.3 Σの最尤推定 A.4 トレースと微分(式(A.12)の証明) 付録B イェンセンの不等式 B.1 凸関数とイェンセンの不等式 B.2 凹関数とlog関数 B.3 ELBOの導出 付録C 階層型VAEの理論と実装 C.1 2階層VAEの構成要素 C.2 ELBOの式展開 C.3 モンテカルロ法によるELBOの近似 C.4 2階層VAEの実装 C.5 実装コード 付録D 数式記号一覧 D.1 本書で使用する記号 D.2 本書で使用する数式 おわりに 参考文献 索引 コラム目次 「積の対数」は「対数の和」 EMアルゴリズムにおける対数尤度の単調増加性 ラグランジュの未定乗数法 2つの正規分布間のKLダイバージェンス イェンセンの不等式を使ったELBOの導出 L1ノルムとL2ノルム 期待値の線形性と関連する変数の期待値 正規分布の平均ベクトルに関する微分 正規分布の共分散行列に関する微分 </pre> </div> </section> </section> <div class="related-items"> <div class="sectionRow__Heading"> <h3>関連書籍</h3> </div> <div class="sectionRow__Content"> <div class="sectionRow__BestLarge" id="bestEbooks"> <div class="sectionRow__Best" id="bestEbook1"> <a class="sectionRow__BestCover" href="/books/9784873119953/"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-87311-995-3.jpeg" alt="機械学習エンジニアのためのTransformers"> </a> </div> <div class="sectionRow__Best hidden" id="bestEbook2"> <a class="sectionRow__BestCover" href="/books/9784814400874/"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-8144-0087-4.jpeg" alt="コンピュータシステムの理論と実装 第2版"> </a> </div> <div class="sectionRow__Best hidden" id="bestEbook3"> <a class="sectionRow__BestCover" href="/books/9784814400751/"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-8144-0075-1.jpeg" alt="生成 Deep Learning 第2版"> </a> </div> <div class="sectionRow__Best hidden" id="bestEbook4"> <a class="sectionRow__BestCover" href="/books/9784873117584/"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-87311-758-4.jpeg" alt="ゼロから作るDeep Learning"> </a> </div> <div class="sectionRow__Best hidden" id="bestEbook5"> <a class="sectionRow__BestCover" href="/books/9784873119755/"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-87311-975-5.jpeg" alt="ゼロから作るDeep Learning ❹"> </a> </div> <div class="sectionRow__Best hidden" id="bestEbook6"> <a class="sectionRow__BestCover" href="/books/9784873119069/"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-87311-906-9.jpeg" alt="ゼロから作るDeep Learning ❸ "> </a> </div> <div class="sectionRow__Best hidden" id="bestEbook7"> <a class="sectionRow__BestCover" href="/books/9784873118369/"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-87311-836-9.jpeg" alt="ゼロから作るDeep Learning ❷"> </a> </div> </div> <div class="sectionRow__BestThumbnails" id="bestEbookThumbnails"> <button class="sectionRow__bestBookThumbnailsItem" data-for="bestEbook1" aria-selected="true" title="機械学習エンジニアのためのTransformers"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-87311-995-3.jpeg" alt="94561304286856"> </button> <button class="sectionRow__bestBookThumbnailsItem" data-for="bestEbook2" title="コンピュータシステムの理論と実装 第2版"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-8144-0087-4.jpeg" alt="94561304695160"> </button> <button class="sectionRow__bestBookThumbnailsItem" data-for="bestEbook3" title="生成 Deep Learning 第2版"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-8144-0075-1.jpeg" alt="94561306645952"> </button> <button class="sectionRow__bestBookThumbnailsItem" data-for="bestEbook4" title="ゼロから作るDeep Learning"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-87311-758-4.jpeg" alt="94561306749064"> </button> <button class="sectionRow__bestBookThumbnailsItem" data-for="bestEbook5" title="ゼロから作るDeep Learning ❹"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-87311-975-5.jpeg" alt="94561306682008"> </button> <button class="sectionRow__bestBookThumbnailsItem" data-for="bestEbook6" title="ゼロから作るDeep Learning ❸ "> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-87311-906-9.jpeg" alt="94561306813472"> </button> <button class="sectionRow__bestBookThumbnailsItem" data-for="bestEbook7" title="ゼロから作るDeep Learning ❷"> <img src="https://www.oreilly.co.jp/books/images/picture_large978-4-87311-836-9.jpeg" alt="94561306813496"> </button> </div> </div> </div> </div> </main> <footer class="footer__FooterSection"> <div class="pageWidth"> <div class="footer__FooterTop"> <div class="footer__List"> <h3><a href="https://www.oreilly.co.jp/company/">About</a></h3> <a href="https://www.oreilly.co.jp/company/">会社概要</a> <a href="https://www.oreilly.co.jp/orj/privacypolicy.shtml">個人情報について</a> <a href="http://www.oreilly.com/">O’Reilly Media</a> </div> <div class="footer__List"> <h3><a href="https://www.oreilly.co.jp/company/">Support</a></h3> <a href="https://www.oreilly.co.jp/feedback/">Contact us</a> <a href="https://www.oreilly.co.jp/bookclub/">Bookclub</a> <a href="https://www.oreilly.co.jp/order/">書籍注文</a> </div> <div class="footer__Apps"> <h3>Download the O’Reilly app</h3> <p>Take O’Reilly with you and learn anywhere, anytime on your phone <span class="nowrap">and tablet.</span> </p> <div class="footer__DownloadLinks"> <a href="https://itunes.apple.com/us/app/safari-to-go/id881697395"><img src="https://cdn.oreillystatic.com/oreilly/images/app-store-logo.png" alt="Apple app store"></a> <a href="https://play.google.com/store/apps/details?id=com.safariflow.queue"><img src="https://cdn.oreillystatic.com/oreilly/images/google-play-logo.png" alt="Google play store"></a> </div> </div> </div> <div class="footer__FooterBase"> <a aria-current="page" class="footer__Logo" title="Go to home page" href="/"><img src="/assets/images/logo_oreilly_japan.svg" alt="O'Reilly logo"></a> <p>© 2022, O’Reilly Japan, Inc. oreilly.co.jpに掲載されているすべてのトレードマークおよび登録商標は、それぞれの所有者に帰属します。</p> </div> </div> </footer> <script src="/assets/scripts/cookies-policy.6d8dcbae.js"></script> <script> let coll = document.getElementsByClassName('collapsible'); for (let i = 0; i < coll.length; i++) { let button = document.createElement('button'); button.setAttribute('type', 'button'); button.setAttribute('class', 'collapsible'); button.textContent = coll[i].textContent; coll[i].replaceWith(button) coll[i].addEventListener('click', function () { this.classList.toggle('collapsible-active'); let content = this.nextElementSibling; if (content.style.maxHeight) { content.style.maxHeight = null; } else { content.style.maxHeight = content.scrollHeight + 'px'; } }); } </script> <script src="/assets/scripts/scripts.js"></script> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-6009404-1"></script> <script> window.dataLayer = window.dataLayer || []; function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('config', 'UA-6009404-1'); </script> </body> </html>