深度学习序列——自编码器(AE)模型

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/hit1524468/article/details/80232512

1. 模型简介

自编码器是无监督学习的重要的学习方法,因为,该模型实现神经网络学习恒等映射函数h,即 x = h(x)


2. 模型实现

# Auto-Encoder 
#
# Author: HSW
# Date: 2018-05-06
#

import tensorflow as tf
import numpy      as np

def axvier_init(fan_in, fan_out, constant = 1):
    ''' Initial weights '''
    low   = -constant + np.sqrt(6.0 / (fan_in + fan_out))
    high =  constant + np.sqrt(6.0 / (fan_in + fan_out))

    return tf.random_uniform((fan_in, fan_out), minval = low, maxval = high, dtype = tf.float32)


class AutoEncoder(object):

    def __init__(self, AutoEncoder_Shape = (128, 256, 128), transfer_function = tf.nn.softplus, optimizer = tf.train.AdamOptimizer()):
        ''' Constructor Funcion '''
        self.LayerCnt          = len(AutoEncoder_Shape)
        self.AutoEncoder_Shape = AutoEncoder_Shape
        self.transfer          = transfer_function        
        self.weights           = self.init_weights()
        self.layers            = self.init_layers()
        self.total_loss        = self.init_loss()
        self.optimizer         = optimizer.minimize(self.total_loss)
        init                   = tf.global_variables_initializer()
        self.sess              = tf.Session()
        self.sess.run(init) 
        


    def init_weights(self):
        ''' AutoEncoder Weights ''' 
        all_weights = dict()
        for iter in range(self.LayerCnt - 1):
            weightName              = "weight" + str(iter + 1)
            biasName                = "bias"   + str(iter + 1)
            if iter == self.LayerCnt - 2: 
                all_weights[weightName] = tf.Variable(axvier_init(self.AutoEncoder_Shape[iter], self.AutoEncoder_Shape[iter + 1]))
                all_weights[biasName]   = tf.Variable(tf.zeros([self.AutoEncoder_Shape[iter + 1]]))
            else:
                all_weights[weightName] = tf.Variable(tf.zeros([self.AutoEncoder_Shape[iter], self.AutoEncoder_Shape[iter + 1]]))
                all_weights[biasName]   = tf.Variable(tf.zeros([self.AutoEncoder_Shape[iter + 1]]))

        return all_weights

    def init_layers(self):
        ''' AutoEncoder: Input Layer + Hidden Layer + Output Layer ''' 
        all_layers = dict()
        all_layers['inputs'] = tf.placeholder(tf.float32, [None, self.AutoEncoder_Shape[0]])
        
        for iter in range(self.LayerCnt - 1):
            if iter == 0:
                # inputs Layer => 1th Hidden Layer
                preLayerName          = 'inputs'
                layerName             = 'hidden' + str(iter + 1)
                weightName            = 'weight' + str(iter + 1)
                biasName              = 'bias'   + str(iter + 1) 
                all_layers[layerName] = self.transfer(tf.add(tf.matmul(all_layers[preLayerName], self.weights[weightName]), self.weights[biasName]))
            elif iter == self.LayerCnt - 2:
                # Last Hidden Layer => outputs Layer
                preLayerName          = 'hidden' + str(iter) 
                layerName             = 'outputs'
                weightName            = 'weight' + str(iter + 1)
                biasName              = 'bias'   + str(iter + 1)
                all_layers[layerName] = tf.add(tf.matmul(all_layers[preLayerName], self.weights[weightName]), self.weights[biasName])
            else:
                # pre-Hidden Layer => cur-Hidden Layer
                preLayerName          = 'hidden' + str(iter)
                layerName             = 'hidden' + str(iter + 1)
                weightName            = 'weight' + str(iter + 1)
                biasName              = 'bias'   + str(iter + 1)
                all_layers[layerName] = self.transfer(tf.add(tf.matmul(all_layers[preLayerName], self.weights[weightName]), self.weights[biasName]))

        return all_layers 
                
                
    def init_loss(self):
        ''' AutoEncoder Loss '''
        return 0.5 * tf.reduce_sum(tf.pow(tf.subtract(self.layers['outputs'], self.layers['inputs']), 2))

    def partial_fit(self, inputs):
        ''' For train '''
        cost, opt = self.sess.run((self.total_loss, self.optimizer), feed_dict={self.layers['inputs']: inputs})

        return cost

    def calc_total_cost(self, inputs):
        ''' For evalution '''
        
        return self.sess.run(self.total_loss, feed_dict={self.inputs: inputs})

      

    def transform(self, inputs, layerNum):
        ''' inputs => Encode Feature '''
        hiddenName = 'hidden' + str(layerNum)
        
        return self.sess.run(self.layers[hiddenName], feed_dict={self.layers['inputs']: inputs})

    def generate(self, hidden = None, layerNum = 1):
        ''' Encode Feature => outputs '''
        hiddenName = 'hidden' + str(layerNum)

        return self.sess.run(self.layers['outputs'], feed_dict={self.layers[hiddenName]: hidden})

    def reconstruct(self, inputs):
        ''' inputs => outputs '''

        return self.sess.run(self.layers['outputs'], feed_dict={self.layers['inputs']: inputs})

    def getWeigths(self, layerNum):
        ''' weight of layerNum-th layer '''
        weightName = 'weight' + str(layerNum)
        
        return self.sess.run(self.weights[weightName])

    def getBiases(self, layerNum):
        ''' bias of layerNum-th layer '''
        biasName = 'bias' + str(layerNum)

        return self.sess.run(self.weights[biasName]) 
        
    
if __name__ == "__main__":

    autoEncoder = AutoEncoder();

    print(autoEncoder.layers)
    print(autoEncoder.weights['weight1'])
    print(autoEncoder.weights['bias1'])
    print(autoEncoder.weights['weight2'])
    print(autoEncoder.weights['bias2'])

3. 测试


展开阅读全文

没有更多推荐了,返回首页