loss functions as discussed in the background subsectionthe problem of neural style transfer revolves around loss functions of content and style in this subsectionwe will discuss and define the required loss functions content loss in any cnn-based modelactivations from top layers contain more global and abstract information (high-level structures like faceand bottom layers will contain local information (low-level structures like eyesnoseedgesand cornersabout the image we would want to leverage the top layers of cnn for capturing the right representations for the content of an image hencefor the content lossconsidering we will be using the pretrained vgg- cnnwe can define our loss function as the norm (scaled and squared euclidean distancebetween the activations of top layer (giving feature representationscomputed over the target image and the activations of the same layer computed over the generated image assuming we usually get feature representations relevant to the content of images from the top layers of cnnthe generated image is expected to look similar to the base target image the following snippet showcases the function to compute the content loss def content_loss(basecombination)return sum( square(combination base)style loss the original paper on neural style transfera neural algorithm of artistic style by gatys et al leverages multiple convolutional layers in the cnn (instead of oneto extract meaningful patterns and representations capturing information pertaining to appearance or style from the reference style image across all spatial scales irrespective of the image content staying true to the original paperwe will be leveraging the gram matrix and computing the same over the feature representations generated by the convolution layers the gram matrix computes the inner product between the feature maps produced in any given conv layer the inner products terms are proportional to the covariances of corresponding feature sets and hence captures patterns of correlations between the features of layer that tend to activate together these feature correlations help capture relevant aggregate statistics of the patterns of particular spatial scalewhich correspond to the styletextureand appearance and not the components and objects present in an image the style loss is thus defined as the scaled and squared frobenius norm of the difference between the gram matrices of the reference style and the generated images minimizing this loss helps ensure that the textures found at different spatial scales in the reference style image will be similar in generated image the following snippet thus defines style loss function based on gram matrix calculation def style_loss(stylecombinationheightwidth)def build_gram_matrix( )features batch_flatten( permute_dimensions( ( ))gram_matrix dot(featuresk transpose(features)return gram_matrix build_gram_matrix(stylec build_gram_matrix(combinationchannels size height width return sum( square( )( (channels * (size * )