Deepdream error: element 0 of tensors does not require grad and does not have a grad_fn

Hi everyone.
I’m trying to code the deepdream example, here is my code:

import numpy as np
from PIL import Image
import glob
import cv2
import os
from os.path import join as pjoin
from pdb import set_trace
import copy
import scipy

import matplotlib as mpl
import matplotlib.pyplot as plt

import torch
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
import torch.optim as optim
import torch.nn as nn

from import DataLoader, Dataset, random_split, TensorDataset, WeightedRandomSampler
from torchvision.transforms import Compose, ToTensor, Normalize, Resize, ToPILImage, CenterCrop, RandomResizedCrop
from torchvision import transforms
from torchvision.datasets import ImageFolder
from torchvision.models import resnet18, inception_v3
  from torchvision.models.utils import load_state_dict_from_url
except ImportError:
  from torch.hub import load_state_dict_from_url

import albumentations as albu
from albumentations.pytorch import ToTensorV2

import requests
from PIL import Image
import io
import cv2

from pdb import set_trace
from collections import OrderedDict

# Detect if we have a GPU available
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

URL = ""

response = requests.get(URL).content
im =
img =  np.array(im)
img = img[:, :, ::-1].copy() # RGB to BGR
img = img.astype(np.float32)
img /= 255.0

torch_img = torch.from_numpy(img)
torch_img = torch.permute(torch_img, (2, 0, 1))
torch_img = torch.unsqueeze(torch_img, 0)
torch_img = nn.Parameter(torch_img.clone().requires_grad_(True).to(DEVICE))

pretrained_model = torchvision.models.inception_v3(pretrained=True)
activation = OrderedDict()
def get_activation(name):
    def hook(model, input, output):
        activation[name] = output.detach()
    return hook

pretrained_model =
torch_img =

opt = optim.Adam([torch_img.requires_grad_()], lr=step)

for iter_idx in range(iterations):
    # Calculate loss
    out = pretrained_model(torch_img)
    activation2 = activation.copy()
    loss = 0
    for layer_name in layer_list:
        scaling = 1.0*activation[layer_name].numel()
        # set_trace()
        rms_loss = nn.MSELoss()
        # loss += (layer_settings[layer_name]*torch.sum(torch.square(activation2[layer_name])))/scaling
        loss += (layer_settings[layer_name]*rms_loss(activation2[layer_name], torch.zeros_like(activation[layer_name]).to(DEVICE)))/scaling
        # set_trace()
    # set_trace()
    loss = (-1)*loss
    # loss.requires_grad = True
#     if loss > max_loss:
#         break
    # Gradient ascent
    print("Epoch: %d. Loss: %f" % (iter_idx, loss.item()))

However, I got this error:

RuntimeError                              Traceback (most recent call last)
Cell In[30], line 95
     90     # loss.requires_grad = True
     91 #     if loss > max_loss:
     92 #         break
     93     # Gradient ascent
     94     opt.zero_grad()
---> 95     loss.backward()  
     96     opt.step()
     98     print("Epoch: %d. Loss: %f" % (iter_idx, loss.item()))

File /opt/conda/lib/python3.10/site-packages/torch/, in Tensor.backward(self, gradient, retain_graph, create_graph, inputs)
    477 if has_torch_function_unary(self):
    478     return handle_torch_function(
    479         Tensor.backward,
    480         (self,),
    485         inputs=inputs,
    486     )
--> 487 torch.autograd.backward(
    488     self, gradient, retain_graph, create_graph, inputs=inputs
    489 )

File /opt/conda/lib/python3.10/site-packages/torch/autograd/, in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs)
    195     retain_graph = create_graph
    197 # The reason we repeat same the comment below is that
    198 # some Python versions print out the first line of a multi-line function
    199 # calls in the traceback and some print out the last line
--> 200 Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
    201     tensors, grad_tensors_, retain_graph, create_graph, inputs,
    202     allow_unreachable=True, accumulate_grad=True)

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

Does anyone know how to fix it ? Thanks a lot.

You are explicitly detaching all your stored activations, which will not allow you to backpropagate through them.

Hi, thanks a lot, it works now! I copied it from the last code I wrote which ran fine but for some reason in my current code there was .detach() for some reason.