neural style transfer 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 evaluator evaluator(finallyyou can run the gradient-ascent process using scipy' -bfgs algorithmsaving the current generated image at each iteration of the algorithm (herea single iteration represents steps of gradient ascentlisting st yle- ransfer loop from scipy optimize import fmin_l_bfgs_b from scipy misc import imsave import time result_prefix 'my_resultiterations this is the initial statethe target image you flatten the image because scipy optimize fmin_l_bfgs_b can only process flat vectors preprocess_image(target_image_pathruns -bfgsoptimization flatten(over the pixels of the for in range(iterations)generated image to print('start of iteration'iminimize the neural style start_time time time(loss note that you have xmin_valinfo fmin_l_bfgs_b(evaluator lossto pass the function that xcomputes the loss and the fprime=evaluator gradsfunction that computes maxfun= the gradients as two print('current loss value:'min_valseparate arguments img copy(reshape((img_heightimg_width )img deprocess_image(imgfname result_prefix '_at_iteration_% pngi saves the current imsave(fnameimggenerated image print('image saved as'fnameend_time time time(print('iteration % completed in %ds(iend_time start_time)figure shows what you get keep in mind that what this technique achieves is merely form of image retexturingor texture transfer it works best with stylereference images that are strongly textured and highly self-similarand with content targets that don' require high levels of detail in order to be recognizable it typically can' achieve fairly abstract feats such as transferring the style of one portrait to another the algorithm is closer to classical signal processing than to aiso don' expect it to work like magiclicensed to