Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

TereLiye

object HelloWorld {
   def main(args: Array[String]) {
      println("Hello, world!")
      val y = List(List(0.1, 0.4, 0.5),List(0.2, 0.8, 0.1),List(0.6, 0.9, 0.45))
      println(y)
      val maximum = max(flatten(y))
      val minimum = min(flatten(y))
      val z = multisplit(List(List(7,8,9,10,11,12)),Nil,3)
      val z2=join(List(List(1.0, 2.0, 3.0), List(4.0, 5.0, 6.0)),z,Nil)
      //println(z)
      //println(z2)
      
      println(join(List(List(1.0, 2.0), List(3.0, 4.0), List(6.0, 7.0)),List(List( 2.0), List(4.0), List(6.0,7.0)),Nil))
      val a=List(List(8.0, 2.0,3.0,4.0,5.0,6.0), List(7.0, 8.0,9.0,10.0,11.0,12.0), List(13.0, 14.0,15.0,16.0,17.0,18.0))
      println(multijoin(a,Nil,3,0))
      println(singlepooling(max,a,Nil,3))
        println(activationLayer(cube,List(List(3,2),List(3,4))))
   }
   
   
   def split(in:List[List[Double]],size:Int): List[List[Double]] = {
       if(in==Nil)
       Nil
       else
       {
        if(size == 0)
        in
        else
        {
           if(in.tail == Nil)
            split(List(List(in.head.head),in.head.tail),size-1)
           
           else
            split(List(in.head:::List(in.tail.head.head),flatten(in.tail).tail),size-1)
       }
       }
   }
   
  def multisplit(in:List[List[Double]],out:List[List[Double]],size:Int): List[List[Double]] = {
    if(in==List(List()))
    out
    else
    {
        val s=split(in,size)
        // if(split(in,size)==Nil)
        // out:::in
        // else
    multisplit(s.tail,out:::List(s.head),size)
    }
  }
  
  def join(l1:List[List[Double]],l2:List[List[Double]],l3:List[List[Double]]):List[List[Double]]= {
      if(l1==Nil && l2!=Nil)
      l2
      else if(l2==Nil && l1!=Nil)
      l1
      else if(l1==Nil && l2==Nil)
      l3
      else
      join(l1.tail,l2.tail,l3:::(List(l1.head:::l2.head)))
  }
  
  def multijoin(m:List[List[Double]],out:List[List[Double]],size:Int,count:Int):List[List[Double]] ={
      if(count==size)
      out
      else
      {
          val ms=multisplit(List(m.head),Nil,size)
          multijoin(m.tail,join(out,ms,Nil),size,count+1)
      }
      
  }
  
    
    // def splitmax(in:List[Int],out:List[Int],size:Int,count:Int): List[Int] ={
    //     if(count == 0)
    //         Nil
    //     else
    //     {
    //         splitmax(flatten((split(List(in),size)).tail),out:::List(max(split(List(in),size).head)),size,count-1)//count _ size
    //     }
    // }
  
//   def sphelper(poolingFunc:List[Double]=>Double,Image:List[List[Double]],out:List[List[Double]], k:Int,size:Int,count:Int):List[List[Double]] = {
//       if(count==(size/k))
//       out
//       else
//       sphelper(poolingFunc,Image.tail,out:::List(split(List(Image.head)).head))
//   }
   
   def singlepooling(poolingFunc:List[Double]=>Double,Image:List[List[Double]],out:List[Double], size:Int):List[Double] = {
       if(Image==Nil)
       out
       else
       {
       val mj = multijoin(Image,Nil,Image.size,0)
       singlepooling(poolingFunc,Image.tail,out:::List(poolingFunc(Image.head)),size)
       }
   }
   
   
   
   def cube(x: Double): Double = x * x * x
     
     def activationHelper(f: Double => Double, m: List[Double],out:List[Double]): List[Double] =
     {
         if(m==Nil)
         out
         else
         activationHelper(f,m.tail,out:::List(f(m.head)))
     }
     
     def activation(f: Double => Double, m: List[List[Double]],out: List[List[Double]]): List[List[Double]] =
     {
         if(m==Nil)
         out
         else
         {
             activation(f,m.tail,out:::List(activationHelper(f,m.head,Nil)))
         }
     }
     def activationLayer(f: Double => Double, m: List[List[Double]]): List[List[Double]] =
     {
         activation(f,m,Nil)
     }
     
     def max(xs: List[Double]): Double = {
    if(xs.tail.nonEmpty){
      val tl = max(xs.tail)
      if(tl > xs.head) tl
      else xs.head
    }else{
      xs.head
    }
  }
  
  def min(xs: List[Double]): Double = {
    if(xs.tail.nonEmpty){
      val tl = min(xs.tail)
      if(tl < xs.head) tl
      else xs.head
    }else{
      xs.head
    }
  }
   
      def flatten[A](list: List[List[A]]):List[A] = {
  if (list.length==0) List[A]() 
  else list.head ++ flatten(list.tail)
   }
   
   def normalizehelper(m:List[Double],out:List[Int],maxi:Double,mini:Double):List[Int] = {
       if(m==Nil)
       out
       else
       normalizehelper(m.tail,out:::List((scala.math.round((m.head-mini)/(maxi-mini)*255)).toInt),maxi,mini)
   }
   
   def normalize(m:List[List[Double]],out:List[List[Int]],maximum:Double,minimum:Double): List[List[Int]] = {
        if(m==Nil)
        out
        else
        {
            
        normalize(m.tail,out:::List((normalizehelper(m.head,Nil,maximum,minimum))),maximum,minimum)
        }
   }
   
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.