breeze.numerics.pow Scala Examples

The following examples show how to use breeze.numerics.pow. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: RelationWithItemToItem.scala    From AI   with Apache License 2.0 5 votes vote down vote up
package com.bigchange.mllib

import breeze.numerics.{sqrt, pow}
import org.apache.spark.{HashPartitioner, SparkConf, SparkContext}



object RelationWithItemToItem {

  def main(args: Array[String]) {

    val sc = new SparkContext(new SparkConf()
      .setAppName("Item to Item")
      .setMaster("local"))
    // announce the top number of items to get
    val topK = 2

    val userItem = sc.textFile("/rating.dat")
      .map(_.split("\t")).map(x =>(x(0),x(1),x(2))).distinct().cache()
    // cal item -> (user,rating) and item -> sqrt(ratings)
    val itemUser = userItem.map(x => (x._2,(x._1,x._3.toDouble))).partitionBy(new HashPartitioner(20))
    // sqrt : 规整化 rating 的值
    val itemPowSqrt = userItem.map(x => (x._2,pow(x._3.toDouble,2.0))).reduceByKey(_+_).mapValues(x => sqrt(x))
    // cal item -> ((user,rating),sqrt(ratings)) => user -> (item,rating/sqrt(ratings))
    val userItemSqrt = itemUser.join(itemPowSqrt).map(x =>{
      val item = x._1
      val sqrtRatings = x._2._2
      val user = x._2._1._1
      val rating = x._2._1._2
      (user,(item,rating / sqrtRatings))
    })
    // cal the relation of item to item in user dimension => get the score of item to item which connection the relation of items
    val itemToItem = userItemSqrt.join(userItemSqrt).map(x =>{
      val item1 = x._2._1._1
      val rating1 = x._2._1._2
      val item2 = x._2._2._1
      val rating2 = x._2._2._2
      val score = rating1 * rating2
      if(item1 == item2){
        ((item1,item2),-1.0)
      }else{
        ((item1,item2),score)
      }
    })

    itemToItem.reduceByKey(_+_).map(x => (x._1._1,(x._1._2,x._2))).groupByKey().foreach(x => {
      val sourceItem = x._1
      val topItem = x._2.toList.filter(_._2 > 0).sortWith(_._2 > _._2).take(topK)
      println(s"item = $sourceItem,topK relative item list:$topItem")
    })
    sc.stop()
  }

} 
Example 2
Source File: StandardizedMomentSummarizer.scala    From flint   with Apache License 2.0 5 votes vote down vote up
package com.twosigma.flint.timeseries.summarize.summarizer

import breeze.numerics.pow
import com.twosigma.flint.rdd.function.summarize.summarizer.subtractable.{ NthCentralMomentOutput, NthCentralMomentState, NthCentralMomentSummarizer => NthCentralMomentSum }
import com.twosigma.flint.timeseries.summarize.summarizer.StandardizedMomentSummarizerType.StandardizedMomentType
import com.twosigma.flint.timeseries.row.Schema
import com.twosigma.flint.timeseries.summarize.ColumnList.Sequence
import com.twosigma.flint.timeseries.summarize._
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.types._


object StandardizedMomentSummarizerType extends Enumeration {
  type StandardizedMomentType = Value
  val Skewness = Value("skewness")
  val Kurtosis = Value("kurtosis")
}

case class StandardizedMomentSummarizerFactory(column: String, standardizedMomentType: StandardizedMomentType)
  extends BaseSummarizerFactory(column) {
  override def apply(inputSchema: StructType): StandardizedMomentSummarizer = {
    val moment = standardizedMomentType match {
      case StandardizedMomentSummarizerType.Skewness => 3
      case StandardizedMomentSummarizerType.Kurtosis => 4
    }
    StandardizedMomentSummarizer(
      inputSchema,
      prefixOpt,
      requiredColumns,
      moment,
      standardizedMomentType,
      standardizedMomentType.toString
    )
  }
}

case class StandardizedMomentSummarizer(
  override val inputSchema: StructType,
  override val prefixOpt: Option[String],
  requiredColumns: ColumnList,
  moment: Integer,
  standardizedMomentType: StandardizedMomentType,
  outputColumnName: String
) extends LeftSubtractableSummarizer with FilterNullInput {
  private val Sequence(Seq(column)) = requiredColumns
  private val columnIndex = inputSchema.fieldIndex(column)
  private final val valueExtractor = asDoubleExtractor(inputSchema(columnIndex).dataType, columnIndex)

  override type T = Double
  override type U = NthCentralMomentState
  override type V = NthCentralMomentOutput

  override val summarizer = NthCentralMomentSum(moment)
  override val schema = Schema.of(s"${column}_$outputColumnName" -> DoubleType)

  override def toT(r: InternalRow): T = valueExtractor(r)

  override def fromV(v: V): InternalRow = {
    val variance = v.nthCentralMoment(2)
    val standardizedMoment = v.nthCentralMoment(moment) / pow(variance, moment / 2.0)
    standardizedMomentType match {
      case StandardizedMomentSummarizerType.Kurtosis =>
        InternalRow(standardizedMoment - 3.0)
      case StandardizedMomentSummarizerType.Skewness =>
        InternalRow(standardizedMoment)
    }
  }
} 
Example 3
Source File: GPModelTest.scala    From automl   with Apache License 2.0 5 votes vote down vote up
package com.tencent.angel.spark.automl

import breeze.linalg.{DenseMatrix, DenseVector}
import breeze.numerics.{cos, pow}
import com.tencent.angel.spark.automl.tuner.kernel.Matern5Iso
import com.tencent.angel.spark.automl.tuner.model.GPModel
import org.scalatest.FunSuite

class GPModelTest extends FunSuite {

  test("test_linear") {
    // Test linear: y=2*x
    val X = DenseMatrix((1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)).t
    val y = 2.0 * DenseVector(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)
    val z = DenseMatrix((2.5, 4.5, 6.5, 8.5, 10.0, 12.0)).t
    val truePredZ = 2.0 * DenseVector(2.5, 4.5, 6.5, 8.5, 10.0, 12.0)

    val covFunc = Matern5Iso()
    val initCovParams = DenseVector(1.0, 1.0)
    val initNoiseStdDev = 0.01

    val gpModel = GPModel(covFunc, initCovParams, initNoiseStdDev)
    gpModel.fit(X, y)

    println("Fitted covariance function params:")
    println(gpModel.covParams)
    println("Fitted noiseStdDev:")
    println(gpModel.noiseStdDev)
    println("\n")

    val prediction = gpModel.predict(z)
    println("Mean and Var:")
    println(prediction)
    println("True value:")
    println(truePredZ)
  }

  test("test_cosine") {
    // Test no_linear: y=cos(x)+1
    val X = DenseMatrix((1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)).t
    val y = cos(DenseVector(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)) + 1.0
    val z = DenseMatrix((2.5, 4.5, 6.5, 8.5, 10.0, 12.0)).t
    val truePredZ = cos(DenseVector(2.5, 4.5, 6.5, 8.5, 10.0, 12.0)) + 1.01

    val covFunc = Matern5Iso()
    val initCovParams = DenseVector(1.0, 1.0)
    val initNoiseStdDev = 0.01

    val gpModel = GPModel(covFunc, initCovParams, initNoiseStdDev)
    gpModel.fit(X, y)

    println("Fitted covariance function params:")
    println(gpModel.covParams)
    println("Fitted noiseStdDev:")
    println(gpModel.noiseStdDev)
    println("\n")

    val prediction = gpModel.predict(z)
    println("Mean and Var:")
    println(prediction)
    println("True value:")
    println(truePredZ)
  }

