我正在尝试使用与众所周知的MNIST不同的DB来实现VAE。我阅读了很多有关VAE的文章,并实现了一个非常简单的自动编码器,但我不知道为什么它无法收敛。这是主要代码:
tf.reset_default_graph()
with tf.device("/gpu:0"):
x = tf.placeholder(dtype=tf.float32, shape=[None,num_var], name="x")
keep_prob = tf.placeholder(dtype=tf.float32, shape=(), name='keep_prob')
def encoder(x, keep_prob):
layer = tf.layers.dense(x, units=NHIDDEN[0], activation=tf.nn.sigmoid)
layer = tf.nn.dropout(layer, keep_prob)
layer = tf.layers.dense(layer, units=NHIDDEN[1], activation=tf.nn.sigmoid)
layer = tf.nn.dropout(layer, keep_prob)
layer = tf.layers.dense(layer, units=NHIDDEN[2], activation=tf.nn.sigmoid)
layer = tf.nn.dropout(layer, keep_prob)
mu = tf.layers.dense(layer, units=NHIDDEN[3], activation=None)
sigma = tf.layers.dense(layer, units=NHIDDEN[3], activation=None)
epsi = tf.random_normal(tf.shape(sigma), dtype = tf.float32)
sigma_exp = tf.exp(sigma*0.5)
hidden_layer = mu + tf.exp(sigma*0.5)*epsi
return hidden_layer, sigma, mu, sigma_exp
def decoder(hid, keep_prob):
layer = tf.layers.dense(hid, units=NHIDDEN[2], activation=tf.nn.sigmoid)
layer = tf.nn.dropout(layer, keep_prob)
` layer = tf.layers.dense(layer, units=NHIDDEN[1], activation=tf.nn.sigmoid)
layer = tf.nn.dropout(layer, keep_prob)
layer = tf.layers.dense(layer, units=NHIDDEN[0], activation=tf.nn.sigmoid)
layer = tf.nn.dropout(layer, keep_prob)
output_non_act = tf.layers.dense(layer, units=num_var, activation=None)
reconstructed_layer = tf.nn.sigmoid(output_non_act)
return reconstructed_layer, output_non_act
codif, sigma, mu, sigma_e = encoder(x, keep_prob)
decod, output_non_act = decoder(codif, keep_prob)
loss = tf.reduce_sum(tf.squared_difference(decod, x),1)
loss_var = 0.5*tf.reduce_sum(tf.exp(sigma)+tf.square(mu)-1-sigma)
loss_tot = loss + loss_var
optimizer = tf.train.AdamOptimizer(learning_coeff).minimize(loss_tot)
KL散度几乎立即变为0,而AE_error仍然很高。任何想法?我尝试使用不同的激活功能(elu,relu,none ...)和不同的数据库。我用图像写了类似的东西,它起作用了。听起来很奇怪!