Concept

Content Loss in CNN-Based Style Transfer

Similar to the loss function used in linear regression, the content loss in a CNN-based style transfer model measures the difference in content features between the synthesized image and the content image using a squared loss function. Both inputs to this squared loss function are the raw feature map outputs of the specified content layer, which are computed via a feature extraction function. Because the target content features extracted from the original content image are static, they do not require gradient computation and are treated as stated values during the optimization of the synthesized image.

# PyTorch def content_loss(Y_hat, Y): # We detach the target content from the tree used to dynamically compute # the gradient: this is a stated value, not a variable. Otherwise the loss # will throw an error. return torch.square(Y_hat - Y.detach()).mean()

0

1

Updated 2026-05-21

Contributors are:

Who are from:

Tags

D2L

Dive into Deep Learning @ D2L