  test("testSquare") {
    // Test no_linear: y=x^2
    val X = DenseMatrix((1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0)).t
    val y = DenseVector(1.0, 4.0, 9.0, 16.0, 25.0, 36.0, 49.0, 64.0, 81.0)
    val z = DenseMatrix((2.5, 4.5, 6.5, 8.5, 10.0, 12.0)).t
    val truePredZ = pow(z, 2)

    val covFunc = Matern5Iso()
    val initCovParams = DenseVector(1.0, 1.0)
    val initNoiseStdDev = 0.01

    val gpModel = GPModel(covFunc, initCovParams, initNoiseStdDev)
    gpModel.fit(X, y)

    println("Fitted covariance function params:")
    println(gpModel.covParams)
    println("Fitted noiseStdDev:")
    println(gpModel.noiseStdDev)
    println("\n")

    val prediction = gpModel.predict(z)
    println("Mean and Var:")
    println(prediction)
    println("True value:")
    println(truePredZ)
  }
} 
Example 4
Source File: LCCSLiu.scala    From seqspark   with Apache License 2.0 5 votes vote down vote up
package org.dizhang.seqspark.stat


import breeze.linalg.{sum, DenseVector => DV}
import breeze.numerics.pow
import org.dizhang.seqspark.stat.LCCSLiu._
import org.dizhang.seqspark.stat.{LinearCombinationChiSquare => LCCS}
import org.dizhang.seqspark.util.General.RichDouble
import org.slf4j.LoggerFactory


object LCCSLiu {

  val logger = LoggerFactory.getLogger(getClass)

  case class CDFLiu(pvalue: Double, ifault: Int) extends LCCS.CDF {
    def trace = Array(0.0)
    override def toString = "Pvalue:   %10f".format(pvalue)
  }

  trait CentralOneDF extends LinearCombinationChiSquare {
    def degreeOfFreedom = DV.ones[Double](size)
    def nonCentrality = DV.zeros[Double](size)
  }

  trait Old extends LCCSLiu {
    def a = if (squareOfS1LargerThanS2) 1.0/(s1 - (s1.square - s2).sqrt) else 1.0/s1
    def df = if (squareOfS1LargerThanS2) a.square - 2 * delta else c2.cube/c3.square
  }
  trait New extends LCCSLiu {
    def a = if (squareOfS1LargerThanS2) 1.0/(s1 - (s1.square - s2).sqrt) else 1.0/s2.sqrt
    def df = if (squareOfS1LargerThanS2) a.square - 2 * delta else 1.0/s2
  }
  @SerialVersionUID(7778550101L)
  case class Simple(lambda: DV[Double]) extends LCCSLiu with CentralOneDF with Old {
    val c1 = ck(1)
    val c2 = ck(2)
    val c3 = ck(3)
    val c4 = ck(4)
  }
  @SerialVersionUID(7778550201L)
  case class Modified(lambda: DV[Double]) extends LCCSLiu with CentralOneDF with New {
    val c1 = ck(1)
    val c2 = ck(2)
    val c3 = ck(3)
    val c4 = ck(4)
  }
  case class SimpleMoments(cs: IndexedSeq[Double]) extends LCCSLiu with CentralOneDF with Old {
    def lambda = DV.zeros[Double](0)
    override val c1 = cs(0)
    override val c2 = cs(1)
    override val c3 = cs(2)
    override val c4 = cs(3)
  }
  case class ModifiedMoments(cs: IndexedSeq[Double]) extends LCCSLiu with CentralOneDF with New {
    def lambda = DV.zeros[Double](0)
    override val c1 = cs(0)
    override val c2 = cs(1)
    override val c3 = cs(2)
    override val c4 = cs(3)
  }
}
@SerialVersionUID(7778550001L)
trait LCCSLiu extends LinearCombinationChiSquare {

