adds the content loss adds the total variation loss generative deep learning loss variable( you'll define the loss by layer_features outputs_dict[content_layeradding all components to target_image_features layer_features[ :::this scalar variable combination_features layer_features[ :::loss +content_weight content_loss(target_image_featurescombination_featuresfor layer_name in style_layersadds style loss layer_features outputs_dict[layer_namecomponent for style_reference_features layer_features[ :::each target layer combination_features layer_features[ :::sl style_loss(style_reference_featurescombination_featuresloss +(style_weight len(style_layers)sl loss +total_variation_weight total_variation_loss(combination_imagefinallyyou'll set up the gradient-descent process in the original gatys et al paperoptimization is performed using the -bfgs algorithmso that' what you'll use here this is key difference from the deepdream example in section the -bfgs algorithm comes packaged with scipybut there are two slight limitations with the scipy implementationit requires that you pass the value of the loss function and the value of the gradients as two separate functions it can only be applied to flat vectorswhereas you have image array it would be inefficient to compute the value of the loss function and the value of the gradients independentlybecause doing so would lead to lot of redundant computation between the twothe process would be almost twice as slow as computing them jointly to bypass thisyou'll set up python class named evaluator that computes both the loss value and the gradients value at oncereturns the loss value when called the first timeand caches the gradients for the next call listing gets the gradients of the generated image with regard to the loss set ing up the gradient -descent process grads gradients(losscombination_image)[ fetch_loss_and_grads function([combination_image][lossgrads]class evaluator(object)def __init__(self)self loss_value none self grads_values none def loss(selfx)assert self loss_value is none reshape(( img_heightimg_width )outs fetch_loss_and_grads([ ]this class wraps fetch_loss_and_grads in way that lets you retrieve the losses and gradients via two separate method callswhich is required by the scipy optimizer you'll use licensed to function to fetch the values of the current loss and the current gradients