title: Simple TensorFlow LSTM Network: ValueError: No gradients provided for any variable
body: <p>I'm attempting to create a bare-bones, simple LSTM example to predict the next point in a sequence of floating point numbers. To keep things simple, I chose linear sample data. I'm using TensorFlow 2.3.1 and NumPy 1.18.5.</p>
  <p>Here's the setup of the input data, a basic rolling horizon of length 5:</p>
  <pre><code># Sample data -- linear function
  linear_data=0.05+0.05*np.array(range(20))
  
  # Size of window in rolling horizon
  window = 5
  
  # Tensorflow expects 3D: i,j,k = samples, window/sequence data, 
  # features (one since we're univariate)
  rolling_x = np.zeros([len(linear_data)-window, window, 1])
  rolling_y = np.zeros([len(linear_data)-window, 1])
  
  # Populate the rolling horizon data
  for idx in [t for t in range(len(linear_data)-window)]:
      rolling_x[idx, :, 0] = linear_data[idx:idx+window]
      rolling_y[idx, :] = linear_data[idx+window]
      
  print(rolling_x[:,:,0])
  
  &gt;&gt;&gt; [[0.05 0.1  0.15 0.2  0.25]
  &gt;&gt;&gt; [0.1  0.15 0.2  0.25 0.3 ]
  &gt;&gt;&gt; [0.15 0.2  0.25 0.3  0.35]
  ...
  &gt;&gt;&gt; [0.75 0.8  0.85 0.9  0.95]]
  </code></pre>
  <p>and where <code>rolling_y</code> contains the next item in the sequence. My understanding is that the data should be structured with the batch/sample data on first axis, sequence/window on the second, and number of features on the last (1 in this case since I'm univariate).</p>
  <p>From here I build a really simple model:</p>
  <pre><code># Build the model
  tf_model = Sequential()
  tf_model.add(LSTM(
      units=32,
      input_shape=[window, 1]
  ))
  tf_model.add(Dense(units=1))
  tf_model.compile()
  </code></pre>
  <p>It compiles fine, but when I try to train (<code>tf_model.fit(rolling_x, rolling_y)</code>) I get the following error:</p>
  <pre><code>    c:\users\____\documents\data_analytics\lstm-demo\env\lib\site-packages\tensorflow\python\keras\engine\training.py:806 train_function  *
          return step_function(self, iterator)
      c:\users\____\documents\data_analytics\lstm-demo\env\lib\site-packages\tensorflow\python\keras\engine\training.py:796 step_function  **
          outputs = model.distribute_strategy.run(run_step, args=(data,))
      c:\users\____\documents\data_analytics\lstm-demo\env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:1211 run
          return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
      c:\users\____\documents\data_analytics\lstm-demo\env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2585 call_for_each_replica
          return self._call_for_each_replica(fn, args, kwargs)
      c:\users\____\documents\data_analytics\lstm-demo\env\lib\site-packages\tensorflow\python\distribute\distribute_lib.py:2945 _call_for_each_replica
          return fn(*args, **kwargs)
      c:\users\____\documents\data_analytics\lstm-demo\env\lib\site-packages\tensorflow\python\keras\engine\training.py:789 run_step  **
          outputs = model.train_step(data)
      c:\users\____\documents\data_analytics\lstm-demo\env\lib\site-packages\tensorflow\python\keras\engine\training.py:756 train_step
          _minimize(self.distribute_strategy, tape, self.optimizer, loss,
      c:\users\____\documents\data_analytics\lstm-demo\env\lib\site-packages\tensorflow\python\keras\engine\training.py:2736 _minimize
          gradients = optimizer._aggregate_gradients(zip(gradients,  # pylint: disable=protected-access
      c:\users\____\documents\data_analytics\lstm-demo\env\lib\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py:562 _aggregate_gradients
          filtered_grads_and_vars = _filter_grads(grads_and_vars)
      c:\users\____\documents\data_analytics\lstm-demo\env\lib\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py:1270 _filter_grads
          raise ValueError(&quot;No gradients provided for any variable: %s.&quot; %
  
      ValueError: No gradients provided for any variable: ['lstm_3/lstm_cell_3/kernel:0', 'lstm_3/lstm_cell_3/recurrent_kernel:0', 'lstm_3/lstm_cell_3/bias:0', 'dense_3/kernel:0', 'dense_3/bias:0'].
  </code></pre>