  def ck(k: Int): Double = {
    val lbk = pow(lambda, k)
    (lbk dot degreeOfFreedom) + k * (lbk dot nonCentrality)
  }
  def c1:Double
  def c2:Double
  def c3:Double
  def c4:Double
  def s1:Double = c3/c2.cube.sqrt
  def s2:Double = c4/c2.square
  def muQ:Double = c1
  def sigmaQ:Double = (2 * c2).sqrt
  protected lazy val squareOfS1LargerThanS2: Boolean = {
    s1.square > s2
  }
  def a: Double
  def delta:Double = if (squareOfS1LargerThanS2) s1 * a.cube - a.square else 0.0
  def df: Double
  def sigmaX:Double = 2.0.sqrt * a
  def muX:Double = df + delta

  def cdf(cutoff: Double): CDFLiu = {
    //logger.debug(s"muX: $muX sigmaX: $sigmaX muQ: $muQ sigmaQ: $sigmaQ df: $df delta: $delta ")
    val nccs = NonCentralChiSquare(df + delta, delta)
    val norm =  (cutoff - muQ)/sigmaQ
    val norm1 = norm * sigmaX + muX
    val pv = nccs.cdf(norm1)
    if (pv >= 0.0 && pv <= 1.0) {
      CDFLiu(pv, 0)
    } else {
      CDFLiu(pv, 1)
    }
  }
} 
Example 5
Source File: Emmax.scala    From seqspark   with Apache License 2.0 5 votes vote down vote up
package org.dizhang.seqspark.stat
import breeze.linalg.{DenseMatrix=>BDM,CSCMatrix=>BSM,DenseVector=>BDV,SparseVector=>BSV, _}
import breeze.numerics.{exp, pow, log}
import org.apache.spark.mllib.linalg.distributed.{RowMatrix => RM}



trait Emmax {

}

object Emmax {

  def eigenH(K: BDM[Double]): (BDV[Double], BDM[Double]) = {
    val res = eigSym(K)
    (res.eigenvalues - 1.0, res.eigenvectors)
  }

  def eigenSHS(K: BDM[Double], X: BDM[Double]): (BDV[Double], BDM[Double]) = {
    val n = X.rows
    val q = X.cols
    val Xt = X.t
    val S = BDM.eye[Double](n) - X * inv(Xt * X) * Xt
    val res = eigSym(S * (K + BDM.eye[Double](n)) * S)

    (res.eigenvalues(0 until (n-q)) - 1.0, res.eigenvectors(::, 0 until (n-q)))
  }

  def mle(y: BDV[Double], X: BDM[Double], K: BDM[Double],
          ngrids :Int =100, llim: Int = -10, ulim: Int =10, esp: Double = 1e-10) = {
    val n: Int = y.length
    val t = K.rows
    val q = X.cols
    val eigH = eigenH(K)
    val eigSHS = eigenSHS(K, X)

    val etas: BDV[Double] = (y.t * eigSHS._2).t
    val logDelta: BDV[Double] = BDV((0 to ngrids).map(x => x/ngrids.toDouble * (ulim - llim) + llim): _*)
    val m = logDelta.length
    val delta: BDV[Double] = exp(logDelta)
    val lambdas: BDM[Double] = tile(eigSHS._1, 1, m) + tile(delta, 1, n - q).t
    val xis: BDM[Double] = tile(eigH._1, 1, m) + tile(delta, 1, n).t
    val etasq: BDM[Double] = tile(pow(etas, 2), 1, n-q).t
    val ll: BDV[Double] = 0.5 * (n.toDouble * (log(n/(2*math.Pi)) - 1 - log(sum(etasq /:/ lambdas, Axis._0).t))
      - sum(log(xis), Axis._0).t )
    val dLl: BDV[Double] = 0.5 * delta *:* (n.toDouble * sum(etasq /:/ pow(lambdas, 2), Axis._0).t
      /:/ sum(etasq /:/ lambdas, Axis._0).t - sum(1.0 / xis, Axis._0).t)
    
  }

} 
Example 6
Source File: Qk21Spec.scala    From seqspark   with Apache License 2.0 5 votes vote down vote up
package org.dizhang.seqspark.numerics

import breeze.linalg.DenseVector
import breeze.numerics.pow
import breeze.stats.distributions.ChiSquared
import org.scalatest.{FlatSpec, Matchers}


