visualizing what convnets learn listing funct ion generat filt er visualizat ions builds loss function that maximizes the activation of the nth filter of the layer under consideration computes the gradient of the input picture with regard to this loss def generate_pattern(layer_namefilter_indexsize= )layer_output model get_layer(layer_nameoutput loss mean(layer_output[:::filter_index]grads gradients(lossmodel input)[ grads /( sqrt( mean( square(grads)) - iterate function([model input][lossgrads]normalization tricknormalizes the gradient returns the loss and grads given the input picture input_img_data np random random(( sizesize ) runs gradient ascent for steps step for in range( )loss_valuegrads_value iterate([input_img_data]input_img_data +grads_value step starts from gray image with some noise img input_img_data[ return deprocess_image(imglet' try it (see figure )plt imshow(generate_pattern('block _conv ' )figure pat ern hat he zerot channel in layer block _conv responds to maximally it seems that filter in layer block _conv is responsive to polka-dot pattern now the fun partyou can start visualizing every filter in every layer for simplicityyou'll only look at the first filters in each layerand you'll only look at the first layer of each convolution block (block _conv block _conv block _conv block conv block _conv you'll arrange the outputs on an grid of filter patternswith some black margins between each filter pattern (see figures licensed to