performance optimization

各位看官请注意,本文主要依据《神经网络设计(Neural Network Design)》撰写,想阅读原文请移步Neural Network Design,想继续啃方便面,请继续往下看。

当神经网络结构(神经元多少、层数、连接方式、激活函数)确定之后,对其训练和学习就是对 F(w) (performance function、cost function、energy function,之间的区别与联系可以看lecun的一篇文章,在这里称作性能函数)的优化问题。也就是寻找一组合适的链接权 w ,使得性能函数取得最小值。一般的神经网络方法都是迭代的方法,给定一个 w0,然后通过一定的方法得到一个更新 Δwk ,然后对 w 按照下面的公式进行更新:

wk+1=wk+αkpk

或者

Δwk=wk+1wk=αkpk

其中, pk 表示搜索方向; αk 是学习因子,决定更新的步长。

因此呢,所谓的学习方法,也就是去计算 pk αk 。我们首先介绍一些计算 pk αk 的方法,然后将其结合Backpropagation算法,将其应用到神经网络的学习中去。

performance optimization

对于 F(w) 的优化是基于泰勒展式(Taylor Series Expansion):

F(wk+1)=F(wk+Δwk)F(wk)+F(w)T|w=wkΔwk+12ΔwTk2F(w)|w=wkΔwk

于是乎就出现先了一阶、二阶优化方法(为什么没有三阶、四阶呢?关键是性能函数 F(w) 的三阶、四阶导数存在嘛、好求吗?比如说常用的欧式距离,三阶导数是0呢。。。即使存在,二阶导数已经累死人了,三阶导数你敢求吗?实际应用的神经网络的连接数随随便便都已经数以万计了吧)

首先咱们说说一阶方法。
根据泰勒展式

F(wk+1)F(wk)+F(w)T|w=wkΔwk

F(w)T|w=wk gTk ,其实 gk 也就是梯度。要使得 F(wk+1)<F(wk) ,故而 gTkαkpk<0 。又因为 αk>0 ,所以
gTkpk<0
.
那么任何满足上式的 pk 都可以作为搜索方向,乘以一个足够小的 αk 都可以使得性能函数变小。为了加快收敛速度,自然是选择与 gk 反向作为 pk 的方向了。方便起见呢,就是
pk=gk

这就是传说中的梯度下降算法了。

另外一个需要解决的问题就是 αk 取多少合适?足够小,可以收敛,但是速度太慢了啊。太大又容易产生震荡,甚至直接就发散跑了。。。
那有没有一个范围可以指导一下呢?答案是有。(可以看看Lecun的这篇文章)其实,当 pk 选择之后,就变成了相对于 α 来优化 F(x) 。有很多搜索方法,来选择 αk 。以后再讨论。

现在说说这个二阶方法,也就是所谓的牛顿法,运气好的话,一步就得到解析解。
同样的根据泰勒展式

F(wk+1)=F(wk+Δwk)F(wk)+gTkΔwk+12ΔwTk2F(w)|w=wkΔwk

这个时候呢对 Δwk 求导,并等于零,我们就得到
gk+AkΔwk=0
,其中 Ak=2F(w)|w=wk 也就是hessian 矩阵。
那么我们得到 Δwk=A1gk
所以牛顿法就是 wk+1=wkA1gk .
一般来说呢,牛顿法比最速下降法快,但是呢 A1 不一定存在啊,存在了也不一定收敛啊。。。要命。。。。

那就有必要引入一个新的方法,也就是共轭梯度法。。

未完待续


You don't have to accept slow Ruby or Rails performance. In this comprehensive guide to Ruby optimization, you'll learn how to write faster Ruby code--but that's just the beginning. See exactly what makes Ruby and Rails code slow, and how to fix it. Alex Dymo will guide you through perils of memory and CPU optimization, profiling, measuring, performance testing, garbage collection, and tuning. You'll find that all those "hard" things aren't so difficult after all, and your code will run orders of magnitude faster. This is the first book ever that consolidates all the Ruby performance optimization advice in one place. It's your comprehensive guide to memory optimization, CPU optimization, garbage collector tuning, profiling, measurements, performance testing, and more. You'll go from performance rookie to expert. First, you'll learn the best practices for writing Ruby code that's easy not only on the CPU, but also on memory, and that doesn't trigger the dreaded garbage collector. You'll find out that garbage collection accounts for 80% of slowdowns, and often takes more than 50% of your program's execution time. And you'll discover the bottlenecks in Rails code and learn how selective attribute loading and preloading can mitigate the performance costs of ActiveRecord. As you advance to Ruby performance expert, you'll learn how profile your code, how to make sense out of profiler reports, and how to make optimization decisions based on them. You'll make sure slow code doesn't creep back into your Ruby application by writing performance tests, and you'll learn the right way to benchmark Ruby. And finally, you'll dive into the Ruby interpreter internals to really understand why garbage collection makes Ruby so slow, and how you can tune it up. What You Need: Some version of Ruby. The advice from this book applies to all modern Ruby versions from 1.9 to 2.2. 80% of the material will also be useful for legacy Ruby 1.8 users, and there is 1.8-specific advice as well. Table of Contents Chapter 1. What Makes Ruby Code Fast Chapter 2. Fix Common Performance Problems Chapter 3. Make Rails Faster Chapter 4. Profile Chapter 5. Learn to Optimize with the Profiler Chapter 6. Profile Memory Chapter 7. Measure Chapter 8. Test Performance Chapter 9. Think Outside the Box Chapter 10. Tune Up the Garbage Collector
性能优化在数值密集型代码中至关重要。数值密集型代码是指那些需要大量计算的代码,如数值模拟、图像处理或科学计算等。 首先,为了优化性能,我们可以考虑并行化计算。通过将计算任务分割成多个子任务,然后在多个处理单元上同时执行这些子任务,可以显著提高代码的计算效率。这可以通过使用多线程或并行计算框架来实现。并行化计算可以最大限度地利用计算资源,加快代码的执行速度。 其次,我们可以通过调整算法以提高性能。对于数值密集型代码,算法的效率直接影响到代码的性能。一种常见的优化方法是利用更高效的算法来替代原有的算法。例如,使用更适合特定问题的算法、采用更高效的迭代方法或优化循环结构等。通过选择合适的算法,可以减少计算量并提高代码的执行速度。 此外,我们还可以通过优化内存使用来提高性能。数值密集型代码通常需要大量的内存来存储数据。合理地管理内存,会对代码的性能产生重要影响。使用合适的数据结构,减少内存的分配与释放操作,以及优化内存的访问模式,可以提高代码的数据访问效率,从而提高整体性能。 最后,代码的性能优化也需要综合考虑硬件平台的特点。例如,利用SIMD指令集、GPU加速或专用硬件加速等技术,可以进一步提高性能。因此,在性能优化时,需要深入了解硬件平台的特性,并针对其特点进行相应的优化策略。 综上所述,性能优化数值密集型代码是一项复杂的任务,需要考虑并行化、调整算法、优化内存使用和利用硬件特性等方面。通过综合应用这些策略,我们可以充分挖掘计算资源的潜力,并提高数值密集型代码的执行效率。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值