プリンキピア

第20章敵対的生成ネットワーク

20.1. Generative Adversarial Networks20.1 敵対的生成ネットワーク

Throughout most of this book, we have talked about how to make predictions. In some form or another, we used deep neural networks to learn mappings from data examples to labels. This kind of learning is called discriminative learning, as in, we’d like to be able to discriminate between photos of cats and photos of dogs. Classifiers and regressors are both examples of discriminative learning. And neural networks trained by backpropagation have upended everything we thought we knew about discriminative learning on large complicated datasets. Classification accuracies on high-res images have gone from useless to human-level (with some caveats) in just 5-6 years. We will spare you another spiel about all the other discriminative tasks where deep neural networks do astoundingly well.
英語のヒント
この本のほとんどを通じて、予測をする方法について話してきた。何らかの形で、深層ニューラルネットワークを使って、データの事例からラベルへの写像を学習してきた。この種の学習は識別学習と呼ばれる。たとえば、猫の写真と犬の写真を識別できるようになりたい、というようにである。分類器も回帰器も識別学習の例である。そして、誤差逆伝播法で訓練したニューラルネットワークは、大きく複雑なデータセットでの識別学習について知っていると思っていたことを、すべてひっくり返した。高解像度の画像での分類の正解率は、わずか5〜6年で、役に立たない水準から(いくつかの但し書きつきで)人間の水準にまで達した。深層ニューラルネットワークが驚くほどうまくいく、ほかのすべての識別タスクについての長話はもう繰り返さないでおく。
But there is more to machine learning than just solving discriminative tasks. For example, given a large dataset, without any labels, we might want to learn a model that concisely captures the characteristics of this data. Given such a model, we could sample synthetic data examples that resemble the distribution of the training data. For example, given a large corpus of photographs of faces, we might want to be able to generate a new photorealistic image that looks like it might plausibly have come from the same dataset. This kind of learning is called generative modeling.
英語のヒント
しかし、機械学習には識別タスクを解くこと以上のものがある。たとえば、ラベルのない大きなデータセットが与えられたとき、このデータの特徴を簡潔にとらえるモデルを学習したいかもしれない。そうしたモデルがあれば、訓練データの分布に似た合成のデータ事例を抜き出せるだろう。たとえば、顔写真の大きなコーパスが与えられたとき、同じデータセットから来たと思えそうな、写真のようにリアルな新しい画像を生成できるようになりたいかもしれない。この種の学習は生成モデリングと呼ばれる。
Until recently, we had no method that could synthesize novel photorealistic images. But the success of deep neural networks for discriminative learning opened up new possibilities. One big trend over the last three years has been the application of discriminative deep nets to overcome challenges in problems that we do not generally think of as supervised learning problems. The recurrent neural network language models are one example of using a discriminative network (trained to predict the next character) that once trained can act as a generative model.
英語のヒント
最近まで、写真のようにリアルな新しい画像を合成できる方法はなかった。しかし、識別学習での深層ニューラルネットワークの成功は、新しい可能性を開いた。ここ3年の大きな流れの一つは、ふつうは教師あり学習の問題とは考えない問題の難題を克服するために、識別的な深層ネットワークを当てはめることである。リカレントニューラルネットワークの言語モデルは、(次の文字を予測するように訓練した)識別的なネットワークを使い、いったん訓練すれば生成モデルとして働けるようにする一つの例である。
In 2014, a breakthrough paper introduced Generative adversarial networks (GANs) (Goodfellow et al., 2014), a clever new way to leverage the power of discriminative models to get good generative models. At their heart, GANs rely on the idea that a data generator is good if we cannot tell fake data apart from real data. In statistics, this is called a two-sample test - a test to answer the question whether datasets X={x1,…,xn} and X′={x1′,…,xn′} were drawn from the same distribution. The main difference between most statistics papers and GANs is that the latter use this idea in a constructive way. In other words, rather than just training a model to say “hey, these two datasets do not look like they came from the same distribution”, they use the two-sample test to provide training signals to a generative model. This allows us to improve the data generator until it generates something that resembles the real data. At the very least, it needs to fool the classifier even if our classifier is a state of the art deep neural network.
英語のヒント
2014年、画期的な論文が、識別モデルの力を活用してよい生成モデルを得る、巧みな新しい方法である敵対的生成ネットワーク(GAN)を紹介した(Goodfellow et al., 2014)。GANの核心にあるのは、偽のデータを本物のデータと見分けられなければ、そのデータ生成器はよい、という考え方である。統計学では、これは2標本検定、つまりデータセットX={x1,…,xn}とX′={x1′,…,xn′}が同じ分布から抜き出されたかどうかという問いに答える検定と呼ばれる。たいていの統計学の論文とGANの主な違いは、後者がこの考え方を建設的に使うことである。言い換えると、「ねえ、この二つのデータセットは同じ分布から来たようには見えないよ」と言うようにモデルを訓練するだけでなく、2標本検定を使って生成モデルに訓練の信号を与える。これによって、本物のデータに似たものを生成するようになるまで、データ生成器を改善できる。少なくとも、分類器が最先端の深層ニューラルネットワークであっても、生成器はその分類器をだます必要がある。
Fig. 20.1.1図20.1.1
Generative Adversarial Networks
英語のヒント
敵対的生成ネットワーク
The GAN architecture is illustrated in Fig. 20.1.1. As you can see, there are two pieces in GAN architecture - first off, we need a device (say, a deep network but it really could be anything, such as a game rendering engine) that might potentially be able to generate data that looks just like the real thing. If we are dealing with images, this needs to generate images. If we are dealing with speech, it needs to generate audio sequences, and so on. We call this the generator network. The second component is the discriminator network. It attempts to distinguish fake and real data from each other. Both networks are in competition with each other. The generator network attempts to fool the discriminator network. At that point, the discriminator network adapts to the new fake data. This information, in turn is used to improve the generator network, and so on.
英語のヒント
GANのアーキテクチャを図20.1.1に示す。見てのとおり、GANのアーキテクチャには二つの部品がある。まず、本物とそっくりに見えるデータを生成できそうな装置(たとえば深層ネットワークだが、ゲームの描画エンジンのような、本当に何でもよい)が必要である。画像を扱っているなら、これは画像を生成する必要がある。音声を扱っているなら、音声の系列を生成する必要がある、などである。これを生成器ネットワークと呼ぶ。2番目の部品は識別器ネットワークである。これは偽のデータと本物のデータを互いに区別しようとする。二つのネットワークは互いに競い合う。生成器ネットワークは識別器ネットワークをだまそうとする。そこで、識別器ネットワークは新しい偽のデータに適応する。この情報は、今度は生成器ネットワークを改善するのに使われる、という具合である。
The discriminator is a binary classifier to distinguish if the input x is real (from real data) or fake (from the generator). Typically, the discriminator outputs a scalar prediction o∈ℝ for input 𝐱, such as using a fully connected layer with hidden size 1, and then applies sigmoid function to obtain the predicted probability D(𝐱)=1/(1+e−o). Assume the label y for the true data is 1 and 0 for the fake data. We train the discriminator to minimize the cross-entropy loss, i.e.,
英語のヒント
識別器は、入力xが本物(本物のデータから)か偽物(生成器から)かを見分ける二値分類器である。ふつう、識別器は、たとえば隠れ層の大きさが1の全結合層を使って、入力𝐱に対してスカラーの予測o∈ℝを出力し、それからシグモイド関数を当てはめて予測確率D(𝐱)=1/(1+e−o)を得る。真のデータのラベルyは1、偽のデータでは0と仮定する。識別器を、交差エントロピー損失を最小化するように訓練する。つまり、
minD{−ylog⁡D(𝐱)−(1−y)log⁡(1−D(𝐱))},(20.1.1)
For the generator, it first draws some parameter 𝐳∈ℝd from a source of randomness, e.g., a normal distribution 𝐳∼𝒩(0,1). We often call 𝐳 as the latent variable. It then applies a function to generate 𝐱′=G(𝐳). The goal of the generator is to fool the discriminator to classify 𝐱′=G(𝐳) as true data, i.e., we want D(G(𝐳))≈1. In other words, for a given discriminator D, we update the parameters of the generator G to maximize the cross-entropy loss when y=0, i.e.,
英語のヒント
生成器は、まず何らかのパラメータ𝐳∈ℝdを、ランダムさの源、たとえば正規分布𝐳∼𝒩(0,1)から抜き出す。𝐳はしばしば潜在変数と呼ばれる。それから関数を当てはめて𝐱′=G(𝐳)を生成する。生成器の目標は、識別器をだまして𝐱′=G(𝐳)を真のデータと分類させること、つまりD(G(𝐳))≈1にすることである。言い換えると、与えられた識別器Dについて、y=0のときの交差エントロピー損失を最大化するように生成器Gのパラメータを更新する。つまり、
maxG{−(1−y)log⁡(1−D(G(𝐳)))}=maxG{−log⁡(1−D(G(𝐳)))}.(20.1.2)
If the generator does a perfect job, then D(𝐱′)≈1, so the above loss is near 0, which results in the gradients that are too small to make good progress for the discriminator. So commonly, we minimize the following loss:
英語のヒント
生成器が完璧な仕事をすればD(𝐱′)≈1となるので、上の損失は0に近くなり、その結果、勾配が小さすぎて識別器にとってよい進歩ができなくなる。そこでふつうは、次の損失を最小化する。
minG{−ylog⁡(D(G(𝐳)))}=minG{−log⁡(D(G(𝐳)))},(20.1.3)
which is just feeding 𝐱′=G(𝐳) into the discriminator but giving label y=1.
英語のヒント
これは、𝐱′=G(𝐳)を識別器に入れるが、ラベルy=1を与えるということにすぎない。
To sum up, D and G are playing a “minimax” game with the comprehensive objective function:
英語のヒント
まとめると、DとGは、次の総合的な目的関数で「ミニマックス」ゲームをしている。
minDmaxG{−Ex∼Datalog⁡D(𝐱)−Ez∼Noiselog⁡(1−D(G(𝐳)))}.(20.1.4)
Many of the GANs applications are in the context of images. As a demonstration purpose, we are going to content ourselves with fitting a much simpler distribution first. We will illustrate what happens if we use GANs to build the world’s most inefficient estimator of parameters for a Gaussian. Let’s get started.
英語のヒント
GANの応用の多くは画像の文脈にある。説明の目的で、まずはずっと単純な分布に当てはめることで満足しておこう。GANを使って、ガウス分布のパラメータの世界で最も非効率な推定器を作ると何が起こるかを示す。さあ始めよう。
%matplotlib inline
import torch
from torch import nn
from d2l import torch as d2l