Implement Wasserstein Critic Loss for Wasserstein GANs (WGAN). In WGANs, the discriminator is replaced with a critic that outputs real-valued scores instead of probabilities.
Wasserstein Critic Loss:
L=E[D(fake)]−E[D(real)]where D is the critic network (no sigmoid activation)
real_scores: np.ndarray - Critic outputs for real samplesfake_scores: np.ndarray - Critic outputs for fake samplesInput: real_scores=[2.0, 1.5, 3.0], fake_scores=[-1.0, 0.0, 0.5]
Output: -2.333
mean(fake)=-0.167, mean(real)=2.167 → -0.167-2.167=-2.333
Input: real_scores=[1.0, 2.0, 3.0], fake_scores=[2.0, 2.0, 2.0]
Output: 0.0
Equal means result in zero loss
Input: real_scores=[0.0, 0.0], fake_scores=[1.0, 2.0, 3.0]
Output: 2.0
Fake scores higher than real scores → positive loss
Convert inputs to numpy arrays and compute means using np.mean().
The loss is simply the difference: mean_fake - mean_real.
Return as scalar: float() to ensure proper type.
Sign in to take notes on this problem
Accepts: array
Accepts: array
Implement Wasserstein Critic Loss for Wasserstein GANs (WGAN). In WGANs, the discriminator is replaced with a critic that outputs real-valued scores instead of probabilities.
Wasserstein Critic Loss:
L=E[D(fake)]−E[D(real)]where D is the critic network (no sigmoid activation)
real_scores: np.ndarray - Critic outputs for real samplesfake_scores: np.ndarray - Critic outputs for fake samplesInput: real_scores=[2.0, 1.5, 3.0], fake_scores=[-1.0, 0.0, 0.5]
Output: -2.333
mean(fake)=-0.167, mean(real)=2.167 → -0.167-2.167=-2.333
Input: real_scores=[1.0, 2.0, 3.0], fake_scores=[2.0, 2.0, 2.0]
Output: 0.0
Equal means result in zero loss
Input: real_scores=[0.0, 0.0], fake_scores=[1.0, 2.0, 3.0]
Output: 2.0
Fake scores higher than real scores → positive loss
Convert inputs to numpy arrays and compute means using np.mean().
The loss is simply the difference: mean_fake - mean_real.
Return as scalar: float() to ensure proper type.
Sign in to take notes on this problem
Accepts: array
Accepts: array