def loss(selfx)assert self loss_value is none reshape(( self heightself width )outs fetch_loss_and_grads([ ]loss_value outs[ grad_values outs[ flatten(astype('float 'self loss_value loss_value self grad_values grad_values return self loss_value def grads(selfx)assert self loss_value is not none grad_values np copy(self grad_valuesself loss_value none self grad_values none return grad_values the loss and gradients are retrieved as follows the snippet also creates an object of the evaluator class in [ ]get the gradients of the generated image wrt the loss grads gradients(lossgenerated_image)[ function to fetch the values of the current loss and the current gradients fetch_loss_and_grads function([generated_image][lossgrads]evaluator object evaluator evaluator(height=img_heightwidth=img_widthstyle transfer in action the final piece of the puzzle is to use all the building blocks and see the style transfer in action the art/style and content images are available data directory for reference the following snippet outlines how loss and gradients are evaluated we also write back outputs after regular intervals ( and so on iterationsto later understand how the process of neural style transfer transforms the images in consideration in [ ]result_prefix 'style_transfer_result_'+target_img split(')[ result_prefix result_prefix+' '+source_paper iterations run scipy-based optimization ( -bfgsover the pixels of the generated image so as to minimize the neural style loss this is our initial statethe target image note that `scipy optimize fmin_l_bfgs_bcan only process flat vectors preprocess_image(target_imgheight=img_heightwidth=img_widthx flatten(for in range(iterations)print('start of iteration'( + )start_time time time(xmin_valinfo fmin_l_bfgs_b(evaluator lossxfprime=evaluator gradsmaxfun=