Answers.Body: <blockquote>
  <p>Can my loss function be evaluating the model until it dies? 1/survival time could be the loss value to be minimized by gradient descent.</p>
  </blockquote>
  <p>In order to use backpropagation and gradient descent, you have to relate the loss function directly to the output of the neural network. Your proposed loss function is too indirect, it is not possible to turn it <em>directly</em> into a gradient that could be used to alter the neural network weights.</p>
  <p>In addition, the specific function of time you have chosen will be difficult to optimise, as incrememental improvements from e.g. 10 to 11 time steps surviving will provide a much lower signal for adjusting behaviour than the improvement from  e.g. 2 to 3 lifetime. If the environment has enough randomness (and typically these kind of a-life scenarios do), then the signal here could be swamped by random events and very hard to optimise, requiring a larger number of samples in order to extract expected improvements to the loss function.</p>
  <blockquote>
  <p>Is this a reinforcement learning problem (I don't think so..?)</p>
  </blockquote>
  <p>It is very close to a definition of reinforcement learning (RL) problem. For a RL problem you need the following things:</p>
  <ul>
  <li><p>An environment in which an agent exists, and which has a measurable state.</p>
  </li>
  <li><p>A set of actions = sequential decisions that need to be made, and that have consequences.</p>
  </li>
  <li><p>The consequences of any action are:</p>
  <ul>
  <li>A consistent (but allowing for some randomness) change to the state of the environment. Here you have change to agent's location and resource levels.</li>
  <li>A consistent scalar reward value that measures how well the agent is achieving a goal. This can also be partially random, and can also be sparse, only achieved in certain specific states.</li>
  </ul>
  </li>
  </ul>
  <p>In your problem definition you don't have a reward signal, but it would be easy to add one. A suitable one would be <span class="math-container">$+1$</span> per time step.</p>
  <p>Technically your problem would also be partially observable (sometimes called a POMDP), in that the agent does not get to see resources available in other locations. It only knows its current location, its internal state and resources available at its current location. This is not a major issue, although you should note that adding some kind of memory (either open-ended memory as in a recurrent neural network, or explicitly added to the state) would allow for more efficient agents. That's not a RL issue as such, any learning process without ability to form or use memories would be limited in this environment, and you might want to look into that as a later experiment.</p>
  <p>How RL helps is that it provides a framework to convert your problem definition into measurements and <em>gradients</em> for the neural network to improve its performance.</p>
  <p>As you have set up your neural network to predict best action choice, this would naturally lend itself to policy gradient methods in RL, such as REINFORCE. As it is a simple problem, I would expect REINFORCE with baseline to perform well enough for it.</p>
  <p>I will not explain the algorithm here in full detail, but the basic approach is to have the agent act with the current network for a few episodes, collecting data on its choices and performance. You will get a dataset of (state, action, return = sum of rewards to end of episode). You then use that as a labelled dataset to train a minibatch as if the action choice was correct ground truth, but multiply each gradient by (return - baseline) where the baseline is typically the average return seen so far from that state. You may need a second neural network during training to estimate that expected return (aka state value). After using a minibatch once, you will need to discard it, as it represents results for the previous iteration of the network before the weight updates. There are ways around this, but not typically done in REINFORCE - instead the approach is to just keep generating data and train on the new mini-batch as fast as you can go.</p>
  <p>RL offers other methods which may work just as well to solve your problem, but I suspect REINFORCE will serve you well since it will allow the agent to randomise direction choice which is important when it cannot see where the resources are and has no memory of where it has searched.</p>
  <p>You don't have to use RL for this problem. An alternative that may work for you is using genetic algorithms to tune the network architecture and weights. It avoids using gradients, but I would still recommend a simple fitness function equal to number of time steps survived. There is a framework called NEAT which is ideal for this sort of a-life control problem.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-15T01:31:59.233Z
Answers.Text: Thanks! I'm checking out the [neat-python](https://github.com/CodeReclaimers/neat-python) library as I originally wanted to do this with genetic algorithms!
Answers.UserDisplayName: Tristan Kos
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-14T10:04:54.607Z
Answers.LastActivityDate: 2020-10-14T13:26:09.067Z
Answers.LastEditDate: 2020-10-14T13:26:09.067Z
Answers.UserDisplayName: Neil Slater
Body: <p>I am interested in models that exhibit behavior. My goal is a model that survives indefinitely on a two dimensional resource landscape. One dimension represents the location (0 to 1) and the second says if there is a resource available at that location (-1 = resource one, 0 = no resource, 1 = resource two).</p>
  <p>The landcape looks like this:</p>
  <pre><code>location = [0, 0.2, 0.4, 0.6, 0.8, 1]
  resource = [-1, 0,   0,   0,   0,  1] (I added spaces so the elements line up)
  </code></pre>
  <p>My model represents an organism deciding if it will move or rest on the landscape at each time step. The organism has reserves of each resource. The organism fills its reserve of a resource if it rests on the resource and loses 1 unit of both resources at each time step. I am considering neural networks to represent my organisms. The input would be 4 values; The location on the landscape, the resource value at that location, and the reserve levels of resource one and two. The output would be 3 values; move right, rest, move left. The highest value decides what happens. To survive indefinitely the model will have to bounce between the ends of the landscape, briefly resting on the resource. Model evaluation would go like this: start the model in the middle of the landscape with full resource reserves. Allow time to pass until one of the resource reserves is depleted (the organism dies).</p>
  <p>My question is this: Can my loss function be evaluating the model until it dies? 1/survival time could be the loss value to be minimized by gradient descent. Is this a reinforcement learning problem (I don't think so..?) Thanks!!</p>
  
Tags: <neural-networks><reinforcement-learning><tensorflow><keras><objective-functions>
Title: Evaluate model multiple times in loss function? Is this reinforcement learning?
UserDisplayName: Tristan Kos
ThreadText: # Evaluate model multiple times in loss function? Is this reinforcement learning?
  
  (Asked by: username_0 on 2020-10-14)
  
  I am interested in models that exhibit behavior. My goal is a model that survives indefinitely on a two dimensional resource landscape. One dimension represents the location (0 to 1) and the second says if there is a resource available at that location (-1 = resource one, 0 = no resource, 1 = resource two).
  
  The landcape looks like this:
  
  ```
  location = [0, 0.2, 0.4, 0.6, 0.8, 1]
  resource = [-1, 0,   0,   0,   0,  1] (I added spaces so the elements line up)
  
  ```
  
  My model represents an organism deciding if it will move or rest on the landscape at each time step. The organism has reserves of each resource. The organism fills its reserve of a resource if it rests on the resource and loses 1 unit of both resources at each time step. I am considering neural networks to represent my organisms. The input would be 4 values; The location on the landscape, the resource value at that location, and the reserve levels of resource one and two. The output would be 3 values; move right, rest, move left. The highest value decides what happens. To survive indefinitely the model will have to bounce between the ends of the landscape, briefly resting on the resource. Model evaluation would go like this: start the model in the middle of the landscape with full resource reserves. Allow time to pass until one of the resource reserves is depleted (the organism dies).
  
  My question is this: Can my loss function be evaluating the model until it dies? 1/survival time could be the loss value to be minimized by gradient descent. Is this a reinforcement learning problem (I don't think so..?) Thanks!!
  
  **Tags:** neural-networks, reinforcement-learning, tensorflow, keras, objective-functions
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-10-14. Score: 1 | Accepted answer
  
  > Can my loss function be evaluating the model until it dies? 1/survival time could be the loss value to be minimized by gradient descent.
  
  In order to use backpropagation and gradient descent, you have to relate the loss function directly to the output of the neural network. Your proposed loss function is too indirect, it is not possible to turn it *directly* into a gradient that could be used to alter the neural network weights.
  
  In addition, the specific function of time you have chosen will be difficult to optimise, as incrememental improvements from e.g. 10 to 11 time steps surviving will provide a much lower signal for adjusting behaviour than the improvement from e.g. 2 to 3 lifetime. If the environment has enough randomness (and typically these kind of a-life scenarios do), then the signal here could be swamped by random events and very hard to optimise, requiring a larger number of samples in order to extract expected improvements to the loss function.
  
  > Is this a reinforcement learning problem (I don't think so..?)
  
  It is very close to a definition of reinforcement learning (RL) problem. For a RL problem you need the following things:
  
  * An environment in which an agent exists, and which has a measurable state.
  * A set of actions = sequential decisions that need to be made, and that have consequences.
  * The consequences of any action are:
  
    + A consistent (but allowing for some randomness) change to the state of the environment. Here you have change to agent's location and resource levels.
    + A consistent scalar reward value that measures how well the agent is achieving a goal. This can also be partially random, and can also be sparse, only achieved in certain specific states.
  
  In your problem definition you don't have a reward signal, but it would be easy to add one. A suitable one would be $+1$ per time step.
  
  Technically your problem would also be partially observable (sometimes called a POMDP), in that the agent does not get to see resources available in other locations. It only knows its current location, its internal state and resources available at its current location. This is not a major issue, although you should note that adding some kind of memory (either open-ended memory as in a recurrent neural network, or explicitly added to the state) would allow for more efficient agents. That's not a RL issue as such, any learning process without ability to form or use memories would be limited in this environment, and you might want to look into that as a later experiment.
  
  How RL helps is that it provides a framework to convert your problem definition into measurements and *gradients* for the neural network to improve its performance.
  
  As you have set up your neural network to predict best action choice, this would naturally lend itself to policy gradient methods in RL, such as REINFORCE. As it is a simple problem, I would expect REINFORCE with baseline to perform well enough for it.
  
  I will not explain the algorithm here in full detail, but the basic approach is to have the agent act with the current network for a few episodes, collecting data on its choices and performance. You will get a dataset of (state, action, return = sum of rewards to end of episode). You then use that as a labelled dataset to train a minibatch as if the action choice was correct ground truth, but multiply each gradient by (return - baseline) where the baseline is typically the average return seen so far from that state. You may need a second neural network during training to estimate that expected return (aka state value). After using a minibatch once, you will need to discard it, as it represents results for the previous iteration of the network before the weight updates. There are ways around this, but not typically done in REINFORCE - instead the approach is to just keep generating data and train on the new mini-batch as fast as you can go.
  
  RL offers other methods which may work just as well to solve your problem, but I suspect REINFORCE will serve you well since it will allow the agent to randomise direction choice which is important when it cannot see where the resources are and has no memory of where it has searched.
  
  You don't have to use RL for this problem. An alternative that may work for you is using genetic algorithms to tune the network architecture and weights. It avoids using gradients, but I would still recommend a simple fitness function equal to number of time steps survived. There is a framework called NEAT which is ideal for this sort of a-life control problem.
  
  > **Comments:**
  > **username_0**: Thanks! I'm checking out the [neat-python](https://github.com/CodeReclaimers/neat-python) library as I originally wanted to do this with genetic algorithms!  
  
  ---
  
  