keras ValueError:没有为任何变量提供梯度:tensorflow GAN

mnemlml8  于 2023-01-26  发布在  其他
关注(0)|答案(1)|浏览(170)

我在使用Tensorflow GANs时遇到了这个奇怪的错误,即使在我向鉴别器提供梯度、检查范围、检查图层是否正确连接,或者优化器或图像预处理是否存在问题之后,也没有任何结果。以下是一些代码片段:

# Create the generator
def make_generator_model():
    model = tf.keras.Sequential()
    model.add(layers.Dense(7*7*256, use_bias=False, input_shape=(100,)))
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())

    model.add(layers.Reshape((7, 7, 256)))
    assert model.output_shape == (None, 7, 7, 256)

    model.add(layers.Conv2DTranspose(128, (5, 5), strides=(1, 1), padding='same', use_bias=False))
    assert model.output_shape == (None, 7, 7, 128)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())

    model.add(layers.Conv2DTranspose(64, (5, 5), strides=(2, 2), padding='same', use_bias=False))
    assert model.output_shape == (None, 14, 14, 64)
    model.add(layers.BatchNormalization())
    model.add(layers.LeakyReLU())

    model.add(layers.Conv2DTranspose(3, (5, 5), strides=(2, 2), padding='same', use_bias=False, activation='tanh'))
    assert model.output_shape == (None, 28, 28, 3)

    return model

# Create the discriminator
def make_discriminator_model():
    model = tf.keras.Sequential()
    model.add(layers.Conv2D(64, (5, 5), strides=(2, 2), padding='same',
                                     input_shape=[28, 28, 3]))
    model.add(layers.LeakyReLU())
    model.add(layers.Dropout(0.3))

    model.add(layers.Conv2D(128, (5, 5), strides=(2, 2), padding='same'))
    model.add(layers.LeakyReLU())
    model.add(layers.Dropout(0.3))

    model.add(layers.Flatten())
    model.add(layers.Dense(1))

    return model

# Define the loss functions and optimizers for the generator and discriminator
cross_entropy = BinaryCrossentropy(from_logits=True)

def discriminator_loss(real_output, fake_output):
    real_loss = cross_entropy(tf.ones_like(real_output), real_output)
    fake_loss = cross_entropy(tf.zeros_like(fake_output), fake_output)

    return real_loss + fake_loss

def generator_loss(fake_output):
    return cross_entropy(tf.ones_like(fake_output), fake_output)

这里是我定义我的生成器和鉴别器,并定义函数来计算它的损失

generator = make_generator_model()
discriminator = make_discriminator_model()



# Define the target size for the images
target_size = (28, 28)
images = []
corrupted_images_count = 0

# Resize the images
def reshape_images(images):
    reshaped_images = []
    for image in images:
        reshaped_image = cv2.resize(image, (28, 28))
        reshaped_images.append(reshaped_image)
    return reshaped_images

# Iterate over all the images in the dataset folder
print("Resizing and reshaping the images")
for filename in os.listdir('P2datasetFull/train/preprocessed'):
    # Load the image using OpenCV
    try:
        image = cv2.imread(os.path.join('P2datasetFull/train/preprocessed', filename))
    except:
        print("Corrupted Image: Skipping")
    images.append(image)

print(f'{corrupted_images_count} corrupted images found')
reshaped_images = reshape_images(images)

# Reshape the images
reshaped_images = np.array(reshaped_images)
reshaped_images = reshaped_images.reshape(-1, 28, 28, 3)

# Divide each pixel by 255
reshaped_images = reshaped_images / 255.0

然后我对图像进行预处理

# Define the optimizers for the generator and discriminator
print("Optimizing the models")
generator_optimizer = tf.keras.optimizers.Adam(1e-4)
discriminator_optimizer = tf.keras.optimizers.Adam(1e-4)

# Load the dataset and prepare it for training
batch_size = 2500
dataset = tf.data.Dataset.from_tensor_slices(np.array(reshaped_images))
dataset = dataset.shuffle(buffer_size=len(images))
dataset = dataset.batch(batch_size)

这里是我优化模型的地方

epochs = 20

# Define the training loop
print("Training the model")
for epoch in range(epochs):
    for image_batch in dataset:
        # Generate fake images
        noise = tf.random.normal([batch_size, 100])
        fake_images = generator(noise)

        # Compute the discriminator loss
        real_output = discriminator(image_batch, training=True)
        fake_output = discriminator(fake_images)
        disc_loss = discriminator_loss(real_output, fake_output)

        # Compute the generator loss
        gen_loss = generator_loss(fake_output)

        # Use tf.GradientTape to record the operations
        with tf.GradientTape() as disc_tape, tf.GradientTape() as gen_tape:
            real_output = discriminator(image_batch, training=True)
            fake_output = discriminator(fake_images, training=True)
            disc_loss = discriminator_loss(real_output, fake_output)
            gen_loss = generator_loss(fake_output)
            

        # Compute the gradients
        gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)
        gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)

        # Apply the gradients to the optimizer
        discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))
        generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))

模型在这里被训练,但是错误可能发生在gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)行或者下一行

def generate_and_save_images(model, epoch, test_input):
  # Generate images from the model
  predictions = model(test_input, training=False)

  # Rescale the pixel values from [-1, 1] to [0, 1]
  predictions = (predictions + 1) / 2.0

  # Save the generated images
  for i in range(predictions.shape[0]):
    img = predictions[i, :, :, :]
    img = tf.keras.preprocessing.image.array_to_img(img)
    img.save(os.path.join('generated', f'image_{epoch}_{i}.jpg'))
    cv2.imshow(img)
    cv2.waitKey(0)

# Generate a random noise as input for the generator
print("Testing the model")
test_input = tf.random.normal([batch_size, 100])

# Generate and save the images
print("Generating and saving the images.")
generate_and_save_images(generator, 0, test_input)

最后生成并保存图像,但执行仍然没有到达该部分。

  • 知道为什么吗 *
hgqdbh6s

hgqdbh6s1#

你必须记录下这条线内的一切

with tf.GradientTape() as disc_tape, tf.GradientTape() as gen_tape:

包括正向通行证......因此,你应该有一个这样的列车环线:

for epoch in range(epochs):
    for image_batch in dataset:
        # Generate fake images
        noise = tf.random.normal([batch_size, 100])
 
        # Use tf.GradientTape to record the operations
        with tf.GradientTape() as disc_tape, tf.GradientTape() as gen_tape:
            fake_images = generator(noise)

            # Compute the discriminator loss
            real_output = discriminator(image_batch, training=True)
            fake_output = discriminator(fake_images)
            disc_loss = discriminator_loss(real_output, fake_output)

            # Compute the generator loss
            gen_loss = generator_loss(fake_output)

            real_output = discriminator(image_batch, training=True)
            fake_output = discriminator(fake_images, training=True)
            disc_loss = discriminator_loss(real_output, fake_output)
            gen_loss = generator_loss(fake_output)
            

        # Compute the gradients
        gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)
        gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)

        # Apply the gradients to the optimizer
        discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))
        generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))

此代码中还有其他错误,这些错误与问题无关,因此我将避免指出它们......只要考虑您覆盖了GradientTape块中的disc_loss即可

相关问题