<p>You can find why they doing it in the original paper of <a href="https://arxiv.org/abs/1508.06576" rel="nofollow noreferrer">A Neural Algorithm of Artistic Style</a>, in short, people doing it mainly because after the calculate gradient of loss the constant value will be cancel out, so the formula will looking nicer.</p>
<p><a href="https://i.stack.imgur.com/jy6fL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jy6fL.png" alt="enter image description here" /></a></p>
<p><code>Flij</code> is the activation of the i filter at position j in layer l</p>
<p><a href="https://i.stack.imgur.com/VTtC7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VTtC7.png" alt="enter image description here" /></a></p>
<p><code>Nl</code> and <code>Ml</code> are number of feature maps and the height times the width of the feature map at layer l, <code>El</code> is style loss at layer l</p>
<p><a href="https://i.stack.imgur.com/oSpo3.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oSpo3.png" alt="enter image description here" /></a></p>
<p>As you can see, the constant value is cancel out</p>
<p>Of course you could do the math without cancel out the constant value for gradient, after all it just a constant value, will not have much effect on result</p>
<p>Paper did the same to the content loss as to the style loss:</p>
<p><a href="https://i.stack.imgur.com/jxZWZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jxZWZ.png" alt="enter image description here" /></a></p>
<p><a href="https://i.stack.imgur.com/uDTge.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uDTge.png" alt="enter image description here" /></a></p>
<p><code>Plij</code> is content image's feature representation in layer l</p>
<p>While code in <a href="https://keras.io/examples/generative/neural_style_transfer/" rel="nofollow noreferrer">keras example of neural style transfer</a> did not cancel out the constant value for content loss:</p>
<pre><code>def content_loss(base, combination):
    return tf.reduce_sum(tf.square(combination - base))
</code></pre>
<p>Or you could think as it cancel out constant value using <code>content_weight</code>:</p>
<pre><code>loss = loss + content_weight * content_loss(
    base_image_features, combination_features
)
</code></pre>
keras style transfer example has an unexplained constant
<p>I am following along the keras example for neural style transfer and in the style loss function they divide by the number 4, I have read the original paper by Gatys et al. and other articles in search of the meaning of this integer and its contribution to the whole pprocess and cannot find an explanation.</p>
<p>code is from: <a href="https://keras.io/examples/generative/neural_style_transfer/" rel="nofollow noreferrer">https://keras.io/examples/generative/neural_style_transfer/</a></p>
<p>Does anyone know what it is ?</p>
<pre><code> def style_loss(style, combination):
    S = gram_matrix(style)
    C = gram_matrix(combination)
    channels = 3
    size = img_nrows * img_ncols
    return tf.reduce_sum(tf.square(S - C)) / (4.0 * (channels ** 2) * (size ** 2))
</code></pre>