Random behavior of pytorch in class model visualization

I tried to implement the class model visualization (i.e., gradient ascent; update the images). I have already set the random seed. But, I am not sure why every time when I run let say 10 iterations, the output is always different. Can anyone tell me which part of the code introduce randomness?

# ------ load vgg16
vgg16=models.vgg16(pretrained=True)
vgg16.cuda()
vgg16.eval() 

# ------ random seed
rseed = 1
torch.manual_seed(rseed)
torch.cuda.manual_seed(rseed)

# ------ create empty img
img=torch.zeros(3,227,227).cuda()
img=img.unsqueeze(0) #Add an extra batch dimension
image=Variable(img,requires_grad=True) #Make it a variable which requires gradient

for i in range(num_iterations):       
    # ----- forward pass
    scores=vgg16(image)
    
    # ----- zero the graidents
    vgg16.zero_grad()
    
    # ----- backpropagate certain class gradient
    grad = torch.zeros(1,scores.size(1))
    grad[0,label_index] = 1
    scores.backward(grad.cuda())
        
    # update the image data
    image.data.add_(lr*image.grad.data)

no one can help ? :frowning:

the gradient wrt weights kernels for Convolution are non-deterministic in CuDNN. I think that’s your different outputs issue.

1 Like