class Qk21Spec extends FlatSpec with Matchers {

  val chisq = ChiSquared(1.0)

  def f(input: DenseVector[Double]): DenseVector[Double] = {
    input.map(x => chisq.pdf(x))
  }

  "A Qk21" should "behave well" in {
    //val res = Qk21(f, 0.0, 1.0)
    //println(res)
  }
} 
Example 7
Source File: normDist.scala    From DynaML   with Apache License 2.0 5 votes vote down vote up
package io.github.mandar2812.dynaml.algebra

import breeze.generic.UFunc
import breeze.linalg.sum
import breeze.numerics.{abs, pow}


object normDist extends UFunc {

  implicit object implDV extends Impl2[SparkVector, Double, Double] {
    def apply(a: SparkVector, p: Double) = {
      assert(p >= 1.0, "For an L_p norm to be computed p >= 1.0")
      math.pow(a._vector.values.map(x => math.pow(math.abs(x), p)).sum(), 1.0/p)
    }
  }
}

object normBDist extends UFunc {
  implicit object implBlockedDV extends Impl2[SparkBlockedVector, Double, Double] {
    def apply(a: SparkBlockedVector, p: Double) = {
      assert(p >= 1.0, "For an L_p norm to be computed p >= 1.0")
      math.pow(a._vector.values.map(x => sum(pow(abs(x), p))).sum(), 1.0/p)
    }
  }

  implicit object implPartitionedDV extends Impl2[PartitionedVector, Double, Double] {
    def apply(a: PartitionedVector, p: Double) = {
      assert(p >= 1.0, "For an L_p norm to be computed p >= 1.0")
      math.pow(a._data.map(_._2).map(x => sum(pow(abs(x), p))).sum, 1.0/p)
    }
  }


} 
Example 8
Source File: Normalize.scala    From ScalaNetwork   with GNU General Public License v2.0 5 votes vote down vote up
package kr.ac.kaist.ir.deep.layer

import breeze.linalg.sum
import breeze.numerics.pow
import kr.ac.kaist.ir.deep.fn._
import play.api.libs.json.{JsObject, Json}


  abstract override def updateBy(delta: Iterator[ScalarMatrix], error: ScalarMatrix): ScalarMatrix = {
    val Xsq = pow(X, 2.0f)
    val lenSq = sum(Xsq)
    val len: Scalar = Math.sqrt(lenSq).toFloat

    // Note that length is the function of x_i.
    // Let z_i := x_i / len(x_i).
    // Then d z_i / d x_i = (len^2 - x_i^2) / len^3 = (1 - z_i^2) / len,
    //      d z_j / d x_i = - x_i * x_j / len^3 = - z_i * z_j / len
    val rows = dFdX.rows
    val dZdX = ScalarMatrix $0(rows, rows)
    var r = 0
    while (r < rows) {
      //dZ_r
      var c = 0
      while (c < rows) {
        if (r == c) {
          //dX_c
          dZdX.update(r, c, (1.0f - Xsq(r, 0) / lenSq) / len)
        } else {
          dZdX.update(r, c, (-X(r, 0) * X(c, 0)) / (len * lenSq))
        }
        c += 1
      }
      r += 1
    }

    // un-normalize the error
    super.updateBy(delta, dZdX * error)
  }
} 
Example 9
Source File: FFTUtil.scala    From ofdm   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
// Originally written for Fall 2018 EE290C modem class project
package ofdm
package fft

import breeze.numerics.pow


object FFTUtil {
  def factorize(x: Int): (List[Int], List[Int]) = {
    var a : Int = 2
    var factors = List[Int]()
    var powers  = List[Int]()
    var y = x
    while (a * a <= y) {
      if (y % a == 0) {
        factors = factors :+ a
        var power = 0
        do {
          y = y / a
          power += 1
        }
        while (y % a == 0)
        powers = powers :+ power
      }
      else a = a + 1
    }
    if (y != 1) {
      factors = factors :+ y
      powers = powers :+ 1
    }
    (factors, powers)
  }

