alexis-jacq / Pytorch-Tutorials

Pytorch tutorials for Neural Style transfert
136 stars 29 forks source link

error trying https://github.com/alexis-jacq/Pytorch-Tutorials/blob/master/Neural_Style.ipynb on PyTorch-0.4 #6

Closed stonebig closed 6 years ago

stonebig commented 6 years ago

code:

run = [0]
while run[0] <= 300:

    def closure():
        optimizer.zero_grad()
        model.forward(input)
        style_score = 0
        content_score = 0

        for sl in style_losses:
            style_score += sl.backward()
        for cl in content_losses:
            content_score += cl.backward()

        run[0]+=1
        if run[0] % 10 == 0:
            print("run " + str(run) + ":")
            print(style_score.data[0])
            print(content_score.data[0])

        return content_score+style_score

    optimizer.step(closure)

plt.subplot(224)
imshow(input.data)
plt.show()

error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-0659e65a7a4a> in <module>()
     21         return content_score+style_score
     22 
---> 23     optimizer.step(closure)
     24 
     25 plt.subplot(224)

C:\WinP\bd36\buQt5\winp64-3.6.x.1\python-3.6.5.amd64\lib\site-packages\torch\optim\lbfgs.py in step(self, closure)
    101 
    102         # evaluate initial f(x) and df/dx
--> 103         orig_loss = closure()
    104         loss = float(orig_loss)
    105         current_evals = 1

<ipython-input-22-0659e65a7a4a> in closure()
      9 
     10         for sl in style_losses:
---> 11             style_score += sl.backward()
     12         for cl in content_losses:
     13             content_score += cl.backward()

<ipython-input-15-ed2dfef0a270> in backward(self, retain_variables)
     15 
     16     def backward(self, retain_variables=True):
---> 17         self.loss.backward(retain_variables=retain_variables)
     18         return self.loss

TypeError: backward() got an unexpected keyword argument 'retain_variables'
alexis-jacq commented 6 years ago

Indeed, this code is no longer maintained and was not adapted to the last release of pytorch. The maintained version is available here: https://pytorch.org/tutorials/advanced/neural_style_tutorial.html#sphx-glr-advanced-neural-style-tutorial-py

stonebig commented 6 years ago

ok, thanks. I'm a newcomer in Torch, so not fully aware were to look first, sorry for the noise.

.

stonebig commented 6 years ago

struggling how to save the shown image at the end in a jpg file. something is tricky.