プリンキピア

前付けまえがき

About This Book この本について

This book represents our attempt to make deep learning approachable, teaching you the concepts, the context, and the code.
英語のヒント
この本は、深層学習を身近なものにしようとする私たちの試みである。概念、その背景、そしてコードを教える。

One Medium Combining Code, Math, and HTML コード、数学、HTMLを一つにまとめた媒体

For any computing technology to reach its full impact, it must be well understood, well documented, and supported by mature, well-maintained tools. The key ideas should be clearly distilled, minimizing the onboarding time needed to bring new practitioners up to date. Mature libraries should automate common tasks, and exemplar code should make it easy for practitioners to modify, apply, and extend common applications to suit their needs.
英語のヒント
どんな計算技術も、その影響力を十分に発揮するには、よく理解され、よく文書化され、成熟し手入れの行き届いた道具に支えられていなければならない。鍵となる考え方ははっきりと煮詰められ、新しく加わる実践者が追いつくまでの立ち上がりの時間を最小限にすべきである。成熟したライブラリはよくある作業を自動化し、手本となるコードは、実践者がよくある応用を自分の必要に合わせて修正し、適用し、拡張するのをたやすくすべきである。
As an example, take dynamic web applications. Despite a large number of companies, such as Amazon, developing successful database-driven web applications in the 1990s, the potential of this technology to aid creative entrepreneurs was realized to a far greater degree only in the past ten years, owing in part to the development of powerful, well-documented frameworks.
英語のヒント
一例として、動的なWebアプリケーションを考えてみよう。1990年代には、Amazonをはじめ多くの企業がデータベースを使ったWebアプリケーションを成功させていたにもかかわらず、この技術が創造的な起業家を助ける力がはるかに大きく発揮されたのは、ここ10年ほどのことにすぎない。その理由の一つは、強力でよく文書化されたフレームワークが開発されたことにある。
Testing the potential of deep learning presents unique challenges because any single application brings together various disciplines. Applying deep learning requires simultaneously understanding (i) the motivations for casting a problem in a particular way; (ii) the mathematical form of a given model; (iii) the optimization algorithms for fitting the models to data; (iv) the statistical principles that tell us when we should expect our models to generalize to unseen data and practical methods for certifying that they have, in fact, generalized; and (v) the engineering techniques required to train models efficiently, navigating the pitfalls of numerical computing and getting the most out of available hardware. Teaching the critical thinking skills required to formulate problems, the mathematics to solve them, and the software tools to implement those solutions all in one place presents formidable challenges. Our goal in this book is to present a unified resource to bring would-be practitioners up to speed.
英語のヒント
深層学習の可能性を試すことには独特の難しさがある。一つの応用がさまざまな分野をまとめ上げるからである。深層学習を応用するには、次のことを同時に理解する必要がある。(i)問題をある特定の形で定式化する動機。(ii)与えられたモデルの数学的な形。(iii)モデルをデータに当てはめるための最適化アルゴリズム。(iv)モデルが未知のデータに汎化すると期待してよいのはいつかを教えてくれる統計的原理と、実際に汎化したことを確かめる実用的な方法。(v)数値計算の落とし穴を避け、手持ちのハードウェアを最大限に生かしながら、モデルを効率よく訓練するのに必要な工学的技術。問題を定式化するのに必要な批判的思考の技能、それを解く数学、そしてその解を実装するソフトウェアの道具を、すべて一か所で教えることは、手ごわい課題である。この本での私たちの目標は、実践者を目指す人々が一気に追いつけるような、一つにまとまった資料を提供することである。
When we started this book project, there were no resources that simultaneously (i) remained up to date; (ii) covered the breadth of modern machine learning practices with sufficient technical depth; and (iii) interleaved exposition of the quality one expects of a textbook with the clean runnable code that one expects of a hands-on tutorial. We found plenty of code examples illustrating how to use a given deep learning framework (e.g., how to do basic numerical computing with matrices in TensorFlow) or for implementing particular techniques (e.g., code snippets for LeNet, AlexNet, ResNet, etc.) scattered across various blog posts and GitHub repositories. However, these examples typically focused on how to implement a given approach, but left out the discussion of why certain algorithmic decisions are made. While some interactive resources have popped up sporadically to address a particular topic, e.g., the engaging blog posts published on the website Distill, or personal blogs, they only covered selected topics in deep learning, and often lacked associated code. On the other hand, while several deep learning textbooks have emerged—e.g., Goodfellow et al. (2016), which offers a comprehensive survey on the basics of deep learning—these resources do not marry the descriptions to realizations of the concepts in code, sometimes leaving readers clueless as to how to implement them. Moreover, too many resources are hidden behind the paywalls of commercial course providers.
英語のヒント
この本の企画を始めたとき、次の三つを同時に満たす資料はなかった。(i)最新であり続けている。(ii)現代の機械学習の実践の幅を十分な技術的深さで扱っている。(iii)教科書に期待される質の説明と、実地のチュートリアルに期待される、きれいで実行できるコードとを織り交ぜている。ある深層学習フレームワークの使い方を示すコード例(たとえば、TensorFlowで行列を使った基本的な数値計算をする方法)や、特定の手法を実装するコード例(たとえば、LeNet、AlexNet、ResNetなどのコード断片)は、さまざまなブログ記事やGitHubのリポジトリにたくさん散らばっていた。しかし、そうした例はたいてい、ある手法をどう実装するかに焦点を当てており、なぜそのアルゴリズム上の決定がなされるのかという議論は抜け落ちていた。特定の話題を扱う対話的な資料もときおり現れた。たとえばDistillというWebサイトで公開された魅力的なブログ記事や、個人のブログである。しかし、それらは深層学習の一部の話題しか扱わず、関連するコードを欠いていることが多かった。一方で、深層学習の教科書もいくつか登場した。たとえば深層学習の基礎を包括的に概観したGoodfellow et al. (2016)である。だが、こうした資料は説明とコードによる概念の実現とを結びつけておらず、読者がそれをどう実装すればよいか見当もつかないまま取り残されることもあった。さらに、あまりに多くの資料が、商業的な講座提供者の有料の壁の向こうに隠されている。
We set out to create a resource that could (i) be freely available for everyone; (ii) offer sufficient technical depth to provide a starting point on the path to actually becoming an applied machine learning scientist; (iii) include runnable code, showing readers how to solve problems in practice; (iv) allow for rapid updates, both by us and also by the community at large; and (v) be complemented by a forum for interactive discussion of technical details and to answer questions.
英語のヒント
私たちが作ろうとしたのは、次のような資料である。(i)誰でも無料で利用できる。(ii)実際に応用機械学習の科学者になる道の出発点となるのに十分な技術的深さがある。(iii)実行できるコードを含み、問題を実際にどう解くかを読者に示す。(iv)私たち自身によっても、また広くコミュニティによっても、すばやく更新できる。(v)技術的な細部について対話的に議論し、質問に答えるフォーラムによって補われている。
These goals were often in conflict. Equations, theorems, and citations are best managed and laid out in LaTeX. Code is best described in Python. And webpages are native in HTML and JavaScript. Furthermore, we want the content to be accessible both as executable code, as a physical book, as a downloadable PDF, and on the Internet as a website. No workflows seemed suited to these demands, so we decided to assemble our own (Section 23.6). We settled on GitHub to share the source and to facilitate community contributions; Jupyter notebooks for mixing code, equations and text; Sphinx as a rendering engine; and Discourse as a discussion platform. While our system is not perfect, these choices strike a compromise among the competing concerns. We believe that Dive into Deep Learning might be the first book published using such an integrated workflow.
英語のヒント
これらの目標はしばしば互いに対立した。数式、定理、引用はLaTeXで管理し組版するのが最もよい。コードはPythonで記述するのが最もよい。そしてWebページはHTMLとJavaScriptが本来の姿である。さらに私たちは、内容を、実行できるコードとしても、紙の本としても、ダウンロードできるPDFとしても、インターネット上のWebサイトとしても利用できるようにしたかった。こうした要求に合うワークフローは見当たらなかったので、自分たちで組み立てることにした(23.6節)。ソースの共有とコミュニティからの貢献の促進にはGitHub、コードと数式と文章を混ぜるにはJupyterノートブック、描画エンジンにはSphinx、議論の場にはDiscourseを選んだ。私たちのシステムは完璧ではないが、これらの選択は競合する要求のあいだの妥協点を押さえている。『Dive into Deep Learning』は、このように統合されたワークフローを使って出版された最初の本かもしれないと私たちは考えている。

