用TensorFlow实现自编码器

#初学者的笔记,不对的地方大家多多指教
# -*- coding:utf-8 -*-
#导入对应的安装包和读入数据
import numpy as np
import sklearn.preprocessing as prep
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#参数的初始化
def xavier_init(fan_in,fan_out,constant = 1):
    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
class AdditiveGaussianNoiseAutoencoder(object):
    def __init__(self,n_input,n_hidden,transfer_function = tf.nn.softplus,optimizer = tf.train.AdamOptimizer(),scale = 0.1):
        self.n_input = n_input    #输入层节点数
        self.n_hidden = n_hidden  #隐藏层节点数
        self.transfer = transfer_function #激活函数,默认为softplus
        self.scale = tf.placeholder(tf.float32)  
self.training_scale = scale network_weights = self._initialize_weights() self.weights = network_weights self.x = tf.placeholder(tf.float32,[None,self.n_input]) self.n_hidden = self.transfer(tf.add(tf.matmul(self.x+scale*tf.random_normal((n_input,)),self.weights['w1']),self.weights['b1'])) self.reconstruction = tf.add(tf.matmul(self.n_hidden,self.weights['w2']),self.weights['b2']) self.cost = 0.5*tf.reduce_sum(tf.pow(tf.subtract(self.reconstruction,self.x),2.0)) #重建数据 self.optimizer = optimizer.minimize(self.cost) init = tf.global_variables_initializer() self.sess = tf.Session() self.sess.run(init)#参数初始化函数
def _initialize_weights(self): all_weights = dict() all_weights['w1'] = tf.Variable(xavier_init(self.n_input,self.n_hidden)) all_weights['b1'] = tf.Variable(tf.zeros([self.n_hidden],dtype=tf.float32)) all_weights['w2'] = tf.Variable(tf.zeros([self.n_hidden,self.n_input],dtype=tf.float32)) all_weights['b2'] = tf.Variable(tf.zeros([self.n_input],dtype=tf.float32)) return all_weights
#训练函数
 def partial_fit(self,x): cost,opt = self.sess.run((self.cost,self.optimizer),feed_dict={self.x:x,self.scale:self.training_scale}) return cost
#计算损失函数
 def calc_total__cost(self,x): return self.sess.run(self.cost,feed_dict = {self.x:x,self.scale:self.training_scale})
#隐藏层计算结果
 def transform(self,x): return self.sess.run(self.n_hidden,feed_dict={self.x:x,self.scale:self.training_scale})#输出重建后结果
def generate(self,hidden = None): if hidden is None: hidden = np.random.normal(size = self.weights['b1']) return self.sess.run(self.reconstruction,feed_dict={self.n_hidden:hidden})
#整体运行一遍复原过程,包括提取高阶特征和通过高阶特征复原数据
 def restronstruct(self,x): return self.sess.run(self.reconstruction,feed_dict={self.x:x,self.scale:self.training_scale})#获取隐藏层权重w1
def getWeights(self): return self.sess.run(self.weights['w1'])#获取隐藏层偏置b1
def getBiases(self): return self.sess.run(self.weights['b1'])#获得mnist数据集mnist = input_data.read_data_sets('MNIST_data/',one_hot = True)#对数据进行标准化处理def standard_scale(x_train,x_test): preprocessor = prep.StandardScaler().fit(x_train) x_train = preprocessor.transform(x_train) x_test = preprocessor.transform(x_test) return x_train,x_test#随机获取数据
def get_random_block_from_data(data,batch_size): start_index = np.random.randint(0,len(data)-batch_size) return data[start_index:(start_index+batch_size)]
#使用上面的函数进行数据的标准化处理
x_train,x_test = standard_scale(mnist.train.images,mnist.test.images)
#参数的设置
n_samples = int(mnist.train.num_examples)training_epochs = 20batch_size = 128display_step = 1autoencoder =AdditiveGaussianNoiseAutoencoder(n_input=784,n_hidden=200,transfer_function=tf.nn.softplus,optimizer=tf.train.AdamOptimizer(learning_rate=0.001),scale = 0.01)#训练和测试
for epoch in range(training_epochs): avg_cost = 0 total_batch = int(n_samples/batch_size) for i in range(batch_size): batch_xs = get_random_block_from_data(x_train,batch_size) cost = autoencoder.partial_fit(batch_xs) avg_cost+=cost/n_samples*batch_size if epoch % display_step == 0: print("Epoch:",'%04d'%(epoch+1),"cost = ","{:.9f}".format(avg_cost))print("Total cost:"+str(autoencoder.calc_total__cost(x_test)))
 
  

你可能感兴趣的:(TensorFlow)