From af6adc11f53fa251f6358463d112da478ad631f8 Mon Sep 17 00:00:00 2001 From: Vlad Date: Thu, 25 Oct 2018 00:20:10 +0300 Subject: [PATCH] fix BCEloss error a wrapper proposed by yzgao to solve numerical instability --- 1. Vanilla GAN PyTorch.ipynb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/1. Vanilla GAN PyTorch.ipynb b/1. Vanilla GAN PyTorch.ipynb index e1fe594..3182096 100644 --- a/1. Vanilla GAN PyTorch.ipynb +++ b/1. Vanilla GAN PyTorch.ipynb @@ -218,7 +218,14 @@ "g_optimizer = optim.Adam(generator.parameters(), lr=0.0002)\n", "\n", "# Loss function\n", - "loss = nn.BCELoss()\n", + "class StableBCELoss(nn.modules.Module):", + " def __init__(self):", + " super(StableBCELoss, self).__init__()", + " def forward(self, input, target):", + " neg_abs = - input.abs()", + " loss = input.clamp(min=0) - input * target + (1 + neg_abs.exp()).log()", + " return loss.mean()", + "loss = StableBCELoss()\n", "\n", "# Number of steps to apply to the discriminator\n", "d_steps = 1 # In Goodfellow et. al 2014 this variable is assigned to 1\n",