近来学习深度学习和神经网络,此篇博客总结自Neural networks and deep learning CHAP1


Perceptrons

输入x1, x2…是二进制输入,产生一个二进制输出。
感知器可以用作一种决策器。
感知器可以当作NAND门。

   output= 0 if w⋅x+b≤01 if w⋅x+b>0

权重或者偏差(阈值)轻微的变动会引起结果的很大的变化。
假设我们采用感知器(Perceptrons)网络中的所有权重和偏差,并将它们乘以正常数c,c> 0。神经网络的行为不会更改。


Sigmoid neurons

输入x1, x2…在01之间,产生一个在01之间的输出。
sigmoid函数:σ(z)≡1/1+e^(−z), z ≡ wx + b。

output = 1/(1+exp(−∑jwjxj−b))
Δoutput ≈ (∑j∂output/∂wj)Δwj+(∂output/∂b)Δb

权重或者偏差(阈值)轻微的变动会引起结果较小的变化。
假设我们采用Sigmoid神经元网络中的所有权重和偏差,并将它们乘以正常数c,c> 0。当c→∞时,神经网络的行为就是感知器网络的行为。


The architecture of neural networks

输入层、输入神经元
输出层、输出神经元
隐层、隐层神经元
前馈神经网络:前一层的输出作用于后一层的输入(没有回路)
递归神经网络
输入和输出神经元的设计通常比较简单。比如一个64x64的灰度图像是否是“9”,则输入神经元有4096个,输出神经元只有一个


A simple network to classify handwritten digits

启发式方法识别数字(所以直接识别0-9更容易而不是以4位二进制的方式来识别)


Learning with gradient descent

成本函数:C(w,b) ≡ 1/2n(∑x∥y(x)−a∥^2),a是输出向量
成本函数值越接近0,说明输出值和拟合的输出值越接近
ΔC ≈ (∂C/∂v1)Δv1 + (∂C/∂v2)Δv2, Δv ≡ (Δv1,…,Δvm)^T, 梯度向量∇C ≡ (∂C/∂v1,…,∂C/∂vm)^T
所以 ΔC ≈ ∇C⋅Δv,令Δv = −η∇C,则 ΔC ≈ −η∇C⋅∇C = −η∥∇C∥^2,所以ΔC≤0,则梯度下降ΔC
更新规则:通过 v→v′=v−η∇C 来更新v(位置)值,η:学习率
可以证明,令 ∥Δv∥=ϵ 当 Δv = −η∇C 时,C下降的最大
利用梯度下降,找出最合适的w和b使成本函数值最小:使用权值和偏差代替上式的v,也就是说v有两个分量w、b,那么更新规则变为:

wk→w′k=wk−η∂C/∂wk
bl→b′l=bl−η∂C/∂bl

请注意,此成本函数的形式为C = 1/n(ΣxCx),也就是说,它是针对个别培训样例的平均成本为 Cx ≡ ∥y(x)-a∥^2。实际上,为了计算梯度∇C,我们需要分别为每个训练输入x计算梯度∇Cx,然后对它们求平均值,∇C=1/nΣx∇Cx。那么当训练的输入非常多的时候,会造成神经网络的学习速度低下

随机梯度下降:其思想是通过计算随机选择的训练输入的小样本的∇Cx来估计梯度∇C。通过对这个小样本进行平均,我们可以快速得到真实梯度∇C的良好估计,这有助于加快梯度下降,从而学习。即从所有样本中取一部分样本(mini-batch)进行输入,并近似的认为
[(∑m,j=1)∇CXj]/m ≈ (∑x∇Cx)/n = ∇C
我们可以通过计算随机选择的小批量的梯度来估计总梯度。

wk→w′k=wk−η/m(∑j∂CXj/∂wk)
bl→b′l=bl−η/m(∑j∂CXj/∂bl)

然后选择另一个随机选择的小批量进行训练。直到我们用完样本的输入,就说完成了一轮(epochs)训练。那时我们重新开始一论新的训练