Learning by Doing やりながら学ぶ

Many textbooks present concepts in succession, covering each in exhaustive detail. For example, the excellent textbook of Bishop (2006), teaches each topic so thoroughly that getting to the chapter on linear regression requires a nontrivial amount of work. While experts love this book precisely for its thoroughness, for true beginners, this property limits its usefulness as an introductory text.
英語のヒント
多くの教科書は、概念を順々に提示し、一つひとつを細部まで徹底して扱う。たとえば、Bishop (2006)の優れた教科書は、どの話題もあまりに徹底して教えるので、線形回帰の章にたどり着くまでにかなりの労力が要る。専門家はまさにその徹底ぶりゆえにこの本を愛するが、本当の初心者にとっては、この性質が入門書としての有用性を狭めてしまう。
In this book, we teach most concepts just in time. In other words, you will learn concepts at the very moment that they are needed to accomplish some practical end. While we take some time at the outset to teach fundamental preliminaries, like linear algebra and probability, we want you to taste the satisfaction of training your first model before worrying about more esoteric concepts.
英語のヒント
この本では、ほとんどの概念を必要になったその時に教える。言い換えれば、何か実際的な目的を果たすのにその概念が必要になった、まさにその瞬間に学ぶことになる。線形代数や確率のような基本的な準備は最初に少し時間をかけて教えるが、もっと難解な概念を気にかける前に、最初のモデルを訓練する満足感を味わってほしい。
Aside from a few preliminary notebooks that provide a crash course in the basic mathematical background, each subsequent chapter both introduces a reasonable number of new concepts and provides several self-contained working examples, using real datasets. This presented an organizational challenge. Some models might logically be grouped together in a single notebook. And some ideas might be best taught by executing several models in succession. By contrast, there is a big advantage to adhering to a policy of one working example, one notebook: This makes it as easy as possible for you to start your own research projects by leveraging our code. Just copy a notebook and start modifying it.
英語のヒント
基本的な数学の背景を駆け足で学ぶいくつかの準備用のノートブックを除けば、その後の各章は、ほどよい数の新しい概念を導入するとともに、実際のデータセットを使った、それだけで完結した動く例をいくつか示す。これは構成上の難題となった。いくつかのモデルは、論理的には一つのノートブックにまとめたほうがよいかもしれない。また、いくつかの考え方は、複数のモデルを続けて実行することで最もよく教えられるかもしれない。一方で、一つの動く例に一つのノートブックという方針を守ることには大きな利点がある。私たちのコードを活用して、自分自身の研究計画をできるだけ簡単に始められるのである。ノートブックをコピーして、修正し始めればよい。
Throughout, we interleave the runnable code with background material as needed. In general, we err on the side of making tools available before explaining them fully (often filling in the background later). For instance, we might use stochastic gradient descent before explaining why it is useful or offering some intuition for why it works. This helps to give practitioners the necessary ammunition to solve problems quickly, at the expense of requiring the reader to trust us with some curatorial decisions.
英語のヒント
本書全体を通じて、実行できるコードと、必要に応じた背景となる題材とを織り交ぜる。一般に、道具を完全に説明する前に使えるようにするほうを選ぶ(背景はしばしば後から補う)。たとえば、確率的勾配降下法がなぜ役に立つのかを説明したり、なぜうまく働くのかについての直観を示したりする前に、それを使うかもしれない。こうすることで、実践者は問題をすばやく解くのに必要な武器を手にできる。その代わり、読者には、何を選んで載せるかについての私たちの判断を信頼してもらう必要がある。
This book teaches deep learning concepts from scratch. Sometimes, we delve into fine details about models that would typically be hidden from users by modern deep learning frameworks. This comes up especially in the basic tutorials, where we want you to understand everything that happens in a given layer or optimizer. In these cases, we often present two versions of the example: one where we implement everything from scratch, relying only on NumPy-like functionality and automatic differentiation, and a more practical example, where we write succinct code using the high-level APIs of deep learning frameworks. After explaining how some component works, we rely on the high-level API in subsequent tutorials.
英語のヒント
この本は、深層学習の概念をゼロから教える。現代の深層学習フレームワークでは普通は利用者から隠されているモデルの細部に、踏み込むこともある。とりわけ基本的なチュートリアルでは、ある層や最適化手法の中で起きていることをすべて理解してほしいので、そうなる。こうした場合、例を二つの版で示すことが多い。一つは、NumPyのような機能と自動微分だけに頼り、すべてをゼロから実装する版である。もう一つは、深層学習フレームワークの高水準APIを使って簡潔なコードを書く、より実用的な版である。ある部品の働きを説明した後は、それ以降のチュートリアルでは高水準APIに頼る。

