前面介绍了VAE-GAN 论文:Autoencoding beyond pixels usingALearnedSimilarityMmetric及视频
这篇文章通过代码介绍了VAE-GAN,特色如下:
1 多GPU
2 学习rate动态改变!
3 隐变量空间可视化
4 特征向量代数计算
5 神经元激活可视化
6 训练学习快
效果:
微信代码格式不好看,可以阅读原文访问原文章:https://github.com/timsainb/Tensorflow-MultiGPU-VAE-GAN
l_x_tilde
and l_x
here become layers of high level features that the discriminator learns.
x
and x_tilde
ref
Train Encoder on minimization of:
kullback_leibler_loss(z_x, gaussian)
mean_squared_error(l_x_tilde_, l_x)
Train Generator on minimization of:
kullback_leibler_loss(z_x, gaussian)
mean_squared_error(l_x_tilde_, l_x)
-1*log(d_x_p)
Train Discriminator on minimization of:
-1*log(d_x) + log(1 - d_x_p)
gpus
to a list of the GPUs you're using. The network will then split up the work between those gpusgpus = [2] # Here I set CUDA to only see one GPU
os.environ["CUDA_VISIBLE_DEVICES"]=','.join([str(i) for i in gpus])
num_gpus = len(gpus) # number of GPUs to use
iter_ = data_iterator()
iter_ = data_iterator()
#face_batch, label_batch
各个神经网络配置:
with tf.variable_scope("enc")
. This way, we can reuse these variables using reuse=True
. We can also specify which variables to train using which error functions based upon the label enc
def inference(x):
"""
Run the models. Called inference because it does the same thing as tensorflow's cifar tutorial
"""
z_p = tf.random_normal((batch_size, hidden_size), 0, 1) # normal dist for GAN
eps = tf.random_normal((batch_size, hidden_size), 0, 1) # normal dist for VAE
with pt.defaults_scope(activation_fn=tf.nn.elu,
batch_normalize=True,
learned_moments_update_rate=0.0003,
variance_epsilon=0.001,
scale_after_normalization=True):
with tf.variable_scope("enc"):
z_x_mean, z_x_log_sigma_sq = encoder(x) # get z from the input
with tf.variable_scope("gen"):
z_x = tf.add(z_x_mean,
tf.mul(tf.sqrt(tf.exp(z_x_log_sigma_sq)), eps)) # grab our actual z
x_tilde = generator(z_x)
with tf.variable_scope("dis"):
_, l_x_tilde = discriminator(x_tilde)
with tf.variable_scope("gen", reuse=True):
x_p = generator(z_p)
with tf.variable_scope("dis", reuse=True):
d_x, l_x = discriminator(x) # positive examples
with tf.variable_scope("dis", reuse=True):
d_x_p, _ = discriminator(x_p)
return z_x_mean, z_x_log_sigma_sq, z_x, x_tilde, l_x_tilde, x_p, d_x, l_x, d_x_p, z_p
.........................略
动态学习率;学习率后面动态生成
tower_grads_e
defines the list of gradients for the encoder for each towerwith graph.as_default():
# Start the Session
init = tf.initialize_all_variables()
saver = tf.train.Saver() # initialize network saver
sess = tf.InteractiveSession(graph=graph,config=tf.ConfigProto(allow_soft_placement=True, log_device_placement=True))
sess.run(init)
example_data, _ = iter_.next()
np.shape(example_data)
(32, 12288)
#tf.train...
epoch = 0
tf.train.Saver.restore(saver, sess, 'models/faces_multiGPU_64_0000.tfmod')
e_current_lr = e_learning_rate*sigmoid(np.mean(d_real),-.5,10)
训练代码:
生成的图片
查看变量空间对应的显示图片内容
代码略.......................
.................代码略
加黄头发
............................