增量学习:将小批量的大小设定为1。那么输入一个x,就更新权重和偏差。然后重新选择另一个输入,更新权重和偏差。

Implementing our network to classify digits

"""
network.py
~~~~~~~~~~A module to implement the stochastic gradient descent learning
algorithm for a feedforward neural network.  Gradients are calculated
using backpropagation.  Note that I have focused on making the code
simple, easily readable, and easily modifiable.  It is not optimized,
and omits many desirable features.
"""#### Libraries
# Standard library
import random# Third-party libraries
import numpy as npclass Network(object):def __init__(self, sizes):"""The list ``sizes`` contains the number of neurons in therespective layers of the network.  For example, if the listwas [2, 3, 1] then it would be a three-layer network, with thefirst layer containing 2 neurons, the second layer 3 neurons,and the third layer 1 neuron.  The biases and weights for thenetwork are initialized randomly, using a Gaussiandistribution with mean 0, and variance 1.  Note that the firstlayer is assumed to be an input layer, and by convention wewon't set any biases for those neurons, since biases are onlyever used in computing the outputs from later layers."""self.num_layers = len(sizes)self.sizes = sizesself.biases = [np.random.randn(y, 1) for y in sizes[1:]]self.weights = [np.random.randn(y, x)for x, y in zip(sizes[:-1], sizes[1:])]def feedforward(self, a):"""Return the output of the network if ``a`` is input."""for b, w in zip(self.biases, self.weights):a = sigmoid(np.dot(w, a)+b)return adef SGD(self, training_data, epochs, mini_batch_size, eta,test_data=None):"""Train the neural network using mini-batch stochasticgradient descent.  The ``training_data`` is a list of tuples``(x, y)`` representing the training inputs and the desiredoutputs.  The other non-optional parameters areself-explanatory.  If ``test_data`` is provided then thenetwork will be evaluated against the test data after eachepoch, and partial progress printed out.  This is useful fortracking progress, but slows things down substantially."""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:print "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):"""Update the network's weights and biases by applyinggradient descent using backpropagation to a single mini batch.The ``mini_batch`` is a list of tuples ``(x, y)``, and ``eta``is the learning rate."""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)]self.weights = [w-(eta/len(mini_batch))*nwfor w, nw in zip(self.weights, nabla_w)]self.biases = [b-(eta/len(mini_batch))*nbfor b, nb in zip(self.biases, nabla_b)]def backprop(self, x, y):"""Return a tuple ``(nabla_b, nabla_w)`` representing thegradient for the cost function C_x.  ``nabla_b`` and``nabla_w`` are layer-by-layer lists of numpy arrays, similarto ``self.biases`` and ``self.weights``."""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())# Note that the variable l in the loop below is used a little# differently to the notation in Chapter 2 of the book.  Here,# l = 1 means the last layer of neurons, l = 2 is the# second-last layer, and so on.  It's a renumbering of the# scheme in the book, used here to take advantage of the fact# that Python can use negative indices in lists.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):"""Return the number of test inputs for which the neuralnetwork outputs the correct result. Note that the neuralnetwork's output is assumed to be the index of whicheverneuron in the final layer has the highest activation."""test_results = [(np.argmax(self.feedforward(x)), y)for (x, y) in test_data]return sum(int(x == y) for (x, y) in test_results)def cost_derivative(self, output_activations, y):"""Return the vector of partial derivatives \partial C_x /\partial a for the output activations."""return (output_activations-y)#### Miscellaneous functions
def sigmoid(z):"""The sigmoid function."""return 1.0/(1.0+np.exp(-z))def sigmoid_prime(z):"""Derivative of the sigmoid function."""return sigmoid(z)*(1-sigmoid(z))
"""
mnist_loader
~~~~~~~~~~~~A library to load the MNIST image data.  For details of the data
structures that are returned, see the doc strings for ``load_data``
and ``load_data_wrapper``.  In practice, ``load_data_wrapper`` is the
function usually called by our neural network code.
"""#### Libraries
# Standard library
import cPickle
import gzip# Third-party libraries
import numpy as npdef load_data():"""Return the MNIST data as a tuple containing the training data,the validation data, and the test data.The ``training_data`` is returned as a tuple with two entries.The first entry contains the actual training images.  This is anumpy ndarray with 50,000 entries.  Each entry is, in turn, anumpy ndarray with 784 values, representing the 28 * 28 = 784pixels in a single MNIST image.The second entry in the ``training_data`` tuple is a numpy ndarraycontaining 50,000 entries.  Those entries are just the digitvalues (0...9) for the corresponding images contained in the firstentry of the tuple.The ``validation_data`` and ``test_data`` are similar, excepteach contains only 10,000 images.This is a nice data format, but for use in neural networks it'shelpful to modify the format of the ``training_data`` a little.That's done in the wrapper function ``load_data_wrapper()``, seebelow."""f = gzip.open('../data/mnist.pkl.gz', 'rb')training_data, validation_data, test_data = cPickle.load(f)f.close()return (training_data, validation_data, test_data)def load_data_wrapper():"""Return a tuple containing ``(training_data, validation_data,test_data)``. Based on ``load_data``, but the format is moreconvenient for use in our implementation of neural networks.In particular, ``training_data`` is a list containing 50,0002-tuples ``(x, y)``.  ``x`` is a 784-dimensional numpy.ndarraycontaining the input image.  ``y`` is a 10-dimensionalnumpy.ndarray representing the unit vector corresponding to thecorrect digit for ``x``.``validation_data`` and ``test_data`` are lists containing 10,0002-tuples ``(x, y)``.  In each case, ``x`` is a 784-dimensionalnumpy.ndarry containing the input image, and ``y`` is thecorresponding classification, i.e., the digit values (integers)corresponding to ``x``.Obviously, this means we're using slightly different formats forthe training data and the validation / test data.  These formatsturn out to be the most convenient for use in our neural networkcode."""tr_d, va_d, te_d = load_data()training_inputs = [np.reshape(x, (784, 1)) for x in tr_d[0]]training_results = [vectorized_result(y) for y in tr_d[1]]training_data = zip(training_inputs, training_results)validation_inputs = [np.reshape(x, (784, 1)) for x in va_d[0]]validation_data = zip(validation_inputs, va_d[1])test_inputs = [np.reshape(x, (784, 1)) for x in te_d[0]]test_data = zip(test_inputs, te_d[1])return (training_data, validation_data, test_data)def vectorized_result(j):"""Return a 10-dimensional unit vector with a 1.0 in the jthposition and zeroes elsewhere.  This is used to convert a digit(0...9) into a corresponding desired output from the neuralnetwork."""e = np.zeros((10, 1))e[j] = 1.0return e