Content and Structure 内容と構成

The book can be divided into roughly three parts, dealing with preliminaries, deep learning techniques, and advanced topics focused on real systems and applications (Fig. 1).
英語のヒント
この本はおおまかに三つの部分に分けられる。準備、深層学習の技術、そして実際のシステムと応用に焦点を当てた発展的な話題である(図1)。
Fig. 1図1
Book structure.
英語のヒント
本の構成。
  • Part 1: Basics and Preliminaries. Section 1 is an introduction to deep learning. Then, in Section 2, we quickly bring you up to speed on the prerequisites required for hands-on deep learning, such as how to store and manipulate data, and how to apply various numerical operations based on elementary concepts from linear algebra, calculus, and probability. Section 3 and Section 5 cover the most fundamental concepts and techniques in deep learning, including regression and classification; linear models; multilayer perceptrons; and overfitting and regularization.
    英語のヒント
    第1部:基礎と準備。第1章は深層学習への入門である。次に第2章では、実際に手を動かす深層学習に必要な前提知識を手早く身につける。たとえば、データをどう保存し操作するか、線形代数・微積分・確率の初歩的な概念にもとづくさまざまな数値演算をどう適用するか、である。第3章と第5章では、回帰と分類、線形モデル、多層パーセプトロン、過学習と正則化を含む、深層学習の最も基本的な概念と技術を扱う。
  • Part 2: Modern Deep Learning Techniques. Section 6 describes the key computational components of deep learning systems and lays the groundwork for our subsequent implementations of more complex models. Next, Section 7 and Section 8 present convolutional neural networks (CNNs), powerful tools that form the backbone of most modern computer vision systems. Similarly, Section 9 and Section 10 introduce recurrent neural networks (RNNs), models that exploit sequential (e.g., temporal) structure in data and are commonly used for natural language processing and time series prediction. In Section 11, we describe a relatively new class of models, based on so-called attention mechanisms, that has displaced RNNs as the dominant architecture for most natural language processing tasks. These sections will bring you up to speed on the most powerful and general tools that are widely used by deep learning practitioners.
    英語のヒント
    第2部:現代の深層学習の技術。第6章では、深層学習システムの鍵となる計算上の部品を説明し、より複雑なモデルを後で実装するための土台を築く。次に第7章と第8章では、畳み込みニューラルネットワーク(CNN)を紹介する。これは、現代のコンピュータビジョンのシステムのほとんどで背骨となっている強力な道具である。同様に、第9章と第10章では、リカレントニューラルネットワーク(RNN)を導入する。これはデータの系列的な(たとえば時間的な)構造を利用するモデルで、自然言語処理や時系列予測でよく使われる。第11章では、いわゆるアテンション機構にもとづく比較的新しい種類のモデルを説明する。これは、自然言語処理のほとんどの課題で、支配的なアーキテクチャとしてRNNに取って代わった。これらの章を読めば、深層学習の実践者に広く使われている最も強力で汎用的な道具に追いつくことができる。
  • Part 3: Scalability, Efficiency, and Applications (available online). In Chapter 12, we discuss several common optimization algorithms used to train deep learning models. Next, in Chapter 13, we examine several key factors that influence the computational performance of deep learning code. Then, in Chapter 14, we illustrate major applications of deep learning in computer vision. Finally, in Chapter 15 and Chapter 16, we demonstrate how to pretrain language representation models and apply them to natural language processing tasks.
    英語のヒント
    第3部:スケーラビリティ、効率、応用(オンラインで公開)。第12章では、深層学習モデルの訓練に使われる一般的な最適化アルゴリズムをいくつか論じる。次に第13章では、深層学習のコードの計算性能に影響する鍵となる要因をいくつか検討する。続いて第14章では、コンピュータビジョンにおける深層学習の主要な応用を示す。最後に、第15章と第16章では、言語表現モデルを事前学習し、それを自然言語処理の課題に応用する方法を示す。

