# 简单易懂的自动编码器

（左右滑动，查看完整代码）

n_inputs = 28*28n_hidden1 = 300n_hidden2 = 150# 定义输入占位符：不需要yX = tf.placeholder(tf.float32, [None, n_inputs])# 定义训练参数initializer = tf.contrib.layers.variance_scaling_initializer()W1 = tf.Variable(initializer([n_inputs, n_hidden1]), name="W1")b1 = tf.Variable(tf.zeros([n_hidden1,]), name="b1")W2 = tf.Variable(initializer([n_hidden1, n_hidden2]), name="W2")b2 = tf.Variable(tf.zeros([n_hidden2,]), name="b2")W3 = tf.transpose(W2, name="W3")b3 = tf.Variable(tf.zeros([n_hidden1,]), name="b3")W4 = tf.transpose(W1, name="W4")b4 = tf.Variable(tf.zeros([n_inputs,]), name="b4")# 构建模型h1 = tf.nn.sigmoid(tf.nn.xw_plus_b(X, W1, b1))h2 = tf.nn.sigmoid(tf.nn.xw_plus_b(h1, W2, b2))h3 = tf.nn.sigmoid(tf.nn.xw_plus_b(h2, W3, b3))outputs = tf.nn.sigmoid(tf.nn.xw_plus_b(h3, W4, b4))# 定义lossloss = -tf.reduce_mean(tf.reduce_sum(X * tf.log(outputs) +                    (1 - X) * tf.log(1 - outputs), axis=1))train_op = tf.train.AdamOptimizer(1e-02).minimize(loss)

（左右滑动，查看完整代码）

# 构建模型h1 = tf.nn.sigmoid(tf.nn.xw_plus_b(X, W1, b1))h1_recon = tf.nn.sigmoid(tf.nn.xw_plus_b(h1, W4, b4))h2 = tf.nn.sigmoid(tf.nn.xw_plus_b(h1, W2, b2))h2_recon = tf.nn.sigmoid(tf.nn.xw_plus_b(h2, W3, b3))outputs = tf.nn.sigmoid(tf.nn.xw_plus_b(h2_recon, W4, b4))learning_rate = 1e-02# X->h1with tf.name_scope("layer1"):    layer1_loss = -tf.reduce_mean(tf.reduce_sum(X * tf.log(h1_recon) +                            (1-X) * tf.log(1-h1_recon), axis=1))    layer1_train_op = tf.train.AdamOptimizer(learning_rate).minimize(layer1_loss,                                            var_list=[W1, b1, b4])# h1->h2with tf.name_scope("layer2"):    layer2_loss = -tf.reduce_mean(tf.reduce_sum(h1 * tf.log(h2_recon) +                  (1 - h1) * tf.log(1 - h2_recon), axis=1))    layer2_train_op = tf.train.AdamOptimizer(learning_rate).minimize(layer2_loss,                      var_list=[W2, b2, b3])

1. Hands-On Machine Learning withScikit-Learn and TensorFlow, Aurélien Géron, 2017.

2. Deep Learning Tutorials：AutoEncoders, Denoising Autoencoders.

http://deeplearning.net/tutorial/dA.html#daa

3. Learning deep architectures for AI, Foundations and Trends inMachine Learning, Y. Bengio, 2009.

3.RNN入门与实践

80%的AI从业者已关注我们微信公众号