  def gcd_extended(a: Int, b: Int): (Int, Int, Int) = {
    if (a == 0) {
      (b, 0, 1)
    } else {
      val (b_new, x_old, y_old) = gcd_extended(b % a, a)
      (b_new, y_old - b / a * x_old, x_old)
    }
  }

  def mult_inv(g: Int, n: Int): Int = {
    (gcd_extended(g, n)._2 % n + n) % n
  }

  def primitive_root(n: Int): Int = {
    val powers = factorize(n - 1)._1.map((n - 1) / _)
    (2 until n).toList.find(x => {
      val modded = powers.map(pow(x, _) % n)
      !modded.contains(1)
    }).getOrElse(0) // TODO: Raise error if not found?
  }

  def is_prime(i: Int) : Boolean = {
    if (i <= 1) false
    else if (i == 2) true
    else !(2 to (i - 1)).exists(x => i % x == 0)
  }

  def is_power(i: Int) : Boolean = {
    factorize(i)._1.length == 1
  }

  def is_power_of(i: Int, base: Int) : Boolean = {
    if (i == base) true
    else if (i % base != 0) false
    else is_power_of(i / base, base)
  }
} 
Example 10
Source File: BPDecoderTester.scala    From ofdm   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package ldpc

import breeze.numerics.pow
import chisel3._
import dsptools.DspTester
import dsptools.numbers._

import scala.util.Random

class BPDecoderTester[T <: Data](dut: BPDecoder[T], nTrials: Int = 100, ebn0: Seq[Double] = Seq(1.0)) extends DspTester(dut) {
  updatableDspVerbose.withValue(false) {
    var errors = 0
    var perrors = 0
    var nchecked = 0

    def checkOutput(input: Seq[Boolean], hasChecked: Boolean): Boolean = {
      if (hasChecked) {
        return hasChecked
      }

      poke(dut.out.ready, true)
      if (peek(dut.out.valid)) {
        var sawError = false
        for ((o, i) <- dut.out.bits.zip(input)) {
          // expect(o, i)
          if (peek(o) != i) {
            errors += 1
            sawError = true
          }
        }
        if (sawError) {
          perrors += 1
        }
        nchecked += 1
        return true
      }

      return false
    }

    for (e <- ebn0) {
      var checked = false
      errors  = 0
      perrors = 0
      nchecked = 0

      for (trial <- 0 until nTrials) {
        checked = false
        val input: Seq[Boolean] = for (_ <- 0 until dut.params.k) yield scala.util.Random.nextBoolean()
        val codeword = SWEncoder(input, dut.params)
        // println(codeword.map(_.toInt).mkString(", "))
        val withNoise = codeword.map(cw =>
          (2 * cw.toInt - 1) + math.sqrt(1.0 / pow(10.0, e / 10.0)) * Random.nextGaussian())
        // val eee = withNoise.map(_ >= 0.0).zip(codeword).map({ case (n, c) => (n == c).toInt }).sum
        // println(s"Errors = $eee")

        for ((i, ncw) <- dut.in.bits.zip(withNoise)) {
          poke(i, ncw)
        }

        poke(dut.in.valid, 1)

        checked = checkOutput(input, checked)
        step(1)
        while (!peek(dut.in.ready)) {
          checked = checkOutput(input, checked)
          step(1)
        }
        poke(dut.in.valid, 0)
        while (!checked) {
          checked = checkOutput(input, checked)
          step(1)
        }
      }
      println(s"ebn0 = $e, errors = $errors, perrors = $perrors, nchecked = $nchecked, total = ${nTrials * dut.params.k}, BER = ${errors.toDouble / (nTrials * dut.params.k)}, PER = ${perrors.toDouble / nTrials}")
    }

  }
}

