Can't optimize variable with Inception v3: RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

(Roman Koshkin) #1

I am trying to create a generative network based on the pre-trained Inception_v3.

  • I fix all the weights in the model
  • create a Variable whose size is (2, 3, 299, 299)
  • create targets of size (2, 1000) that I want my final layer activations to become as close as possible to by optimizing the Variable.

And it gives me the error: «RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation».

This is strange, because if I do the same thing with VGG16, everything works:

# minimalist code with Inception_v3 that should work but throws the error:

import torch
from torch.autograd import Variable
import torch.optim as optim
import torch.nn as nn
import torchvision

torch.set_default_tensor_type('torch.FloatTensor')
Iv3 = torchvision.models.inception_v3(pretrained=True)
for i in Iv3.parameters():
    i.requires_grad = False

criterion = nn.CrossEntropyLoss()
x = Variable(torch.randn(2, 3, 299, 299), requires_grad=True)
target = torch.empty(2, dtype=torch.long).random_(1000)
output = Iv3(x)
loss = criterion(output[0], target)
loss.backward()
print(x.grad)

# ########the same code but with VGG16 works ##########################

import torch
from torch.autograd import Variable
import torch.optim as optim
import torch.nn as nn
import torchvision

torch.set_default_tensor_type('torch.FloatTensor')
vgg16 = torchvision.models.vgg16(pretrained=True)
for i in vgg16.parameters():
    i.requires_grad = False

criterion = nn.CrossEntropyLoss()
x = Variable(torch.randn(2, 3, 229, 229), requires_grad=True)
target = torch.empty(2, dtype=torch.long).random_(1000)
output = vgg16(x)
loss = criterion(output, target)
loss.backward()
print(x.grad)

Please help.

(Xu Mingle) #2

It seems that we are facing same questions.

There are really strange.

My question is : here

(Xu Mingle) #3

I managed to solve this question.

I guess that the models, Inception_v3 and VGG16, are a little bit different.

Please check whether there is any nn.ReLU(True) or nn.LeakyReLU(True). And you need change it from ‘Ture’ to ‘False’

good luck

(Roman Koshkin) #4

Ok, when I create a model (it is called Iv3), I look inside by list(Iv3.children()). Surprisingly, I don’t see any ReLU or LeakyReLU whatsoever. Again, when I run list(vgg16.children()), I see the ReLU

(Roman Koshkin) #5

Turns out the problem was due to Pytorch 1.0.0. No problem with Pytorch 0.4.1. though.