1
1
# -*- coding: utf-8 -*-
2
2
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.
12
5
变分自动编码器
13
6
"""
14
7
from __future__ import division , print_function , absolute_import
26
19
class Layer :
27
20
def __init__ (self , input , n_output ):
28
21
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]
30
23
b = tf .Variable (tf .constant (0. , shape = [ n_output ]))
31
24
32
25
self .raw_output = tf .matmul (input , W ) + b
@@ -35,7 +28,7 @@ def __init__(self, input, n_output):
35
28
36
29
# 样本集X
37
30
n_X = 784 # 28 * 28
38
- n_z = 100
31
+ n_z = 20 # latent variable count
39
32
X = tf .placeholder (tf .float32 , shape = [ None , n_X ])
40
33
41
34
# Encoder
@@ -45,17 +38,18 @@ def __init__(self, input, n_output):
45
38
mu = Layer (Layer (X , ENCODER_HIDDEN_COUNT ).output , n_z ).raw_output
46
39
47
40
## \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 )
49
43
50
44
## 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
52
46
53
47
54
48
# epsilon = N(0, I) 采样模块
55
49
epsilon = tf .random_normal (tf .shape (sigma ), name = 'epsilon' )
56
50
57
51
# 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
59
53
60
54
# Decoder ||f(z) - X|| ^ 2 重建的X与X的欧式距离,更加成熟的做法是使用crossentropy
61
55
def buildDecoderNetwork (z ):
0 commit comments