visualizing what convnets learn visualizing convnet filters another easy way to inspect the filters learned by convnets is to display the visual pattern that each filter is meant to respond to this can be done with gradient ascent in input space applying gradient descent to the value of the input image of convnet so as to maximize the response of specific filterstarting from blank input image the resulting input image will be one that the chosen filter is maximally responsive to the process is simpleyou'll build loss function that maximizes the value of given filter in given convolution layerand then you'll use stochastic gradient descent to adjust the values of the input image so as to maximize this activation value for instancehere' loss for the activation of filter in the layer block _conv of the vgg networkpretrained on imagenet listing defining he loss ensor for filt er visualizat ion from keras applications import vgg from keras import backend as model vgg (weights='imagenet'include_top=falselayer_name 'block _conv filter_index layer_output model get_layer(layer_nameoutput loss mean(layer_output[:::filter_index]to implement gradient descentyou'll need the gradient of this loss with respect to the model' input to do thisyou'll use the gradients function packaged with the backend module of keras listing obt aining he gradient of he loss wit regard he input grads gradients(lossmodel input)[ the call to gradients returns list of tensors (of size in this casehenceyou keep only the first element-which is tensor non-obvious trick to use to help the gradient-descent process go smoothly is to normalize the gradient tensor by dividing it by its norm (the square root of the average of the square of the values in the tensorthis ensures that the magnitude of the updates done to the input image is always within the same range listing gradient -normalizat ion rick grads /( sqrt( mean( square(grads)) - add - before dividing to avoid accidentally dividing by now you need way to compute the value of the loss tensor and the gradient tensorgiven an input image you can define keras backend function to do thisiterate is licensed to