简体   繁体   中英

ValueError: No gradients provided for any variable in TensorFlow when building a GAN

Despite there are a few questions related to the same error, I coulnd't solve my problem looking at those.

I'm trying to build a GAN for a uni asignment. My code is very similar to the intro example in this tutorial from TF's website.

Below are what I think are the relevant parts of the code (can provide more details if needed eg. how the discriminator model is built). The line that gives me the error is:

generator_optimizer.apply_gradients(zip(gradients_of_generator, generador.trainable_variables))

It might be related with the layers of my generator since it is almost the only difference with TF's example code..


    def create_generator(max_len, vocab_size):
      model = tf.keras.Sequential()
      model.add(tf.keras.layers.Embedding(vocab_size, output_dim=64, input_length=max_len))  
      model.add(tf.keras.layers.LSTM(units=1024, activation='tanh'))
      model.add(tf.keras.layers.Dense(units=1024, activation='sigmoid'))
      model.add(tf.keras.layers.Dense(units=MAX_LEN, activation=None))
    
      return model
    
    generator = create_generator(MAX_LEN, VOCABULARY_SIZE)
    
    for epoch in range(EPOCHS):
    
      noise = (tf.random.uniform([BATCH_SIZE, LATENT_DIM], minval=0, maxval = VOCABULARY_SIZE))
    
      with tf.GradientTape() as disc_tape, tf.GradientTape() as gen_tape:
    
        # Generator loss    
        fake_revs = generator(noise)
        pred_class_fake_revs = discriminator(fake_revs)
        gen_loss, gen_acc = generator_loss_and_accuracy(pred_class_fake_revs)
    
        # Disc loss
        real_revs = reviews_tok_padded[np.random.randint(0, len(reviews_tok_padded),BATCH_SIZE)]
        pred_class_real_revs = discriminator(real_revs) 
        disc_loss, disc_acc = discriminator_loss_and_accuracy(pred_class_real_revs, pred_class_fake_revs)
    
        gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)    
        disc_grad = disc_tape.gradient(disc_loss, discriminator.trainable_variables)
    
        generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
        disc_optimizer.apply_gradients(zip(disc_grad, discriminator.trainable_variables))

The exact error I get is:

ValueError: No gradients provided for any variable: ['embedding_22/embeddings:0', 'lstm_22/lstm_cell_30/kernel:0', 'lstm_22/lstm_cell_30/recurrent_kernel:0', 'lstm_22/lstm_cell_30/bias:0', 'dense_44/kernel:0', 'dense_44/bias:0', 'dense_45/kernel:0', 'dense_45/bias:0'].

Edit: after some further investigation it is clear that the issue is that the tape does not compute the gradient, so the variable gradients_of_generator equals none for all generator.trainable_variables. However, I don't know why this happens.

Any help is appreciated. Thank you!

So, I finally found what was causing the issue. It is related with the layers in the discriminator model, which is not even included in the code chunk above as I thought that was not the problem (because when I tested the discriminator as a standalone model, it worked). Here is how it is defined:


    def crear_discriminador():
      model = tf.keras.Sequential()    
      model.add(tf.keras.layers.Embedding(input_dim=VOCABULARY_SIZE, output_dim=64, input_length=MAX_LEN))
      model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(units=64, activation='tanh')))
      model.add(tf.keras.layers.Dense(64, activation='relu'))
      model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
    
    return model

The issue is that the Embedding layer is not differentiable, so when combining the generator and the discriminator, the embedding layer in the discriminator was preventing the gradient from being calculated for the layers in the generator.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM