Публикация показывает, как работает генеративно-состязательная сеть: генератор превращает шум в данные, а дискриминатор учится отличать подделку от реальных примеров. Разбор выполнен по шагам с ручными вычислениями и градиентами.

В статье разобран принцип работы генеративно-состязательной сети (GAN), основанной на работе Яна Гудфеллоу и соавторов. Показано, как нейросеть может не только классифицировать данные, но и генерировать реалистичные изображения. Идея GAN строится на состязании двух сетей: генератор преобразует шум в поддельные данные, а дискриминатор учится отличать их от реальных и тем самым заставляет генератор улучшаться. Разбор включает 9 шагов: от исходных шумовых и реальных векторов до прохождения через слои генератора и дискриминатора, применения ReLU и сигмоиды, а затем вычисления градиентов для обучения обеих частей модели. В конце приводятся примеры поддельных данных, предсказаний на подделках и реальных примерах, а также градиентов дискриминатора и генератора. Основной вывод: состязательная часть GAN сводится к одному и тому же вычитанию, которое выполняется дважды для разных целей обучения.

Источники 1
  • Пошаговый разбор GAN вручную на примере вычислений Machine Learning with Python
    Generative Adversarial Network (GAN) by hand ✍️ ~ 9 steps walkthrough below
    
    The Gen in GenAI came from this landmark paper by Ian Goodfellow et al., 12 years ago.
    
    The paper showed that a neural network can not only classify but also turn upside down to generate realistic looking images.
    
    The secret? We pit two of them against each other: a Generator turns noise into fake data, and a Discriminator learns to tell fake from real, pushing the Generator to keep doing better.
    
    One runs upside down, the other right way up.
    
    I drew and calculated one entirely by hand.
    
    Goal: generate realistic 4D data out of 2D noise, filling in every cell yourself.
    
    = 1. Given =
    
    Four noise vectors in 2D, and four real data vectors in 4D.
    
    = 2. Generator, first layer =
    
    Let us multiply the noise by weights and biases to get new features.
    
    = 3. ReLU =
    
    We apply the activation, and -1 and -2 are crossed out and set to 0.
    
    = 4. Generator, second layer =
    
    Let us multiply again. ReLU applies here too, but every value is already positive, so nothing changes. What comes out is the fake data F, made by a two-layer generator out of nothing but noise.
    
    = 5. Discriminator, first layer =
    
    We feed it both, the four fakes and the four real vectors, through the same weights. It never learns which is which from the layout, only from the numbers.
    
    = 6. Discriminator, second layer =
    
    Let us reduce each data vector to a single feature Z. Eight vectors in, eight numbers out.
    
    = 7. Sigmoid =
    
    We turn each Z into a probability Y. A 1 means the discriminator is certain the data is real, a 0 means certain it is fake.
    
    = 8. Training the Discriminator =
    
    Let us take the gradients as Y minus YD, where YD is what the discriminator should have said: 0 for the four fakes, 1 for the four real. Why so simple? Because pairing sigmoid with binary cross entropy loss makes the math collapse to exactly this subtraction. Its loss uses both halves of the page.
    
    = 9. Training the Generator =
    
    We do it again, as Y minus YG, and YG is [1, 1, 1, 1]: the generator wants the discriminator to call every fake real. Same predictions, different target, opposite goal. Its loss uses only the fakes.
    
    The outputs:
    Fake data F = [1, 2, 3, 1], [1, 1, 2, 1], [2, 2, 4, 2], [1, 0, 1, 1]
    Predictions on fakes = [.7, .5, .9, .3]
    Predictions on real = [.7, .9, .9, 1]
    Discriminator gradients = [.7, .5, .9, .3] and [-.3, -.1, -.1, 0]
    Generator gradients = [-.3, -.5, -.1, -.7]
    
    The takeaway: the adversarial part is one subtraction done twice. The same eight predictions, scored against two opposite targets, send one set of gradients back through the blue weights and another back through the green ones.