加高斯噪声的自编码器

取自于TensorFlow实战

import  tensorflow as tf
import sklearn.preprocessing as prep
import  matplotlib.pyplot as plt
import numpy as np
#定义tensorfl的CPU运算优先级
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
#导入MNIST手写数字集数据
from tensorflow.examples.tutorials.mnist import input_data

#定义Xavier初始化器
def xavier_init(fan_in,fan_out,constant=1):
    factor=constant*np.sqrt(6.0/(fan_in+fan_out))
    return tf.random_uniform((fan_in,fan_out),minval=-factor,maxval=factor,dtype=tf.float32)
#定义加高斯噪声的自编码器
class AdditiveGaussianNoiseAutoencode(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
        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])
        #定义隐藏层(降维特征层),激活函数为softplus
        self.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.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
    #定义部分拟合,返回cost
    def partial_fit(self,x):
        cost,_=self.sess.run((self.cost,self.optimizer),feed_dict={self.x:x,self.scale:self.training_scale})
        return cost
    #不触发训练运算,返回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.hidden,feed_dict={self.x:x,self.scale:self.training_scale})
    #由隐藏层计算输出层
    def generate(self,hidden=None):
        if hidden==None:
            hidden=np.random.normal(size=self.weights['b1'])
        return self.sess.run(self.reconstruction,feed_dict={self.hidden:hidden})
    #由输出计算输出
    def reconstruct(self,x):
        return self.sess.run(self.reconstruction,feed_dict={self.x:x,self.scale:self.training_scale})
    #计算权重偏置
    def get_weights(self):
        return self.sess.run(self.weights['w1'])

    def get_biases(self):
        return self.sess.run(self.weights['b1'])

#导入手写数据集
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
#随机获取mini——batch
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_epoch=20
batch_size=128
display_step=1
#定义类
autoencode=AdditiveGaussianNoiseAutoencode(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_epoch):
    avg_cost=0
    total_batch=int(n_samples/batch_size)
    for i in range(total_batch):
        batch_x=get_random_block_from_data(mnist.train.images,batch_size)

        cost=autoencode.partial_fit(batch_x)
        avg_cost+=cost/n_samples*batch_size

    if epoch%display_step==0:
        print('Epoch:{:4d},cost={:.9f}'.format(epoch+1,avg_cost))
print('total cost:',autoencode.calc_total_cost(mnist.train.images))

#显示前四个数据的自编码结果
input=mnist.test.images[:4]
output=autoencode.reconstruct(input)
fig,ax=plt.subplots(2,4)
for i,data in enumerate(input.reshape(4,28,28)):
    ax[0,i].imshow(data,'gray')
    ax[0,i].axis('off')
for i,data in enumerate(output.reshape(4,28,28)):
    ax[1,i].imshow(data,'gray')
    ax[1,i].axis('off')
plt.show()



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值