因此,我尝试遵循DCGAN指南在tensorflow上生成图像https://www.tensorflow.org/tutorials/generative/dcgan,并且我已经非常紧密地复制了代码,只是将数据集更改为我想要使用的数据集。每当我试图训练模型时,我都会遇到这个错误-
ValueError:Layer sequential_1需要1个输入,但它收到了2个输入张量。接收到的输入:[lt;tf.Tensor'图像:0'形状=(256,28,28,3(dtype=float32>,<tf.Tsensor'图像_1:0'形状=(56,(dtype=ent32>]
特别是train_step函数中的这一行导致错误,
real_output = discriminator(images, training=True)
当它在列车功能内被调用时
train(normalizedData, epochs)
鉴别器函数的定义是这样的,在代码的前面:
def make_discriminator_model():
model = tf.keras.Sequential()
model.add(layers.Conv2D(64, (5,5), strides=(2,2), padding='same', input_shape=[28, 28, 1]))
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
discriminator = make_discriminator_model()
以下是该块的其余内容。
@tf.function
def train_step(images):
noise = tf.random.normal([batch_size, noise_dim])
with tf.GradientTape() as gen_tape, tf.GradientTape() as disc_tape:
generated_images = generator(noise, training=True)
real_output = discriminator(images, training=True)
fake_output = discriminator(generated_images, training=True)
gen_loss = generator_loss(fake_output)
disc_loss = discriminator_loss(real_output, fake_output)
gradients_of_generator = gen_tape.gradient(gen_loss, generator.trainable_variables)
gradients_of_discriminator = disc_tape.gradient(disc_loss, discriminator.trainable_variables)
generator_optimizer.apply_gradients(zip(gradients_of_generator, generator.trainable_variables))
discriminator_optimizer.apply_gradients(zip(gradients_of_discriminator, discriminator.trainable_variables))
def train(dataset, epochs):
for epoch in range(epochs):
start = time.time()
for image_batch in dataset:
train_step(image_batch)
display.clear_output(wait=True)
generate_and_save_images(generator,
epoch + 1,
seed)
if (epoch + 1) % 15 == 0:
checkpoint.save(file_prefix = checkpoint_prefix)
print('Time for epoch {} is {} sec'.format(epoch + 1, time.time()-start))
display.clear_output(wait=True)
generate_and_save_images(generator,
epochs,
seed)
def generate_and_save_images(model, epoch, test_input):
predictions = model(test_input, training=False)
fig = plt.figure(figsize=(4,4))
for i in range(predictions.shape[0]):
plt.subplot(4,4,i+1)
plt.imshow(predicitons[i, :, :, 0] * 127.5 + 127.5, cmap='gist_rainbow')
plt.axis('off')
plt.savefig('image_at_epoch_{:04d}.png'.format(epoch))
plt.show()
train(normalizedData, epochs)
关于这个值错误,我在这里看到了这个问题的不同变体,根据我所收集的信息,顺序层被输入的是列表而不是元组?
感谢您的时间和您能提供的任何帮助。
错误告诉您,您对鉴别器的输入是[<tf.Tensor 'images:0' shape=(256, 28, 28, 3) dtype=float32>, <tf.Tensor 'images_1:0' shape=(256,) dtype=int32>]
的形状,但您定义的鉴别器具有input_shape=[28, 28, 1]
检查您在real_output = discriminator(images, training=True)
线上输入鉴别器的images
,确保images
与鉴别器输入的形状相同,例如(256,28,28,3(
我在GAN教程teansorflow文档中遇到了同样的问题https://www.tensorflow.org/tutorials/generative/dcgan,请使用tf.reshape来重塑日期集
discriminator(tf.reshape(images, (1, 28, 28, 1)), training=True)
它对我有用。