title: Keras fit not training model weights
body: <p>I am trying to fit a simple histogram model with custom weights and no input. It should fit a histogram for the data generated by:</p>
  <pre class="lang-py prettyprint-override"><code>train_data = [max(0,int(np.round(np.random.randn()*2+5))) for i in range(1000)]
  </code></pre>
  <p>The model is defined by</p>
  <pre class="lang-py prettyprint-override"><code>d = 15
  class hist_model(tf.keras.Model):
      def __init__(self):
          super(hist_model,self).__init__()
          self._theta = self.add_weight(shape=[1,d],initializer='zero',trainable=True)
          
      
      def call(self,x):
          return self._theta
  </code></pre>
  <p>The problem I have is that training using <code>model.fit</code> doesn't work: The model weights don't change at all during training. I tried:</p>
  <pre class="lang-py prettyprint-override"><code>model = hist_model()
  model.compile(optimizer = tf.keras.optimizers.SGD(learning_rate=1e-2),
                  loss=&quot;sparse_categorical_crossentropy&quot;)
  history = model.fit(train_data,train_data,verbose=2,batch_size=1,epochs=10)
  model.summary()
  </code></pre>
  <p>Which returns:</p>
  <pre><code>Epoch 1/3
  1000/1000 - 1s - loss: 2.7080
  Epoch 2/3
  1000/1000 - 1s - loss: 2.7080
  Epoch 3/3
  1000/1000 - 1s - loss: 2.7080
  Model: &quot;hist_model_17&quot;
  _________________________________________________________________
  Layer (type)                 Output Shape              Param #   
  =================================================================
  Total params: 15
  Trainable params: 15
  Non-trainable params: 0
  ________________________
  </code></pre>
  <p>I tried writing a custom training loop for the same model, it worked well. This is the code for the custom training:</p>
  <pre class="lang-py prettyprint-override"><code>optimizer = tf.keras.optimizers.SGD(learning_rate=1e-3)
  loss_fn = keras.losses.SparseCategoricalCrossentropy(from_logits=True)
  for epoch in range(3):
      running_loss = 0
      for data in train_data:
          with tf.GradientTape() as tape:
              loss_value = loss_fn(data,model(data))
              running_loss += loss_value.numpy()
              grad = tape.gradient(loss_value,model.trainable_weights)
              optimizer.apply_gradients(zip(grad, model.trainable_weights))
      print(f'Epoch {epoch} loss: {loss_value}')
  </code></pre>
  <p>I still don't understand why the fit method doesn't work. What am I missing? Thanks!</p>