@@ -41,6 +41,13 @@ def hook_cnn_layer(self):
4141 """
4242 def hook_fn (module , grad_in , grad_out ):
4343 self .conv_output = grad_out [0 , self .cnn_filter ]
44+ print ('---- conv out -----' )
45+ print (type (self .conv_output ))
46+ print (self .conv_output .shape )
47+ print (grad_out .shape )
48+ print (grad_out [0 , self .cnn_filter ])
49+ print ('\n ----- grad in ------' )
50+ print (grad_in [0 ][0 ][0 ])
4451 # saving the number of filters in that layer
4552 self .num_filters = grad_out .shape [1 ]
4653 self .model [self .cnn_layer ].register_forward_hook (hook_fn )
@@ -72,13 +79,15 @@ def vis_cnn_layer(self):
7279 # self.conv_output = x[0, self.cnn_filter]
7380 # loss function according to Erhan et al. (2009)
7481 loss = - torch .mean (self .conv_output )
82+ print (loss .shape )
83+ print (loss .data )
7584 loss .backward () # calculate gradients
7685 optimizer .step () # update weights
7786 self .layer_img = rebuild_image (processed_image ) # reconstruct image
7887
7988 print ('Epoch {}/{} --> Loss {:.3f}' .format (e + 1 , self .epochs , loss .data .numpy ()))
8089
81- if e % 10 == 0 :
90+ if e % 5 == 0 :
8291 img_path = 'activ_max_imgs/am_vis_l' + str (self .cnn_layer ) + \
8392 '_f' + str (self .cnn_filter ) + '_iter' + str (e + 1 ) + '.jpg'
8493 save_image (self .layer_img , img_path )
0 commit comments