- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 Keras 中训练生成对抗网络 (GAN)。
我的日志报告两个网络(鉴别器和组合模型)都达到了 100% 的准确度。这表明出现了问题。
我尝试运行推理,发现鉴别器确实 100% 准确,但生成器只生成噪声,根本无法欺骗鉴别器。
我的问题:为什么 Keras 报告我的组合模型的准确性为 100%?
代码:
generator = create_generator(input_shape=(374,))
in_vector = Input(shape=(374,))
fake_images = generator(in_vector)
discriminator = create_discriminator()
disc_optimizer = keras.optimizers.SGD(lr=1e-4)
discriminator.compile(optimizer=disc_optimizer, loss='binary_crossentropy', metrics=['accuracy'])
discriminator.trainable = False
for l in discriminator.layers:
l.trainable = False
gan_output = discriminator(fake_images)
gan = Model(in_vector, gan_output)
gan_optimizer = keras.optimizers.RMSprop(lr=1e-5)
gan.compile(optimizer=gan_optimizer, loss='binary_crossentropy', metrics=['accuracy'])
start_time = datetime.datetime.now()
tensorboard = TensorBoard(log_dir=f'data/logs/gawwn/{start_time}')
tensorboard.set_model(gan)
d_train_logs = ['train_discriminator_loss',
'train_discriminator_accuracy']
g_train_logs = ['train_generator_loss',
'train_generator_accuracy']
val_logs = ['val_discriminator_loss',
'val_discriminator_accuracy',
'val_generator_loss',
'val_generator_accuracy']
d_train_step, g_train_step, val_step = 0, 0, 0
valid = np.ones((batch_size, 1))
fake = np.zeros((batch_size, 1))
noise_sigma = 0.00
noise_decay = 0.95
for epoch in range(1, 1 + epochs):
d_loss = [1]
while d_loss[0] > d_loss_thres:
for i, (x_vectors, x_images, y) in enumerate(train_loader.load_batch(batch_size)):
# ---------------------
# Train Discriminator
# ---------------------
# Generate a batch of new images
gen_imgs = generator.predict(x_vectors)
# Train the discriminator
data = np.concatenate([y, gen_imgs], axis=0)
labels = np.concatenate([valid[:len(y)], fake[:len(y)]])
train_batch = list(zip(data, labels))
np.random.shuffle(train_batch)
data, labels = zip(*train_batch)
data, labels = np.array(data), np.array(labels)
d_loss = discriminator.train_on_batch(data, labels)
# d_loss_real = discriminator.train_on_batch(y, valid[:len(y)])
# d_loss_fake = discriminator.train_on_batch(gen_imgs, fake[:len(y)])
# d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
write_log(tensorboard, d_train_logs, d_loss, d_train_step)
d_train_step += 1
time_elaped = datetime.datetime.now() - start_time
print(f'D step {d_train_step}: loss={d_loss[0]}; acc={d_loss[1]}; time={time_elaped}')
g_loss = [1]
while g_loss[0] > g_loss_thres:
for i, (x_vectors, x_images, y) in enumerate(train_loader.load_batch(batch_size)):
# ---------------------
# Train Generator
# ---------------------
# Train the generator (to have the discriminator label samples as valid)
g_loss = gan.train_on_batch(x_vectors, valid[:len(y)])
# Plot the progress
write_log(tensorboard, g_train_logs, g_loss, g_train_step)
g_train_step += 1
time_elaped = datetime.datetime.now() - start_time
print(f'G step {g_train_step}: loss={g_loss[0]}; acc={g_loss[1]}; time={time_elaped}')
# If at save interval => save generated image samples
if epoch % sample_interval == 0:
d_losses = []
g_losses = []
for x_vectors, x_images, y in val_loader.load_batch(batch_size):
gen_imgs = generator.predict(x_vectors)
d_loss_real = discriminator.test_on_batch(y, valid[:len(y)])
d_loss_fake = discriminator.test_on_batch(gen_imgs, fake[:len(y)])
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
d_losses.append(d_loss)
g_loss = gan.test_on_batch(x_vectors, valid[:len(y)])
g_losses.append(g_loss)
d_loss = np.average(d_losses, axis=0)
g_loss = np.average(g_losses, axis=0)
write_log(tensorboard, val_logs, [d_loss[0], d_loss[1], g_loss[0], g_loss[1]], val_step)
val_step += 1
sample_images(val_loader, generator, epoch)
save_model(generator, epoch, 'generator')
save_model(discriminator, epoch, 'discriminator')
最后几个步骤的结果:
D step 349: loss=0.09932675957679749; acc=1.0; time=0:05:58.468997
D step 350: loss=0.10563915222883224; acc=0.9900000095367432; time=0:05:59.088657
D step 351: loss=0.09658461064100266; acc=1.0; time=0:05:59.533442
G step 214: loss=0.167491614818573; acc=0.9800000190734863; time=0:06:00.196747
G step 215: loss=0.13409791886806488; acc=1.0; time=0:06:00.891946
G step 216: loss=0.1523411124944687; acc=0.9722222089767456; time=0:06:01.402974
D step 352: loss=0.10553492605686188; acc=0.9900000095367432; time=0:06:02.015083
D step 353: loss=0.10318870842456818; acc=0.9900000095367432; time=0:06:02.654599
D step 354: loss=0.07871382683515549; acc=1.0; time=0:06:03.131933
G step 217: loss=0.1493617743253708; acc=0.9800000190734863; time=0:06:03.827815
G step 218: loss=0.12147567421197891; acc=0.9599999785423279; time=0:06:04.537494
G step 219: loss=0.17327196896076202; acc=1.0; time=0:06:05.099841
D step 355: loss=0.10441411286592484; acc=0.9900000095367432; time=0:06:05.768096
D step 356: loss=0.09612423181533813; acc=1.0; time=0:06:06.451947
D step 357: loss=0.1072489321231842; acc=0.9861111044883728; time=0:06:06.937882
推论:
>>> np.reshape(discriminator.predict(ground_truth), (5, 10))
array([[0.5296475 , 0.52787906, 0.5270807 , 0.5260455 , 0.528732 ,
0.52820367, 0.53157693, 0.52730876, 0.5244186 , 0.52673554],
[0.5229454 , 0.5239704 , 0.53051734, 0.52862865, 0.52718925,
0.52680767, 0.52621156, 0.5308223 , 0.52489233, 0.5297055 ],
[0.53033316, 0.5260847 , 0.5300899 , 0.52788675, 0.529595 ,
0.52183014, 0.5321261 , 0.5251559 , 0.52876014, 0.52384466],
[0.528658 , 0.52737784, 0.53003156, 0.52685475, 0.53047454,
0.52759105, 0.52710444, 0.52546424, 0.52709824, 0.52520245],
[0.5283209 , 0.52810913, 0.52451426, 0.5196351 , 0.5299184 ,
0.5274567 , 0.52686375, 0.5269972 , 0.5248108 , 0.5263274 ]],
dtype=float32)
>>> np.reshape(gan.predict(input_vector), (5, 10))
array([[0.4719111 , 0.47217596, 0.47209665, 0.47233126, 0.4741753 ,
0.4712048 , 0.4721919 , 0.47193947, 0.47010162, 0.47092766],
[0.47291884, 0.47334394, 0.4714141 , 0.46976995, 0.47092718,
0.47233835, 0.47164065, 0.47276756, 0.47107005, 0.47187868],
[0.47153524, 0.47157907, 0.4706026 , 0.47128928, 0.47320494,
0.47089615, 0.47108623, 0.47432283, 0.47186196, 0.47404772],
[0.47164053, 0.47348404, 0.4701542 , 0.4741918 , 0.4702833 ,
0.47303212, 0.4726331 , 0.47118646, 0.47191456, 0.47318774],
[0.47043982, 0.47027725, 0.47308347, 0.47376725, 0.4733549 ,
0.47157207, 0.47205287, 0.47177386, 0.47119975, 0.4707804 ]],
dtype=float32)
最佳答案
注意gan = Model(in_vector, gan_output)
,因此您的模型被定义为从输入向量到判别器输出的所有层,包括中间的生成器。所以当你打电话
gan.compile(optimizer=gan_optimizer, loss='binary_crossentropy',metrics=['accuracy'])
,它自动使用鉴别器的输出来确定准确性。因此,为了获得生成器的准确性,您可以使用回调并手动计算“准确性”,但是这可能是为您的生成器定义的(加上生成器没有典型的准确性指标,当您考虑时,您打算做什么比较一下?)。 此外,如果您的生成器产生随机噪声,这并不意味着准确度应该为 0,并且由于您仅使用判别器的准确度并且它成功识别输出不属于基础分布,因此准确度仍为 100百分比(这很容易,因为生成器的输出是随机噪声)。简而言之,判别器的高精度并不意味着生成器成功地欺骗了判别器。事实上,当判别器的准确率接近 50% 时,这意味着生成器确实很好地对输入数据进行了建模,而判别器无法区分两者并进行随机猜测。因此,您所看到的是预期行为
关于tensorflow - Keras 报告错误的准确性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50580811/
我有兴趣在 tf.keras 中训练一个模型,然后用 keras 加载它。我知道这不是高度建议,但我对使用 tf.keras 来训练模型很感兴趣,因为 tf.keras 更容易构建输入管道 我想利用
我进行了大量搜索,但仍然无法弄清楚如何编写具有多个交互输出的自定义损失函数。 我有一个神经网络定义为: def NeuralNetwork(): inLayer = Input((2,));
我正在阅读一篇名为 Differential Learning Rates 的文章在 Medium 上,想知道这是否可以应用于 Keras。我能够找到在 pytorch 中实现的这项技术。这可以在 K
我正在实现一个神经网络分类器,以打印我正在使用的这个神经网络的损失和准确性: score = model.evaluate(x_test, y_test, verbose=False) model.m
我最近在查看模型摘要时遇到了这个问题。 我想知道,[(None, 16)] 和有什么区别?和 (None, 16) ?为什么输入层有这样的输入形状? 来源:model.summary() can't
我正在尝试使用 Keras 创建自定义损失函数。我想根据输入计算损失函数并预测神经网络的输出。 我尝试在 Keras 中使用 customloss 函数。我认为 y_true 是我们为训练提供的输出,
我有一组样本,每个样本都是一组属性的序列(例如,一个样本可以包含 10 个序列,每个序列具有 5 个属性)。属性的数量总是固定的,但序列的数量(时间戳)可能因样本而异。我想使用这个样本集在 Keras
Keras 在训练集和测试集文件夹中发现了错误数量的类。我有 3 节课,但它一直说有 4 节课。有人可以帮我吗? 这里的代码: cnn = Sequential() cnn.add(Conv2D(32
我想编写一个自定义层,在其中我可以在两次运行之间将变量保存在内存中。例如, class MyLayer(Layer): def __init__(self, out_dim = 51, **kwarg
我添加了一个回调来降低学习速度: keras.callbacks.ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=100,
在 https://keras.io/layers/recurrent/我看到 LSTM 层有一个 kernel和一个 recurrent_kernel .它们的含义是什么?根据我的理解,我们需要 L
问题与标题相同。 我不想打开 Python,而是使用 MacOS 或 Ubuntu。 最佳答案 Python 库作者将版本号放入 .__version__ 。您可以通过在命令行上运行以下命令来打印它:
Keras 文档并不清楚这实际上是什么。我知道我们可以用它来将输入特征空间压缩成更小的空间。但从神经设计的角度来看,这是如何完成的呢?它是一个自动编码器,RBM吗? 最佳答案 据我所知,嵌入层是一个简
我想实现[http://ydwen.github.io/papers/WenECCV16.pdf]中解释的中心损失]在喀拉斯 我开始创建一个具有 2 个输出的网络,例如: inputs = Input
我正在尝试实现多对一模型,其中输入是大小为 的词向量d .我需要输出一个大小为 的向量d 在 LSTM 结束时。 在此 question ,提到使用(对于多对一模型) model = Sequenti
我有不平衡的训练数据集,这就是我构建自定义加权分类交叉熵损失函数的原因。但问题是我的验证集是平衡的,我想使用常规的分类交叉熵损失。那么我可以在 Keras 中为验证集传递不同的损失函数吗?我的意思是用
DL 中的一项常见任务是将输入样本归一化为零均值和单位方差。可以使用如下代码“手动”执行规范化: mean = np.mean(X, axis = 0) std = np.std(X, axis =
我正在尝试学习 Keras 并使用 LSTM 解决分类问题。我希望能够绘制 准确率和损失,并在训练期间更新图。为此,我正在使用 callback function . 由于某种原因,我在回调中收到的准
在 Keras 内置函数中嵌入使用哪种算法?Word2vec?手套?其他? https://keras.io/layers/embeddings/ 最佳答案 简短的回答是都不是。本质上,GloVe 的
我有一个使用 Keras 完全实现的 LSTM RNN,我想使用梯度剪裁,梯度范数限制为 5(我正在尝试复制一篇研究论文)。在实现神经网络方面,我是一个初学者,我将如何实现? 是否只是(我正在使用 r
我是一名优秀的程序员,十分优秀!