Search code examples
tensorflowbackpropagationsoftmax

Tensorflow freeze model but backpropagate error and update input layer


I try to explain my goal. With a trained model I want to select the output class and update the feeded image.

  1. Assign an image to the 'input_layer'.
  2. Forward and compute error of 'output_layer' against desired output/class.
  3. Backpropagate the error to the 'input_layer' without updating the weights and biases of the net.
  4. Update the input layer, the original image, with the backpropagated error.

Some hint?


Solution

  • You can use tf.gradients to back propagate to the input layer:

    ...
    logits = run_net(image)
    g = tf.gradients(logits[target_class], image)
    image += g[0] * step
    ...
    

    Good examples of doing this can be found in the Deep Dream demo code (see for example "Naive feature visualization" or "Multiscale image generation."