本文整理汇总了Java中org.deeplearning4j.nn.conf.distribution.UniformDistribution类的典型用法代码示例。如果您正苦于以下问题:Java UniformDistribution类的具体用法?Java UniformDistribution怎么用?Java UniformDistribution使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
UniformDistribution类属于org.deeplearning4j.nn.conf.distribution包,在下文中一共展示了UniformDistribution类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。
示例1: getConfiguration
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
@Override
protected MultiLayerConfiguration getConfiguration()
{
final int[] hiddenLayerNodes = parameters.getHiddeLayerNodes();
final int nLayers = hiddenLayerNodes.length + 1;
final ListBuilder list = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.iterations(parameters.getIterations()).learningRate(parameters.getLearningRate()).rmsDecay(0.95)
.seed(parameters.getSeed()).regularization(true).l2(0.001).list(nLayers).pretrain(false).backprop(true);
for (int i = 0; i < nLayers; i++)
{
int nIn;
if (i == 0)
{
nIn = parameters.getInputSize();
}
else
{
nIn = hiddenLayerNodes[i - 1];
}
if (i < nLayers - 1)
{
final GravesLSTM layer = new GravesLSTM.Builder().nIn(nIn).nOut(hiddenLayerNodes[i])
.updater(Updater.RMSPROP).activation("tanh").weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(-0.08, 0.08)).build();
list.layer(i, layer);
}
else
{
final RnnOutputLayer outputLayer = new RnnOutputLayer.Builder(LossFunction.MCXENT).activation("softmax")
.updater(Updater.RMSPROP).nIn(hiddenLayerNodes[1]).nOut(parameters.getOutputSize())
.weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(-0.08, 0.08)).build();
list.layer(i, outputLayer);
}
}
return list.build();
}
开发者ID:amrabed,项目名称:DL4J,代码行数:40,代码来源:LstmModel.java示例2: testSerialization
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
@Test
public void testSerialization() {
final MultiLayerConfiguration conf1 = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(new AdaGrad(0.1))
.l2(0.001)
.seed(12345).list().pretrain(false)
.layer(0, new org.deeplearning4j.nn.conf.layers.GravesBidirectionalLSTM.Builder()
.activation(Activation.TANH).nIn(2).nOut(2).weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(-0.05, 0.05)).build())
.layer(1, new org.deeplearning4j.nn.conf.layers.GravesBidirectionalLSTM.Builder()
.activation(Activation.TANH).nIn(2).nOut(2).weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(-0.05, 0.05)).build())
.layer(2, new org.deeplearning4j.nn.conf.layers.RnnOutputLayer.Builder()
.lossFunction(LossFunctions.LossFunction.MCXENT).nIn(2).nOut(2)
.activation(Activation.TANH).build())
.backprop(true).build();
final String json1 = conf1.toJson();
final MultiLayerConfiguration conf2 = MultiLayerConfiguration.fromJson(json1);
final String json2 = conf1.toJson();
TestCase.assertEquals(json1, json2);
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:30,代码来源:GravesBidirectionalLSTMTest.java示例3: main
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
public static void main( String[] args ) throws Exception {
int lstmLayerSize = 200; //Number of units in each GravesLSTM layer
int miniBatchSize = 32; //Size of mini batch to use when training
int examplesPerEpoch = 50 * miniBatchSize; //i.e., how many examples to learn on between generating samples
int exampleLength = 100; //Length of each training example
int numEpochs = 30; //Total number of training + sample generation epochs
int nSamplesToGenerate = 4; //Number of samples to generate after each training epoch
int nCharactersToSample = 300; //Length of each sample to generate
String generationInitialization = null; //Optional character initialization; a random character is used if null
// Above is Used to 'prime' the LSTM with a character sequence to continue/complete.
// Initialization characters must all be in CharacterIterator.getMinimalCharacterSet() by default
Random rng = new Random(12345);
//Get a DataSetIterator that handles vectorization of text into something we can use to train
// our GravesLSTM network.
CharacterIterator iter = getShakespeareIterator(miniBatchSize,exampleLength,examplesPerEpoch);
int nOut = iter.totalOutcomes();
//Set up network configuration:
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1)
.learningRate(0.1)
.rmsDecay(0.95)
.seed(12345)
.regularization(true)
.l2(0.001)
.list(3)
.layer(0, new GravesLSTM.Builder().nIn(iter.inputColumns()).nOut(lstmLayerSize)
.updater(Updater.RMSPROP)
.activation("tanh").weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(-0.08, 0.08)).build())
.layer(1, new GravesLSTM.Builder().nIn(lstmLayerSize).nOut(lstmLayerSize)
.updater(Updater.RMSPROP)
.activation("tanh").weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(-0.08, 0.08)).build())
.layer(2, new RnnOutputLayer.Builder(LossFunction.MCXENT).activation("softmax") //MCXENT + softmax for classification
.updater(Updater.RMSPROP)
.nIn(lstmLayerSize).nOut(nOut).weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(-0.08, 0.08)).build())
.pretrain(false).backprop(true)
.build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
net.setListeners(new ScoreIterationListener(1));
//Print the number of parameters in the network (and for each layer)
Layer[] layers = net.getLayers();
int totalNumParams = 0;
for( int i=0; i<layers.length; i++ ){
int nParams = layers[i].numParams();
System.out.println("Number of parameters in layer " + i + ": " + nParams);
totalNumParams += nParams;
}
System.out.println("Total number of network parameters: " + totalNumParams);
//Do training, and then generate and print samples from network
for( int i=0; i<numEpochs; i++ ){
net.fit(iter);
System.out.println("--------------------");
System.out.println("Completed epoch " + i );
System.out.println("Sampling characters from network given initialization \""+ (generationInitialization == null ? "" : generationInitialization) +"\"");
String[] samples = sampleCharactersFromNetwork(generationInitialization,net,iter,rng,nCharactersToSample,nSamplesToGenerate);
for( int j=0; j<samples.length; j++ ){
System.out.println("----- Sample " + j + " -----");
System.out.println(samples[j]);
System.out.println();
}
iter.reset(); //Reset iterator for another epoch
}
System.out.println("\n\nExample complete");
}
开发者ID:jpatanooga,项目名称:dl4j-rnn-timeseries-examples,代码行数:76,代码来源:GravesLSTMCharModellingExample.java示例4: main
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
public static void main( String[] args ) throws Exception {
int lstmLayerSize = 200; //Number of units in each GravesLSTM layer
int miniBatchSize = 32; //Size of mini batch to use when training
int examplesPerEpoch = 50 * miniBatchSize; //i.e., how many examples to learn on between generating samples
int exampleLength = 100; //Length of each training example
int numEpochs = 30; //Total number of training + sample generation epochs
int nSamplesToGenerate = 4; //Number of samples to generate after each training epoch
int nCharactersToSample = 130; //Length of each sample to generate
String generationInitialization = null; //Optional character initialization; a random character is used if null
// Above is Used to 'prime' the LSTM with a character sequence to continue/complete.
// Initialization characters must all be in CharacterIterator.getMinimalCharacterSet() by default
Random rng = new Random(12345);
//Get a DataSetIterator that handles vectorization of text into something we can use to train
// our GravesLSTM network.
CharacterIterator iter = getShakespeareIterator(miniBatchSize,exampleLength,examplesPerEpoch);
int nOut = iter.totalOutcomes();
//Set up network configuration:
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1)
.learningRate(0.1)
.rmsDecay(0.95)
.seed(12345)
.regularization(true)
.l2(0.001)
.list(3)
.layer(0, new GravesLSTM.Builder().nIn(iter.inputColumns()).nOut(lstmLayerSize)
.updater(Updater.RMSPROP)
.activation("tanh").weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(-0.08, 0.08)).build())
.layer(1, new GravesLSTM.Builder().nIn(lstmLayerSize).nOut(lstmLayerSize)
.updater(Updater.RMSPROP)
.activation("tanh").weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(-0.08, 0.08)).build())
.layer(2, new RnnOutputLayer.Builder(LossFunction.MCXENT).activation("softmax") //MCXENT + softmax for classification
.updater(Updater.RMSPROP)
.nIn(lstmLayerSize).nOut(nOut).weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(-0.08, 0.08)).build())
.pretrain(false).backprop(true)
.build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
net.setListeners(new ScoreIterationListener(1));
//Print the number of parameters in the network (and for each layer)
Layer[] layers = net.getLayers();
int totalNumParams = 0;
for( int i=0; i<layers.length; i++ ){
int nParams = layers[i].numParams();
System.out.println("Number of parameters in layer " + i + ": " + nParams);
totalNumParams += nParams;
}
System.out.println("Total number of network parameters: " + totalNumParams);
String[] initStrings = { "diary", "gozer", "are", "I", "dear" };
//Do training, and then generate and print samples from network
for( int i=0; i<numEpochs; i++ ){
net.fit(iter);
System.out.println("--------------------");
System.out.println("Completed epoch " + i );
System.out.println("Sampling characters from network given initialization \""+ (generationInitialization == null ? "" : generationInitialization) +"\"");
String[] samples = sampleCharactersFromNetwork( initStrings[ i % initStrings.length ] ,net,iter,rng,nCharactersToSample,nSamplesToGenerate);
for( int j=0; j<samples.length; j++ ){
System.out.println("----- Sample " + j + " -----");
System.out.println( "Init String: " + initStrings[ i % initStrings.length ] );
System.out.println(samples[j]);
System.out.println();
}
iter.reset(); //Reset iterator for another epoch
}
System.out.println("\n\nExample complete");
}
开发者ID:jpatanooga,项目名称:dl4j-rnn-timeseries-examples,代码行数:79,代码来源:LSTM_GenericTimeseriesClassification.java示例5: elementWiseMultiplicationLayerTest
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
@Test
public void elementWiseMultiplicationLayerTest(){
for(Activation a : new Activation[]{Activation.IDENTITY, Activation.TANH}) {
ComputationGraphConfiguration conf = new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.CONJUGATE_GRADIENT).updater(new NoOp())
.seed(12345L)
.weightInit(new UniformDistribution(0, 1))
.graphBuilder()
.addInputs("features")
.addLayer("dense", new DenseLayer.Builder().nIn(4).nOut(4)
.activation(Activation.TANH)
.build(), "features")
.addLayer("elementWiseMul", new ElementWiseMultiplicationLayer.Builder().nIn(4).nOut(4)
.activation(a)
.build(), "dense")
.addLayer("loss", new LossLayer.Builder(LossFunctions.LossFunction.COSINE_PROXIMITY)
.activation(Activation.IDENTITY).build(), "elementWiseMul")
.setOutputs("loss")
.pretrain(false).backprop(true).build();
ComputationGraph netGraph = new ComputationGraph(conf);
netGraph.init();
log.info("params before learning: " + netGraph.getLayer(1).paramTable());
//Run a number of iterations of learning manually make some pseudo data
//the ides is simple: since we do a element wise multiplication layer (just a scaling), we want the cos sim
// is mainly decided by the fourth value, if everything runs well, we will get a large weight for the fourth value
INDArray features = Nd4j.create(new double[][]{{1, 2, 3, 4}, {1, 2, 3, 1}, {1, 2, 3, 0}});
INDArray labels = Nd4j.create(new double[][]{{1, 1, 1, 8}, {1, 1, 1, 2}, {1, 1, 1, 1}});
netGraph.setInputs(features);
netGraph.setLabels(labels);
netGraph.computeGradientAndScore();
double scoreBefore = netGraph.score();
String msg;
for (int epoch = 0; epoch < 5; epoch++)
netGraph.fit(new INDArray[]{features}, new INDArray[]{labels});
netGraph.computeGradientAndScore();
double scoreAfter = netGraph.score();
//Can't test in 'characteristic mode of operation' if not learning
msg = "elementWiseMultiplicationLayerTest() - score did not (sufficiently) decrease during learning - activationFn="
+ "Id" + ", lossFn=" + "Cos-sim" + ", outputActivation=" + "Id"
+ ", doLearningFirst=" + "true" + " (before=" + scoreBefore
+ ", scoreAfter=" + scoreAfter + ")";
assertTrue(msg, scoreAfter < 0.8 * scoreBefore);
// expectation in case linear regression(with only element wise multiplication layer): large weight for the fourth weight
log.info("params after learning: " + netGraph.getLayer(1).paramTable());
boolean gradOK = checkGradients(netGraph, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, new INDArray[]{features}, new INDArray[]{labels});
msg = "elementWiseMultiplicationLayerTest() - activationFn=" + "ID" + ", lossFn=" + "Cos-sim"
+ ", outputActivation=" + "Id" + ", doLearningFirst=" + "true";
assertTrue(msg, gradOK);
TestUtils.testModelSerialization(netGraph);
}
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:65,代码来源:GradientCheckTests.java示例6: testGradientCnnFfRnn
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
@Test
public void testGradientCnnFfRnn() {
//Test gradients with CNN -> FF -> LSTM -> RnnOutputLayer
//time series input/output (i.e., video classification or similar)
int nChannelsIn = 3;
int inputSize = 10 * 10 * nChannelsIn; //10px x 10px x 3 channels
int miniBatchSize = 4;
int timeSeriesLength = 10;
int nClasses = 3;
//Generate
Nd4j.getRandom().setSeed(12345);
INDArray input = Nd4j.rand(new int[] {miniBatchSize, inputSize, timeSeriesLength});
INDArray labels = Nd4j.zeros(miniBatchSize, nClasses, timeSeriesLength);
Random r = new Random(12345);
for (int i = 0; i < miniBatchSize; i++) {
for (int j = 0; j < timeSeriesLength; j++) {
int idx = r.nextInt(nClasses);
labels.putScalar(new int[] {i, idx, j}, 1.0);
}
}
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().updater(new NoOp()).seed(12345)
.weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(-2, 2)).list()
.layer(0, new ConvolutionLayer.Builder(5, 5).nIn(3).nOut(5).stride(1, 1)
.activation(Activation.TANH).build()) //Out: (10-5)/1+1 = 6 -> 6x6x5
.layer(1, new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2)
.stride(1, 1).build()) //Out: (6-2)/1+1 = 5 -> 5x5x5
.layer(2, new DenseLayer.Builder().nIn(5 * 5 * 5).nOut(4).activation(Activation.TANH).build())
.layer(3, new GravesLSTM.Builder().nIn(4).nOut(3).activation(Activation.TANH).build())
.layer(4, new RnnOutputLayer.Builder().lossFunction(LossFunction.MCXENT).nIn(3).nOut(nClasses)
.activation(Activation.SOFTMAX).build())
.setInputType(InputType.convolutional(10, 10, 3)).pretrain(false).backprop(true).build();
//Here: ConvolutionLayerSetup in config builder doesn't know that we are expecting time series input, not standard FF input -> override it here
conf.getInputPreProcessors().put(0, new RnnToCnnPreProcessor(10, 10, 3));
MultiLayerNetwork mln = new MultiLayerNetwork(conf);
mln.init();
System.out.println("Params per layer:");
for (int i = 0; i < mln.getnLayers(); i++) {
System.out.println("layer " + i + "\t" + mln.getLayer(i).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);
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:52,代码来源:LSTMGradientCheckTests.java示例7: testLSTMWithMerging
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
@Test
public void testLSTMWithMerging() {
Nd4j.getRandom().setSeed(12345);
ComputationGraphConfiguration conf =
new NeuralNetConfiguration.Builder().seed(12345)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(0.2, 0.6))
.updater(new NoOp()).graphBuilder().addInputs("input")
.setOutputs("out")
.addLayer("lstm1",
new GravesLSTM.Builder().nIn(3).nOut(4)
.activation(Activation.TANH).build(),
"input")
.addLayer("lstm2",
new GravesLSTM.Builder().nIn(4).nOut(4)
.activation(Activation.TANH).build(),
"lstm1")
.addLayer("dense1",
new DenseLayer.Builder().nIn(4).nOut(4)
.activation(Activation.SIGMOID).build(),
"lstm1")
.addLayer("lstm3",
new GravesLSTM.Builder().nIn(4).nOut(4)
.activation(Activation.TANH).build(),
"dense1")
.addVertex("merge", new MergeVertex(), "lstm2", "lstm3")
.addLayer("out", new RnnOutputLayer.Builder().nIn(8).nOut(3)
.activation(Activation.SOFTMAX)
.lossFunction(LossFunctions.LossFunction.MCXENT).build(),
"merge")
.inputPreProcessor("dense1", new RnnToFeedForwardPreProcessor())
.inputPreProcessor("lstm3", new FeedForwardToRnnPreProcessor()).pretrain(false)
.backprop(true).build();
ComputationGraph graph = new ComputationGraph(conf);
graph.init();
Random r = new Random(12345);
INDArray input = Nd4j.rand(new int[] {3, 3, 5});
INDArray labels = Nd4j.zeros(3, 3, 5);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
labels.putScalar(new int[] {i, r.nextInt(3), j}, 1.0);
}
}
if (PRINT_RESULTS) {
System.out.println("testLSTMWithMerging()");
for (int j = 0; j < graph.getNumLayers(); j++)
System.out.println("Layer " + j + " # params: " + graph.getLayer(j).numParams());
}
boolean gradOK = GradientCheckUtil.checkGradients(graph, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, new INDArray[] {input},
new INDArray[] {labels});
String msg = "testLSTMWithMerging()";
assertTrue(msg, gradOK);
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:61,代码来源:GradientCheckTestsComputationGraph.java示例8: testRWInit
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
@Test
public void testRWInit() {
for (boolean rwInit : new boolean[]{false, true}) {
for (int i = 0; i < 3; i++) {
NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder()
.weightInit(new UniformDistribution(0, 1))
.list();
if(rwInit) {
switch (i) {
case 0:
b.layer(new LSTM.Builder().nIn(10).nOut(10)
.weightInitRecurrent(new UniformDistribution(2, 3))
.build());
break;
case 1:
b.layer(new GravesLSTM.Builder().nIn(10).nOut(10)
.weightInitRecurrent(new UniformDistribution(2, 3))
.build());
break;
case 2:
b.layer(new SimpleRnn.Builder().nIn(10).nOut(10)
.weightInitRecurrent(new UniformDistribution(2, 3)).build());
break;
default:
throw new RuntimeException();
}
} else {
switch (i) {
case 0:
b.layer(new LSTM.Builder().nIn(10).nOut(10).build());
break;
case 1:
b.layer(new GravesLSTM.Builder().nIn(10).nOut(10).build());
break;
case 2:
b.layer(new SimpleRnn.Builder().nIn(10).nOut(10).build());
break;
default:
throw new RuntimeException();
}
}
MultiLayerNetwork net = new MultiLayerNetwork(b.build());
net.init();
INDArray rw = net.getParam("0_RW");
double min = rw.minNumber().doubleValue();
double max = rw.maxNumber().doubleValue();
if(rwInit){
assertTrue(String.valueOf(min), min >= 2.0);
assertTrue(String.valueOf(max), max <= 3.0);
} else {
assertTrue(String.valueOf(min), min >= 0.0);
assertTrue(String.valueOf(max), max <= 1.0);
}
}
}
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:62,代码来源:TestRecurrentWeightInit.java示例9: testGravesBackwardBasicHelper
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
private static void testGravesBackwardBasicHelper(int nIn, int nOut, int lstmNHiddenUnits, int miniBatchSize,
int timeSeriesLength) {
INDArray inputData = Nd4j.ones(miniBatchSize, nIn, timeSeriesLength);
NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder()
.layer(new org.deeplearning4j.nn.conf.layers.GravesBidirectionalLSTM.Builder().nIn(nIn)
.nOut(lstmNHiddenUnits).weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(0, 1)).activation(Activation.TANH).build())
.build();
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray params = Nd4j.create(1, numParams);
GravesBidirectionalLSTM lstm =
(GravesBidirectionalLSTM) conf.getLayer().instantiate(conf, null, 0, params, true);
lstm.setBackpropGradientsViewArray(Nd4j.create(1, conf.getLayer().initializer().numParams(conf)));
//Set input, do a forward pass:
lstm.activate(inputData);
assertNotNull(lstm.input());
INDArray epsilon = Nd4j.ones(miniBatchSize, lstmNHiddenUnits, timeSeriesLength);
Pair<Gradient, INDArray> out = lstm.backpropGradient(epsilon);
Gradient outGradient = out.getFirst();
INDArray nextEpsilon = out.getSecond();
INDArray biasGradientF = outGradient.getGradientFor(GravesBidirectionalLSTMParamInitializer.BIAS_KEY_FORWARDS);
INDArray inWeightGradientF =
outGradient.getGradientFor(GravesBidirectionalLSTMParamInitializer.INPUT_WEIGHT_KEY_FORWARDS);
INDArray recurrentWeightGradientF = outGradient
.getGradientFor(GravesBidirectionalLSTMParamInitializer.RECURRENT_WEIGHT_KEY_FORWARDS);
assertNotNull(biasGradientF);
assertNotNull(inWeightGradientF);
assertNotNull(recurrentWeightGradientF);
INDArray biasGradientB = outGradient.getGradientFor(GravesBidirectionalLSTMParamInitializer.BIAS_KEY_BACKWARDS);
INDArray inWeightGradientB =
outGradient.getGradientFor(GravesBidirectionalLSTMParamInitializer.INPUT_WEIGHT_KEY_BACKWARDS);
INDArray recurrentWeightGradientB = outGradient
.getGradientFor(GravesBidirectionalLSTMParamInitializer.RECURRENT_WEIGHT_KEY_BACKWARDS);
assertNotNull(biasGradientB);
assertNotNull(inWeightGradientB);
assertNotNull(recurrentWeightGradientB);
assertArrayEquals(biasGradientF.shape(), new int[] {1, 4 * lstmNHiddenUnits});
assertArrayEquals(inWeightGradientF.shape(), new int[] {nIn, 4 * lstmNHiddenUnits});
assertArrayEquals(recurrentWeightGradientF.shape(), new int[] {lstmNHiddenUnits, 4 * lstmNHiddenUnits + 3});
assertArrayEquals(biasGradientB.shape(), new int[] {1, 4 * lstmNHiddenUnits});
assertArrayEquals(inWeightGradientB.shape(), new int[] {nIn, 4 * lstmNHiddenUnits});
assertArrayEquals(recurrentWeightGradientB.shape(), new int[] {lstmNHiddenUnits, 4 * lstmNHiddenUnits + 3});
assertNotNull(nextEpsilon);
assertArrayEquals(nextEpsilon.shape(), new int[] {miniBatchSize, nIn, timeSeriesLength});
//Check update:
for (String s : outGradient.gradientForVariable().keySet()) {
lstm.update(outGradient.getGradientFor(s), s);
}
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:61,代码来源:GravesBidirectionalLSTMTest.java示例10: testGravesBidirectionalLSTMForwardPassHelper
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
@Test
public void testGravesBidirectionalLSTMForwardPassHelper() throws Exception {
//GravesBidirectionalLSTM.activateHelper() has different behaviour (due to optimizations) when forBackprop==true vs false
//But should otherwise provide identical activations
Nd4j.getRandom().setSeed(12345);
final int nIn = 10;
final int layerSize = 15;
final int miniBatchSize = 4;
final int timeSeriesLength = 7;
final NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder()
.layer(new org.deeplearning4j.nn.conf.layers.GravesBidirectionalLSTM.Builder().nIn(nIn)
.nOut(layerSize).weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(0, 1)).activation(Activation.TANH).build())
.build();
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray params = Nd4j.create(1, numParams);
final GravesBidirectionalLSTM lstm =
(GravesBidirectionalLSTM) conf.getLayer().instantiate(conf, null, 0, params, true);
final INDArray input = Nd4j.rand(new int[] {miniBatchSize, nIn, timeSeriesLength});
lstm.setInput(input);
final INDArray fwdPassFalse = LSTMHelpers.activateHelper(lstm, lstm.conf(), new ActivationSigmoid(),
lstm.input(),
lstm.getParam(GravesBidirectionalLSTMParamInitializer.RECURRENT_WEIGHT_KEY_FORWARDS),
lstm.getParam(GravesBidirectionalLSTMParamInitializer.INPUT_WEIGHT_KEY_FORWARDS),
lstm.getParam(GravesBidirectionalLSTMParamInitializer.BIAS_KEY_FORWARDS), false, null, null,
false, true, GravesBidirectionalLSTMParamInitializer.INPUT_WEIGHT_KEY_FORWARDS, null, true,
null, CacheMode.NONE).fwdPassOutput;
final INDArray[] fwdPassTrue = LSTMHelpers.activateHelper(lstm, lstm.conf(), new ActivationSigmoid(),
lstm.input(),
lstm.getParam(GravesBidirectionalLSTMParamInitializer.RECURRENT_WEIGHT_KEY_FORWARDS),
lstm.getParam(GravesBidirectionalLSTMParamInitializer.INPUT_WEIGHT_KEY_FORWARDS),
lstm.getParam(GravesBidirectionalLSTMParamInitializer.BIAS_KEY_FORWARDS), false, null, null,
true, true, GravesBidirectionalLSTMParamInitializer.INPUT_WEIGHT_KEY_FORWARDS, null, true, null,
CacheMode.NONE).fwdPassOutputAsArrays;
//I have no idea what the heck this does --Ben
for (int i = 0; i < timeSeriesLength; i++) {
final INDArray sliceFalse = fwdPassFalse.tensorAlongDimension(i, 1, 0);
final INDArray sliceTrue = fwdPassTrue[i];
assertTrue(sliceFalse.equals(sliceTrue));
}
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:49,代码来源:GravesBidirectionalLSTMTest.java示例11: testGravesBackwardBasicHelper
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
private static void testGravesBackwardBasicHelper(int nIn, int nOut, int lstmNHiddenUnits, int miniBatchSize,
int timeSeriesLength) {
INDArray inputData = Nd4j.ones(miniBatchSize, nIn, timeSeriesLength);
NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder()
.layer(new org.deeplearning4j.nn.conf.layers.GravesLSTM.Builder().nIn(nIn)
.nOut(lstmNHiddenUnits).weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(0, 1)).activation(Activation.TANH).build())
.build();
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray params = Nd4j.create(1, numParams);
GravesLSTM lstm = (GravesLSTM) conf.getLayer().instantiate(conf, null, 0, params, true);
lstm.setBackpropGradientsViewArray(Nd4j.create(1, conf.getLayer().initializer().numParams(conf)));
//Set input, do a forward pass:
lstm.activate(inputData);
assertNotNull(lstm.input());
INDArray epsilon = Nd4j.ones(miniBatchSize, lstmNHiddenUnits, timeSeriesLength);
Pair<Gradient, INDArray> out = lstm.backpropGradient(epsilon);
Gradient outGradient = out.getFirst();
INDArray nextEpsilon = out.getSecond();
INDArray biasGradient = outGradient.getGradientFor(GravesLSTMParamInitializer.BIAS_KEY);
INDArray inWeightGradient = outGradient.getGradientFor(GravesLSTMParamInitializer.INPUT_WEIGHT_KEY);
INDArray recurrentWeightGradient = outGradient.getGradientFor(GravesLSTMParamInitializer.RECURRENT_WEIGHT_KEY);
assertNotNull(biasGradient);
assertNotNull(inWeightGradient);
assertNotNull(recurrentWeightGradient);
assertArrayEquals(biasGradient.shape(), new int[] {1, 4 * lstmNHiddenUnits});
assertArrayEquals(inWeightGradient.shape(), new int[] {nIn, 4 * lstmNHiddenUnits});
assertArrayEquals(recurrentWeightGradient.shape(), new int[] {lstmNHiddenUnits, 4 * lstmNHiddenUnits + 3});
assertNotNull(nextEpsilon);
assertArrayEquals(nextEpsilon.shape(), new int[] {miniBatchSize, nIn, timeSeriesLength});
//Check update:
for (String s : outGradient.gradientForVariable().keySet()) {
lstm.update(outGradient.getGradientFor(s), s);
}
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:45,代码来源:GravesLSTMTest.java示例12: testGravesLSTMForwardPassHelper
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
@Test
public void testGravesLSTMForwardPassHelper() throws Exception {
//GravesLSTM.activateHelper() has different behaviour (due to optimizations) when forBackprop==true vs false
//But should otherwise provide identical activations
Nd4j.getRandom().setSeed(12345);
int nIn = 10;
int layerSize = 15;
int miniBatchSize = 4;
int timeSeriesLength = 7;
NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder()
.layer(new org.deeplearning4j.nn.conf.layers.GravesLSTM.Builder().nIn(nIn).nOut(layerSize)
.weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(0, 1))
.activation(Activation.TANH).build())
.build();
int numParams = conf.getLayer().initializer().numParams(conf);
INDArray params = Nd4j.create(1, numParams);
GravesLSTM lstm = (GravesLSTM) conf.getLayer().instantiate(conf, null, 0, params, true);
INDArray input = Nd4j.rand(new int[] {miniBatchSize, nIn, timeSeriesLength});
lstm.setInput(input);
Method actHelper = GravesLSTM.class.getDeclaredMethod("activateHelper", boolean.class, INDArray.class,
INDArray.class, boolean.class);
actHelper.setAccessible(true);
//Call activateHelper with both forBackprop == true, and forBackprop == false and compare
Class<?> innerClass = Class.forName("org.deeplearning4j.nn.layers.recurrent.FwdPassReturn");
Object oFalse = actHelper.invoke(lstm, false, null, null, false); //GravesLSTM.FwdPassReturn object; want fwdPassOutput INDArray
Object oTrue = actHelper.invoke(lstm, false, null, null, true); //want fwdPassOutputAsArrays object
Field fwdPassOutput = innerClass.getDeclaredField("fwdPassOutput");
fwdPassOutput.setAccessible(true);
Field fwdPassOutputAsArrays = innerClass.getDeclaredField("fwdPassOutputAsArrays");
fwdPassOutputAsArrays.setAccessible(true);
INDArray fwdPassFalse = (INDArray) fwdPassOutput.get(oFalse);
INDArray[] fwdPassTrue = (INDArray[]) fwdPassOutputAsArrays.get(oTrue);
for (int i = 0; i < timeSeriesLength; i++) {
INDArray sliceFalse = fwdPassFalse.tensorAlongDimension(i, 1, 0);
INDArray sliceTrue = fwdPassTrue[i];
assertTrue(sliceFalse.equals(sliceTrue));
}
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:49,代码来源:GravesLSTMTest.java示例13: testPreTraining
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
@Test
public void testPreTraining() {
ComputationGraphConfiguration conf =
new NeuralNetConfiguration.Builder()
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.updater(new Sgd(1e-6))
.l2(2e-4).graphBuilder().addInputs("in")
.addLayer("layer0",
new VariationalAutoencoder.Builder().nIn(4).nOut(3)
.weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(0,
1))
.activation(Activation.TANH)
.lossFunction(LossFunctions.LossFunction.KL_DIVERGENCE)
.build(),
"in")
.addLayer("layer1",
new VariationalAutoencoder.Builder().nIn(4).nOut(3)
.weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(0,
1))
.activation(Activation.TANH)
.lossFunction(LossFunctions.LossFunction.KL_DIVERGENCE)
.build(),
"in")
.addLayer("layer2",
new VariationalAutoencoder.Builder().nIn(3).nOut(3)
.weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(0,
1))
.activation(Activation.TANH)
.lossFunction(LossFunctions.LossFunction.KL_DIVERGENCE)
.build(),
"layer1")
.addLayer("out", new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
LossFunctions.LossFunction.MCXENT).nIn(3 + 3).nOut(3)
.weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(0, 1))
.activation(Activation.SOFTMAX).build(),
"layer0", "layer2")
.setOutputs("out").pretrain(true).backprop(false).build();
ComputationGraph net = new ComputationGraph(conf);
net.init();
net.setListeners(new ScoreIterationListener(1));
DataSetIterator iter = new IrisDataSetIterator(10, 150);
net.fit(iter);
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:51,代码来源:TestComputationGraphNetwork.java示例14: testConvolutionalNoBias
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
@Test
public void testConvolutionalNoBias() throws Exception {
int[] minibatchSizes = {1, 4};
int width = 6;
int height = 6;
int inputDepth = 2;
int nOut = 3;
Field f = org.deeplearning4j.nn.layers.convolution.ConvolutionLayer.class.getDeclaredField("helper");
f.setAccessible(true);
Random r = new Random(12345);
for (int minibatchSize : minibatchSizes) {
for (boolean convHasBias : new boolean[]{true, false}) {
INDArray input = Nd4j.rand(new int[]{minibatchSize, inputDepth, height, width});
INDArray labels = Nd4j.zeros(minibatchSize, nOut);
for (int i = 0; i < minibatchSize; i++) {
labels.putScalar(i, r.nextInt(nOut), 1.0);
}
MultiLayerConfiguration.Builder builder = new NeuralNetConfiguration.Builder()
.weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(-1, 1))
.updater(new NoOp()).seed(12345L)
.list()
.layer(0, new ConvolutionLayer.Builder(2, 2).stride(2, 2).padding(1, 1).nOut(3)
.hasBias(convHasBias)
.activation(Activation.TANH).build())
.layer(1, new ConvolutionLayer.Builder(2, 2).stride(2, 2).padding(0, 0).nOut(3)
.hasBias(convHasBias)
.activation(Activation.TANH).build())
.layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
.activation(Activation.SOFTMAX).nOut(nOut).build())
.setInputType(InputType.convolutional(height, width, inputDepth)).pretrain(false)
.backprop(true);
MultiLayerConfiguration conf = builder.build();
MultiLayerNetwork mln = new MultiLayerNetwork(conf);
mln.init();
org.deeplearning4j.nn.layers.convolution.ConvolutionLayer c0 =
(org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) mln.getLayer(0);
ConvolutionHelper ch0 = (ConvolutionHelper) f.get(c0);
assertTrue(ch0 instanceof CudnnConvolutionHelper);
org.deeplearning4j.nn.layers.convolution.ConvolutionLayer c1 =
(org.deeplearning4j.nn.layers.convolution.ConvolutionLayer) mln.getLayer(1);
ConvolutionHelper ch1 = (ConvolutionHelper) f.get(c1);
assertTrue(ch1 instanceof CudnnConvolutionHelper);
String name = new Object() {}.getClass().getEnclosingMethod().getName() + ", minibatch = "
+ minibatchSize + ", convHasBias = " + convHasBias;
if (PRINT_RESULTS) {
System.out.println(name);
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(name, gradOK);
}
}
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:69,代码来源:CuDNNGradientChecks.java示例15: testSerializationConfigurations
import org.deeplearning4j.nn.conf.distribution.UniformDistribution; //导入依赖的package包/类
@Test
public void testSerializationConfigurations() {
SerializerInstance si = sc.env().serializer().newInstance();
//Check network configurations:
Map<Integer, Double> m = new HashMap<>();
m.put(0, 0.5);
m.put(10, 0.1);
MultiLayerConfiguration mlc = new NeuralNetConfiguration.Builder()
.updater(new Nadam(new MapSchedule(ScheduleType.ITERATION,m))).list().layer(0, new OutputLayer.Builder().nIn(10).nOut(10).build())
.build();
testSerialization(mlc, si);
ComputationGraphConfiguration cgc = new NeuralNetConfiguration.Builder().weightInit(WeightInit.DISTRIBUTION)
.dist(new UniformDistribution(-1, 1))
.updater(new Adam(new MapSchedule(ScheduleType.ITERATION,m)))
.graphBuilder()
.addInputs("in").addLayer("out", new OutputLayer.Builder().nIn(10).nOut(10).build(), "in")
.setOutputs("out").build();
testSerialization(cgc, si);
//Check main layers:
Layer[] layers = new Layer[] {new OutputLayer.Builder().nIn(10).nOut(10).build(),
new RnnOutputLayer.Builder().nIn(10).nOut(10).build(), new LossLayer.Builder().build(),
new CenterLossOutputLayer.Builder().nIn(10).nOut(10).build(),
new DenseLayer.Builder().nIn(10).nOut(10).build(),
new ConvolutionLayer.Builder().nIn(10).nOut(10).build(), new SubsamplingLayer.Builder().build(),
new Convolution1DLayer.Builder(2, 2).nIn(10).nOut(10).build(),
new ActivationLayer.Builder().activation(Activation.TANH).build(),
new GlobalPoolingLayer.Builder().build(), new GravesLSTM.Builder().nIn(10).nOut(10).build(),
new LSTM.Builder().nIn(10).nOut(10).build(), new DropoutLayer.Builder(0.5).build(),
new BatchNormalization.Builder().build(), new LocalResponseNormalization.Builder().build()};
for (Layer l : layers) {
testSerialization(l, si);
}
//Check graph vertices
GraphVertex[] vertices = new GraphVertex[] {new ElementWiseVertex(ElementWiseVertex.Op.Add),
new L2NormalizeVertex(), new LayerVertex(null, null), new MergeVertex(), new PoolHelperVertex(),
new PreprocessorVertex(new CnnToFeedForwardPreProcessor(28, 28, 1)),
new ReshapeVertex(new int[] {1, 1}), new ScaleVertex(1.0), new ShiftVertex(1.0),
new SubsetVertex(1, 1), new UnstackVertex(0, 2), new DuplicateToTimeSeriesVertex("in1"),
new LastTimeStepVertex("in1")};
for (GraphVertex gv : vertices) {
testSerialization(gv, si);
}
}
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:55,代码来源:TestKryo.java本文标签属性:
示例:示例志愿表
代码:代码是什么
java:javascript18岁
UniformDistribution:UniformDistribution