Deep Learning(1)相关推荐

  1. 几何深度学习(Geometric Deep Learning)技术

    几何深度学习(Geometric Deep Learning)技术 几何深度学习综述 从论文Geometric Deep Learning: Grids, Groups, Graphs, Geodes ...

  2. 深度学习编译器综述The Deep Learning Compiler

    深度学习编译器综述The Deep Learning Compiler The Deep Learning Compiler: A Comprehensive Survey 参考文献: https:/ ...

  3. 全文翻译(全文合集):TVM: An Automated End-to-End Optimizing Compiler for Deep Learning

    全文翻译(全文合集):TVM: An Automated End-to-End Optimizing Compiler for Deep Learning 摘要 人们越来越需要将机器学习应用到各种各样 ...

  4. 全文翻译(二): TVM: An Automated End-to-End Optimizing Compiler for Deep Learning

    全文翻译(二): TVM: An Automated End-to-End Optimizing Compiler for Deep Learning 3.优化计算图 计算图是在DL框架中表示程序的常 ...

  5. 全文翻译(一):TVM: An Automated End-to-End Optimizing Compiler for Deep Learning

    全文翻译(一):TVM: An Automated End-to-End Optimizing Compiler for Deep Learning 摘要 人们越来越需要将机器学习应用到各种各样的硬件 ...

  6. TVM优化Deep Learning GPU算子

    TVM优化Deep Learning GPU算子 高效的深度学习算子是深度学习系统的核心.通常,这些算子很难优化,需要HPC专家付出巨大的努力. 端到端张量IR / DSL堆栈TVM使这一过程变得更加 ...

  7. 深度学习编译与优化Deep Learning Compiler and Optimizer

    深度学习编译与优化Deep Learning Compiler and Optimizer

  8. Deep Learning部署TVM Golang运行时Runtime

    Deep Learning部署TVM Golang运行时Runtime 介绍 TVM是一个开放式深度学习编译器堆栈,用于编译从不同框架到CPU,GPU或专用加速器的各种深度学习模型.TVM支持来自Te ...

  9. 深度学习加速器堆栈Deep Learning Accelerator Stack

    深度学习加速器堆栈Deep Learning Accelerator Stack 通用张量加速器(VTA)是一种开放的.通用的.可定制的深度学习加速器,具有完整的基于TVM的编译器堆栈.设计了VTA来 ...

  10. Deep learning的一些教程 (转载)

    几个不错的深度学习教程,基本都有视频和演讲稿.附两篇综述文章和一副漫画.还有一些以后补充. Jeff Dean 2013 @ Stanford http://i.stanford.edu/infose ...

