# （2编写网络）自己动手，编写神经网络程序，解决Mnist问题，并网络化部署

+关注继续查看

1、sigmod函数，基本上就是基于定义的；

#########helper函数########
#计算sigmoid，这个函数来自定义
def sigmoid(z):
return 1.0/(1.0+np.exp(-z))
#计算sigmoid的导数，这个函数可以被证明
def sigmoid_prime(z):
return sigmoid(z)*(1 - sigmoid(z))

2、构造函数
###########Main函数########
#使用例子 net = GoNetwork([2, 3, 1])
class GoNetwork(object):

def __init__(self, sizes):#构造函数
self.num_layers = len(sizes)#层数
self.sizes = sizes #每层size
#随机生成子节点
self.biases= [np.random.randn(y, 1) for y in sizes[1:]]
# net.weights 是一个存储着连接第二层和第三层神经元权重的 Numpy 矩阵。
self.weights = [np.random.randn(y, x)
for x, y in zip(sizes[:-1], sizes[1:])]

sizes = [2,  3,  1]

numpy.random.randn(d0, d1, ..., dn)

bbb = [np.random.randn(3, 2)]

2.5 * np.random.randn(2, 4) + 3

返回：

 
array([[ 4.128****53,  1.764****44 ,  2.732****92,  2.90839231],
       [ 0.174****86,  4.92026887,  1.574****66, -0.4305991 ]]) 
aaa =[ np.random.randn(y, 1) for y in sizes[1:]]

zip(sizes[:-1], sizes[1:])

3、前向网络,主要用于测试当前网络
def feedforward(self,a):
for b,w in zip(self.biases,self.weights):
a = sigmoid(np.dot(w,a)+b)
return a

4、评价函数，基本上也是按照定义进行设定的
def evaluate(self, test_data):
test_results = [(np.argmax(self.feedforward(x)), y)#这里需要注意feedforward的参数x,实际上它是一个in/out参数。
for (x, y) in test_data]
return sum(int(x == y) for (x, y) in test_results)#做出了正确的预测

5、代价函数
#cost代价函数
def cost_derivative(self, output_activations, y):
return (output_activations-y)

def update_mini_batch(self, mini_batch, eta):
nabla_b = [np.zeros(b.shape) for b in self.biases]#生成b和w形状的以0填充的矩阵
nabla_w = [np.zeros(w.shape) for w in self.weights]
for x, y in mini_batch:
delta_nabla_b, delta_nabla_w = self.backprop(x, y)#理解反向传播就是一种快速计算梯度的方法
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]
self.weights = [w-(eta/len(mini_batch))*nw
for w, nw in zip(self.weights, nabla_w)]
self.biases = [b-(eta/len(mini_batch))*nb
for b, nb in zip(self.biases, nabla_b)]

nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]

for x, y in mini_batch:
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
nabla_b = [nb+dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]
nabla_w = [nw+dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]

6、后向传播函数，其目的是进行梯度下降计算，是最为复杂的部分
#反向传播就是一种快速计算代价函数梯度的方法，也就是计算delta的一种方法
def backprop(self, x, y):
#都以空矩阵来进行初始化
nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]
# feedforward
activation = x
activations = [x] # list to store all the activations, layer by layer
zs = [] # list to store all the z vectors, layer by layer
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation)+b #前向传播
zs.append(z)
activation = sigmoid(z)
activations.append(activation)
# backward pass
delta = self.cost_derivative(activations[-1], y) * \
sigmoid_prime(zs[-1])
nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta, activations[-2].transpose())
for l in range(2, self.num_layers):
z = zs[-l]
sp = sigmoid_prime(z)
delta = np.dot(self.weights[-l+1].transpose(), delta) * sp
nabla_b[-l] = delta
nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())
return (nabla_b, nabla_w)

nabla_b = [np.zeros(b.shape) for b in self.biases]
nabla_w = [np.zeros(w.shape) for w in self.weights]

# feedforward
activation = x
activations = [x] # list to store all the activations, layer by layer
zs = [] # list to store all the z vectors, layer by layer
for b, w in zip(self.biases, self.weights):
z = np.dot(w, activation)+b #前向传播
zs.append(z)
activation = sigmoid(z)
activations.append(activation)

delta = self.cost_derivative(activations[-1], y) * sigmoid_prime(zs[-1])

nabla_b[-1] = delta
nabla_w[-1] = np.dot(delta, activations[-2].transpose())

delta = np.dot(self.weights[-l+1].transpose(), delta) * sp

7、随机梯度下降算法，到了这里也就是将上面的合起来
#随机梯度下降算法
def SGD(self, training_data, epochs, mini_batch_size, eta,test_data=None):

training_data = list(training_data)
n = len(training_data)

if test_data:
test_data = list(test_data)
n_test = len(test_data)
#⾸先随机地将训练数据打乱
for j in range(epochs):
random.shuffle(training_data)
#再将它分成多个适当⼤⼩的⼩批量数据
mini_batches = [
training_data[k:k+mini_batch_size]
for k in range(0, n, mini_batch_size)]
for mini_batch in mini_batches:#最主要的一行代码
self.update_mini_batch(mini_batch, eta)
if test_data:
print("Epoch {} : {} / {}".format(j,self.evaluate(test_data),n_test))
else:
print("Epoch {} complete".format(j)) |
5月前
|

CVPR 2023 | 神经网络超体？新国立LV lab提出全新网络克隆技术
CVPR 2023 | 神经网络超体？新国立LV lab提出全新网络克隆技术
83 0
|
7月前
|

【PNN分类】基于脉冲神经网络PNN网络变压器故障诊断附matlab代码
【PNN分类】基于脉冲神经网络PNN网络变压器故障诊断附matlab代码
115 0