• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala INDArray类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Scala中org.nd4j.linalg.api.ndarray.INDArray的典型用法代码示例。如果您正苦于以下问题:Scala INDArray类的具体用法?Scala INDArray怎么用?Scala INDArray使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了INDArray类的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。

示例1: SVM

//设置package包名称以及导入依赖的类
package fastml4j.classification

import org.nd4s.Implicits._
import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.factory.Nd4j
import fastml4j.optimizer._
import fastml4j.losses._
import org.nd4j.linalg.dataset.DataSet

class SVM(val lambdaL2: Double,
  val alpha: Double = 0.01,
  val maxIterations: Int = 1000,
  val stohasticBatchSize: Int = 100,
  val optimizerType: String = "PegasosSGD",
  val eps: Double = 1e-6) extends ClassificationModel {

  var weights: INDArray = Nd4j.zeros(1)
  var losses: Seq[Double] = Seq[Double]()


  def fit(dataSet: DataSet, initWeights: Option[INDArray] = None) = {

    val optimizer: Optimizer = optimizerType match {
      case "GradientDescent" => new GradientDescent(maxIterations, alpha, eps)
    //  case "GradientDescentDecreasingLearningRate" => new GradientDescentDecreasingLearningRate(maxIterations, alpha, eps)
      case "PegasosSGD" => new PegasosSGD(maxIterations, alpha, eps)
      case _ => throw new Exception("Optimizer %s is not supported".format(optimizerType))
    }

    val (weightsOut, lossesOut) = optimizer.optimize(
      new HingeLoss(lambdaL2),
      initWeights = initWeights.getOrElse(Nd4j.zeros(dataSet.numInputs)),
      dataSet)

    weights = weightsOut
    losses = lossesOut
  }

  def predictClass(inputVector: INDArray): Double = {
    val sign = math.signum((inputVector dot weights.T).sumT[Double])
    if( sign != 0 ) sign else 1.0
  }

  def predict(inputVector:  INDArray): Double = {
    (inputVector dot weights).sumT[Double]
  }


} 
开发者ID:rzykov,项目名称:fastml4j,代码行数:50,代码来源:SVM.scala


示例2: MNISTVisualizer

//设置package包名称以及导入依赖的类
package org.dl4scala.examples.feedforward.anomalydetection

import java.awt.{GridLayout, Image}
import java.awt.image.BufferedImage
import javax.swing.{ImageIcon, JFrame, JLabel, JPanel}

import org.nd4j.linalg.api.ndarray.INDArray

import scala.collection.mutable.ArrayBuffer


class MNISTVisualizer(imageScale: Double, digits: ArrayBuffer[INDArray], title: String, gridWidth: Int) {
  def this(imageScale: Double, digits: ArrayBuffer[INDArray], title: String) = {
    this(imageScale, digits, title, 5)
  }

  def visualize(): Unit = {
    val frame = new JFrame
    frame.setTitle(title)
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    val panel = new JPanel
    panel.setLayout(new GridLayout(0, gridWidth))
    val list = getComponents
    for (image <- list) {
      panel.add(image)
    }
    frame.add(panel)
    frame.setVisible(true)
    frame.pack()
  }

  def getComponents: ArrayBuffer[JLabel] = {
    val images = new ArrayBuffer[JLabel]()
    for (arr <- digits) {
      val bi = new BufferedImage(28, 28, BufferedImage.TYPE_BYTE_GRAY)
      for(i <- 0 until 784) {
        bi.getRaster.setSample(i % 28, i / 28, 0, (255 * arr.getDouble(i)).asInstanceOf[Int])
      }
      val orig = new ImageIcon(bi)
      val imageScaled = orig.getImage.getScaledInstance((imageScale * 28).asInstanceOf[Int],
        (imageScale * 28).asInstanceOf[Int], Image.SCALE_REPLICATE)
      val scaled = new ImageIcon(imageScaled)
      images.append(new JLabel(scaled))
    }
    images
  }
} 
开发者ID:endymecy,项目名称:dl4scala,代码行数:48,代码来源:MNISTVisualizer.scala


