自编码器

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 20 11:17:17 2018

@author: swx
"""
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
import sklearn.preprocessing as prep

#定义这样一个初始化器Xaiver,
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)
#tf.random_uniform((m, n), minval=low,maxval=high,dtype=tf.float32)))返回m*n的矩阵,产生于low和high之间,产生的值是均匀分布的

class AdditiveGaussianNoiseAutoencoder(object):
    def __init__(self, n_input, n_hidden, transfer_function=tf.nn.softplus,
               optimizer = tf.train.AdamOptimizer(), scale=0.1):#softplus: ζ(x)=ln(1+exp(x)) 可以看做是RELU的平滑后的函数
        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_weight = self._initialize_weights()
        self.weights = network_weight
        self.x = tf.placeholder(tf.float32,[None,self.n_input])
        self.hidden = self.transfer(tf.add(tf.matmul(
                self.x + scale * tf.random_normal((n_input,)),
                self.weights['w1']), self.weights['b1']))#tf.random_normal()函数用于从服从指定正太分布的数值中取出指定个数的值。
                                                        #tf.random_normal(shape, mean=0.0, stddev=1.0,dtype=tf.float32, seed=None, name=None
        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))#tf.pow(x, y, name=None) # tensor ‘x’ is [[2, 2], [3, 3]]
                                                                # tensor ‘y’ is [[8, 16], [2, 3]]
                                                                #tf.pow(x, y) ==> [[256, 65536], [9, 27]]
                                                                #可以试一下不同shapr 的tensor 的POWER操作
        self.optimizer = optimizer.minimize(self.cost)
        init = tf.global_variables_initializer()#有必要么?我没看到Variable量啊是不是在weights中呢
        self.sess = tf.Session()
        self.sess.run(init) #run()这个东西我没看懂,run的API要再看看

#def一个字典        
    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
    
    #用一个batch做训练并返回一个loss
    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.hidden, feed_dict = {self.x: X, 
            self.scale: self.training_scale
        })
    def genrate(self, hidden = None):
        if hidden is None:
            hidden = np.random.normal(size = self.weights["b1"])#" he '有什么区别
        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 getWeights(self):
        return self.sess.run(self.weights['w1'])
    def getBiases(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):
    preprocesor = prep.StandardScaler().fit(X_train)#??
    X_train = preprocesor.transform(X_train)
    X_test = preprocesor.transform(X_test)
    return X_train, X_test

def get_random_blocj_from_data(data, batch_size):
    start_index = np.random.randint(0, len(data) - batch_size)#可以看一下.randint
    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 = 20
batch_size = 128
display_step = 1

autoencoder = 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(total_batch):
        batch_xs = get_random_blocj_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)))

代码意义如题:

Output :

Extracting MNIST_data\train-images-idx3-ubyte.gz
Extracting MNIST_data\train-labels-idx1-ubyte.gz
Extracting MNIST_data\t10k-images-idx3-ubyte.gz
Extracting MNIST_data\t10k-labels-idx1-ubyte.gz
Epoch: 0001 cost= 18250.973550000
Epoch: 0002 cost= 11715.462490909
Epoch: 0003 cost= 10982.739083523
Epoch: 0004 cost= 9750.323880682
Epoch: 0005 cost= 9720.300893182
Epoch: 0006 cost= 9421.699218182
Epoch: 0007 cost= 10323.683318750
Epoch: 0008 cost= 8372.231630114
Epoch: 0009 cost= 7866.114038068
Epoch: 0010 cost= 8019.446246591
Epoch: 0011 cost= 8566.497238068
Epoch: 0012 cost= 8713.336972727
Epoch: 0013 cost= 8565.204978409
Epoch: 0014 cost= 8006.694885227
Epoch: 0015 cost= 8714.736692614
Epoch: 0016 cost= 8540.321734091
Epoch: 0017 cost= 8637.853359659
Epoch: 0018 cost= 8031.826144886
Epoch: 0019 cost= 7985.633525000
Epoch: 0020 cost= 8456.986656250
Total cost:693377.0

一步一步来看:

去噪自编码器就是将图片重构,目的近似于图像处理中的滤波。

导入的数据集就是mnist

import sklearn.preprocessing as prep

sklearn是机器学习常用的一个库,很庞大,我还没有完全理解,这里调用的preprocessing模块是对数据做预处理的模块,代码中用到了standardscaler()函数,意义在于将输入数据去均值,通过标准差映射到均值为0的空间,写了段程序test一下

from sklearn.preprocessing import StandardScaler
import numpy as np
x=np.arange(10).reshape(5,2)
print(x)
ss=StandardScaler()
ss.fit(x)
print (ss.transform(x))

Output:

[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
[[-1.41421356 -1.41421356]
 [-0.70710678 -0.70710678]
 [ 0.          0.        ]
 [ 0.70710678  0.70710678]
 [ 1.41421356  1.41421356]]

有了大致的了解。

接下来第一个def:

#定义这样一个初始化器Xaiver,
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)
#tf.random_uniform((m, n), minval=low,maxval=high,dtype=tf.float32)))返回m*n的矩阵,产生于low和high之间,产生的值是均匀分布的

test:

import tensorflow as tf
import numpy as np
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)
matrix = xavier_init(3, 2, )
with tf.Session() as sess:
    print (sess.run(matrix))
 

OUTPUT:

[[ 0.47449267  0.61771238]
 [-0.82766742 -0.98358673]
 [-0.29671848 -0.54098582]]

不太难理解

接下来我定义了这个类 可以看出这个类只有一个隐含层 因为我的字典中只存了w1 b1 w2 b2 分别对应隐层和输出层的权重和偏置,损失函数用的是平方误差然后去训练中去优化这个误差,代码虽然很长的理解起来不算特别难。

我写代码时思考的问题:

tf.pow():

test:

import tensorflow as tf
import numpy as np
x = [[2,2],
     [2,2]]
y = [[1,1],
     [2,2]]
z = tf.pow(x,y)
with tf.Session() as sess:
    print(sess.run(z))

OUTPUT:

[[2 2]
 [4 4]]

对应位置幂运算

还有就是我在写代码中发现自己的python能力有严重的不足,比如说定义类的时候__int__我总是写成_int_,这会出现no object的报错,引以为戒。接下来我会注重python中数据结构,语法,以及tensorflow中一些函数,机器学习中优化器的学习。这一个月只写了这么一篇学习笔记是因为上个月参加了研究生电子设计大赛,项目名称是基于CNN的血液图像识别分类计数,大概就是血常规检测的样子,由于时间紧迫,能力不足仅仅获得了上海市的二等奖,明年我会继续参加。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值