0

I've been given a fully trained model by another researcher that has inputs as placeholders. Regarding it as a function f(x), I would like to find x to minimize my distance metric (loss function) dist(x, f(x)). This could be something like the euclidean distance between the two points.

I tried to use TensorFlow's built-in optimizer functions. The issue is that tf.train.AdamOptimizer(1e-4).minimize(loss, var_list[input_placeholder]) fails, complaining that input_placeholder isn't of a supported type. Thus, I cannot get gradients for my input.

How can I optimize a function in TensorFlow when the inputs have to be specified in this way? Unfortunately, these placeholders are not passed through a Variable first, and I have to treat that model as a black box.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • I have seen [this](https://stackoverflow.com/questions/41918795/minimize-a-function-of-one-variable-in-tensorflow) related question, but it uses a `Variable` to store the input to optimize. If I could feed a `Variable` into a placeholder, I could do what the answer there does. – Alex Reinking Oct 01 '17 at 09:26
  • Isn't `d(x - f(x))` `1 - df(x)`? – pjpj Oct 01 '17 at 10:27
  • No, sorry, `d` is some distance function, not the differentiation operator. I'll update my question to make that clear – Alex Reinking Oct 01 '17 at 10:55

1 Answers1

0

Using the Keras functional API detailed in this question, I created a dense layer with no bias to sit right before the model I was given. Holding its input as a constant all 1's vector, I optimized the joined model using only the Variable in the dense layer, giving me the optimal vector as the output of that layer.

All TensorFlow Optimizer subclasses allow you to minimize while only modifying a particular set of Variables, which I got out of Keras fairly simply.

Alex Reinking
  • 16,724
  • 5
  • 52
  • 86
  • If someone knows of a better solution, please let me know. It might also be worth adding something like a "Constant" layer to Keras to enable doing something like this more easily. – Alex Reinking Oct 02 '17 at 08:51