本文整理汇总了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]
}
}
示例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
}
}
示例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
}
}
示例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
}
}
示例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)
}
}
示例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]
}
}
示例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
}
}
}
示例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())
}
}
示例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
}
}
}