Code コード

Most sections of this book feature executable code. We believe that some intuitions are best developed via trial and error, tweaking the code in small ways and observing the results. Ideally, an elegant mathematical theory might tell us precisely how to tweak our code to achieve a desired result. However, deep learning practitioners today must often tread where no solid theory provides guidance. Despite our best attempts, formal explanations for the efficacy of various techniques are still lacking, for a variety of reasons: the mathematics to characterize these models can be so difficult; the explanation likely depends on properties of the data that currently lack clear definitions; and serious inquiry on these topics has only recently kicked into high gear. We are hopeful that as the theory of deep learning progresses, each future edition of this book will provide insights that eclipse those presently available.
英語のヒント
この本のほとんどの節には、実行できるコードが含まれている。直観のなかには、コードを少しずつ変えて結果を観察するという試行錯誤によって最もよく養われるものがあると私たちは考えている。理想的には、エレガントな数学の理論が、望む結果を得るためにコードをどう調整すればよいかを正確に教えてくれるかもしれない。しかし今日の深層学習の実践者は、しっかりした理論が手引きとならない場所に踏み込まなければならないことが多い。私たちが最善を尽くしても、さまざまな手法がなぜ有効なのかについての形式的な説明は、いまだに欠けている。理由はいろいろある。これらのモデルを特徴づける数学がとても難しいこともある。説明は、今のところ明確な定義のないデータの性質に依存しているようである。そして、こうした話題についての本格的な探究は、最近になってようやく本腰が入ったところである。深層学習の理論が進むにつれ、この本の将来の版がそれぞれ、今得られるものを凌駕する洞察を提供できることを願っている。
To avoid unnecessary repetition, we capture some of our most frequently imported and used functions and classes in the d2l package. Throughout, we mark blocks of code (such as functions, classes, or collection of import statements) with #@save to indicate that they will be accessed later via the d2l package. We offer a detailed overview of these classes and functions in Section 23.8. The d2l package is lightweight and only requires the following dependencies:
英語のヒント
不要な繰り返しを避けるため、最もよくインポートされ使われる関数やクラスの一部をd2lパッケージにまとめている。本書全体を通じて、後でd2lパッケージから呼び出すコードのブロック(関数、クラス、import文の集まりなど)には、#@saveという印を付ける。これらのクラスと関数の詳しい概要は23.8節で示す。d2lパッケージは軽量で、必要とする依存関係は次のものだけである。
#@save
import collections
import hashlib
import inspect
import math
import os
import random
import re
import shutil
import sys
import tarfile
import time
import zipfile
from collections import defaultdict
import pandas as pd
import requests
from IPython import display
from matplotlib import pyplot as plt
from matplotlib_inline import backend_inline

