Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neural style transfer tutorial requires_grad_ error #1736

Closed
chacorp opened this issue Oct 30, 2021 · 3 comments · Fixed by #2371
Closed

Neural style transfer tutorial requires_grad_ error #1736

chacorp opened this issue Oct 30, 2021 · 3 comments · Fixed by #2371
Assignees
Labels
docathon-h1-2023 A label for the docathon in H1 2023 easy

Comments

@chacorp
Copy link

chacorp commented Oct 30, 2021

Hi, thanks for the all those easy tutorials!

recently i've run the tutorial codes for Neural style transfer and got an error like this

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-32-e24e09205fd4> in <module>()
      1 output = run_style_transfer(cnn, cnn_normalization_mean, cnn_normalization_std,
----> 2                             content_img, style_img, input_img)
      3 
      4 plt.figure()
      5 imshow(output, title='Output Image')

<ipython-input-31-3ee3a2602483> in run_style_transfer(cnn, normalization_mean, normalization_std, content_img, style_img, input_img, num_steps, style_weight, content_weight)
     10     # update all the requires_grad fields accordingly
     11     input_img.requires_grad_(True)
---> 12     model.requires_grad_(False)
     13    

~/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py in __getattr__(self, name)
    533                 return modules[name]
    534         raise AttributeError("'{}' object has no attribute '{}'".format(
--> 535             type(self).__name__, name))
    536 
    537     def __setattr__(self, name, value):

AttributeError: 'Sequential' object has no attribute 'requires_grad_'

Since it was optimizing the input not the model, I changed the code "model.requires_grad_(False)" in run_style_transfer function to "model.eval()". And then it worked out fine!

i'm not sure if I changed it right, But I think this part should be modified in the code!

@svekars
Copy link
Contributor

svekars commented Mar 7, 2023

@svekars svekars added easy docathon-h1-2023 A label for the docathon in H1 2023 labels May 31, 2023
@Suhas-G
Copy link
Contributor

Suhas-G commented May 31, 2023

/assigntome

@Suhas-G
Copy link
Contributor

Suhas-G commented May 31, 2023

Hello,
This example actually works fine. pytorch/pytorch#22576 added the requires_grad_ function for nn.Module as well. Does it still make sense to change to model.eval() ?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docathon-h1-2023 A label for the docathon in H1 2023 easy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants