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

Scala Nd4j类代码示例

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

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



在下文中一共展示了Nd4j类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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: RNNEmbedding

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

import org.deeplearning4j.nn.conf.NeuralNetConfiguration
import org.deeplearning4j.nn.conf.layers.{EmbeddingLayer, GravesLSTM, RnnOutputLayer}
import org.deeplearning4j.nn.conf.preprocessor.{FeedForwardToRnnPreProcessor, RnnToFeedForwardPreProcessor}
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork
import org.nd4j.linalg.activations.Activation
import org.nd4j.linalg.factory.Nd4j
import org.nd4j.linalg.lossfunctions.LossFunctions

import scala.util.Random


object RNNEmbedding {
  def main(args: Array[String]): Unit = {
    val nClassesIn = 10
    val batchSize = 3
    val timeSeriesLength = 8
    val inEmbedding = Nd4j.create(batchSize, 1, timeSeriesLength)
    val outLabels = Nd4j.create(batchSize, 4, timeSeriesLength)

    val r = new Random(12345)
    (0 until batchSize).foreach{i =>
      (0 until timeSeriesLength).foreach{j =>
        val classIdx = r.nextInt(nClassesIn)
        inEmbedding.putScalar(Array[Int](i, 0, j), classIdx)
        val labelIdx = r.nextInt(4)
        outLabels.putScalar(Array[Int](i, labelIdx, j), 1.0)
      }
    }

    val conf = new NeuralNetConfiguration.Builder()
      .activation(Activation.RELU)
      .list()
      .layer(0, new EmbeddingLayer.Builder().nIn(nClassesIn).nOut(5).build())
      .layer(1, new GravesLSTM.Builder().nIn(5).nOut(7).activation(Activation.SOFTSIGN).build())
      .layer(2, new RnnOutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(7).nOut(4).activation(Activation.SOFTMAX).build())
      .inputPreProcessor(0, new RnnToFeedForwardPreProcessor())
      .inputPreProcessor(1, new FeedForwardToRnnPreProcessor())
      .build()

    val net = new MultiLayerNetwork(conf)
    net.init()

    net.setInput(inEmbedding)
    net.setLabels(outLabels)

    net.computeGradientAndScore()
    System.out.println(net.score())
  }
} 
开发者ID:endymecy,项目名称:dl4scala,代码行数:52,代码来源:RNNEmbedding.scala


示例3: MultiLayerNetworkExternalErrors

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

import org.deeplearning4j.nn.conf.layers.DenseLayer
import org.deeplearning4j.nn.conf.{NeuralNetConfiguration, Updater}
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork
import org.deeplearning4j.nn.weights.WeightInit
import org.nd4j.linalg.activations.Activation
import org.nd4j.linalg.factory.Nd4j


object MultiLayerNetworkExternalErrors {
  def main(array: Array[String]): Unit = {
    //Create the model
    val nIn = 4
    val nOut = 3
    Nd4j.getRandom.setSeed(12345)

    val conf = new NeuralNetConfiguration.Builder()
      .seed(12345)
      .activation(Activation.TANH)
      .weightInit(WeightInit.XAVIER)
      .updater(Updater.NESTEROVS)
      .learningRate(0.1)
      .list()
      .layer(0, new DenseLayer.Builder().nIn(nIn).nOut(3).build())
      .layer(1, new DenseLayer.Builder().nIn(3).nOut(3).build())
      .backprop(true).pretrain(false)
      .build()

    val model = new MultiLayerNetwork(conf)
    model.init()

    //Calculate gradient with respect to an external error//Calculate gradient with respect to an external error

    val minibatch = 32
    val input = Nd4j.rand(minibatch, nIn)
    val output = model.output(input) //Do forward pass. Normally: calculate the error based on this

    val externalError = Nd4j.rand(minibatch, nOut)
    val p = model.backpropGradient(externalError) //Calculate backprop gradient based on error array

    //Update the gradient: apply learning rate, momentum, etc
    //This modifies the Gradient object in-place
    val gradient = p.getFirst
    val iteration = 0
    model.getUpdater.update(model, gradient, iteration, minibatch)

    //Get a row vector gradient array, and apply it to the parameters to update the model
    val updateVector = gradient.gradient
    model.params.subi(updateVector)
  }
} 
开发者ID:endymecy,项目名称:dl4scala,代码行数:53,代码来源:MultiLayerNetworkExternalErrors.scala


示例4: WeightedLossFunctionExample

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

import org.deeplearning4j.nn.conf.NeuralNetConfiguration
import org.deeplearning4j.nn.conf.layers.{DenseLayer, OutputLayer}
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork
import org.deeplearning4j.nn.weights.WeightInit
import org.nd4j.linalg.activations.Activation
import org.nd4j.linalg.factory.Nd4j
import org.nd4j.linalg.lossfunctions.impl.LossMCXENT


object WeightedLossFunctionExample {
  def main(args: Array[String]): Unit = {
    val numInputs = 4
    val numClasses = 3 //3 classes for classification

    // Create the weights array. Note that we have 3 output classes, therefore we have 3 weights
    val weightsArray = Nd4j.create(Array[Double](0.5, 0.5, 1.0))

    val conf = new NeuralNetConfiguration.Builder()
      .activation(Activation.RELU)
      .weightInit(WeightInit.XAVIER)
      .learningRate(0.1)
      .list()
      .layer(0, new DenseLayer.Builder().nIn(numInputs).nOut(5)
        .build())
      .layer(1, new DenseLayer.Builder().nIn(5).nOut(5)
        .build())
      .layer(2, new OutputLayer.Builder()
        .lossFunction(new LossMCXENT(weightsArray))     // *** Weighted loss function configured here ***
        .activation(Activation.SOFTMAX)
        .nIn(5).nOut(numClasses).build())
      .backprop(true).pretrain(false)
      .build()

    //Initialize and use the model as before
    val model = new MultiLayerNetwork(conf)
    model.init()
  }
} 
开发者ID:endymecy,项目名称:dl4scala,代码行数:41,代码来源:WeightedLossFunctionExample.scala


示例5: 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


示例6: 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


示例7: 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


示例8: LinearRegressionSuite

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

import org.scalatest._
import org.nd4s.Implicits._
import org.nd4j.linalg.api.ndarray.INDArray
import org.nd4j.linalg.dataset.DataSet
import org.scalatest.Matchers._
import org.nd4j.linalg.factory.Nd4j


class LinearRegressionSuite extends FunSuite with BeforeAndAfter {

  test("Simple test") {
    val coef1 = 1.0
    val coef2 = 0.5

    val x = (0 to   500).map { x => Array(x.toDouble,1.0) }.toArray
    val y = x.map {case Array(x1, x2) => x1 * coef1 + x2 * coef2 + math.random/10 }

    val lr = new LinearRegression(lambdaL2 = 0.0, alpha = 0.000001, eps = 1e-4, maxIterations = 2000)
    lr.fit(new DataSet(x.toNDArray, y.toNDArray), Option(Nd4j.zeros(2)) )

    val weights = lr.weights
    assert(weights.getDouble(0,0) === coef1 +- 1)
    assert(weights.getDouble(0,1) === coef2 +- 1)
  }

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


示例9: 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


示例10: IndexerTest

//设置package包名称以及导入依赖的类
package edu.uchicago.cs.ndnn

import org.junit.Assert._
import org.junit.Test
import org.nd4j.linalg.factory.Nd4j


class IndexerTest {

  @Test
  def testGet: Unit = {
    val a = Nd4j.create(Array(Array(3d, 4d, 5d), Array(1d, 6d, 7d)))
    val b = Nd4j.create(Array(2d, 1d)).reshape(2, 1)

    val fetched = Index.get(a, b)
    assertArrayEquals(Array(2, 1), fetched.shape)
    assertEquals(5d, fetched.getDouble(0, 0), 0.001)
    assertEquals(6d, fetched.getDouble(1, 0), 0.001)
  }

  @Test
  def testPut: Unit = {
    val a = Nd4j.createUninitialized(Array(2, 3)).assign(0)
    val b = Nd4j.create(Array(2d, 1d)).reshape(2, 1)
    val value = Nd4j.create(Array(3d, 5d)).reshape(2, 1)

    Index.put(a, b, value)

    assertEquals(3d, a.getDouble(0, 2), 0.001)
    assertEquals(5d, a.getDouble(1, 1), 0.001)
  }
} 
开发者ID:harperjiang,项目名称:enc-selector,代码行数:33,代码来源:IndexTest.scala


示例11: GraphTest

//设置package包名称以及导入依赖的类
package edu.uchicago.cs.ndnn

import org.junit.Assert._
import org.junit.Test
import org.nd4j.linalg.factory.Nd4j

class GraphTest {

  @Test
  def testTrain: Unit = {
    val graph = new Graph(Xavier, new SGD(0.02, 1), new SquareLoss())

    // Setup
    val x = graph.input("x")
    val w = graph.param("W", Array(3, 2))
    val b = graph.param("b", Array(1, 2))(Zero)

    val wx = new DotMul(x, w)
    val add = new Add(wx, b)
    graph.output(add)

    x.set(Nd4j.create(Array(Array(3d, 2d, 1d), Array(5d, 7d, 6d), Array(8d, 2d, 9d), Array(6d, 4d, 1d))))
    graph.expect(Nd4j.create(Array(Array(25d, 31d), Array(79d, 112d), Array(43d, 110d), Array(49d, 54d))))
    var loss = Double.MaxValue
    for (_ <- 0 to 100) {
      loss = graph.train
    }
    
    assertEquals(0, loss, 0.1)
  }