示例3: getScores

//设置package包名称以及导入依赖的类
package org.dl4scala.examples.nlp.paragraphvectors.tools

import lombok.NonNull
import org.deeplearning4j.models.embeddings.inmemory.InMemoryLookupTable
import org.deeplearning4j.models.word2vec.VocabWord
import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.ops.transforms.Transforms

import scala.collection.JavaConverters._
import java.util



  def getScores(@NonNull vector: INDArray): util.ArrayList[(String, Double)] = {
    val result = new util.ArrayList[(String, Double)]()
    for (label: String <- labelsUsed.asScala) {
      val vecLabel = lookupTable.vector(label)
      if (vecLabel == null) throw new IllegalStateException("Label '" + label + "' has no known vector!")

      val sim = Transforms.cosineSim(vector, vecLabel)
      result.add((label, sim))
    }
    result
  }
} 
开发者ID:endymecy,项目名称:dl4scala,代码行数:26,代码来源:LabelSeeker.scala


示例4: documentAsVector

//设置package包名称以及导入依赖的类
package org.dl4scala.examples.nlp.paragraphvectors.tools

import java.util.concurrent.atomic.AtomicInteger

import lombok.NonNull
import org.deeplearning4j.models.embeddings.inmemory.InMemoryLookupTable
import org.deeplearning4j.models.word2vec.VocabWord
import org.deeplearning4j.text.documentiterator.LabelledDocument
import org.deeplearning4j.text.tokenization.tokenizerfactory.TokenizerFactory
import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.factory.Nd4j
import scala.collection.JavaConverters._


  def documentAsVector(@NonNull document: LabelledDocument): INDArray = {
    val documentAsTokens = tokenizerFactory.create(document.getContent).getTokens.asScala
    val cnt = new AtomicInteger(0)

    for (word <- documentAsTokens) {
      if (vocabCache.containsWord(word)) cnt.incrementAndGet
    }

    val allWords = Nd4j.create(cnt.get, lookupTable.layerSize)
    cnt.set(0)

    for (word <- documentAsTokens) {
      if (vocabCache.containsWord(word)) allWords.putRow(cnt.getAndIncrement, lookupTable.vector(word))
    }

    val mean = allWords.mean(0)

    mean
  }
} 
开发者ID:endymecy,项目名称:dl4scala,代码行数:35,代码来源:MeansBuilder.scala


示例5: FloatNDArray

//设置package包名称以及导入依赖的类
// import scala.math.Fractional
import org.bytedeco.javacpp.caffe.FloatBlob
import org.nd4s.Implicits._
import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.factory.Nd4j

class FloatNDArray(val data: INDArray) extends java.io.Serializable {

    def scalarDivide(v: Float) {
        data /= v
    }

    def plus(other: FloatNDArray) {
        data += other.data
    }

    def shape: Array[Int] = data.shape()

    def asFloat: Array[Float] = data.data().asFloat()
}

object FloatNDArray {
    def apply(shape: Array[Int]) = {
        new FloatNDArray(Nd4j.zeros(shape : _*))
    }

    def apply(data: Array[Float], shape: Array[Int]) = {
        new FloatNDArray(Nd4j.create(data, shape))
    }

    def plus(a: FloatNDArray, b: FloatNDArray) = {
        new FloatNDArray(a.data + b.data)
    }

    def getFloatBlobShape(blob: FloatBlob): Array[Int] = {
        val numAxes = blob.num_axes()
        numAxes match {
            case 0 => Array(1)
            case 1 => Array(1, blob.shape.get(0))
            case n => {
                val shape = new Array[Int](numAxes)
                for (k <- 0 to numAxes - 1) {
                    shape(k) = blob.shape.get(k)
                }
                shape                
            }
        }
    }

    def floatBlobToNDArray(blob: FloatBlob): FloatNDArray = {
        val shape = getFloatBlobShape(blob)
        val data = new Array[Float](shape.product)
        blob.cpu_data.get(data)
        FloatNDArray(data, shape)
    }
} 
开发者ID:pierric,项目名称:Mnist-Caffe-Spark,代码行数:57,代码来源:FloatNDArray.scala