最新文章

  1. Python 笔试集:什么时候 i = i + 1 并不等于 i += 1?
  2. Android:ListView常见错位之CheckBox错位
  3. 计算机安全可靠替代工程,基于安全可靠软硬件的党政军OA系统的整系统优化方法与研究-计算机技术专业论文.docx...
  4. JS去除字符串去除最后的逗号
  5. 微信发ascii_微信公众平台开发(107) 分享到朋友圈和发送给好友
  6. SSL的作用与目前主流的使用场景介绍
  7. R语言和Rstudio的介绍和安装
  8. 企业间数据竞争规则研究
  9. PCB多层板的一些资料
  10. 韩信点兵python程序_韩信点兵(C语言代码)
  11. 启动Activity的流程(Launcher中点击图标启动)
  12. 解决印象笔记无法同步的问题
  13. ios 页面即将消失_20个即将推出的页面介绍及如何正确处理它们
  14. 虚幻蓝图数据传递_数据产品的战略蓝图
  15. 解决org.junit.runners.model.InvalidTestClassError: Invalid test class ‘xxx‘ 1. No runnable methods
  16. R语言使用lm函数构建回归模型、使用MASS包的boxcox函数寻找最佳幂变换提高模型拟合度、可视化boxcox曲线并获取最佳lambda值
  17. 将项目提交到码云时,异常: remote: [31mIncorrect username or password ( access token )[0m
  18. 使用jQuery,写一个简单的轮播图,实现切换功能!
  19. Yumiko Kayukawa 禅·波普·自然界
  20. android首字母检索音乐,新增首字母检索 QQ音乐Android版更新

热门文章

  1. 给天空“染个色”?摄影后期时进行的一些思考
  2. 【面试高频题】CMS垃圾收集器是如何工作的?
  3. 数学笔记15——微积分第二基本定理
  4. 机器人学习--ROS学习入门
  5. [Ubuntu 18.04]公共DNS设置[/etc/resolv.conf]
  6. Yii框架下使用redis做缓存,读写分离
  7. k4-使用百度账号-登陆本系统-操作指南
  8. 干货分享 | 分子对接与分子动力学模拟在药物研发中的应用
  9. 联想T430 安装msata接口的SSD固态硬盘
  10. 玩转Luat 进阶篇②——远程升级功能原理详解