# Path: cortex/built_ins/models/gan.py
# def raise_measure_error(measure):
# def get_positive_expectation(p_samples, measure, average=True):
# def get_negative_expectation(q_samples, measure, average=True):
# def get_boundary(samples, measure):
# def get_weight(samples, measure):
# def generator_loss(q_samples, measure, loss_type=None):
#     def routine(self, inputs, penalty_type: str='contractive',
#                 penalty_amount: float=0.5):
#     def _get_gradient(inp, output):
#     def contractive_penalty(self, network, input, penalty_amount=0.5):
#     def interpolate_penalty(self, network, input, penalty_amount=0.5):
#     def build(self, discriminator_type: str='convnet', discriminator_args={}):
#     def routine(self, real, fake, measure: str='GAN'):
#     def score(self, X_P, X_Q, measure):
#     def visualize(self, real, fake, measure=None):
#     def build(self, dim_in: int=None, discriminator_args=dict(dim_h=[200, 200])):
#     def build(self, dim_z=64, generator_type: str='convnet',
#               generator_args=dict(output_nonlinearity='tanh')):
#     def routine(self, Z, measure: str=None, loss_type: str='non-saturating'):
#     def generate(self, Z):
#     def visualize(self, Z):
#     def __init__(self):
#     def build(self, noise_type='normal', dim_z=64):
#     def train_step(self, generator_updates=1, discriminator_updates=1):
#     def eval_step(self):
#     def visualize(self, images, Z):
# class GradientPenalty(ModelPlugin):
# class Discriminator(ModelPlugin):
# class SimpleDiscriminator(Discriminator):
# class Generator(ModelPlugin):
# class GAN(ModelPlugin):
#         X_P = real
#         X_Q = fake
#         X_P = real
#         X_Q = fake
#         X_Q = generator(Z)
#         X_Q = self.generate(Z)


import logging
import math
import torch
import torch.nn.functional as F
from torch.autograd import Variable
from .gan import apply_penalty, f_divergence
from .modules.conv_decoders import MNISTDeConv as Generator
from .modules.convnets import MNISTConv as Discriminator
    assert R.size() == (B, DIM_C * n_samples * DIM_X * DIM_Y), (R.size(), (B, DIM_C * n_samples * DIM_X * DIM_Y))

    try:
        R = R.view(M, -1, DIM_C * DIM_X * DIM_Y)
    except BaseException:
        R = R.view(M, -1, DIM_C * DIM_X * DIM_Y)
    U.requires_grad = False

    logit = generator(Z)
    assert logit.size()[1:] == X.size()[1:], (logit.size(), X.size())

    g_output = F.sigmoid(logit)
    g_output_ = g_output.view(-1, DIM_C * DIM_X * DIM_Y)

    S = (R <= g_output_).float()
    S = S.view(M, -1, DIM_C, DIM_X, DIM_Y)
    S_ = Variable(S.data.cuda(), volatile=True)
    S = Variable(S.data.cuda(), requires_grad=False)

    gen_out = (U <= g_output_).float()
    gen_out = gen_out.view(-1, DIM_C, DIM_X, DIM_Y)

    real_out = discriminator(X)

    fake_out = discriminator(S.view(-1, DIM_C, DIM_X, DIM_Y))
    fake_out_ = discriminator(S_.view(-1, DIM_C, DIM_X, DIM_Y))
    log_g = -((1. - S) * logit + F.softplus(-logit)).sum(2).sum(2).sum(2)

    if (measure == 'w' and not test_mode) or use_sm:
        fake_out_sm = discriminator(g_output)

# Path: cortex/built_ins/models/gan.py
# def raise_measure_error(measure):
# def get_positive_expectation(p_samples, measure, average=True):
# def get_negative_expectation(q_samples, measure, average=True):
# def get_boundary(samples, measure):
# def get_weight(samples, measure):
# def generator_loss(q_samples, measure, loss_type=None):
#     def routine(self, inputs, penalty_type: str='contractive',
#                 penalty_amount: float=0.5):
#     def _get_gradient(inp, output):
#     def contractive_penalty(self, network, input, penalty_amount=0.5):
#     def interpolate_penalty(self, network, input, penalty_amount=0.5):
#     def build(self, discriminator_type: str='convnet', discriminator_args={}):
#     def routine(self, real, fake, measure: str='GAN'):
#     def score(self, X_P, X_Q, measure):
#     def visualize(self, real, fake, measure=None):
#     def build(self, dim_in: int=None, discriminator_args=dict(dim_h=[200, 200])):
#     def build(self, dim_z=64, generator_type: str='convnet',
#               generator_args=dict(output_nonlinearity='tanh')):
#     def routine(self, Z, measure: str=None, loss_type: str='non-saturating'):
#     def generate(self, Z):
#     def visualize(self, Z):
#     def __init__(self):
#     def build(self, noise_type='normal', dim_z=64):
#     def train_step(self, generator_updates=1, discriminator_updates=1):
#     def eval_step(self):
#     def visualize(self, images, Z):
# class GradientPenalty(ModelPlugin):
# class Discriminator(ModelPlugin):
# class SimpleDiscriminator(Discriminator):
# class Generator(ModelPlugin):
# class GAN(ModelPlugin):
#         X_P = real
#         X_Q = fake
#         X_P = real
#         X_Q = fake
#         X_Q = generator(Z)
#         X_Q = self.generate(Z)


# Path: cortex/built_ins/models/discrete_gan.py
import logging
import math
import torch
import torch.nn.functional as F
from torch.autograd import Variable
from .gan import apply_penalty, f_divergence
from .modules.conv_decoders import MNISTDeConv as Generator
from .modules.convnets import MNISTConv as Discriminator

    assert R.size() == (B, DIM_C * n_samples * DIM_X * DIM_Y), (R.size(), (B, DIM_C * n_samples * DIM_X * DIM_Y))

    try:
        R = R.view(M, -1, DIM_C * DIM_X * DIM_Y)
    except BaseException:
        R = R.view(M, -1, DIM_C * DIM_X * DIM_Y)
    U.requires_grad = False

    logit = generator(Z)
    assert logit.size()[1:] == X.size()[1:], (logit.size(), X.size())

    g_output = F.sigmoid(logit)
    g_output_ = g_output.view(-1, DIM_C * DIM_X * DIM_Y)

    S = (R <= g_output_).float()
    S = S.view(M, -1, DIM_C, DIM_X, DIM_Y)
    S_ = Variable(S.data.cuda(), volatile=True)
    S = Variable(S.data.cuda(), requires_grad=False)

    gen_out = (U <= g_output_).float()
    gen_out = gen_out.view(-1, DIM_C, DIM_X, DIM_Y)

    real_out = discriminator(X)

    fake_out = discriminator(S.view(-1, DIM_C, DIM_X, DIM_Y))
    fake_out_ = discriminator(S_.view(-1, DIM_C, DIM_X, DIM_Y))
    log_g = -((1. - S) * logit + F.softplus(-logit)).sum(2).sum(2).sum(2)

    if (measure == 'w' and not test_mode) or use_sm:
        fake_out_sm = discriminator(g_output)

        d_loss, g_loss, r, f, w, b = f_divergence(measure, real_out, fake_out_sm)