我正在尝试使用 python 和 tensorflow 实现变分自动编码器。我在互联网上看到了各种实现。我成功地使用我找到的各个部件创建了自己的部件,并使它们适合我的具体案例。 我在这里总结了一个自动编码器: my autoncoder on git
简单地说,我有一个自动编码器,其中包含:
1) 具有 2 个卷积层和 1 个扁平层的编码器,
2)潜在空间(维度2),
3) 以及具有编码器相反部分的解码器。
我的问题是当我尝试实现自动编码器的变分部分时。我指的是潜在空间中的数学过程。至少这就是我指出问题的地方。
更清楚地说,我有以下两种情况:
情况1: 在没有实际实现任何变分数学的情况下,只需简单地在潜在空间中设置变量并将它们输入解码器中,而不应用数学。在这种情况下,成本函数只是输入和输出之间的差。您可以在 git 上的这些图中看到该案例的代码(抱歉无法发布更多链接): 图1_代码_部分1.png, 图1_code_part2.png
案例2: 尝试在潜在空间变量中实现数学。您可以在这些图中看到该案例的代码: 图_2_code_part1.png, 图_2_code_part2.png
我在每种情况下得到的潜在空间图是: 图_1.png 图_2.png
我认为变分实现显然有问题,但我不知道是什么。每个实现变分自动编码器的人都使用这些数学公式(至少是我在互联网上找到的那些)。可能我错过了一些东西。
欢迎任何意见/建议。 谢谢!!!
最佳答案
以下是mu
的方法和sigma
与 KL_term
待计算:
我不确定linear
你的代码的一部分。因此,我提出以下建议:
请注意,在这里,在编码器端的全连接层之前,我有一个 conv4
形状层:[7, 7, 256]
.
# These are the weights and biases of the mu and sigma on the encoder side
w_c_mu = tf.Variable(tf.truncated_normal([7 * 7 * 256, latent_dim], stddev=0.1), name='weight_fc_mu')
b_c_mu = tf.Variable(tf.constant(0.1, shape=[latent_dim]), name='biases_fc_mu')
w_c_sig = tf.Variable(tf.truncated_normal([7 * 7 * 256, latent_dim], stddev=0.1), name='weight_fc_sig')
b_c_sig = tf.Variable(tf.constant(0.1, shape=[latent_dim]), name='biases_fc_sig')
epsilon = tf.random_normal([1, latent_dim])
with tf.variable_scope('mu'):
mu = tf.nn.bias_add(tf.matmul(conv4_reshaped, w_c_mu), b_c_mu)
tf.summary.histogram('mu', mu)
with tf.variable_scope('stddev'):
stddev = tf.nn.bias_add(tf.matmul(conv4_reshaped, w_c_sig), b_c_sig)
tf.summary.histogram('stddev', stddev)
with tf.variable_scope('z'):
# This formula was adopted from the following paper: http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=7979344
latent_var = mu + tf.multiply(tf.sqrt(tf.exp(stddev)), epsilon)
tf.summary.histogram('features_sig', stddev)
...
with tf.name_scope('loss_KL'):
temp2 = 1 + tf.log(tf.square(stddev + 1e-9)) - tf.square(mu) - tf.square(stddev)
KL_term = - 0.5 * tf.reduce_sum(temp2, reduction_indices=1)
tf.summary.scalar('KL_term', tf.reduce_mean(KL_term))
with tf.name_scope('total_loss'):
variational_lower_bound = tf.reduce_mean(log_likelihood + KL_term)
tf.summary.scalar('loss', variational_lower_bound)
with tf.name_scope('optimizer'):
update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
with tf.control_dependencies(update_ops):
optimizer = tf.train.AdamOptimizer(0.00001).minimize(variational_lower_bound)
完整代码: https://gist.github.com/issa-s-ayoub/5267558c4f5694d479a84d960c265452
希望有帮助!!
关于python - 变分自动编码器实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44870359/