1

I am looking at this error. I am making a part of the model untrainable, I get an error in the .fit. would like to freeze the start of the model to only trade the head. top is the model, bottom is the .fit

def snakeEyes():
       K = 100
       i       = tf.keras.layers.Input(shape=(7,11,17), name="matrix") 
   
   x1 = tf.keras.layers.Conv2D(K, 2,activation="relu",padding ="same")(i)
   x1 = tf.keras.layers.Conv2D(K, 2,activation="relu")(x1)
   x1 = tf.keras.layers.MaxPooling2D ()(x1)
   x1 = tf.keras.layers.Conv2D(K, 2,activation="relu",padding ="same")(x1)
   x1 = tf.keras.layers.Conv2D(K, 2,activation="relu")(x1)
   x1 = tf.keras.layers.MaxPooling2D ()(x1)
   x1 = tf.keras.layers.Flatten()(x1)
   
   x2 = tf.keras.layers.Conv2D(K, 3,activation="relu",padding ="same")(i)
   x2 = tf.keras.layers.Conv2D(K, 3,activation="relu",padding ="same")(x2)
   x2 = tf.keras.layers.MaxPooling2D ()(x2)
   x2 = tf.keras.layers.Conv2D(K, 3,activation="relu",padding ="same")(x2)
   x2 = tf.keras.layers.Conv2D(K, 3,activation="relu",padding ="same")(x2)
   x2 = tf.keras.layers.MaxPooling2D ()(x2)
   x2 = tf.keras.layers.Flatten()(x2)
   
   x = tf.keras.layers.concatenate([x1,x2])
   
   x = tf.keras.layers.Dense(30,activation="relu")(x)
   logits = tf.keras.layers.Dense(4)(x)
   logits = tf.keras.layers.Softmax()(logits)
   
   
   values    = tf.keras.layers.Dense(64, activation='relu', name="out1")(x)
   values    = tf.keras.layers.Dense(1, activation="linear")(values)
    

   actor = Model(inputs=i, outputs=logits)
   actor.compile()

   critic = Model(inputs=i, outputs=values)
   critic.compile()
    
   policy = Model(inputs=i, outputs=[logits,values])
   policy.compile()
   
   return actor,critic,policy

then in a loop :

  for layer in agent1.critic.layers:layer.trainable = True
  for layer in agent1.actor.layers:layer.trainable = False
  
    
  agent1.critic.fit(state_matrices,rewards)
    
  episode_reward = tf.math.reduce_sum(rewards)
  return episode_reward , size

I get:

ValueError: No gradients provided for any variable: ['conv2d_104/kernel:0', 'conv2d_104/bias:0', ..
Mika S
  • 11
  • 1

1 Answers1

1

Your model is not compiled with any losses or optimizers. You have to define a loss and optimizer to be used and pass them to the compile. Something like:

optimizer = keras.optimizers.Adam(lr=learning_rate)
loss_fn = keras.losses.mean_squared_error()

actor.compile(optimizer=optimizer,
              loss=loss_fn