Learn Before
Concept

Loss Function in CNN-Based Style Transfer

The overall loss function in neural style transfer is defined as the weighted sum of three distinct components: the content loss, the style loss, and the total variation loss. By tuning the respective weight hyperparameters for each component, one can control the trade-off between retaining the original content, applying the desired artistic style, and reducing high-frequency noise in the synthesized image.

content_weight, style_weight, tv_weight = 1, 1e4, 10 def compute_loss(X, contents_Y_hat, styles_Y_hat, contents_Y, styles_Y_gram): # Calculate the content, style, and total variance losses respectively contents_l = [content_loss(Y_hat, Y) * content_weight for Y_hat, Y in zip( contents_Y_hat, contents_Y)] styles_l = [style_loss(Y_hat, Y) * style_weight for Y_hat, Y in zip( styles_Y_hat, styles_Y_gram)] tv_l = tv_loss(X) * tv_weight # Add up all the losses l = sum(styles_l + contents_l + [tv_l]) return contents_l, styles_l, tv_l, l

0

1

Updated 2026-05-21

Contributors are:

Who are from:

Tags

D2L

Dive into Deep Learning @ D2L