generative deep learning nowlet' define tensor that contains the lossthe weighted sum of the norm of the activations of the layers in listing listing defining he loss be maximized creates dictionary that maps layer names to layer instances layer_dict dict([(layer namelayerfor layer in model layers]loss variable( for layer_name in layer_contributionscoeff layer_contributions[layer_nameactivation layer_dict[layer_nameoutput you'll define the loss by adding layer contributions to this scalar variable scaling prod( cast( shape(activation)'float ')loss +coeff sum( square(activation[: - - :])scaling adds the norm of the features of layer to the loss you avoid border artifacts by only involving nonborder pixels in the loss retrieves the layer' output nextyou can set up the gradient-ascent process listing gradient -ascent process this tensor holds the generated imagethe dream computes the gradients of the dream with regard to the loss dream model input grads gradients(lossdream)[ grads / maximum( mean( abs(grads)) - normalizes the gradients (important trickoutputs [lossgradsfetch_loss_and_grads function([dream]outputsdef eval_loss_and_grads( )outs fetch_loss_and_grads([ ]loss_value outs[ grad_values outs[ return loss_valuegrad_values def gradient_ascent(xiterationsstepmax_loss=none)for in range(iterations)loss_valuegrad_values eval_loss_and_grads(xif max_loss is not none and loss_value max_lossbreak print(loss value at' ':'loss_valuex +step grad_values return sets up keras function to retrieve the value of the loss and gradientsgiven an input image this function runs gradient ascent for number of iterations finallythe actual deepdream algorithm firstyou define list of scales (also called octavesat which to process the images each successive scale is larger than the previous one by factor of (it' larger)you start by processing small image and then increasingly scale it up (see figure licensed to