2

Using PyTorch in python, I'm feeding-back one of my CNN's layers into input-space by using an inverse network that I'm training.

However, I'm interested in the representation of only one channel. I get this by setting all values to 0 except from those of the channel that I'm interested in.

image_representation is a [torch.FloatTensor of size 1x64x56x56]

image_representation[0, 1:, :, :] = 0

Inserting image_representation into the inverse net yields an image. I'm feeding this image into the original CNN results into another representation that I can compare with the representation resulting from the original image. This comparison makes my loss function

However, when I run it I get the error

RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

I understand that the gradient cannot be fully computed because of the zeros, but how can i design my code in such a way that it will omit this error, and do the computations only for one of the 64 channels?

2
  • Why don't you use a new Variable? i.e. one channel = Variable(torch.zeros(1,64,56, 56)) one_channel[0, 1:, :, :] = image_representation[0, 1:, :, :], then feed this into your inverse net and the result into your original CNN?
    – mbpaulus
    Jun 23, 2017 at 12:53
  • @Sumaku your solution works for me. output[0, 1, :, :]=0 Aug 1, 2018 at 7:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy