当前位置: 首页>>代码示例>>Scala>>正文


Scala WeightInit类代码示例

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


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

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

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


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