示例6: LinearRegression

//设置package包名称以及导入依赖的类
package fastml4j.regression

import fastml4j.losses.OLSLoss
import fastml4j.optimizer.GradientDescent
import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.dataset.DataSet
import org.nd4j.linalg.factory.Nd4j
import org.nd4s.Implicits._


class LinearRegression(val lambdaL2: Double,
  val alpha: Double = 0.01,
  val maxIterations: Int = 1000,
  val stohasticBatchSize: Int = 100,
  val optimizerType: String = "GradientDescent",
  val eps: Double = 1e-6) {

  var weights: INDArray = Nd4j.zeros(1)
  var losses: Seq[Double] = Seq[Double]()

  def fit(dataSet: DataSet, initWeights: Option[INDArray] = None): Unit = {

    val optimizer = optimizerType match {
      case "GradientDescent" => new GradientDescent(maxIterations, alpha, eps)
      case _ => throw new Exception("Optimizer %s is not supported".format(optimizerType))
    }

    val (weightsOut, lossesOut) = optimizer.optimize(new OLSLoss(lambdaL2),
      initWeights.getOrElse(Nd4j.zeros(dataSet.numExamples)),
      dataSet)
    weights = weightsOut
    losses = lossesOut
  }

  def predict(inputVector: INDArray): Double = {
    (inputVector dot weights).sumT[Double]
  }


} 
开发者ID:rzykov,项目名称:fastml4j,代码行数:41,代码来源:LinearRegression.scala


示例7: gradientLimits

//设置package包名称以及导入依赖的类
package scalarank.ranker

import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.factory.Nd4j
import org.nd4s.Implicits._


  def gradientLimits(gradient: INDArray, x: INDArray, function: INDArray => INDArray): Array[Double] = {
    val rand = Nd4j.randn(x.rows, x.columns)
    Array(1e1, 1, 1e-1, 1e-2).map { ? =>
      (0 until x.columns).map { i =>
        val e = Nd4j.zeros(x.columns)
        e(i) = 1.0
        val approximateGradient = (function(x + e * ?) - function(x - e * ?)) / (2*?)
        Math.abs(approximateGradient(i) - gradient(i))
      }.sum
    }
  }

} 
开发者ID:rjagerman,项目名称:scalarank,代码行数:21,代码来源:GradientCheck.scala


示例8: BroadcastComputationGraph

//设置package包名称以及导入依赖的类
package com.cloudera.datascience.dl4j.cnn.spark

import org.deeplearning4j.nn.conf.ComputationGraphConfiguration
import org.deeplearning4j.nn.graph.ComputationGraph
import org.nd4j.linalg.api.ndarray.INDArray


case class BroadcastComputationGraph(jsonConfig: String, params: INDArray) {
  def toGraph: ComputationGraph = {
    val graph = new ComputationGraph(ComputationGraphConfiguration.fromJson(jsonConfig).clone())
    graph.init(params.unsafeDuplication(), false)
    graph
  }
}
object BroadcastComputationGraph {
  def fromGraph(graph: ComputationGraph): BroadcastComputationGraph = {
    BroadcastComputationGraph(graph.getConfiguration.toJson, graph.params())
  }
} 
开发者ID:sethah,项目名称:dl4j-demo,代码行数:20,代码来源:BroadcastComputationGraph.scala


示例9: loss

//设置package包名称以及导入依赖的类
package org.dele.dl4s.common.layers

import org.nd4j.linalg.api.ndarray.INDArray


trait TLossLayer {
  def loss(est:INDArray, target:INDArray):Double
}

object LossLayers {
  object EcuLossLayer extends TLossLayer {
    override def loss(est: INDArray, target: INDArray): Double = {
      1.0
    }
  }
} 
开发者ID:new2scala,项目名称:dl4s,代码行数:17,代码来源:TLossLayer.scala



注:本文中的org.nd4j.linalg.api.ndarray.INDArray类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala RequestContext类代码示例发布时间:2022-05-23
下一篇:
Scala CompletionStage类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap