没有合适的资源?快使用搜索试试~ 我知道了~
首页深度信念网络分类算法python程序.docx
资源详情
资源评论
资源推荐
Utils.py:
'''
Created on 2020
年
6
月
13
日
@author:
紫薇星君
'''
import numpy
numpy.seterr(all='ignore')
def sigmoid(x):
return 1. / (1 + numpy.exp(-x))
def dsigmoid(x):
return x * (1. - x)
def tanh(x):
return numpy.tanh(x)
def dtanh(x):
return 1. - x * x
def softmax(x):
e = numpy.exp(x - numpy.max(x)) # prevent overflow
if e.ndim == 1:
return e / numpy.sum(e, axis=0)
else:
return e / numpy.array([numpy.sum(e, axis=1)]).T # ndim = 2
def ReLU(x):
return x * (x > 0)
def dReLU(x):
return 1. * (x > 0)
# # probability density for the Gaussian dist
# def gaussian(x, mean=0.0, scale=1.0):
# s = 2 * numpy.power(scale, 2)
# e = numpy.exp( - numpy.power((x - mean), 2) / s )
# return e / numpy.square(numpy.pi * s)
dA.py:
'''
Created on 2020
年
6
月
14
日
@author:
紫薇星君
'''
import sys
import numpy
from utils import *
class dA(object):# 消噪自动编码器
#input 为训练集, n_visible 可见层单元数, n_hidden 隐层单元数,W 连接权值, hbias
隐层偏置, vbias
可见层偏置,
rng
随机种子
def __init__(self, input=None, n_visible=2, n_hidden=3,W=None, hbias=None, vbias=None, rng=None):
self.n_visible = n_visible # 可见层单元数
self.n_hidden = n_hidden # 隐层单元数
if rng is None:
rng = numpy.random.RandomState(1234) #建立随机数种子
if W is None:
a = 1. / n_visible
#initial_W 是 n_visible 行,n_hidden 列,每个元素最小-a,最大 a 的随机数
W = numpy.array(rng.uniform(low=-a,high=a,size=(n_visible, n_hidden)))
if hbias is None: #初始化隐层偏置,统一为 0
hbias = numpy.zeros(n_hidden) # initialize h bias 0
if vbias is None: #初始化可见层偏置,统一为 0
vbias = numpy.zeros(n_visible) # initialize v bias 0
self.rng = rng
self.x = input
self.W = W
self.W_prime = self.W.T
self.hbias = hbias
self.vbias = vbias
return
def get_corrupted_input(self, input, corruption_level):
#corruption_level 噪声百分比,此函数使输入的“1-损坏级别”项保持不变,并将随机选择的大小子集“损坏级别”归
零
assert corruption_level < 1
return self.rng.binomial(size=input.shape,n=1,p=1-corruption_level) * input
# Encode
def get_hidden_values(self, input): #计算隐藏层的值
return sigmoid(numpy.dot(input, self.W) + self.hbias)
# Decode
def get_reconstructed_input(self, hidden): #根据隐藏层的值计算重建的输入
return sigmoid(numpy.dot(hidden, self.W_prime) + self.vbias)
def train(self, lr=0.1, corruption_level=0.3, input=None): #此函数计算 dA 的一个训练步骤的成本和更新
if input is not None:
self.x = input
x = self.x
tilde_x = self.get_corrupted_input(x, corruption_level) #增加 corruption_level 噪音的随机采样作为输入
y = self.get_hidden_values(tilde_x) #计算隐层输出
z = self.get_reconstructed_input(y) #以隐层输出计算重构输入
L_h2 = x - z #可见层的误差
L_h1 = numpy.dot(L_h2, self.W) * y * (1 - y) #隐层误差
L_vbias = L_h2
L_hbias = L_h1
L_W = numpy.dot(tilde_x.T, L_h1) + numpy.dot(L_h2.T, y)
self.W += lr * L_W #权值改变
self.hbias += lr * numpy.mean(L_hbias, axis=0) #隐层偏置改变
self.vbias += lr * numpy.mean(L_vbias, axis=0) #可见层偏置改变
def negative_log_likelihood(self, corruption_level=0.3): #计算重构误差
tilde_x = self.get_corrupted_input(self.x, corruption_level)
y = self.get_hidden_values(tilde_x)
z = self.get_reconstructed_input(y)
cross_entropy = - numpy.mean( numpy.sum(self.x * numpy.log(z) + (1 - self.x) * numpy.log(1 - z),axis=1))
return cross_entropy
def reconstruct(self, x): #重构输出
y = self.get_hidden_values(x) #隐层输出计算
z = self.get_reconstructed_input(y) #可见层重构计算
return z
def test_dA(learning_rate=0.1, corruption_level=0.3, training_epochs=50):
data = numpy.array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0]])
rng = numpy.random.RandomState(123)
# construct dA
da = dA(input=data, n_visible=20, n_hidden=5, rng=rng)
# train
for epoch in range(training_epochs):
da.train(lr=learning_rate, corruption_level=corruption_level)
# cost = da.negative_log_likelihood(corruption_level=corruption_level)
# print >> sys.stderr, 'Training epoch %d, cost is ' % epoch, cost
# learning_rate *= 0.95
# test
x = numpy.array([[1, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0]])
print (da.reconstruct(x))
if __name__ == "__main__":
test_dA()
HiddenLayer.py
'''
Created on 2020
年
6
月
15
日
@author:
紫薇星君
'''
import sys
import numpy
from utils import *
class HiddenLayer(object):
def __init__(self, input, n_in, n_out,W=None, b=None, rng=None, activation=tanh):
#input 输入,n_in 输入单元数,n_out 输出单元数,W 连接权值,b 偏置,activation 激活函数
if rng is None:
rng = numpy.random.RandomState(1234)
if W is None:
a = 1. / n_in
W = numpy.array(rng.uniform(low=-a, high=a,size=(n_in, n_out))) #初始化权值
if b is None:
b = numpy.zeros(n_out) #初始化偏置为 0
self.rng = rng
self.x = input
self.W = W
self.b = b
if activation == tanh: #完成激活函数导函数的定义
self.dactivation = dtanh
elif activation == sigmoid:
self.dactivation = dsigmoid
elif activation == ReLU:
self.dactivation = dReLU
else:
raise ValueError('activation function not supported.')
self.activation = activation #激活函数
def output(self, input=None): #前向计算
if input is not None:
self.x = input
linear_output = numpy.dot(self.x, self.W) + self.b
return self.activation(linear_output)
def forward(self, input=None): #就是前向计算和 output(input=None)一样
return self.output(input=input)
def backward(self, prev_layer, lr=0.1, input=None, dropout=False, mask=None):
if input is not None:
self.x = input
d_y = self.dactivation(prev_layer.x) * numpy.dot(prev_layer.d_y, prev_layer.W.T)
if dropout == True:
d_y *= mask
self.W += lr * numpy.dot(self.x.T, d_y)
self.b += lr * numpy.mean(d_y, axis=0)
剩余15页未读,继续阅读
紫薇大帝
- 粉丝: 33
- 资源: 8
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 27页智慧街道信息化建设综合解决方案.pptx
- 计算机二级Ms-Office选择题汇总.doc
- 单链表的插入和删除实验报告 (2).docx
- 单链表的插入和删除实验报告.pdf
- 物联网智能终端项目设备管理方案.pdf
- 如何打造品牌的模式.doc
- 样式控制与页面布局.pdf
- 武汉理工Java实验报告(二).docx
- 2021线上新品消费趋势报告.pdf
- 第3章 Matlab中的矩阵及其运算.docx
- 基于Web的人力资源管理系统的必要性和可行性.doc
- 基于一阶倒立摆的matlab仿真实验.doc
- 速运公司物流管理模式研究教材
- 大数据与管理.pptx
- 单片机课程设计之步进电机.doc
- 大数据与数据挖掘.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0