Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
loss_logs.logs[name].append(cur_loss)
lgen, gen_iter = lgen + cur_loss, gen_iter + 1
for model_name in self.model_names:
model = getattr(self, model_name)
if isinstance(model, Generator):
grad_logs.update_grads(model_name, model)
elif isinstance(loss, DiscriminatorLoss):
if self.loss_information["generator_iters"] % self.ngen == 0:
cur_loss = loss.train_ops(
**self._get_arguments(self.loss_arg_maps[name])
)
loss_logs.logs[name].append(cur_loss)
ldis, dis_iter = ldis + cur_loss, dis_iter + 1
for model_name in self.model_names:
model = getattr(self, model_name)
if isinstance(model, Discriminator):
grad_logs.update_grads(model_name, model)
return lgen, ldis, gen_iter, dis_iter
def __init__(self, input_dims, label_type="none"):
super(Discriminator, self).__init__()
self.input_dims = input_dims
self.label_type = label_type
def forward(self, z):
r"""Calculates the output tensor on passing the encoding ``z`` through the Generator.
Args:
z (torch.Tensor): A 2D torch tensor of the encoding sampled from a probability
distribution.
Returns:
A 4D torch.Tensor of the generated image.
"""
x = self.fc(z)
x = x.view(-1, self.n, self.init_dim, self.init_dim)
return self.model(x)
class AutoEncodingDiscriminator(Discriminator):
r"""Autoencoding Generator for Boundary Equilibrium GAN (BEGAN) from
`"BEGAN : Boundary Equilibrium Generative Adversarial Networks
by Berthelot et. al." `_ paper
Args:
in_size (int, optional): Height and width of the input image to be evaluated. Must be at
least 16 and should be an exact power of 2.
in_channels (int, optional): Number of channels in the input Tensor.
step_channels (int, optional): Number of channels in multiples of which the DCGAN steps up
the convolutional features. The step up is done as dim :math:`z \rightarrow d \rightarrow
2 \times d \rightarrow 4 \times d \rightarrow 8 \times d` where :math:`d` = step_channels.
scale_factor (int, optional): The scale factor is used to infer properties of the model like
``downsample_pad``, ``downsample_filters`` and ``downsample_stride``.
batchnorm (bool, optional): If True, use batch normalization in the convolutional layers of
the generator.
nonlinearity (torch.nn.Module, optional): Nonlinearity to be used in the intermediate
r"""Calculates the output tensor on passing the encoding ``x`` through the Generator.
Args:
x (torch.Tensor): A 2D torch tensor of the encoding sampled from a probability
distribution.
feature_matching (bool, optional): Returns the activation from a predefined intermediate
layer.
Returns:
A 4D torch.Tensor of the generated image.
"""
x = x.view(-1, x.size(1), 1, 1)
return self.model(x)
class DCGANDiscriminator(Discriminator):
r"""Deep Convolutional GAN (DCGAN) discriminator from
`"Unsupervised Representation Learning With Deep Convolutional Generative Aversarial Networks
by Radford et. al. " `_ paper
Args:
in_size (int, optional): Height and width of the input image to be evaluated. Must be at
least 16 and should be an exact power of 2.
in_channels (int, optional): Number of channels in the input Tensor.
step_channels (int, optional): Number of channels in multiples of which the DCGAN steps up
the convolutional features. The step up is done as dim :math:`z \rightarrow d \rightarrow
2 \times d \rightarrow 4 \times d \rightarrow 8 \times d` where :math:`d` = step_channels.
batchnorm (bool, optional): If True, use batch normalization in the convolutional layers of
the generator.
nonlinearity (torch.nn.Module, optional): Nonlinearity to be used in the intermediate
convolutional layers. Defaults to ``LeakyReLU(0.2)`` when None is passed.
last_nonlinearity (torch.nn.Module, optional): Nonlinearity to be used in the final