d2l = sys.modules[__name__]
Most of the code in this book is based on PyTorch, a popular open-source framework that has been enthusiastically embraced by the deep learning research community. All of the code in this book has passed tests under the latest stable version of PyTorch. However, due to the rapid development of deep learning, some code in the print edition may not work properly in future versions of PyTorch. We plan to keep the online version up to date. In case you encounter any problems, please consult Installation to update your code and runtime environment. Below lists dependencies in our PyTorch implementation.
英語のヒント
この本のコードのほとんどは、深層学習の研究コミュニティに熱心に受け入れられている人気のオープンソースフレームワーク、PyTorchにもとづいている。この本のコードはすべて、PyTorchの最新の安定版でテストを通っている。しかし、深層学習の発展は速いので、印刷版のコードには、将来のPyTorchの版では正しく動かないものがあるかもしれない。オンライン版は最新に保つつもりである。問題に出会ったときは、インストールを参照して、コードと実行環境を更新してほしい。以下に、PyTorchによる実装の依存関係を挙げる。
#@save
import numpy as np
import torch
import torchvision
from PIL import Image
from scipy.spatial import distance_matrix
from torch import nn
from torch.nn import functional as F
from torchvision import transforms

Target Audience 対象とする読者

This book is for students (undergraduate or graduate), engineers, and researchers, who seek a solid grasp of the practical techniques of deep learning. Because we explain every concept from scratch, no previous background in deep learning or machine learning is required. Fully explaining the methods of deep learning requires some mathematics and programming, but we will only assume that you enter with some basics, including modest amounts of linear algebra, calculus, probability, and Python programming. Just in case you have forgotten anything, the online Appendix provides a refresher on most of the mathematics you will find in this book. Usually, we will prioritize intuition and ideas over mathematical rigor. If you would like to extend these foundations beyond the prerequisites to understand our book, we happily recommend some other terrific resources: Linear Analysis by Bollobás (1999) covers linear algebra and functional analysis in great depth. All of Statistics (Wasserman, 2013) provides a marvelous introduction to statistics. Joe Blitzstein’s books and courses on probability and inference are pedagogical gems. And if you have not used Python before, you may want to peruse this Python tutorial.
英語のヒント
この本は、深層学習の実践的な技術をしっかり身につけたい学生(学部生でも大学院生でも)、エンジニア、研究者のためのものである。すべての概念をゼロから説明するので、深層学習や機械学習の予備知識は要らない。深層学習の手法を十分に説明するにはいくらかの数学とプログラミングが必要だが、前提とするのは、ある程度の線形代数、微積分、確率、Pythonプログラミングといった基礎だけである。万一何かを忘れていても、オンラインの付録が、この本に出てくる数学のほとんどを復習させてくれる。普通は、数学的な厳密さよりも直観と考え方を優先する。この本を理解するための前提を超えて、こうした基礎をさらに広げたいなら、ほかの素晴らしい資料を喜んで薦めたい。Bollobás (1999)の『Linear Analysis』は、線形代数と関数解析を非常に深く扱っている。『All of Statistics』(Wasserman, 2013)は統計学への見事な入門である。Joe Blitzsteinの確率と推論についての本と講義は、教育上の宝石である。そして、Pythonをまだ使ったことがなければ、このPythonチュートリアルに目を通すとよいだろう。

Notebooks, Website, GitHub, and Forum ノートブック、Webサイト、GitHub、フォーラム

All of our notebooks are available for download on the D2L.ai website and on GitHub. Associated with this book, we have launched a discussion forum, located at discuss.d2l.ai. Whenever you have questions on any section of the book, you can find a link to the associated discussion page at the end of each notebook.
英語のヒント
私たちのノートブックはすべて、D2L.aiのWebサイトとGitHubでダウンロードできる。この本にあわせて、議論のためのフォーラムをdiscuss.d2l.aiに開設した。本のどの節について質問があるときも、各ノートブックの末尾に、関連する議論のページへのリンクがある。