Denoising Autoencoder

做者:chen_h
微信号 & QQ:862251340
微信公众号:coderpai
简书地址:https://www.jianshu.com/p/f7b...python



降噪自编码器(DAE)是另外一种自编码器的变种。强烈推荐 Pascal Vincent 的论文,该论文很详细的描述了该模型。降噪自编码器认为,设计一个可以恢复原始信号的自编码器未必是最好的,而可以对 “被污染/破坏” 的原始数据进行编码、解码,而后还能恢复真正的原始数据,这样的特征才是好的。算法

从数学上来说,假设原始数据 x 被咱们“故意破坏”了,好比加入高斯噪声,或者把某些维度数据抹掉,变成 x',而后在对 x' 进行编码、解码,获得回复信号 xx = g(f(x')) 。该恢复信号尽量的逼近未被污染的原数据 x 。此时,监督训练的偏差函数就从原来的 L(x, g(f(x))) 变成了 L(x, g(f(x')))微信

从直观上理解,降噪自编码器但愿学到的特征尽量鲁棒,可以在必定程度上对抗原始数据的污染、缺失等状况。Vincent 论文里也对 DAE 提出了基于流行学习的解释,而且在图像数据上进行测试,发现 DAE 可以学出相似 Gabor 边缘提取的特征变换。app

DAE 的系统结构以下图所示:dom

如今使用比较多的噪声主要是 mask noise,即原始数据中部分数据缺失,这是有着强烈的实际意义的,好比图像部分像素被遮挡、文本因记录缘由漏掉一些单词等等。函数

实现代码以下:学习

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tensorflow as tf 
import numpy as np 
import input_data

N_INPUT = 784
N_HIDDEN = 100
N_OUTPUT = N_INPUT
corruption_level = 0.3
epoches = 1000

def main(_):

    w_init = np.sqrt(6. / (N_INPUT + N_HIDDEN))
    weights = {
        "hidden": tf.Variable(tf.random_uniform([N_INPUT, N_HIDDEN], minval = -w_init, maxval = w_init)),
        "out": tf.Variable(tf.random_uniform([N_HIDDEN, N_OUTPUT], minval = -w_init, maxval = w_init))
    }

    bias = {
        "hidden": tf.Variable(tf.random_uniform([N_HIDDEN], minval = -w_init, maxval = w_init)),
        "out": tf.Variable(tf.random_uniform([N_OUTPUT], minval = -w_init, maxval = w_init))
    }

    with tf.name_scope("input"):
        # input data
        x = tf.placeholder("float", [None, N_INPUT])
        mask = tf.placeholder("float", [None, N_INPUT])

    with tf.name_scope("input_layer"):
        # from input data to input layer
        input_layer = tf.mul(x, mask)

    with tf.name_scope("hidden_layer"):
        # from input layer to hidden layer
        hidden_layer = tf.sigmoid(tf.add(tf.matmul(input_layer, weights["hidden"]), bias["hidden"]))

    with tf.name_scope("output_layer"):
        # from hidden layer to output layer
        output_layer = tf.sigmoid(tf.add(tf.matmul(hidden_layer, weights["out"]), bias["out"]))

    with tf.name_scope("cost"):
        # cost function
        cost = tf.reduce_sum(tf.pow(tf.sub(output_layer, x), 2))

    optimizer = tf.train.AdamOptimizer().minimize(cost)

    # load MNIST data
    mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
    trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels

    with tf.Session() as sess:

        init = tf.initialize_all_variables()
        sess.run(init)

        for i in range(epoches):
            for start, end in zip(range(0, len(trX), 100), range(100, len(trX), 100)):
                input_ = trX[start:end]
                mask_np = np.random.binomial(1, 1 - corruption_level, input_.shape)
                sess.run(optimizer, feed_dict={x: input_, mask: mask_np})

            mask_np = np.random.binomial(1, 1 - corruption_level, teX.shape)
            print i, sess.run(cost, feed_dict={x: teX, mask: mask_np})

if __name__ == "__main__":
    tf.app.run()

Reference:测试

[《Extracting and Composing Robust Features with Denoising
Autoencoders》](http://machinelearning.org/ar...编码

《Stacked Denoising Autoencoders: Learning Useful Representations in a Deep Network with a Local Denoising Criterion》人工智能


做者:chen_h
微信号 & QQ:862251340
简书地址:https://www.jianshu.com/p/f7b...

CoderPai 是一个专一于算法实战的平台,从基础的算法到人工智能算法都有设计。若是你对算法实战感兴趣,请快快关注咱们吧。加入AI实战微信群,AI实战QQ群,ACM算法微信群,ACM算法QQ群。长按或者扫描以下二维码,关注 “CoderPai” 微信号(coderpai)
这里写图片描述

这里写图片描述