最初由 IJ Goodfellow 提出的 GAN 使用以下损失函数,
D_loss = - log[D(X)] - log[1 - D(G(Z))]
G_loss = - log[D(G(Z))]
Run Code Online (Sandbox Code Playgroud)
因此,鉴别器尝试最小化 D_loss,生成器尝试最小化 G_loss,其中 X 和 Z 分别是训练输入和噪声输入。D(.) 和 G(.) 分别是鉴别器和生成器神经网络的映射。
正如原始论文所说,当 GAN 被训练几个步骤时,它会达到一个生成器和判别器都无法改进的点,并且 D(Y) 到处都是 0.5,Y 是判别器的一些输入。在这种情况下,当 GAN 被充分训练到这一点时,
D_loss = - log(0.5) - log(1 - 0.5) = 0.693 + 0.693 = 1.386
G_loss = - log(0.5) = 0.693
Run Code Online (Sandbox Code Playgroud)
那么,为什么我们不能使用 D_loss 和 G_loss 值作为评估 GAN 的指标呢?
如果两个损失函数偏离了这些理想值,那么 GAN 肯定需要训练好或架构需要设计好。正如原始论文中的定理 1 所讨论的,这些是 D_loss 和 G_loss 的最佳值,但为什么不能将它们用作评估指标?
loss neural-network objective-function generative-adversarial-network
我正在训练一个 GAN 从两个不同的图像域(源S和目标T)执行样式转换。因为我有可用的类信息,所以我有一个额外的Q网络(除了G和D),它测量目标域及其标签(LeNet 网络)的生成图像的分类结果,并将错误传播到生成器以及D. 从系统的收敛性我注意到它D总是从 8(网络的损失函数误差D)开始并略微下降到 4.5,并且G损失函数误差从 1 开始并迅速下降到 0.2。的损失函数D和G我使用的,可以发现在这里,而丧失功能Q网络是分类交叉熵。迭代中的误差图是:
D和G的损失函数为:
def discriminator_loss(y_true,y_pred):
BATCH_SIZE=10
return K.mean(K.binary_crossentropy(K.flatten(y_pred), K.concatenate([K.ones_like(K.flatten(y_pred[:BATCH_SIZE,:,:,:])),K.zeros_like(K.flatten(y_pred[:BATCH_SIZE,:,:,:])) ]) ), axis=-1)
def discriminator_on_generator_loss(y_true,y_pred):
BATCH_SIZE=10
return K.mean(K.binary_crossentropy(K.flatten(y_pred), K.ones_like(K.flatten(y_pred))), axis=-1)
def generator_l1_loss(y_true,y_pred):
BATCH_SIZE=10
return K.mean(K.abs(K.flatten(y_pred) - K.flatten(y_true)), axis=-1)
Run Code Online (Sandbox Code Playgroud)
D 的误差函数总是那么高有意义吗?什么是错误的解释D和G?是不是开始时的损失D应该很小,迭代后会上升?用损失阈值限制D过度是个好主意G吗?最后,在训练期间计算来自验证集的损失函数而不是我正在使用的训练集的误差是否有意义?(而不是直接使用 train_on_batch 使用 fit 然后在测试集上进行评估)。
编辑:
对于损失,我认为损失discriminator …
最近我一直在玩 StyleGAN 并生成了一个数据集,但是当我尝试运行 train.py 时得到以下信息。
2020-01-08 13:33:21.943217: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
WARNING:tensorflow:From C:\Users\MyName\Desktop\StyleGan\stylegan-master\dnnlib\tflib\tfutil.py:34: The name tf.Dimension is deprecated. Please use tf.compat.v1.Dimension instead.
WARNING:tensorflow:From C:\Users\MyName\Desktop\StyleGan\stylegan-master\dnnlib\tflib\tfutil.py:74: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.
WARNING:tensorflow:From C:\Users\MyName\Desktop\StyleGan\stylegan-master\dnnlib\tflib\tfutil.py:128: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.
Creating the run dir: results\00003-sgan-datasets-1gpu
Copying files to the run dir
dnnlib: Running training.training_loop.training_loop() on localhost...
WARNING:tensorflow:From C:\Users\MyName\Desktop\StyleGan\stylegan-master\dnnlib\tflib\tfutil.py:97: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.
WARNING:tensorflow:From C:\Users\MyName\Desktop\StyleGan\stylegan-master\dnnlib\tflib\tfutil.py:109: The name tf.set_random_seed is …Run Code Online (Sandbox Code Playgroud) python deep-learning tensorflow generative-adversarial-network
我使用 LSTM 模型创建了一个词级文本生成器。但就我而言,并非每个词都适合选择。我希望它们匹配其他条件:
10100010)。然后,生成的句子需要满足给定的结构,例如,01001100(hi 01和朋友 001100)。因此,为了处理这种情况,我创建了一个具有以下结构的 Pandas 数据框:
word last_vowel word_map
----- --------- ----------
hello o 01001
stack a 00100
jhon o 0010
Run Code Online (Sandbox Code Playgroud)
这是我目前的工作流程:
0100100100100,我们可以选择单词hello,因为它的元音结构是01001。0100100100100will be become00100100因为我们已经删除了首字母01001( hello )。00100和jhon 0010。machine-learning reinforcement-learning fst lstm generative-adversarial-network
我正在查看 CycleGAN 的官方 pytorch 实现,作者在那里链接了两个网络的参数,并为两个网络使用了一个优化器。这是如何运作的?是否比对两个不同的网络使用两个不同的优化器更好?
all_params = chain(module_a.parameters(), module_b.parameters())
optimizer = torch.optim.Adam(all_params)
Run Code Online (Sandbox Code Playgroud) 以下是使用 Keras 的 Keras CycleGAN 示例中的 CycleGAN 示例 。
这是我修改后的使用多个 GPU 的实现。为了实现自定义训练,我使用了带有 tf.distribute.Strategy 的参考自定义训练
我想要一个来自 Keras 的 CycleGAN 示例,以便使用 GPU 快速运行。此外,我需要处理和训练大量数据。CycleGAN 使用多个损失函数train_step会返回 4 种损失,目前我只返回一种以便于理解。尽管如此,GPU 上的训练还是非常慢。我无法找到这背后的原因。
难道是我用tf.distribute.Strategy错了?
"""
Title: CycleGAN
Author: [A_K_Nain](https://twitter.com/A_K_Nain)
Date created: 2020/08/12
Last modified: 2020/08/12
Description: Implementation of CycleGAN.
"""
"""
## CycleGAN
CycleGAN is a model that aims to solve the image-to-image translation
problem. The goal of the image-to-image translation problem is to learn the
mapping between an input image and an …Run Code Online (Sandbox Code Playgroud) parallel-processing multi-gpu keras tensorflow generative-adversarial-network
我想使用我自己的图像对在线提供的预训练 GAN 进行微调。例如,在 ImageNet 上训练的 BigGAN 可以生成逼真的图像。但是,我不想在 ImageNet 中生成图像类。我想生成我自己的图像集的人工图像。如何微调预训练模型?它与微调其他神经网络(如 CNN 图像分类模型)相同吗?仅替换/重新训练最后几层就足够了吗?如果我能在 Tensorflow/Keras 代码中看到一些示例,那就太好了。非常感谢!
deep-learning tensorflow tensorflow-hub transfer-learning generative-adversarial-network
我正在尝试生成 mnist 数据集图像。这是我的代码:
fns.py:
import math
import numpy as np
def combine_images(generated_images):
total,width,height = generated_images.shape[:-1]
cols = int(math.sqrt(total))
rows = math.ceil(float(total)/cols)
combined_image = np.zeros((height*rows, width*cols),
dtype=generated_images.dtype)
for index, image in enumerate(generated_images):
i = int(index/cols)
j = index % cols
combined_image[width*i:width*(i+1), height*j:height*(j+1)] = image[:, :, 0]
return combined_image
def show_progress(epoch, batch, g_loss, d_loss, g_acc, d_acc):
msg = "epoch: {}, batch: {}, g_loss: {}, d_loss: {}, g_accuracy: {}, d_accuracy: {}"
print(msg.format(epoch, batch, g_loss, d_loss, g_acc, d_acc))
Run Code Online (Sandbox Code Playgroud)
主要.py:
from tensorflow.python.keras.models import Sequential
from …Run Code Online (Sandbox Code Playgroud) 我试图展示 GAN 网络在某些指定时期的结果。打印当前结果的功能之前是在 TF 中使用的。我需要换成pytorch。
def show_result(G_net, z_, num_epoch, show=False, save=False, path='result.png'):
#test_images = sess.run(G_z, {z: z_, drop_out: 0.0})
test_images = G_net(z_)
size_figure_grid = 5
fig, ax = plt.subplots(size_figure_grid, size_figure_grid, figsize=(5, 5))
for i, j in itertools.product(range(size_figure_grid), range(size_figure_grid)):
ax[i, j].get_xaxis().set_visible(False)
ax[i, j].get_yaxis().set_visible(False)
for k in range(5*5):
i = k // 5
j = k % 5
ax[i, j].cla()
ax[i, j].imshow(np.reshape(test_images[k], (28, 28)), cmap='gray')
label = 'Epoch {0}'.format(num_epoch)
fig.text(0.5, 0.04, label, ha='center')
plt.savefig(name)
file = drive.CreateFile({'title': label, "parents": [{"kind": "https://drive.google.com/drive/u/0/folders/", "id": …Run Code Online (Sandbox Code Playgroud) generative-adversarial-network ×10
python ×5
tensorflow ×5
keras ×3
pytorch ×2
fst ×1
loss ×1
lstm ×1
multi-gpu ×1
plot ×1