# -*- coding: utf-8 -*-
"""
Created on Sun Mar 4 09:21:41 2018 @author: markli
"""
import numpy as np; def ReLU(x):
return max(0,x); def logistic(x):
return 1/(1 + np.exp(-x)); def logistic_derivative(x):
return logistic(x)*(1-logistic(x)); class ConvolutionLayer:
"""
卷积神经网络中的卷积层
"""
def __init__(self,shape,padding,filters,stride):
"""
shape 卷积层形状,元组 (行,列,通道数)
padding 填充零个数的大小
filters 过滤器的形状,元组 (行,列,通道数,个数)
stride 步长
"""
self.shape = shape;
self.padding = padding;
self.stride = stride;
self.fileters = filters[:3];
self.fileternum = filters[3];
self.weights = [];
for i in range(filters[3]):
self.weights.append(np.random.randn(shape[2],filters[0],filters[1]));
self.baises = list(np.random.randn(filters[3])); self.convlutionsize = (int((shape[0] + 2*padding - filters[0])/stride + 1),int((shape[1] + 2*padding - filters[1])/stride + 1));
self.conv = np.ones((filters[3],self.convlutionsize[0],self.convlutionsize[1])); def Convolute(self,Data):
"""
Data 三维数组,若只有两维则通道数设为1.
"""
if(self.padding != 0):
for c in range(self.shape[2]):
ones = np.zeros((self.shape[0]+2*self.padding,self.shape[1]+2*self.padding));
ones[self.padding:self.padding+self.shape[0],self.padding:self.padding+self.shape[1]] = Data[c];
Data[c] = ones;
c,m,n = Data.shape; #遍历每一个过滤器
for f in range(self.fileternum):
t_conv = self.conv[f]; #取出第f个过滤器卷积后的临时容器
w = self.weights[f]; #取出第f个过滤器的权值集合
b = self.baises[f]; #取出第f个过滤器的偏倚
#卷积运算,所有通道一起遍历
row = 0;
for i in range(self.convlutionsize[0]):
col = 0;
for j in range(self.convlutionsize[1]):
data = Data[:,row:row+self.fileters[0],col:col+self.fileters[1]]; #取出卷积运算的数据立方体
s = 0; #存放卷积立方体的乘积的和
#对取出的临时数据的每个通道进行卷积运算
for t_c in range(c):
t_w = w[t_c];
t_data = data[t_c];
temp = sum(np.multiply(t_w,t_data));
s = temp + s;
t_conv[i,j] = ReLU(s+b);
#向右移动过滤器
col = col + self.stride;
#向下移动过滤器
row = row + self.stride;
#更新卷积结果容器
self.conv[f] = t_conv; class PoolLayer:
"""池化层"""
def __init__(self,shape,poolsize,stride,classic="max"):
"""
shape 池化目标的形状, 元组(行,列,通道数)
poolsize 池化矩阵的形状,元组 (行,列)
stride 步长 一般情况下池化的步长等于池化大小
classic 池化方式 max,average
"""
self.shape = shape;
self.stride = stride;
self.poolsize = poolsize;
self.classic = classic;
#生成池化结果矩阵形状
self.pool = np.ones((shape[2],(shape[0]-poolsize[0])/stride + 1,(shape[1]-poolsize[1])/stride + 1));
#生成过度池化矩阵形状
self.c_poolsize = ((shape[0]-poolsize[0])/stride + 1,(shape[1]-poolsize[1])/stride + 1); def Pool(self,Data):
"""
Data 三维数组,若只有两维则通道数设为1.
"""
c,m,n = Data.shape; #在每个通道上进行池化操作
for k in range(c):
p_temp = Data[k];
row = 0;
for i in range(self.c_poolsize[0]):
col = 0;
for j in range(self.c_poolsize[1]):
temp = p_temp[row:row+self.poolsize[0],col:col+self.poolsize[1]];
if(self.classic == "average"):
self.pool[k][i][j] = np.sum(temp) / (self.poolsize[0] * self.poolsize[1]);
if(self.classic == "max"):
self.pool[k][i][j] = np.max(temp);
else:
print("the classic does not exist"); col = col + self.stride; row = row + self.stride; class FullConnectLayer:
"""全连接层"""
def __init__(self,n_in,n_out,action_fun=logistic,action_fun_der=logistic_derivative,flag):
"""
n_in 输入层的单元数
n_out 输出单元个数 及紧邻下一层的单元数
action_fun 激活函数
action_fun_der 激活函数的导函数
flag 初始化权值和偏倚的标记 normal,larger,smaller
"""
self.action_fun = action_fun;
self.action_fun_der = action_fun_der;
self.n_in = n_in;
self.n_out = n_out;
init_weight_biase(flag); def init_weight_biase(self,init_flag):
if(init_flag == "noraml"):
self.weight = np.random.randn(self.n_out,self.n_in);#weight 取值服从N(0,1) 分布
self.biase = np.random.randn(self.n_out,1);
elif(init_flag == "larger"):
self.weight = 2*np.random.randn(self.n_out,self.n_in)-1; #weight 取值范围(-1,1)
self.biases = 2*np.random.randn(self.n_out,1)-1 ; #b 取值范围(-1,1)
elif(init_flag == "smaller"):
self.weight = np.random.randn(self.n_out,self.n_in)/np.sqrt(self.n_out) ; #weight 取值服从N(0,1/x) 分布
self.biase = np.random.randn(self.n_out,1); def Forward(self,inpt):
"""全连接层的前馈传播"""
self.inpt = np.dot(self.weight,inpt) + self.biase;
self.outpt = self.action_fun(self.inpt); """Softmax Layer"""
后向传播的实现还是没有头绪,三层之间如何衔接不知道该怎么设计。本人能力水平有限,欢迎交流。本人微信号 markli52024