Answers.Body: <p>It's just a typical optimization problem. You want to optimize function
  <span class="math-container">\begin{equation}
  f(\theta, x)
  \end{equation}</span>
  with fixed parameters <span class="math-container">$\theta$</span> (network weights) and optimization variable <span class="math-container">$x$</span>. Typically the approach is consisted of choosing a search direction and direction step. For deciding search direction you can use the gradient of objective with respect to decision variable and have an update of form
  <span class="math-container">\begin{equation}
  x = x - \alpha \nabla_x f(\theta, x)
  \end{equation}</span>
  where <span class="math-container">$\nabla_x f(\theta, x)$</span> is the gradient. You can also use Newton or Quasi-Newton methods of form
  <span class="math-container">\begin{equation}
  x = x - \alpha B^{-1}\nabla_x f(\theta, x)
  \end{equation}</span>
  where <span class="math-container">$B$</span> is a Hessian or approximate Hessian of <span class="math-container">$f(\theta, x)$</span> with respect to <span class="math-container">$x$</span>. Step length parameter is usually decided with line search or you can also use trust region approaches which decide step direction with constraints to it's length. For more details about optimization you can consult <em>Numerical Optimization</em> by Nocedal and Wright</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-21T16:54:52.943Z
Answers.Text: Thanks, I am trying to optimize by using gradient descent. But, I am looking for any inbuilt method in Keras to calculate the gradient of loss function w.r.t. input.
Answers.UserDisplayName: Preetz
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-01-20T20:41:41.687Z
Answers.LastActivityDate: 2021-01-20T20:41:41.687Z
Answers.UserDisplayName: Brale
Body: <p>I am working on an optimization problem. First, I have done forward training to work the network as a surrogate model, then I freeze the output and I want to find an optimal value of input for a given output.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-01-20T19:54:41.193Z
Comments.Text: Hello. Please, take a look at https://ai.stackexchange.com/help/how-to-ask and https://ai.stackexchange.com/help/on-topic to know more about our site. There are several things here that are not clear to me. What do you mean by "forward training"? Do you mean that you train your neural network with gradient descent and backpropagation as usual, so, I suppose, you also have a labelled dataset? Can you explain a little bit more in detail why you want to predict the input in this way? Moreover, why did you tag this post with [tag:inverse-rl]? What does this have to do with inverse RL?
Comments.UserDisplayName: nbro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-01-21T16:52:20.913Z
Comments.Text: Yes, I have trained the network in a usual way. I am trying to use this pre-trained model as a surrogate model for my optimization problem. Now I am trying to find optimal input in order to minimize the loss function. For that, I need to find the gradient of loss function with respect to the input. So, I am looking for any inbuilt method in Keras to find gradient w.r.t. input.
Comments.UserDisplayName: Preetz
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-01-21T16:55:30.533Z
Comments.Text: Can you please [edit your post](https://ai.stackexchange.com/posts/25878/edit) and you include these details (and other details that you find necessary to understand your problem) in your post? Moreover, I would like to note that programming issues are off-topic here: For instance, asking "how to do this in library Y" is off-topic here (see https://ai.stackexchange.com/help/on-topic). Here, we focus on theoretical issues. That's why the answer below tries to give you the main idea or mathematical approach.
Comments.UserDisplayName: nbro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-01-21T16:55:31.987Z
Comments.Text: In any case, it's ok to ask, in addition to your main theoretical question, if someone knows, how to do something in a specific library, but that can't be your main question, because otherwise it is off-topic here, as I said, and this type of programming issues are better suited for Stack Overflow.
Comments.UserDisplayName: nbro
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-01-22T12:53:32.223Z
Comments.Text: Once I've done that by simply adding a buffer layer after input. This layer just takes the input, adds bias to it and gives the output (biases are trainable, so instead of optimizing input, the network optimizes biases). It worked for me but I don't know if it's the best way to do so. If you need more info, let me know
Comments.UserDisplayName: amin
Tags: <neural-networks><training><optimization><inverse-rl>
Title: How to make input variable as trainable parameter in a neural network?
UserDisplayName: Preetz
ThreadText: # How to make input variable as trainable parameter in a neural network?
  
  (Asked by: username_0 on 2021-01-20)
  
  I am working on an optimization problem. First, I have done forward training to work the network as a surrogate model, then I freeze the output and I want to find an optimal value of input for a given output.
  
  **Tags:** neural-networks, training, optimization, inverse-rl
  > **Comments:**
  > **username_1**: Hello. Please, take a look at https://ai.stackexchange.com/help/how-to-ask and https://ai.stackexchange.com/help/on-topic to know more about our site. There are several things here that are not clear to me. What do you mean by "forward training"? Do you mean that you train your neural network with gradient descent and backpropagation as usual, so, I suppose, you also have a labelled dataset? Can you explain a little bit more in detail why you want to predict the input in this way? Moreover, why did you tag this post with [tag:inverse-rl]? What does this have to do with inverse RL?  
  > **username_0**: Yes, I have trained the network in a usual way. I am trying to use this pre-trained model as a surrogate model for my optimization problem. Now I am trying to find optimal input in order to minimize the loss function. For that, I need to find the gradient of loss function with respect to the input. So, I am looking for any inbuilt method in Keras to find gradient w.r.t. input.  
  > **username_1**: Can you please [edit your post](https://ai.stackexchange.com/posts/25878/edit) and you include these details (and other details that you find necessary to understand your problem) in your post? Moreover, I would like to note that programming issues are off-topic here: For instance, asking "how to do this in library Y" is off-topic here (see https://ai.stackexchange.com/help/on-topic). Here, we focus on theoretical issues. That's why the answer below tries to give you the main idea or mathematical approach.  
  > **username_1**: In any case, it's ok to ask, in addition to your main theoretical question, if someone knows, how to do something in a specific library, but that can't be your main question, because otherwise it is off-topic here, as I said, and this type of programming issues are better suited for Stack Overflow.  
  > **username_2**: Once I've done that by simply adding a buffer layer after input. This layer just takes the input, adds bias to it and gives the output (biases are trainable, so instead of optimizing input, the network optimizes biases). It worked for me but I don't know if it's the best way to do so. If you need more info, let me know  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2021-01-20. Score: 1
  
  It's just a typical optimization problem. You want to optimize function
  \begin{equation}
  f(\theta, x)
  \end{equation}
  with fixed parameters $\theta$ (network weights) and optimization variable $x$. Typically the approach is consisted of choosing a search direction and direction step. For deciding search direction you can use the gradient of objective with respect to decision variable and have an update of form
  \begin{equation}
  x = x - \alpha \nabla\_x f(\theta, x)
  \end{equation}
  where $\nabla\_x f(\theta, x)$ is the gradient. You can also use Newton or Quasi-Newton methods of form
  \begin{equation}
  x = x - \alpha B^{-1}\nabla\_x f(\theta, x)
  \end{equation}
  where $B$ is a Hessian or approximate Hessian of $f(\theta, x)$ with respect to $x$. Step length parameter is usually decided with line search or you can also use trust region approaches which decide step direction with constraints to it's length. For more details about optimization you can consult *Numerical Optimization* by Nocedal and Wright
  
  > **Comments:**
  > **username_0**: Thanks, I am trying to optimize by using gradient descent. But, I am looking for any inbuilt method in Keras to calculate the gradient of loss function w.r.t. input.  
  
  ---
  
  