Skip to content

Commit 59b51ce

Browse files
committed
done
1 parent 6c093f2 commit 59b51ce

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

examples/3_NeuralNetworks/variational_autoencoder.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3-
""" Varational Auto Encoder Example.
4-
Using an auto encoder on MNIST handwritten digits.
5-
References:
6-
Y. LeCun, L. Bottou, Y. Bengio, and P. Haffner. "Gradient-based
7-
learning applied to document recognition." Proceedings of the IEEE,
8-
86(11):2278-2324, November 1998.
9-
Links:
10-
[MNIST Dataset] http://yann.lecun.com/exdb/mnist/
11-
3+
"""
4+
Varational Auto Encoder Example.
125
变分自动编码器
136
"""
147
from __future__ import division, print_function, absolute_import
@@ -26,7 +19,7 @@
2619
class Layer:
2720
def __init__(self, input, n_output):
2821
self.input = input
29-
W = tf.Variable(tf.random_normal([ int(self.input.get_shape()[1]), n_output ], stddev = 0.001))#tf.shape(input)[0]
22+
W = tf.Variable(tf.truncated_normal([ int(self.input.get_shape()[1]), n_output ], stddev = 0.001))#tf.shape(input)[0]
3023
b = tf.Variable(tf.constant(0., shape = [ n_output ]))
3124

3225
self.raw_output = tf.matmul(input, W) + b
@@ -35,7 +28,7 @@ def __init__(self, input, n_output):
3528

3629
# 样本集X
3730
n_X = 784 # 28 * 28
38-
n_z = 100
31+
n_z = 20 # latent variable count
3932
X = tf.placeholder(tf.float32, shape = [ None, n_X ])
4033

4134
# Encoder
@@ -45,17 +38,18 @@ def __init__(self, input, n_output):
4538
mu = Layer(Layer(X, ENCODER_HIDDEN_COUNT).output, n_z).raw_output
4639

4740
## \Sigma(X) 采用二层网络
48-
sigma = Layer(Layer(X, ENCODER_HIDDEN_COUNT).output, n_z).raw_output
41+
log_sigma = Layer(Layer(X, ENCODER_HIDDEN_COUNT).output, n_z).raw_output # 为了训练不出nan? 至少实验的时候,直接让这个网络代表sigma是算不出来的,请高人指点!!!
42+
sigma = tf.exp(log_sigma)
4943

5044
## KLD = D[N(mu(X), sigma(X))||N(0, I)] = 1/2 * sum(sigma_i + mu_i^2 - log(sigma_i) - 1)
51-
KLD = 0.5 * tf.reduce_sum(sigma + tf.pow(mu, 2) - tf.log(sigma) - 1, reduction_indices = 1) # reduction_indices = 1代表按照每个样本计算一条KLD
45+
KLD = 0.5 * tf.reduce_sum(sigma + tf.pow(mu, 2) - log_sigma - 1, reduction_indices = 1) # reduction_indices = 1代表按照每个样本计算一条KLD
5246

5347

5448
# epsilon = N(0, I) 采样模块
5549
epsilon = tf.random_normal(tf.shape(sigma), name = 'epsilon')
5650

5751
# z = mu + sigma^ 0.5 * epsilon
58-
z = mu + tf.pow(sigma, 0.5) * epsilon
52+
z = mu + tf.exp(0.5 * log_sigma) * epsilon
5953

6054
# Decoder ||f(z) - X|| ^ 2 重建的X与X的欧式距离,更加成熟的做法是使用crossentropy
6155
def buildDecoderNetwork(z):

0 commit comments

Comments
 (0)