Implement Wasserstein Critic Loss
Implement Wasserstein Critic Loss
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)
Function Arguments
real_scores: np.ndarray- Critic outputs for real samplesfake_scores: np.ndarray- Critic outputs for fake samples
Examples
Input: 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
Hint 1
Convert inputs to numpy arrays and compute means using np.mean().
Hint 2
The loss is simply the difference: mean_fake - mean_real.
Hint 3
Return as scalar: float() to ensure proper type.
Requirements
- Compute: L = mean(fake_scores) - mean(real_scores)
- Return scalar loss
- No activation function; scores are raw critic outputs
- Handle arrays of different sizes
Constraints
- Only NumPy
- No clipping inside this function
- Time limit: 100ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array
Implement Wasserstein Critic Loss
Implement Wasserstein Critic Loss
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)
Function Arguments
real_scores: np.ndarray- Critic outputs for real samplesfake_scores: np.ndarray- Critic outputs for fake samples
Examples
Input: 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
Hint 1
Convert inputs to numpy arrays and compute means using np.mean().
Hint 2
The loss is simply the difference: mean_fake - mean_real.
Hint 3
Return as scalar: float() to ensure proper type.
Requirements
- Compute: L = mean(fake_scores) - mean(real_scores)
- Return scalar loss
- No activation function; scores are raw critic outputs
- Handle arrays of different sizes
Constraints
- Only NumPy
- No clipping inside this function
- Time limit: 100ms
Try Similar Problems
Log in to take notes on this problem
Accepts: array
Accepts: array