说明:此代码并非个人原创,是学习其他深度学习视频教程后总结所得。 总体思路: 1. 用手写数字识别作实例进行分析。 2. 具体的思路我不是非常清楚,就是用一个深度神经网络,选定n多参数,然后就可以在一定程度上模拟任何有规律的方程或者其他现象。 3. 求解的过程就是不断让真实值贴近预测的值,这时如果差别较大,可以改变参数的权重,还有偏向值。 4. 也就是cost(w, b) = |y - output|^2,此时的w, b赋值比较随机,所以就相当于在抛物线的两侧,若想下降到O点,也就是cost函数值最小,每次可以把x(也就是w, b)的值减去一个值(斜率的倍数,在左为负,在右为正),使值逼近O点,直接上图cost函数抛物线更新值 5. 此方法美其名曰梯度下降算法结合代码分析: 文章末尾有完整代码 1.初始化权重和偏向,使用numpy.random.randn(m, n),具体意思我想你们应该懂吧 self.weights = [np.random.randn(m, n) for m, n in zip(sizes[1:], sizes[:-1])] self.biases = [np.random.randn(k, 1) for k in sizes[1:]]2.每一轮epochs后打乱重排 random.shuffle(training_data)3.更新值 self.update_mini_batch(mini_batch, eta)4. backpropagation计算偏导值,也就是下图中减号后的偏导部分,不包括前面的伊塔参数,具体自己看吧 delta = self.cost_derivative(activations[-1], y) * sigmoid_prime(zs[-1]) nabla_w[-l] = np.dot(delta, activations[-l-1].transpose())5. 更新w和b值,文章中的代码 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)]6.其他部分比较简单,可以看英文注释,就是求偏导和sigmoid函数等,你们自己看吧。# author: dragon # date: 2018-7-21 # -*- coding: utf-8 -*-import random import numpy as npclass Network(object):def __init__(self, sizes):"""initialization parameter--weights and biases:param sizes:"""self.num_layers = len(sizes)self.sizes = sizesself.weights = [np.random.randn(m, n) for m, n in zip(sizes[1:], sizes[:-1])]self.biases = [np.random.randn(k, 1) for k in sizes[1:]]def feedforward(self, a):"""function value:param a: the input:return: sigmoid number"""for w, b in zip(self.weights, self.biases):a = sigmoid(np.dot(w, a) + b)return adef SGD(self, training_data, epochs, mini_batch_size, eta, test_data=None):"""realize stochastic gradient descent:param train_data::param epochs: the number of cycling:param mini_batch_size: each cycling sizes:param eta: learning rate:param test_data::return:None"""if test_data:n_test = len(test_data)n = len(training_data)for j in xrange(epochs):random.shuffle(training_data)mini_batches = [training_data[k: k+mini_batch_size] for k in xrange(0, n, mini_batch_size)]for mini_batch in mini_batches:self.update_mini_batch(mini_batch, eta)if test_data:#evaluate(test_data) is using to evaluate the accuracyprint "Epoch {0}:{1} / {2}".format(j, self.evaluate(test_data), n_test)else:print "Epoch {0} complete".format(j)def update_mini_batch(self, mini_batch, eta):"""uodate the weights and biases:param mini_batch: a lisr of tuple (x, y):param eta: learning rate:return: None"""nabla_w = [np.zeros(w.shape) for w in self.weights]nabla_b = [np.zeros(b.shape) for b in self.biases]for x, y in mini_batch:#backpropagation funtion is for the sum of derivatedelta_nabla_b, delta_nabla_w = self.backprop(x, y)nabla_w = [nw + dnw for nw, dnw in zip(nabla_w, delta_nabla_w)]nabla_b = [nb + dnb for nb, dnb in zip(nabla_b, delta_nabla_b)]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)]def backprop(self, x, y):"""return a tuple (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]# feedforwardactivation = xactivations = [x] # list to store all the activations, layer by layerzs = [] # list to store all the z vectors, layer by layerfor b, w in zip(self.biases, self.weights):z = np.dot(w, activation) + bzs.append(z)activation = sigmoid(z)activations.append(activation)# backward passdelta = self.cost_derivative(activations[-1], y) * sigmoid_prime(zs[-1])nabla_b[-1] = deltanabla_w[-1] = np.dot(delta, activations[-2].transpose())for l in xrange(2, self.num_layers):z = zs[-l]sp = sigmoid_prime(z)delta = np.dot(self.weights[-l+1].transpose(), delta) * spnabla_b[-l] = deltanabla_w[-l] = np.dot(delta, activations[-l-1].transpose())return (nabla_b, nabla_w)def evaluate(self,test_data):"""network's output is assumed to be the index of whicheverneuron in the final layer has the highest activation:param test_data::return: the number of correct"""test_result = [(np.argmax(self.feedforward(x)), y) for (x, y) in test_data]return sum(int(x == y) for (x, y) in test_result)def cost_derivative(self, output_activation, y):"""return the derivative"""return (output_activation - y)def sigmoid(z):""" the sigmoid function. """return 1.0/(1.0+np.exp(-z))def sigmoid_prime(z):return sigmoid(z)*(1-sigmoid(z))
详细解决方案
神经网络梯度下降算法(gradient descent)笔记
热度:105 发布时间:2023-10-28 22:30:36.0
相关解决方案
- 运用CSS3创建文字颜色渐变(CSS3 Text Gradient)
- MNIST 神经网络:准确率很低
- 第五章 神经网络(待补充)
- 神经网络. epoch, iteration, batchsize相关理解和说明
- 神经网络(深度学习)入门学习
- 2017CS231n李飞飞深度视觉识别笔记(四)——神经网络
- CNN-经典论文阅读-(1998, LeNet-5)Gradient-Based Learning Applied to Document Recognition
- whai is gradient vanishing and exploding ?
- 机器学习:梯度消失(vanishing gradient)与梯度爆炸(exploding gradient)问题
- 模拟退火法、神经网络、遗传算法
- 图像融合:Gradient-directed Composition of Multi-exposure Images
- 图像降噪:Image Smoothing via L0 Gradient Minimization
- GradNet: Gradient-Guided Network for Visual Object Tracking论文解读
- Gradient Descent Provably Optimizes Over-parameterized Neural Networks 论文阅读
- MATLAB 车牌识别程序介绍 SVM、神经网络[毕业设计]
- gradient descent梯度下降算法的优化
- 神经网络梯度下降算法(gradient descent)笔记
- 四、神经网络
- Udacity机器人软件工程师课程笔记(二十六) - 神经网络 - tensorflow - 神经网络构成与优化 - MNIST手写数字识别
- 基于梯度的阈值自由彩色滤波阵列插值 (Gradient based threshold free color filter array interpolation)
- 深度学习之(神经网络)单层感知器(python)(一)
- 神经网络基础学习笔记(二)神经网络
- border渐变色,border-image,linear-gradient
- css3渐变之线性渐变linear-gradient
- PyTorch】RuntimeError: one of the variables needed for gradient computation has been modified by an
- 西瓜书课后题——第五章(神经网络)
- 机器学习中的数学(三):回归(regression)、梯度下降(gradient descent)
- 线性渐变背景 CSS linear-gradient() 函数 background-image: linear-gradient()
- BP 神经网络-从推导到实现(转载b站大神讲解,讲真我没看懂多少)
- 神经网络-激活函数小结