Concept

Style Loss in CNN-Based Style Transfer

Similar to content loss, the style loss in a neural style transfer model uses the squared loss function to measure the difference in style between the synthesized image and the style image. However, instead of comparing raw feature maps directly, the two inputs of the squared loss function for style loss are the Gram matrices derived from the style layer outputs of the synthesized image and the style image. It is typically assumed that the Gram matrix based on the original style image has been precomputed, allowing it to act as a static target detached from gradient computation during training.

# PyTorch def style_loss(Y_hat, gram_Y): return torch.square(gram(Y_hat) - gram_Y.detach()).mean()

0

1

Updated 2026-05-21

Contributors are:

Who are from:

Tags

D2L

Dive into Deep Learning @ D2L