楼主: ReneeBK
1169 5

breeze:Numerical Processing Library for Scala [推广有奖]

  • 1关注
  • 62粉丝

VIP

学术权威

14%

还不是VIP/贵宾

-

TA的文库  其他...

R资源总汇

Panel Data Analysis

Experimental Design

威望
1
论坛币
49402 个
通用积分
51.7504
学术水平
370 点
热心指数
273 点
信用等级
335 点
经验
57815 点
帖子
4006
精华
21
在线时间
582 小时
注册时间
2005-5-8
最后登录
2023-11-26

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

本帖隐藏的内容

https://github.com/scalanlp/breeze


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Processing Numerical processI Process Library

本帖被以下文库推荐

沙发
ReneeBK 发表于 2016-4-21 10:30:23 |只看作者 |坛友微信交流群
  1. package breeze.linalg
  2. package examples

  3. import breeze.numerics._
  4. import breeze.stats.distributions._
  5. import breeze.benchmark.{MyRunner, BreezeBenchmark}
  6. import breeze.linalg.DenseVector

  7. /**
  8. * Created by dlwh on 8/20/15.
  9. *
  10. * Based on code from Ivan Nikolaev
  11. */
  12. class GaussMixtureBenchmark extends BreezeBenchmark {

  13.   val x = DenseVector(5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0)
  14.   val c = DenseVector(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
  15.   val gamma = 5.0
  16.   private val n: Int = 1000

  17.   def timeGMMVectors(reps:Int) = {
  18.     val denseVectors = IndexedSeq.fill(n)(x)
  19.     (0 until reps).foreach { i =>
  20.       GaussMixtureTransform.samplesTransform(denseVectors, c, gamma)
  21.     }
  22.   }

  23.   def timeGMMMat(reps:Int) = {
  24.     val matrix = DenseMatrix.fill(n, 10)(5.0)
  25.     (0 until reps).foreach { i =>
  26.       GaussMixtureTransform.samplesTransform(matrix, c, gamma)
  27.     }
  28.   }

  29.   def timeGMMMatColMajor(reps:Int) = {
  30.     val matrix = DenseMatrix.fill(10, n)(5.0)
  31.     (0 until reps).foreach { i =>
  32.       GaussMixtureTransform.samplesTransformColMajor(matrix, c, gamma)
  33.     }
  34.   }

  35.   def timeCenterMat(reps:Int) = {
  36.     val matrix = DenseMatrix.fill(n, 10)(5.0)
  37.     (0 until reps).foreach { i =>
  38.       matrix(*, ::) - c
  39.     }
  40.   }

  41.   def timeCenterMatColMajor(reps:Int) = {
  42.     val matrix = DenseMatrix.fill(10, n)(5.0)
  43.     (0 until reps).foreach { i =>
  44.       matrix(::, *) - c
  45.     }
  46.   }

  47.   def timeCenterVector(reps:Int) = {
  48.     val denseVectors = IndexedSeq.fill(n)(x)
  49.     (0 until reps).foreach { i =>
  50.       denseVectors.foreach(_ - c)
  51.     }
  52.   }

  53. }

  54. object GaussMixtureTransform {
  55.   def sampleTransform(sample: DenseVector[Double], centers: DenseVector[Double], gamma: Double): Double = {
  56.     val diff: DenseVector[Double] = sample - centers
  57.     exp(-gamma * (diff dot diff))
  58.   }

  59.   def samplesTransform(samples: Iterable[DenseVector[Double]], centers: DenseVector[Double], gamma: Double): Double = {
  60.     samples.map((sample: DenseVector[Double]) => sampleTransform(sample, centers, gamma)).sum
  61.   }

  62.   def samplesTransform(samples: DenseMatrix[Double], centers: DenseVector[Double], gamma: Double): Double = {
  63.     val diff: DenseMatrix[Double] = samples(*, ::) - centers
  64.     val prod = diff :*= diff
  65.     val sum1:DenseVector[Double] = sum(prod, Axis._1) *= (-gamma)
  66.     val exped = exp(sum1)
  67.     val sum2 = sum(exped)
  68.     sum2


  69. //    sum(exp(sum(diff :*= diff, Axis._1) *= (-gamma)))
  70.   }

  71.   def samplesTransformColMajor(samples: DenseMatrix[Double], centers: DenseVector[Double], gamma: Double): Double = {
  72.     val diff: DenseMatrix[Double] = samples(::, *) - centers
  73.     val prod = diff :*= diff
  74.     val sum1 = sum(prod, Axis._0) *= (-gamma)
  75.     val exped = exp(sum1)
  76.     val sum2 = sum(exped)
  77.     sum2


  78.     //    sum(exp(sum(diff :*= diff, Axis._1) *= (-gamma)))
  79.   }
  80. }


  81. object GaussMixtureBenchmark extends MyRunner(classOf[GaussMixtureBenchmark])
复制代码

使用道具

藤椅
ReneeBK 发表于 2016-4-21 10:31:38 |只看作者 |坛友微信交流群
  1. package breeze.numerics

  2. import breeze.benchmark.{MyRunner, BreezeBenchmark}
  3. import breeze.linalg.{softmax, DenseVector}
  4. import breeze.stats.distributions.Rand
  5. import spire.syntax.cfor._

  6. /**
  7. * Created by dlwh on 10/3/15.
  8. */
  9. class SoftmaxBenchmark extends BreezeBenchmark {
  10.   val dv = DenseVector.rand(5000, Rand.uniform.map(_.toFloat))

  11.   def timeSoftmaxFloat(reps: Int) = {
  12.     var sum = 0.0
  13.     cforRange(0 until reps) { _ =>
  14.       sum += softmax(dv)
  15.     }

  16.     sum
  17.   }

  18. }

  19. object SoftmaxBenchmark extends MyRunner(classOf[SoftmaxBenchmark])
复制代码

使用道具

板凳
ReneeBK 发表于 2016-4-21 10:32:09 |只看作者 |坛友微信交流群
  1. package breeze.polynomial

  2. import breeze.benchmark._
  3. import breeze.linalg.BuildsRandomVectors
  4. import breeze.stats.distributions._

  5. import spire.math._
  6. import spire.math.poly._
  7. import spire.implicits._

  8. object DensePolynomialBenchmark extends MyRunner(classOf[DensePolynomialBenchmark])

  9. class DensePolynomialBenchmark extends BreezeBenchmark with BuildsRandomVectors {

  10.   def randomPoly(order: Int) = {
  11.     val uniform = Uniform(0,1)
  12.     val array = new Array[Double](order)
  13.     var i=0
  14.     while (i < order) {
  15.       array(i) = uniform.draw()
  16.       i += 1
  17.     }
  18.     Polynomial.dense(array)
  19.   }

  20.   def timePolyOnDenseVector(reps: Int) = runWith2(reps, { randomPoly(10) }, {randomArray(1024*4) })( (poly, arr) => {
  21.     poly(arr)
  22.   })

  23.   def timePolyOnDenseMatrix(reps: Int) = runWith2(reps, { randomPoly(10) }, {randomMatrix(256,256) })( (poly, arr) => {
  24.     poly(arr)
  25.   })

  26. }
复制代码

使用道具

报纸
ReneeBK 发表于 2016-4-21 10:32:49 |只看作者 |坛友微信交流群
  1. package breeze.stats.mcmc

  2. import breeze.benchmark._
  3. import breeze.stats.distributions._
  4. import breeze.stats.mcmc._

  5. import spire.implicits.cfor

  6. object MetropolisHastingsRunner extends MyRunner(classOf[MetropolisHastingsBenchmark])

  7. class MetropolisHastingsBenchmark extends BreezeBenchmark {

  8.   val burnIn = 1024*1024
  9.   val dropCount = 25
  10.   val numSamples = 1024*1024
  11.   val bufferSize = 1024*32

  12.   val epsilon = 1e-8
  13.   def likelihood(x: Double) = 2*math.log1p(1+epsilon-x) + 3*math.log1p(x*x*x+epsilon) //Epsilon is present to avoid throwing exceptions in the unlikely event either 0 or 1 is sampled

  14.   def gaussianJump(x: Double) = Gaussian(x, 1)
  15.   def gaussianJumpLogProb(start: Double, end: Double) = 0.0 // It would actually be this, but due to symmetry why bother? -math.pow(start-end,2)/2.0

  16.   def pullAllSamples(m: Rand[Double]) = {
  17.     var result = 0.0
  18.     cfor(0)(i => i<numSamples, i => i+1)(i => {
  19.       result = m.draw()
  20.     })
  21.     result
  22.   }

  23.   def pullAllSamplesWithWork(m: Rand[Double]) = {
  24.     var result = 0.0
  25.     cfor(0)(i => i<numSamples/4, i => i+1)(i => {
  26.       val x = m.draw()
  27.       cfor(0)(j => j < 400, j => j+1)(j => {
  28.         result += math.log(math.exp(x)) / (1+x*x)
  29.       })
  30.     })
  31.     result
  32.   }

  33.   def timeMarkovChain(reps: Int) = run(reps) {
  34.     val m = MarkovChain.metropolisHastings(0.5, gaussianJump _)(likelihood _)
  35.     pullAllSamples(m)
  36.   }

  37.   def timeMarkovChainEquiv(reps: Int) = run(reps) {
  38.     val m = ArbitraryMetropolisHastings(likelihood _, gaussianJump _, gaussianJumpLogProb _, 0.5, burnIn=0, dropCount=0)
  39.     pullAllSamples(m)
  40.   }

  41.   def timeMetropolisHastings(reps: Int) = run(reps) {
  42.     val m = ArbitraryMetropolisHastings(likelihood _, (_:Double) =>  Uniform(0,1), gaussianJumpLogProb _, 0.5, burnIn=burnIn, dropCount=dropCount)
  43.     pullAllSamples(m)
  44.   }

  45.   def timeMetropolisHastingsWithWork(reps: Int) = run(reps) {
  46.     val m = ArbitraryMetropolisHastings(likelihood _, (_:Double) =>  Uniform(0,1), gaussianJumpLogProb _, 0.5, burnIn=0, dropCount=dropCount)
  47.     pullAllSamplesWithWork(m)
  48.   }

  49.   def timeThreadedBufferedWithWork(reps: Int) = run(reps) {
  50.     val wrapped = ArbitraryMetropolisHastings(likelihood _, (_:Double) =>  Uniform(0,1), gaussianJumpLogProb _, 0.5, burnIn=0, dropCount=dropCount)
  51.     val m = ThreadedBufferedRand(wrapped, bufferSize=bufferSize)
  52.     val result = pullAllSamplesWithWork(m)
  53.     m.stop()
  54.     result
  55.   }
  56. }
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-20 11:02