Learn Before
Formula
Total Variation Loss in CNN-Based Style Transfer
In neural style transfer, the learned synthesized image often accumulates high-frequency noise, manifesting as particularly bright or dark pixels. To mitigate this, a noise reduction method known as total variation denoising is applied. Denoting as the pixel value at coordinate , the total variation loss is defined as:
Reducing this total variation loss encourages the values of neighboring pixels on the synthesized image to become closer, thereby promoting a smoother and visually cleaner final output.
# PyTorch def tv_loss(Y_hat): return 0.5 * (torch.abs(Y_hat[:, :, 1:, :] - Y_hat[:, :, :-1, :]).mean() + torch.abs(Y_hat[:, :, :, 1:] - Y_hat[:, :, :, :-1]).mean())
0
1
Updated 2026-05-21
Tags
D2L
Dive into Deep Learning @ D2L