object BPDecoderTester {
  def apply[T <: Data : Real](protoLLR: T, params: LdpcParams, nTrials: Int = 100, ebn0: Seq[Double] = Seq(1.0)): Boolean = {
    chisel3.iotesters.Driver.execute(Array[String]("-tbn", "verilator"), () =>
      new BPDecoder(protoLLR, params)) { c =>
      new BPDecoderTester(c, nTrials = nTrials, ebn0 = ebn0)
    }
  }
} 
Example 11
Source File: PeakDetectSpec.scala    From ofdm   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package ofdm

import breeze.math.Complex
import breeze.numerics.pow
import chisel3.{Driver => _, _}
import chisel3.experimental.FixedPoint
import chisel3.iotesters.{Driver, PeekPokeTester, TesterOptionsManager}
import dsptools.numbers._
import dsptools.numbers.implicits._
import org.scalatest.{FlatSpec, Matchers}

import scala.util.Random

class PeakDetectTester[T <: Data](c: PeakDetect[T]) extends PeekPokeTester(c) {

}

class PeakDetectSpec extends FlatSpec with Matchers {
  behavior of "PeakDetect"

  val manager = new TesterOptionsManager {
    interpreterOptions = interpreterOptions.copy(setVerbose = false, writeVCD = true)
    testerOptions = testerOptions.copy(backendName = "firrtl")
  }

  def nextComplex(snrdB: Double): Complex = {
    Complex(Random.nextGaussian(), Random.nextGaussian()) * pow(10.0, snrdB / 10.0)
  }


  it should "detect peaks with FixedPoint" ignore {
    val p = PeakDetectParams(
      protoCorr=FixedPoint(32.W, 16.BP),
      protoEnergyFF=FixedPoint(32.W, 31.BP),
      protoEnergyMult=FixedPoint(32.W, 16.BP),
      windowSize = 4
    )
    Driver.execute(() => new PeakDetect(p)
    , optionsManager = manager) { c => new dsptools.DspTester(c) {

      poke(c.io.config.energyFF, 99.0/100.0)
      poke(c.io.config.energyMult, 100.0 * 8.0) // 1/(1-energyFF) = 100, 8.0=9dB
      poke(c.io.config.energyOffset, 0.0) // don't use

      poke(c.io.in.valid, 1)
      // don't care about raw signal in this tester
      poke(c.io.in.bits.raw, Complex(0,0))


      // poke in some noise at -15dB
      for (_ <- 0 until 200) {
        poke(c.io.in.bits.corr, nextComplex(-15.0))
        step(1)
      }

      val peakVal: Complex = Complex(1,0)
      println("Poking in ramp")

      // ramp up to 0dB, shouldn't see a peak yet
      for (i <- 0 until 10) {
        poke(c.io.in.bits.corr, peakVal / (10.0 - i))
        expect(c.io.outLast, 0)
        step(1)
      }

      // back down to -15dB, should get a peak out in 4 cycles
      for (i <- 0 until 4) {
        poke(c.io.in.bits.corr, nextComplex(-15.0))
        expect(c.io.outLast, 0)
        step(1)
      }
      expect(c.io.outLast, 1)
      expect(c.io.out.valid, 1)
      expect(c.io.out.bits.corr, peakVal)

    }} should be (true)
  }
} 
Example 12
Source File: Norms.scala    From doddle-model   with Apache License 2.0 5 votes vote down vote up
package io.picnicml.doddlemodel.preprocessing

import breeze.linalg.{Axis, max, sum}
import breeze.numerics.{abs, pow, sqrt}
import io.picnicml.doddlemodel.data.{Features, RealVector}

object Norms {

  sealed trait Norm {
    def apply(x: Features): RealVector
  }

  final case object L1Norm extends Norm {
    override def apply(x: Features): RealVector = sum(abs(x), Axis._1)
  }

  final case object L2Norm extends Norm {
    override def apply(x: Features): RealVector = sqrt(sum(pow(x, 2), Axis._1))
  }

  final case object MaxNorm extends Norm {
    override def apply(x: Features): RealVector = max(abs(x), Axis._1)
  }
} 
Example 13
Source File: SoftmaxClassifier.scala    From doddle-model   with Apache License 2.0 5 votes vote down vote up
package io.picnicml.doddlemodel.linear

import breeze.linalg._
import breeze.numerics.{exp, log, pow}
import cats.syntax.option._
import io.picnicml.doddlemodel.data.{Features, RealVector, Simplex, Target}
import io.picnicml.doddlemodel.linear.typeclasses.LinearClassifier
import io.picnicml.doddlemodel.syntax.OptionSyntax._


case class SoftmaxClassifier private (lambda: Float, numClasses: Option[Int], private val w: Option[RealVector]) {
  private var yPredProbaCache: Simplex = _
}

object SoftmaxClassifier {

  def apply(lambda: Float = 0.0f): SoftmaxClassifier = {
    require(lambda >= 0.0f, "L2 regularization strength must be non-negative")
    SoftmaxClassifier(lambda, none, none)
  }

  private val wSlice: Range.Inclusive = 1 to -1

  @SerialVersionUID(0L)
  implicit lazy val ev: LinearClassifier[SoftmaxClassifier] = new LinearClassifier[SoftmaxClassifier] {

    override def numClasses(model: SoftmaxClassifier): Option[Int] = model.numClasses

    override protected def w(model: SoftmaxClassifier): Option[RealVector] = model.w

    override protected[doddlemodel] def copy(model: SoftmaxClassifier, numClasses: Int): SoftmaxClassifier =
      model.copy(numClasses = numClasses.some)

    override protected def copy(model: SoftmaxClassifier, w: RealVector): SoftmaxClassifier =
      model.copy(w = w.some)

    override protected def predictStateless(model: SoftmaxClassifier, w: RealVector, x: Features): Target =
      convert(argmax(predictProbaStateless(model, w, x)(*, ::)), Float)

    override protected def predictProbaStateless(model: SoftmaxClassifier, w: RealVector, x: Features): Simplex = {
      val z = x * w.asDenseMatrix.reshape(x.cols, model.numClasses.getOrBreak - 1, View.Require)
      val maxZ = max(z)
      val zExpPivot = DenseMatrix.horzcat(exp(z - maxZ), DenseMatrix.fill[Float](x.rows, 1)(exp(-maxZ)))
      zExpPivot(::, *) /:/ sum(zExpPivot(*, ::))
    }

    override protected[linear] def lossStateless(model: SoftmaxClassifier,
                                                 w: RealVector, x: Features, y: Target): Float = {
      model.yPredProbaCache = predictProbaStateless(model, w, x)
      val yPredProbaOfTrueClass = 0 until x.rows map { rowIndex =>
        val targetClass = y(rowIndex).toInt
        model.yPredProbaCache(rowIndex, targetClass)
      }

      val wMatrix = w.asDenseMatrix.reshape(x.cols, model.numClasses.getOrBreak - 1, View.Require)
      sum(log(DenseMatrix(yPredProbaOfTrueClass))) / (-x.rows.toFloat) +
        .5f * model.lambda * sum(pow(wMatrix(wSlice, ::), 2))
    }

    override protected[linear] def lossGradStateless(model: SoftmaxClassifier,
                                                     w: RealVector, x: Features, y: Target): RealVector = {
      val yPredProba = model.yPredProbaCache(::, 0 to -2)

      val indicator = DenseMatrix.zeros[Float](yPredProba.rows, yPredProba.cols)
      0 until indicator.rows foreach { rowIndex =>
        val targetClass = y(rowIndex).toInt
        if (targetClass < model.numClasses.getOrBreak - 1) indicator(rowIndex, targetClass) = 1.0f
      }

      val grad = (x.t * (indicator - yPredProba)) / (-x.rows.toFloat)
      val wMatrix = w.asDenseMatrix.reshape(x.cols, model.numClasses.getOrBreak - 1, View.Require)
      grad(wSlice, ::) += model.lambda * wMatrix(wSlice, ::)
      grad.toDenseVector
    }
  }
}