title: Keras can not recognize the optimizer
body: <p>I implemented a Keras model using eager execution. When I run <em>model.fit</em> function in the last line of code, I get this error:</p>
  <pre><code>compile() missing 2 required positional arguments: 'AE_optimizer' and 'classification_optimizer'
  </code></pre>
  <p>I can not figure out the reason for the error. Why the optimizer is not recognized by the Keras API?</p>
  <p>Here is the source code that I am using.
  I use a class and define two components for the model: the encoder and projection head. I use contrastive learning to train the encoder. For this purpose, I have a contrastive loss inside the class. The compiler function has the tracker and the optimizer, but it raises the error. In <em>train_step</em> function, I use two augmented version of the input (x) and then I pass them through the projection head network for calculating the contrastive loss.</p>
  <p>Keras does not start to train, it shows the error in the last line <em>pretraining_model.fit()</em> function.</p>
  <pre><code>from tensorflow import keras
  from tensorflow.keras import layers
  # Define the contrastive model with model-subclassing
  class ContrastiveModel(keras.Model):
      def __init__(self):
          super().__init__()
  
          self.temperature = 1.0
          
          self.encoder = keras.Sequential(
          [
              keras.Input(shape=(42,)),
              layers.Dense(128, activation='relu'),
              layers.Dense(20, activation='relu'),
          ],
          name=&quot;encoder&quot;,
      )
          # Non-linear MLP as projection head
          self.projection_head = keras.Sequential(
              [
                  keras.Input(shape=(20,)),
                  layers.Dense(20, activation=&quot;relu&quot;),
                  layers.Dense(20),
              ],
              name=&quot;projection_head&quot;,
          )
          
  
      def compile(self, contrastive_optimizer, **kwargs):
          super().compile(**kwargs)
  
          self.contrastive_optimizer = contrastive_optimizer
          
          # self.contrastive_loss will be defined as a method
          
  
          self.contrastive_loss_tracker = keras.metrics.Mean(name=&quot;c_loss&quot;)
          self.contrastive_accuracy = keras.metrics.CategoricalAccuracy(name='c_accuracy')#SparseCategoricalAccuracy(name=&quot;c_acc&quot;)
          
    
  
      @property
      def metrics(self):
          return [
              self.contrastive_loss_tracker,
              self.contrastive_accuracy,
          ]
  
      def contrastive_loss(self, projections_1, projections_2):
          # InfoNCE loss (information noise-contrastive estimation)
          # NT-Xent loss (normalized temperature-scaled cross entropy)
  
          # Cosine similarity: the dot product of the l2-normalized feature vectors
          projections_1 = tf.math.l2_normalize(projections_1, axis=1)
          projections_2 = tf.math.l2_normalize(projections_2, axis=1)
          similarities = (
              tf.matmul(projections_1, projections_2, transpose_b=True) / self.temperature
          )
  
          # The similarity between the representations of two augmented views of the
          # same image should be higher than their similarity with other views
          batch_size = tf.shape(projections_1)[0]
          contrastive_labels = tf.range(batch_size)
          self.contrastive_accuracy.update_state(contrastive_labels, similarities)
          self.contrastive_accuracy.update_state(
              contrastive_labels, tf.transpose(similarities)
          )
  
          # The temperature-scaled similarities are used as logits for cross-entropy
          # a symmetrized version of the loss is used here
          loss_1_2 = keras.losses.sparse_categorical_crossentropy(
              contrastive_labels, similarities, from_logits=True
          )
          loss_2_1 = keras.losses.sparse_categorical_crossentropy(
              contrastive_labels, tf.transpose(similarities), from_logits=True
          )
          return (loss_1_2 + loss_2_1) / 2
  
      def train_step(self, data):
          # Both labeled and unlabeled images are used, without labels
          masks1, augmented1 = augmentation(d1,data)
  
  
          masks2, augmented2 = augmentation(d2,data)
  
  
          with tf.GradientTape() as tape:
              features_1 = self.encoder(augmented1, training=True)
              features_2 = self.encoder(augmented2, training=True)
              
              # The representations are passed through a projection mlp
              projections_1 = self.projection_head(features_1, training=True)
              projections_2 = self.projection_head(features_2, training=True)
              closs = self.contrastive_loss(projections_1, projections_2)          
              
              total_loss = closs 
  
          trainable_var =  self.encoder.trainable_weights + self.projection_head.trainable_weights 
          gradients = tape.gradient(total_loss, trainable_var,)
          self.contrastive_optimizer.apply_gradients(zip(gradients, trainable_var,))
          self.contrastive_loss_tracker.update_state(closs)
               
          return {m.name: m.result() for m in self.metrics}
  
  # Contrastive pretraining
  pretraining_model = ContrastiveModel()
  pretraining_model.compile(contrastive_optimizer=keras.optimizers.Adam(),)
  
  pretraining_history = pretraining_model.fit(train_dataset, epochs=25)
  </code></pre>
  <p>Can you guide me on why this error is happening?</p>