set the source research paper followed and set the content and style layers source_paper 'gatyscontent_layerstyle_layers set_cnn_layers(source=source_paper#build the weighted loss function initialize total loss loss variable( add content loss layer_features layers[content_layertarget_image_features layer_features[ :::combination_features layer_features[ :::loss +content_weight content_loss(target_image_featurescombination_featuresadd style loss for layer_name in style_layerslayer_features layers[layer_namestyle_reference_features layer_features[ :::combination_features layer_features[ :::sl style_loss(style_reference_featurescombination_featuresheight=img_heightwidth=img_widthloss +(style_weight len(style_layers)sl add total variation loss loss +total_variation_weight total_variation_loss(generated_imagecustom optimizer the objective is to iteratively minimize the overall loss with the help of an optimization algorithm in the paper by gatys et al optimization was done using the -bfgs algorithmwhich is an optimization algorithm based on quasi-newton methodswhich is popularly used for solving non-linear optimization problems and parameter estimation this method usually converges faster than standard gradient descent scipy has an implementation available in scipy optimize fmin_l_bfgs_b(howeverlimitations include the function being applicable only to flat vectorsunlike image matrices which we are dealing withand the fact that value of loss function and gradients need to be passed as two separate functions we build an evaluator class based on patterns followed by keras creator francois chollet to compute both loss and gradients values in one pass instead of independent and separate computations this will return the loss value when called the first time and will cache the gradients for the next call thusit would be more efficient than computing both independently the following snippet defines the evaluator class class evaluator(object)def __init__(selfheight=nonewidth=none)self loss_value none self grads_values none self height height self width width