RuntimeError: Only Tensors of floating point dtype can require gradients
RuntimeError: Only Tensors of floating point dtype can require gradients

got following error from 


  input = Variable(preprocessed_img, requires_grad = True)


img=train_loader.dataset.data[0]
print(type(img))
img_tensor = torch.tensor(img)
preprocess_image(img)

&gt; def preprocess_image(img):    means=[0.485, 0.456, 0.406]     stds=[0.229,
&gt; 0.224, 0.225]
&gt; 
&gt;   preprocessed_img = img.copy()[: , :, ::-1]  for i in range(3):
&gt;       preprocessed_img[:, :, i] = preprocessed_img[:, :, i] - means[i]
&gt;       preprocessed_img[:, :, i] = preprocessed_img[:, :, i] / stds[i]
&gt;   preprocessed_img = \
&gt;       np.ascontiguousarray(np.transpose(preprocessed_img, (2, 0, 1)))
&gt;   preprocessed_img = torch.from_numpy(preprocessed_img)
&gt;   preprocessed_img.unsqueeze_(0)  input = Variable(preprocessed_img,
&gt; requires_grad = True)     return input


Use torch.Tensor(img) instead of torch.tensor(img). This should probably solve your issue.