  @Test
  def testTest: Unit = {

    val graph = new Graph(Xavier, new SGD(0.01), new SoftMaxLogLoss())
    val x = graph.input("x")
    val w = graph.param("W", Array(3, 6))

    val wx = new DotMul(x, w)
    val softmax = new SoftMax(wx)
    graph.output(softmax)

    x.set(Nd4j.create(Array(Array(1d, 2d, 3d), Array(2d, 1d, 9d), Array(5d, 2d, 7d))))
    w.set(Nd4j.create(Array(Array(0.1, 0.5, 2.7, 3.1, 0.2, 0.7), Array(1.1, 2.2, 1.3, 4.2, 5.2, 1.9), Array(3.8, 4.7, 2.2, 0.6, 1.9, 1.3))))

    graph.expect(Nd4j.create(Array(2d, 1d, 4d)).reshape(3, -1))

    assertEquals(22.27 / 3, graph.test._1, 0.01)
  }
} 
开发者ID:harperjiang,项目名称:enc-selector,代码行数:51,代码来源:GraphTest.scala


示例12: AppSpec

//设置package包名称以及导入依赖的类
package com.yumusoft.lstmrecommender

import org.scalatest._
import org.nd4j.linalg.factory.Nd4j

class AppSpec extends FlatSpec with Matchers {

  "This project" should "have some tests" in {
    (2 + 2) should be (4)
  }

  it should "Be able to create NDArrays on the CPU or GPU" in {
    val a = Nd4j.ones(2, 2)
    val b = Nd4j.ones(2, 2)

    (a add b).getDouble(0, 0) should be (2.0)
  }
} 
开发者ID:wmeddie,项目名称:lstm-recommender,代码行数:19,代码来源:AppSpec.scala


示例13: ScoreSparkModel

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

import java.io.File

import com.cloudera.datascience.dl4j.cnn.Utils
import org.apache.spark.sql.{Row, SparkSession}
import org.deeplearning4j.util.ModelSerializer
import org.nd4j.linalg.dataset.DataSet
import org.nd4j.linalg.factory.Nd4j
import scopt.OptionParser

object ScoreSparkModel {

  private[this] case class Params (modelPath: String = null, dataPath: String = null)

  private[this] object Params {
    def parseArgs(args: Array[String]): Params = {
      val params = new OptionParser[Params]("train an existing model") {
        opt[String]("test")
          .text("the path of the test data")
          .action((x, c) => c.copy(dataPath = x))
        opt[String]("model")
          .text("location of the model")
          .action((x, c) => c.copy(modelPath = x))
      }.parse(args, Params()).get
      require(params.modelPath != null && params.dataPath != null,
        "You must supply the data path and a model path.")
      params
    }
  }

  def main(args: Array[String]): Unit = {
    val param = Params.parseArgs(args)
    val spark = SparkSession.builder().appName("score a model").getOrCreate()
    try {
      val sc = spark.sparkContext
      sc.hadoopConfiguration.set("mapreduce.input.fileinputformat.input.dir.recursive", "true")
      val restorePath = new File(param.modelPath)
      val restored = ModelSerializer.restoreComputationGraph(restorePath)
      val testRDD = spark.read.parquet(param.dataPath)
        .rdd
        .map { case Row(f: Array[Byte], l: Array[Byte]) =>
          new DataSet(Nd4j.fromByteArray(f), Nd4j.fromByteArray(l))
        }
      val eval = Utils.evaluate(restored, testRDD, 16)
      println(Utils.prettyPrintEvaluationStats(eval))

    } finally {
      spark.stop()
    }
  }
} 
开发者ID:sethah,项目名称:dl4j-demo,代码行数:53,代码来源:ScoreSparkModel.scala


示例14: RegressionTestJUnit

//设置package包名称以及导入依赖的类
package com.rikima.dnn

import org.junit.Test
import org.junit.Assert._
import org.nd4j.linalg.factory.Nd4j


class RegressionTestJUnit {
  @Test
  def testND4J(): Unit = {
    val nd = Nd4j.create(Array(1,2,3,4),Array(2,2,2,3));

    val seed = 1256
    Nd4j.getRandom.setSeed(seed)
  }


  @Test
  def testTest() {
    assertEquals(1, 1)
  }
} 
开发者ID:rikima,项目名称:dl4j_work,代码行数:23,代码来源:RegressionTestJUnit.scala



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
Scala Icon类代码示例发布时间:2022-05-23
下一篇:
Scala PKCS8EncodedKeySpec类代码示例发布时间: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