# -*- coding: utf-8 -*-
"""
Created on Sat Sep 30 18:45:47 2017
@author: 董玮
"""
import argparse
import time
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
from tensorflow.python.framework import ops
"""
类型:抽象类
说明:规则化数据接口,一般用于数据预处理中。
"""
class interface_normalize_data(object):
"""
类型:公有成员变量
说明:规则化数据过程中,定义一个无穷小精度,用来防止数据计算中的非法操作。
"""
epsilon = 1e-8
"""
类型:抽象公有成员函数
说明:用来规则化数据。
参数:
data -- 待处理的数据。
返回值:
data -- 处理后的数据。
"""
def normalize_data(self, data):
pass
"""
类型:实体类,继承自抽象类interface_normalize_data
说明:用于中心化数据,使数据中心在坐标原点上。
"""
class mean_normalization(interface_normalize_data):
def normalize_data(self, data):
#计算数据每个维度的期望,并用每一条数据减去期望。
center = data - tf.reduce_mean(data, axis = 0, keep_dims = True)
return center
"""
类型:实体类,继承自抽象类interface_normalize_data
说明:用于中心化数据,并除以方差,使数据中心在坐标原点上,并且使每个维度之间的跨度相似。
"""
class variance_normalization(interface_normalize_data):
def normalize_data(self, data):
center = data - tf.reduce_mean(data, axis = 0, keep_dims = True)
#计算数据每个维度的方差。
variance = tf.reduce_mean(tf.square(center), axis = 0, keep_dims = True)
#除以方差并在除数上加上无穷小精度。
vscore = center / (variance + self.epsilon)
return vscore
"""
类型:实体类,继承自抽象类interface_normalize_data
说明:用于Z-Score统计,与上述实体类的区别是除以标准差而不是方差。
"""
class zscore_normalization(interface_normalize_data):
def normalize_data(self, data):
center = data - tf.reduce_mean(data, axis = 0, keep_dims = True)
variance = tf.reduce_mean(tf.square(center), axis = 0, keep_dims = True)
#除以标准差并在除数上加上无穷小精度。
zscore = center / tf.sqrt(variance + self.epsilon)
return zscore
"""
类型:抽象类
说明:神经网络初始化参数接口。
"""
class interface_initialize_parameters(object):
"""
类型:公有成员变量
说明:定义输入层、隐藏层、输出层每层的神经元个数。
"""
structure = None
"""
类型:公有成员变量
说明:定义规则化功能。
"""
regularizer = None
"""
类型:公有成员变量
说明:随机种子,用来产生随机数。
"""
seed = 1
"""
类型:抽象公有成员函数
说明:用来初始化参数。
"""
def initialize_parameters(self):
pass
"""
类型:实体类
说明:标准的x-avier参数初始化,继承自抽象类interface_initialize_parameters
"""
class xavier_initialize_parameters(interface_initialize_parameters):
"""
类型:公有成员函数
说明:用来初始化参数。
参数:无
返回值:
parameters -- 返回初始化后的参数。
"""
def initialize_parameters(self):
tf.set_random_seed(self.seed)
parameters = {}
#初始化两类参数,一种是W1、W2、W3……,另一种是b1、b2、b3……。其中数字代表层数。
#W的维度为(当前层神经元数,前一层神经元数)。b的维度为(当前层神经元数,1)。
for l in range(1, len(self.structure)):
parameters["W" + str(l)] = tf.get_variable("W" + str(l), [self.structure[l-1], self.structure[l]],
initializer = tf.contrib.layers.xavier_initializer(self.seed), regularizer = self.regularizer)
parameters["b" + str(l)] = tf.get_variable("b" + str(l), [1, self.structure[l]],
initializer = tf.zeros_initializer())
return parameters
"""
类型:实体类
说明:具有batch normalization功能的x-avier参数初始化,继承自抽象类interface_initialize_parameters
"""
class xavier_initialize_parameters_BN(interface_initialize_parameters):
"""
类型:公有成员函数
说明:用来初始化参数。
参数:无
返回值:
parameters -- 返回初始化后的参数。
"""
def initialize_parameters(self):
tf.set_random_seed(self.seed)
parameters = {}
#因batch normalization需要,初始化三类参数,W1、W2、W3……,gamma1、gamma2、gamma3……,beta1、beta2、beta3……。其中数字代表层数。
#W的维度为(当前层神经元数,前一层神经元数)。gamma与beta的维度均为(当前层神经元数,1)。
for l in range(1, len(self.structure)):
parameters["W" + str(l)] = tf.get_variable("W" + str(l), [self.structure[l-1], self.structure[l]],
initializer = tf.contrib.layers.xavier_initializer(self.seed), regularizer = self.regularizer)
parameters["gamma" + str(l)] = tf.get_variable("gamma" + str(l), [1, self.structure[l]],
initializer = tf.ones_initializer())
parameters["beta" + str(l)] = tf.get_variable("beta" + str(l), [1, self.structure[l]],
initializer = tf.zeros_initializer())
return parameters
"""
类型:抽象类
说明:定义向前向后算法。
"""
class interface_propagation(object):
"""
类型:公有成员变量
说明:规则化数据过程中,定义一个无穷小精度,用来防止数据计算中的非法操作。
"""
epsilon = 1e-8
"""
类型:公有成员变量
说明:是否进行dropout规则化操作。
"""
is_dropout = False
"""
类型:公有成员变量
说明:如果is_dropout为真,那么此变量存储每一层保持神经元个数的概率。
"""
keep_prob = 1.
"""
类型:公有成员变量
说明:定义激活函数。
"""
act_func = None
"""
类型:抽象公有函数
说明:向前算法。
参数:
training_set -- 训练集。
training_label -- 训练标签。
parameters -- 训练参数。
"""
def forward_propagation(self, training_set, training_label, parameters):
pass
"""
类型:具体类
说明:标准的向前算法,继承自interface_propagation。
"""
class propagation_standard(interface_propagation):
def forward_propagation(self, training_set, training_label, parameters):
if(len(parameters) < 2):
return None
#向前算法
A = training_set
layer_number = len(parameters) // 2
for l in range(1, layer_number):
Z = tf.matmul(A, parameters["W"+str(l)]) + parameters["b" + str(l)]
#激活函数
A = self.act_func(Z)
#添加dropout正则化功能
if(self.is_dropout == True):
A = tf.nn.dropout(A, self.keep_prob)
Z = tf.matmul(A, parameters["W"+str(layer_number)]) + parameters["b" + str(layer_number)]
#计算代价函数
cross_entropy = tf.reduce_
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
dnn tensorflow.rar (10个子文件)
input_data.py 7KB
__pycache__
input_data.cpython-35.pyc 6KB
input_data.cpython-36.pyc 5KB
gnumpy.cpython-36.pyc 61KB
nn_tensorflow.py 24KB
MNIST_data
t10k-images-idx3-ubyte.gz 1.57MB
t10k-labels-idx1-ubyte.gz 4KB
t10k-images.idx3-ubyte 7.48MB
train-labels-idx1-ubyte.gz 28KB
train-images-idx3-ubyte.gz 9.45MB
共 10 条
- 1
资源评论
dongweiweiwei
- 粉丝: 5
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功