I’m trying to adjust this 3 implementation of Smoothgrad to operate on batches in order to speed things up. I changed the part of the function smooth_grad where it computes the gradients to:

global model, transform
tensor_input = torch.from_numpy(np.array(input)).type(torch.cuda.FloatTensor) # input is now of shape (w,h,c)
# x is the sample size
final_grad=[]
calc_inputs=[]
for i in range(x):
#print(‘Sample:’, i+1)
temp_input = tensor_input
# According to the paper, noise level corrresponds to stddev/(xmax-xmin). Hence stddev = noise_percentage * (max-min) /100
noise = torch.from_numpy(np.random.normal(loc=0, scale=(percent_noise/100) * (tensor_input.max() - tensor_input.min()).cpu(), size=temp_input.shape)).type(torch.cuda.FloatTensor)
temp_input = (temp_input + noise).cpu().numpy()
temp_input = Image.fromarray(temp_input.astype(np.uint8))
temp_input = Variable(transform(temp_input).unsqueeze(0).cuda(), requires_grad=True)
calc_inputs.append(temp_input.squeeze())
tensor_calc=Variable(torch.stack([i for i in calc_inputs]),requires_grad=True)
tensor_y=Variable(torch.tensor([label for i in range(x)]))
dataset = torch.utils.data.TensorDataset(tensor_calc,tensor_y)
loader = torch.utils.data.DataLoader(dataset, batch_size=50)
for imgs, labels in loader:
model.train()
output = model(imgs)
output.backward(torch.ones(50,1221).cuda())
print(imgs.grad)
#final_grad.append(imgs.grad))
final_grad=sum(final_grad)

But imgs.grad prints None. What am I doing wrong?