本文整理汇总了Scala中org.deeplearning4j.nn.multilayer.MultiLayerNetwork类的典型用法代码示例。如果您正苦于以下问题:Scala MultiLayerNetwork类的具体用法?Scala MultiLayerNetwork怎么用?Scala MultiLayerNetwork使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了MultiLayerNetwork类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: GradientsAndParamsListenerExample
//设置package包名称以及导入依赖的类
package org.dl4scala.examples.userInterface
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork
import org.dl4scala.examples.userInterface.util.{GradientsAndParamsListener, UIExampleUtils}
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator
object GradientsAndParamsListenerExample {
def main(args: Array[String]): Unit = {
// Get our network and training data
val net: MultiLayerNetwork = UIExampleUtils.getMnistNetwork
val trainData: DataSetIterator = UIExampleUtils.getMnistData
System.out.println()
for (layer <- net.getLayers) {
System.out.println(layer)
}
System.out.println()
net.setListeners(new GradientsAndParamsListener(net, 100))
}
}
示例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())
}
}
示例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)
}
}
示例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()
}
}