本文整理汇总了Java中org.deeplearning4j.nn.multilayer.MultiLayerNetwork.setInput方法的典型用法代码示例。如果您正苦于以下问题:Java MultiLayerNetwork.setInput方法的具体用法?Java MultiLayerNetwork.setInput怎么用?Java MultiLayerNetwork.setInput使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.deeplearning4j.nn.multilayer.MultiLayerNetwork
的用法示例。
在下文中一共展示了MultiLayerNetwork.setInput方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testWithPreprocessorsMLN
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testWithPreprocessorsMLN(){
for(WorkspaceMode wm : WorkspaceMode.values()) {
System.out.println(wm);
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.trainingWorkspaceMode(wm)
.inferenceWorkspaceMode(wm)
.list()
.layer(new GravesLSTM.Builder().nIn(10).nOut(5).build())
.layer(new GravesLSTM.Builder().nIn(5).nOut(8).build())
.layer(new RnnOutputLayer.Builder(LossFunctions.LossFunction.MSE).activation(Activation.SIGMOID).nOut(3).build())
.inputPreProcessor(0, new DupPreProcessor())
.setInputType(InputType.recurrent(10))
.build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
INDArray input = Nd4j.zeros(1, 10, 5);
for( boolean train : new boolean[]{false, true}){
net.clear();
net.feedForward(input, train);
}
net.setInput(input);
net.setLabels(Nd4j.rand(1, 3, 5));
net.computeGradientAndScore();
}
}
示例2: testDBNBNMultiLayer
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testDBNBNMultiLayer() throws Exception {
DataSetIterator iter = new MnistDataSetIterator(2, 2);
DataSet next = iter.next();
// Run with separate activation layer
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(123)
.list()
.layer(0, new DenseLayer.Builder().nIn(28 * 28).nOut(10).weightInit(WeightInit.XAVIER)
.activation(Activation.RELU).build())
.layer(1, new BatchNormalization.Builder().nOut(10).build()).layer(2,
new ActivationLayer.Builder()
.activation(Activation.RELU).build())
.layer(3, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).nIn(10).nOut(10)
.build())
.backprop(true).pretrain(false).build();
MultiLayerNetwork network = new MultiLayerNetwork(conf);
network.init();
network.setInput(next.getFeatureMatrix());
INDArray activationsActual = network.preOutput(next.getFeatureMatrix());
assertEquals(10, activationsActual.shape()[1], 1e-2);
network.fit(next);
INDArray actualGammaParam = network.getLayer(1).getParam(BatchNormalizationParamInitializer.GAMMA);
INDArray actualBetaParam = network.getLayer(1).getParam(BatchNormalizationParamInitializer.BETA);
assertTrue(actualGammaParam != null);
assertTrue(actualBetaParam != null);
}
示例3: testCNNDBNMultiLayer
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testCNNDBNMultiLayer() throws Exception {
DataSetIterator iter = new MnistDataSetIterator(2, 2);
DataSet next = iter.next();
// Run with separate activation layer
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(123)
.weightInit(WeightInit.XAVIER).list()
.layer(0, new ConvolutionLayer.Builder(new int[] {1, 1}, new int[] {1, 1}).nIn(1).nOut(6)
.activation(Activation.IDENTITY).build())
.layer(1, new BatchNormalization.Builder().build())
.layer(2, new ActivationLayer.Builder().activation(Activation.RELU).build())
.layer(3, new DenseLayer.Builder().nIn(28 * 28 * 6).nOut(10).activation(Activation.IDENTITY)
.build())
.layer(4, new BatchNormalization.Builder().nOut(10).build())
.layer(5, new ActivationLayer.Builder().activation(Activation.RELU).build())
.layer(6, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.activation(Activation.SOFTMAX).nOut(10).build())
.backprop(true).pretrain(false).setInputType(InputType.convolutional(28, 28, 1)).build();
MultiLayerNetwork network = new MultiLayerNetwork(conf);
network.init();
network.setInput(next.getFeatureMatrix());
INDArray activationsActual = network.preOutput(next.getFeatureMatrix());
assertEquals(10, activationsActual.shape()[1], 1e-2);
network.fit(next);
INDArray actualGammaParam = network.getLayer(1).getParam(BatchNormalizationParamInitializer.GAMMA);
INDArray actualBetaParam = network.getLayer(1).getParam(BatchNormalizationParamInitializer.BETA);
assertTrue(actualGammaParam != null);
assertTrue(actualBetaParam != null);
}
示例4: testGradientCNNMLN
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testGradientCNNMLN() {
//Parameterized test, testing combinations of:
// (a) activation function
// (b) Whether to test at random initialization, or after some learning (i.e., 'characteristic mode of operation')
// (c) Loss function (with specified output activations)
Activation[] activFns = {Activation.SIGMOID, Activation.TANH};
boolean[] characteristic = {false, true}; //If true: run some backprop steps first
LossFunctions.LossFunction[] lossFunctions =
{LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD, LossFunctions.LossFunction.MSE};
Activation[] outputActivations = {Activation.SOFTMAX, Activation.TANH}; //i.e., lossFunctions[i] used with outputActivations[i] here
DataSet ds = new IrisDataSetIterator(150, 150).next();
ds.normalizeZeroMeanZeroUnitVariance();
INDArray input = ds.getFeatureMatrix();
INDArray labels = ds.getLabels();
for (Activation afn : activFns) {
for (boolean doLearningFirst : characteristic) {
for (int i = 0; i < lossFunctions.length; i++) {
LossFunctions.LossFunction lf = lossFunctions[i];
Activation outputActivation = outputActivations[i];
MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.CONJUGATE_GRADIENT).updater(new NoOp())
.weightInit(WeightInit.XAVIER).seed(12345L).list()
.layer(0, new ConvolutionLayer.Builder(1, 1).nOut(6).activation(afn).build())
.layer(1, new OutputLayer.Builder(lf).activation(outputActivation).nOut(3).build())
.setInputType(InputType.convolutionalFlat(1, 4, 1)).pretrain(false).backprop(true);
MultiLayerConfiguration conf = builder.build();
MultiLayerNetwork mln = new MultiLayerNetwork(conf);
mln.init();
String name = new Object() {
}.getClass().getEnclosingMethod().getName();
if (doLearningFirst) {
//Run a number of iterations of learning
mln.setInput(ds.getFeatures());
mln.setLabels(ds.getLabels());
mln.computeGradientAndScore();
double scoreBefore = mln.score();
for (int j = 0; j < 10; j++)
mln.fit(ds);
mln.computeGradientAndScore();
double scoreAfter = mln.score();
//Can't test in 'characteristic mode of operation' if not learning
String msg = name + " - score did not (sufficiently) decrease during learning - activationFn="
+ afn + ", lossFn=" + lf + ", outputActivation=" + outputActivation
+ ", doLearningFirst= " + doLearningFirst + " (before=" + scoreBefore
+ ", scoreAfter=" + scoreAfter + ")";
assertTrue(msg, scoreAfter < 0.8 * scoreBefore);
}
if (PRINT_RESULTS) {
System.out.println(name + " - activationFn=" + afn + ", lossFn=" + lf + ", outputActivation="
+ outputActivation + ", doLearningFirst=" + doLearningFirst);
for (int j = 0; j < mln.getnLayers(); j++)
System.out.println("Layer " + j + " # params: " + mln.getLayer(j).numParams());
}
boolean gradOK = GradientCheckUtil.checkGradients(mln, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);
assertTrue(gradOK);
}
}
}
}
示例5: testCustomUpdater
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testCustomUpdater() {
//Create a simple custom updater, equivalent to SGD updater
double lr = 0.03;
Nd4j.getRandom().setSeed(12345);
MultiLayerConfiguration conf1 = new NeuralNetConfiguration.Builder().seed(12345)
.activation(Activation.TANH).updater(new CustomIUpdater(lr)) //Specify custom IUpdater
.list().layer(0, new DenseLayer.Builder().nIn(10).nOut(10).build())
.layer(1, new OutputLayer.Builder().nIn(10).nOut(10)
.lossFunction(LossFunctions.LossFunction.MSE).build())
.build();
Nd4j.getRandom().setSeed(12345);
MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder().seed(12345)
.activation(Activation.TANH).updater(new Sgd(lr)).list()
.layer(0, new DenseLayer.Builder().nIn(10).nOut(10).build()).layer(1, new OutputLayer.Builder()
.nIn(10).nOut(10).lossFunction(LossFunctions.LossFunction.MSE).build())
.build();
//First: Check updater config
assertTrue(((BaseLayer) conf1.getConf(0).getLayer()).getIUpdater() instanceof CustomIUpdater);
assertTrue(((BaseLayer) conf1.getConf(1).getLayer()).getIUpdater() instanceof CustomIUpdater);
assertTrue(((BaseLayer) conf2.getConf(0).getLayer()).getIUpdater() instanceof Sgd);
assertTrue(((BaseLayer) conf2.getConf(1).getLayer()).getIUpdater() instanceof Sgd);
CustomIUpdater u0_0 = (CustomIUpdater) ((BaseLayer) conf1.getConf(0).getLayer()).getIUpdater();
CustomIUpdater u0_1 = (CustomIUpdater) ((BaseLayer) conf1.getConf(1).getLayer()).getIUpdater();
assertEquals(lr, u0_0.getLearningRate(), 1e-6);
assertEquals(lr, u0_1.getLearningRate(), 1e-6);
Sgd u1_0 = (Sgd) ((BaseLayer) conf2.getConf(0).getLayer()).getIUpdater();
Sgd u1_1 = (Sgd) ((BaseLayer) conf2.getConf(1).getLayer()).getIUpdater();
assertEquals(lr, u1_0.getLearningRate(), 1e-6);
assertEquals(lr, u1_1.getLearningRate(), 1e-6);
//Second: check JSON
String asJson = conf1.toJson();
MultiLayerConfiguration fromJson = MultiLayerConfiguration.fromJson(asJson);
assertEquals(conf1, fromJson);
Nd4j.getRandom().setSeed(12345);
MultiLayerNetwork net1 = new MultiLayerNetwork(conf1);
net1.init();
Nd4j.getRandom().setSeed(12345);
MultiLayerNetwork net2 = new MultiLayerNetwork(conf2);
net2.init();
//Third: check gradients are equal
INDArray in = Nd4j.rand(5, 10);
INDArray labels = Nd4j.rand(5, 10);
net1.setInput(in);
net2.setInput(in);
net1.setLabels(labels);
net2.setLabels(labels);
net1.computeGradientAndScore();
net2.computeGradientAndScore();;
assertEquals(net1.getFlattenedGradients(), net2.getFlattenedGradients());
}
示例6: testMlnToCompGraph
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testMlnToCompGraph() {
Nd4j.getRandom().setSeed(12345);
for( int i=0; i<3; i++ ){
MultiLayerNetwork n;
switch (i){
case 0:
n = getNet1(false);
break;
case 1:
n = getNet1(true);
break;
case 2:
n = getNet2();
break;
default:
throw new RuntimeException();
}
INDArray in = (i <= 1 ? Nd4j.rand(new int[]{8, 3, 10, 10}) : Nd4j.rand(new int[]{8, 5, 10}));
INDArray labels = (i <= 1 ? Nd4j.rand(new int[]{8, 10}) : Nd4j.rand(new int[]{8, 10, 10}));
ComputationGraph cg = n.toComputationGraph();
INDArray out1 = n.output(in);
INDArray out2 = cg.outputSingle(in);
assertEquals(out1, out2);
n.setInput(in);
n.setLabels(labels);
cg.setInputs(in);
cg.setLabels(labels);
n.computeGradientAndScore();
cg.computeGradientAndScore();
assertEquals(n.score(), cg.score(), 1e-6);
assertEquals(n.gradient().gradient(), cg.gradient().gradient());
n.fit(in, labels);
cg.fit(new INDArray[]{in}, new INDArray[]{labels});
assertEquals(n.params(), cg.params());
}
}
示例7: testSerialization
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testSerialization() throws Exception {
for(WorkspaceMode wsm : WorkspaceMode.values()) {
log.info("*** Starting workspace mode: " + wsm);
Nd4j.getRandom().setSeed(12345);
MultiLayerConfiguration conf1 = new NeuralNetConfiguration.Builder()
.activation(Activation.TANH)
.weightInit(WeightInit.XAVIER)
.trainingWorkspaceMode(wsm)
.inferenceWorkspaceMode(wsm)
.updater(new Adam())
.list()
.layer(new Bidirectional(Bidirectional.Mode.ADD, new GravesLSTM.Builder().nIn(10).nOut(10).build()))
.layer(new Bidirectional(Bidirectional.Mode.ADD, new GravesLSTM.Builder().nIn(10).nOut(10).build()))
.layer(new RnnOutputLayer.Builder().lossFunction(LossFunctions.LossFunction.MSE)
.nIn(10).nOut(10).build())
.build();
MultiLayerNetwork net1 = new MultiLayerNetwork(conf1);
net1.init();
INDArray in = Nd4j.rand(new int[]{3, 10, 5});
INDArray labels = Nd4j.rand(new int[]{3, 10, 5});
net1.fit(in, labels);
byte[] bytes;
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ModelSerializer.writeModel(net1, baos, true);
bytes = baos.toByteArray();
}
MultiLayerNetwork net2 = ModelSerializer.restoreMultiLayerNetwork(new ByteArrayInputStream(bytes), true);
in = Nd4j.rand(new int[]{3, 10, 5});
labels = Nd4j.rand(new int[]{3, 10, 5});
INDArray out1 = net1.output(in);
INDArray out2 = net2.output(in);
assertEquals(out1, out2);
net1.setInput(in);
net2.setInput(in);
net1.setLabels(labels);
net2.setLabels(labels);
net1.computeGradientAndScore();
net2.computeGradientAndScore();
assertEquals(net1.score(), net2.score(), 1e-6);
assertEquals(net1.gradient().gradient(), net2.gradient().gradient());
}
}
示例8: testDenseActivationLayer
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testDenseActivationLayer() throws Exception {
DataSetIterator iter = new MnistDataSetIterator(2, 2);
DataSet next = iter.next();
// Run without separate activation layer
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(123)
.list()
.layer(0, new DenseLayer.Builder().nIn(28 * 28 * 1).nOut(10).activation(Activation.RELU)
.weightInit(WeightInit.XAVIER).build())
.layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).weightInit(WeightInit.XAVIER)
.activation(Activation.SOFTMAX).nIn(10).nOut(10).build())
.backprop(true).pretrain(false).build();
MultiLayerNetwork network = new MultiLayerNetwork(conf);
network.init();
network.fit(next);
// Run with separate activation layer
MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(123)
.list()
.layer(0, new DenseLayer.Builder().nIn(28 * 28 * 1).nOut(10).activation(Activation.IDENTITY)
.weightInit(WeightInit.XAVIER).build())
.layer(1, new org.deeplearning4j.nn.conf.layers.ActivationLayer.Builder()
.activation(Activation.RELU).build())
.layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).nIn(10).nOut(10)
.build())
.backprop(true).pretrain(false).build();
MultiLayerNetwork network2 = new MultiLayerNetwork(conf2);
network2.init();
network2.fit(next);
// check parameters
assertEquals(network.getLayer(0).getParam("W"), network2.getLayer(0).getParam("W"));
assertEquals(network.getLayer(1).getParam("W"), network2.getLayer(2).getParam("W"));
assertEquals(network.getLayer(0).getParam("b"), network2.getLayer(0).getParam("b"));
assertEquals(network.getLayer(1).getParam("b"), network2.getLayer(2).getParam("b"));
// check activations
network.init();
network.setInput(next.getFeatureMatrix());
List<INDArray> activations = network.feedForward(true);
network2.init();
network2.setInput(next.getFeatureMatrix());
List<INDArray> activations2 = network2.feedForward(true);
assertEquals(activations.get(1).reshape(activations2.get(2).shape()), activations2.get(2));
assertEquals(activations.get(2), activations2.get(3));
}
示例9: testAutoEncoderActivationLayer
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testAutoEncoderActivationLayer() throws Exception {
int minibatch = 3;
int nIn = 5;
int layerSize = 5;
int nOut = 3;
INDArray next = Nd4j.rand(new int[] {minibatch, nIn});
INDArray labels = Nd4j.zeros(minibatch, nOut);
for (int i = 0; i < minibatch; i++) {
labels.putScalar(i, i % nOut, 1.0);
}
// Run without separate activation layer
Nd4j.getRandom().setSeed(12345);
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(123)
.list()
.layer(0, new AutoEncoder.Builder().nIn(nIn).nOut(layerSize).corruptionLevel(0.0)
.activation(Activation.SIGMOID).build())
.layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.RECONSTRUCTION_CROSSENTROPY)
.activation(Activation.SOFTMAX).nIn(layerSize).nOut(nOut)
.build())
.backprop(true).pretrain(false).build();
MultiLayerNetwork network = new MultiLayerNetwork(conf);
network.init();
network.fit(next, labels); //Labels are necessary for this test: layer activation function affect pretraining results, otherwise
// Run with separate activation layer
Nd4j.getRandom().setSeed(12345);
MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(123)
.list()
.layer(0, new AutoEncoder.Builder().nIn(nIn).nOut(layerSize).corruptionLevel(0.0)
.activation(Activation.IDENTITY).build())
.layer(1, new org.deeplearning4j.nn.conf.layers.ActivationLayer.Builder()
.activation(Activation.SIGMOID).build())
.layer(2, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.RECONSTRUCTION_CROSSENTROPY)
.activation(Activation.SOFTMAX).nIn(layerSize).nOut(nOut)
.build())
.backprop(true).pretrain(false).build();
MultiLayerNetwork network2 = new MultiLayerNetwork(conf2);
network2.init();
network2.fit(next, labels);
// check parameters
assertEquals(network.getLayer(0).getParam("W"), network2.getLayer(0).getParam("W"));
assertEquals(network.getLayer(1).getParam("W"), network2.getLayer(2).getParam("W"));
assertEquals(network.getLayer(0).getParam("b"), network2.getLayer(0).getParam("b"));
assertEquals(network.getLayer(1).getParam("b"), network2.getLayer(2).getParam("b"));
// check activations
network.init();
network.setInput(next);
List<INDArray> activations = network.feedForward(true);
network2.init();
network2.setInput(next);
List<INDArray> activations2 = network2.feedForward(true);
assertEquals(activations.get(1).reshape(activations2.get(2).shape()), activations2.get(2));
assertEquals(activations.get(2), activations2.get(3));
}
示例10: testCNNActivationLayer
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testCNNActivationLayer() throws Exception {
DataSetIterator iter = new MnistDataSetIterator(2, 2);
DataSet next = iter.next();
// Run without separate activation layer
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(123)
.list()
.layer(0, new ConvolutionLayer.Builder(4, 4).stride(2, 2).nIn(1).nOut(20)
.activation(Activation.RELU).weightInit(WeightInit.XAVIER).build())
.layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).weightInit(WeightInit.XAVIER)
.activation(Activation.SOFTMAX).nOut(10).build())
.backprop(true).pretrain(false).setInputType(InputType.convolutionalFlat(28, 28, 1)).build();
MultiLayerNetwork network = new MultiLayerNetwork(conf);
network.init();
network.fit(next);
// Run with separate activation layer
MultiLayerConfiguration conf2 =
new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.seed(123).list()
.layer(0, new ConvolutionLayer.Builder(4, 4).stride(2, 2).nIn(1).nOut(20)
.activation(Activation.IDENTITY).weightInit(WeightInit.XAVIER)
.build())
.layer(1, new org.deeplearning4j.nn.conf.layers.ActivationLayer.Builder()
.activation(Activation.RELU).build())
.layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX)
.nOut(10).build())
.backprop(true).pretrain(false)
.setInputType(InputType.convolutionalFlat(28, 28, 1)).build();
MultiLayerNetwork network2 = new MultiLayerNetwork(conf2);
network2.init();
network2.fit(next);
// check parameters
assertEquals(network.getLayer(0).getParam("W"), network2.getLayer(0).getParam("W"));
assertEquals(network.getLayer(1).getParam("W"), network2.getLayer(2).getParam("W"));
assertEquals(network.getLayer(0).getParam("b"), network2.getLayer(0).getParam("b"));
// check activations
network.init();
network.setInput(next.getFeatureMatrix());
List<INDArray> activations = network.feedForward(true);
network2.init();
network2.setInput(next.getFeatureMatrix());
List<INDArray> activations2 = network2.feedForward(true);
assertEquals(activations.get(1).reshape(activations2.get(2).shape()), activations2.get(2));
assertEquals(activations.get(2), activations2.get(3));
}
示例11: testDropoutLayerWithDenseMnist
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testDropoutLayerWithDenseMnist() throws Exception {
DataSetIterator iter = new MnistDataSetIterator(2, 2);
DataSet next = iter.next();
// Run without separate activation layer
MultiLayerConfiguration confIntegrated = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(123)
.list()
.layer(0, new DenseLayer.Builder().nIn(28 * 28 * 1).nOut(10)
.activation(Activation.RELU).weightInit(
WeightInit.XAVIER)
.build())
.layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).dropOut(0.25)
.nIn(10).nOut(10).build())
.backprop(true).pretrain(false).build();
MultiLayerNetwork netIntegrated = new MultiLayerNetwork(confIntegrated);
netIntegrated.init();
netIntegrated.fit(next);
// Run with separate activation layer
MultiLayerConfiguration confSeparate = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).seed(123)
.list()
.layer(0, new DenseLayer.Builder().nIn(28 * 28 * 1).nOut(10).activation(Activation.RELU)
.weightInit(WeightInit.XAVIER).build())
.layer(1, new DropoutLayer.Builder(0.25).build())
.layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).nIn(10).nOut(10)
.build())
.backprop(true).pretrain(false).build();
MultiLayerNetwork netSeparate = new MultiLayerNetwork(confSeparate);
netSeparate.init();
netSeparate.fit(next);
// check parameters
assertEquals(netIntegrated.getLayer(0).getParam("W"), netSeparate.getLayer(0).getParam("W"));
assertEquals(netIntegrated.getLayer(0).getParam("b"), netSeparate.getLayer(0).getParam("b"));
assertEquals(netIntegrated.getLayer(1).getParam("W"), netSeparate.getLayer(2).getParam("W"));
assertEquals(netIntegrated.getLayer(1).getParam("b"), netSeparate.getLayer(2).getParam("b"));
// check activations
netIntegrated.setInput(next.getFeatureMatrix());
netSeparate.setInput(next.getFeatureMatrix());
Nd4j.getRandom().setSeed(12345);
List<INDArray> actTrainIntegrated = netIntegrated.feedForward(true);
Nd4j.getRandom().setSeed(12345);
List<INDArray> actTrainSeparate = netSeparate.feedForward(true);
assertEquals(actTrainIntegrated.get(1), actTrainSeparate.get(1));
assertEquals(actTrainIntegrated.get(2), actTrainSeparate.get(3));
Nd4j.getRandom().setSeed(12345);
List<INDArray> actTestIntegrated = netIntegrated.feedForward(false);
Nd4j.getRandom().setSeed(12345);
List<INDArray> actTestSeparate = netSeparate.feedForward(false);
assertEquals(actTestIntegrated.get(1), actTrainSeparate.get(1));
assertEquals(actTestIntegrated.get(2), actTestSeparate.get(3));
}
示例12: testDropoutLayerWithConvMnist
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testDropoutLayerWithConvMnist() throws Exception {
DataSetIterator iter = new MnistDataSetIterator(2, 2);
DataSet next = iter.next();
// Run without separate activation layer
Nd4j.getRandom().setSeed(12345);
MultiLayerConfiguration confIntegrated = new NeuralNetConfiguration.Builder().seed(123)
.list().layer(0,
new ConvolutionLayer.Builder(4, 4).stride(2, 2).nIn(1).nOut(20)
.activation(Activation.TANH).weightInit(WeightInit.XAVIER)
.build())
.layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).dropOut(0.5)
.nOut(10).build())
.backprop(true).pretrain(false).setInputType(InputType.convolutionalFlat(28, 28, 1)).build();
// Run with separate activation layer
Nd4j.getRandom().setSeed(12345);
//Manually configure preprocessors
//This is necessary, otherwise CnnToFeedForwardPreprocessor will be in different locatinos
//i.e., dropout on 4d activations in latter, and dropout on 2d activations in former
Map<Integer, InputPreProcessor> preProcessorMap = new HashMap<>();
preProcessorMap.put(1, new CnnToFeedForwardPreProcessor(13, 13, 20));
MultiLayerConfiguration confSeparate = new NeuralNetConfiguration.Builder().seed(123).list()
.layer(0, new ConvolutionLayer.Builder(4, 4).stride(2, 2).nIn(1).nOut(20)
.activation(Activation.TANH).weightInit(WeightInit.XAVIER).build())
.layer(1, new DropoutLayer.Builder(0.5).build())
.layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.weightInit(WeightInit.XAVIER).activation(Activation.SOFTMAX).nOut(10).build())
.inputPreProcessors(preProcessorMap).backprop(true).pretrain(false)
.setInputType(InputType.convolutionalFlat(28, 28, 1)).build();
Nd4j.getRandom().setSeed(12345);
MultiLayerNetwork netIntegrated = new MultiLayerNetwork(confIntegrated);
netIntegrated.init();
Nd4j.getRandom().setSeed(12345);
MultiLayerNetwork netSeparate = new MultiLayerNetwork(confSeparate);
netSeparate.init();
assertEquals(netIntegrated.params(), netSeparate.params());
Nd4j.getRandom().setSeed(12345);
netIntegrated.fit(next);
Nd4j.getRandom().setSeed(12345);
netSeparate.fit(next);
assertEquals(netIntegrated.params(), netSeparate.params());
// check parameters
assertEquals(netIntegrated.getLayer(0).getParam("W"), netSeparate.getLayer(0).getParam("W"));
assertEquals(netIntegrated.getLayer(0).getParam("b"), netSeparate.getLayer(0).getParam("b"));
assertEquals(netIntegrated.getLayer(1).getParam("W"), netSeparate.getLayer(2).getParam("W"));
assertEquals(netIntegrated.getLayer(1).getParam("b"), netSeparate.getLayer(2).getParam("b"));
// check activations
netIntegrated.setInput(next.getFeatureMatrix());
netSeparate.setInput(next.getFeatureMatrix());
Nd4j.getRandom().setSeed(12345);
List<INDArray> actTrainIntegrated = netIntegrated.feedForward(true);
Nd4j.getRandom().setSeed(12345);
List<INDArray> actTrainSeparate = netSeparate.feedForward(true);
assertEquals(actTrainIntegrated.get(1), actTrainSeparate.get(1));
assertEquals(actTrainIntegrated.get(2), actTrainSeparate.get(3));
Nd4j.getRandom().setSeed(12345);
List<INDArray> actTestIntegrated = netIntegrated.feedForward(false);
Nd4j.getRandom().setSeed(12345);
List<INDArray> actTestSeparate = netSeparate.feedForward(false);
assertEquals(actTestIntegrated.get(1), actTrainSeparate.get(1));
assertEquals(actTestIntegrated.get(2), actTestSeparate.get(3));
}
示例13: testCompareRnnOutputRnnLoss
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testCompareRnnOutputRnnLoss(){
Nd4j.getRandom().setSeed(12345);
int timeSeriesLength = 4;
int nIn = 5;
int layerSize = 6;
int nOut = 6;
int miniBatchSize = 3;
MultiLayerConfiguration conf1 =
new NeuralNetConfiguration.Builder().seed(12345L)
.updater(new NoOp())
.list()
.layer(new LSTM.Builder().nIn(nIn).nOut(layerSize).activation(Activation.TANH)
.weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0, 1.0))
.updater(new NoOp()).build())
.layer(new DenseLayer.Builder().nIn(layerSize).nOut(nOut).activation(Activation.IDENTITY).build())
.layer(new RnnLossLayer.Builder(LossFunction.MCXENT)
.activation(Activation.SOFTMAX)
.build())
.pretrain(false).backprop(true).build();
MultiLayerNetwork mln = new MultiLayerNetwork(conf1);
mln.init();
MultiLayerConfiguration conf2 =
new NeuralNetConfiguration.Builder().seed(12345L)
.updater(new NoOp())
.list()
.layer(new LSTM.Builder().nIn(nIn).nOut(layerSize).activation(Activation.TANH)
.weightInit(WeightInit.DISTRIBUTION).dist(new NormalDistribution(0, 1.0))
.updater(new NoOp()).build())
.layer(new org.deeplearning4j.nn.conf.layers.RnnOutputLayer.Builder(LossFunction.MCXENT)
.activation(Activation.SOFTMAX)
.nIn(layerSize).nOut(nOut)
.build())
.pretrain(false).backprop(true).build();
MultiLayerNetwork mln2 = new MultiLayerNetwork(conf2);
mln2.init();
mln2.setParams(mln.params());
INDArray in = Nd4j.rand(new int[]{miniBatchSize, nIn, timeSeriesLength});
INDArray out1 = mln.output(in);
INDArray out2 = mln.output(in);
assertEquals(out1, out2);
Random r = new Random(12345);
INDArray labels = Nd4j.create(miniBatchSize, nOut, timeSeriesLength);
for( int i=0; i<miniBatchSize; i++ ){
for( int j=0; j<timeSeriesLength; j++ ){
labels.putScalar(i, r.nextInt(nOut), j, 1.0);
}
}
mln.setInput(in);
mln.setLabels(labels);
mln2.setInput(in);
mln2.setLabels(labels);
mln.computeGradientAndScore();
mln2.computeGradientAndScore();
assertEquals(mln.gradient().gradient(), mln2.gradient().gradient());
assertEquals(mln.score(), mln2.score(), 1e-6);
TestUtils.testModelSerialization(mln);
}
示例14: testEmbeddingBackwardPass
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testEmbeddingBackwardPass() {
//With the same parameters, embedding layer should have same activations as the equivalent one-hot representation
// input with a DenseLayer
int nClassesIn = 10;
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().activation(Activation.TANH).list()
.layer(0, new EmbeddingLayer.Builder().hasBias(true).nIn(nClassesIn).nOut(5).build()).layer(1,
new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(5).nOut(4)
.activation(Activation.SOFTMAX).build())
.pretrain(false).backprop(true).build();
MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder().activation(Activation.TANH)
.weightInit(WeightInit.XAVIER).list()
.layer(0, new DenseLayer.Builder().nIn(nClassesIn).nOut(5).build()).layer(1,
new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT).nIn(5).nOut(4)
.activation(Activation.SOFTMAX).build())
.pretrain(false).backprop(true).build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
MultiLayerNetwork net2 = new MultiLayerNetwork(conf2);
net.init();
net2.init();
net2.setParams(net.params().dup());
int batchSize = 3;
INDArray inEmbedding = Nd4j.create(batchSize, 1);
INDArray inOneHot = Nd4j.create(batchSize, nClassesIn);
INDArray outLabels = Nd4j.create(batchSize, 4);
Random r = new Random(12345);
for (int i = 0; i < batchSize; i++) {
int classIdx = r.nextInt(nClassesIn);
inEmbedding.putScalar(i, classIdx);
inOneHot.putScalar(new int[] {i, classIdx}, 1.0);
int labelIdx = r.nextInt(4);
outLabels.putScalar(new int[] {i, labelIdx}, 1.0);
}
net.setInput(inEmbedding);
net2.setInput(inOneHot);
net.setLabels(outLabels);
net2.setLabels(outLabels);
net.computeGradientAndScore();
net2.computeGradientAndScore();
System.out.println(net.score() + "\t" + net2.score());
assertEquals(net2.score(), net.score(), 1e-6);
Map<String, INDArray> gradient = net.gradient().gradientForVariable();
Map<String, INDArray> gradient2 = net2.gradient().gradientForVariable();
assertEquals(gradient.size(), gradient2.size());
for (String s : gradient.keySet()) {
assertEquals(gradient2.get(s), gradient.get(s));
}
}
示例15: testEmbeddingLayerRNN
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testEmbeddingLayerRNN() {
int nClassesIn = 10;
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().activation(Activation.TANH).list()
.layer(0, new EmbeddingLayer.Builder().hasBias(true).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()).pretrain(false).backprop(true)
.build();
MultiLayerConfiguration conf2 = new NeuralNetConfiguration.Builder().activation(Activation.TANH)
.weightInit(WeightInit.XAVIER).list()
.layer(0, new DenseLayer.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()).pretrain(false).backprop(true)
.build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
MultiLayerNetwork net2 = new MultiLayerNetwork(conf2);
net.init();
net2.init();
net2.setParams(net.params().dup());
int batchSize = 3;
int timeSeriesLength = 8;
INDArray inEmbedding = Nd4j.create(batchSize, 1, timeSeriesLength);
INDArray inOneHot = Nd4j.create(batchSize, nClassesIn, timeSeriesLength);
INDArray outLabels = Nd4j.create(batchSize, 4, timeSeriesLength);
Random r = new Random(12345);
for (int i = 0; i < batchSize; i++) {
for (int j = 0; j < timeSeriesLength; j++) {
int classIdx = r.nextInt(nClassesIn);
inEmbedding.putScalar(new int[] {i, 0, j}, classIdx);
inOneHot.putScalar(new int[] {i, classIdx, j}, 1.0);
int labelIdx = r.nextInt(4);
outLabels.putScalar(new int[] {i, labelIdx, j}, 1.0);
}
}
net.setInput(inEmbedding);
net2.setInput(inOneHot);
net.setLabels(outLabels);
net2.setLabels(outLabels);
net.computeGradientAndScore();
net2.computeGradientAndScore();
System.out.println(net.score() + "\t" + net2.score());
assertEquals(net2.score(), net.score(), 1e-6);
Map<String, INDArray> gradient = net.gradient().gradientForVariable();
Map<String, INDArray> gradient2 = net2.gradient().gradientForVariable();
assertEquals(gradient.size(), gradient2.size());
for (String s : gradient.keySet()) {
assertEquals(gradient2.get(s), gradient.get(s));
}
}