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


Java MultiLayerNetwork.getLayers方法代码示例

本文整理汇总了Java中org.deeplearning4j.nn.multilayer.MultiLayerNetwork.getLayers方法的典型用法代码示例。如果您正苦于以下问题:Java MultiLayerNetwork.getLayers方法的具体用法?Java MultiLayerNetwork.getLayers怎么用?Java MultiLayerNetwork.getLayers使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.deeplearning4j.nn.multilayer.MultiLayerNetwork的用法示例。


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

示例1: testIterationListener

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testIterationListener() {
    MultiLayerNetwork model1 = new MultiLayerNetwork(getConf());
    model1.init();
    model1.setListeners(Collections.singletonList((IterationListener) new ScoreIterationListener(1)));

    MultiLayerNetwork model2 = new MultiLayerNetwork(getConf());
    model2.setListeners(Collections.singletonList((IterationListener) new ScoreIterationListener(1)));
    model2.init();

    Layer[] l1 = model1.getLayers();
    for (int i = 0; i < l1.length; i++)
        assertTrue(l1[i].getListeners() != null && l1[i].getListeners().size() == 1);

    Layer[] l2 = model2.getLayers();
    for (int i = 0; i < l2.length; i++)
        assertTrue(l2[i].getListeners() != null && l2[i].getListeners().size() == 1);
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:19,代码来源:MultiLayerNeuralNetConfigurationTest.java

示例2: MultiLayerUpdater

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
public MultiLayerUpdater(MultiLayerNetwork network, INDArray updaterState) {
    super(network, updaterState);

    layersByName = new HashMap<>();
    Layer[] l = network.getLayers();
    for (int i = 0; i < l.length; i++) {
        layersByName.put(String.valueOf(i), l[i]);
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:10,代码来源:MultiLayerUpdater.java

示例3: testSetExistingParamsDenseMultiLayer

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testSetExistingParamsDenseMultiLayer() {
    MultiLayerNetwork net = configureMultiLayer();

    for (Layer layer : net.getLayers()) {
        assertNotEquals(paramTable, layer.paramTable());
        layer.setParamTable(paramTable);
        assertEquals(paramTable, layer.paramTable());
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:11,代码来源:BaseLayerTest.java

示例4: main

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的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

示例5: main

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的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

示例6: main

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的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 exampleLength = 1000;					//Length of each training example sequence to use. This could certainly be increased
       int tbpttLength = 50;                       //Length for truncated backpropagation through time. i.e., do parameter updates ever 50 characters
	int numEpochs = 1;							//Total number of training epochs
       int generateSamplesEveryNMinibatches = 10;  //How frequently to generate samples from the network? 1000 characters / 50 tbptt length: 20 parameter updates per minibatch
	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);
	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)
           .weightInit(WeightInit.XAVIER)
           .updater(Updater.RMSPROP)
		.list()
		.layer(0, new GravesLSTM.Builder().nIn(iter.inputColumns()).nOut(lstmLayerSize)
				.activation("tanh").build())
		.layer(1, new GravesLSTM.Builder().nIn(lstmLayerSize).nOut(lstmLayerSize)
				.activation("tanh").build())
		.layer(2, new RnnOutputLayer.Builder(LossFunction.MCXENT).activation("softmax")        //MCXENT + softmax for classification
				.nIn(lstmLayerSize).nOut(nOut).build())
           .backpropType(BackpropType.TruncatedBPTT).tBPTTForwardLength(tbpttLength).tBPTTBackwardLength(tbpttLength)
		.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
       int miniBatchNumber = 0;
	for( int i=0; i<numEpochs; i++ ){
           while(iter.hasNext()){
               DataSet ds = iter.next();
               net.fit(ds);
               if(++miniBatchNumber % generateSamplesEveryNMinibatches == 0){
                   System.out.println("--------------------");
                   System.out.println("Completed " + miniBatchNumber + " minibatches of size " + miniBatchSize + "x" + exampleLength + " characters" );
                   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:arzt,项目名称:deeplearning-example,代码行数:79,代码来源:GravesLSTMCharModellingExample.java

示例7: LSTMTrainer

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
/**
 * Constructor
 * @param trainingSet Text file containing several ABC music files
 * @throws IOException
 */
public LSTMTrainer(String trainingSet, int seed) throws IOException {
    lstmLayerSize_ = 200; // original 200
    batchSize_ = 32; // original 32
    truncatedBackPropThroughTimeLength_ = 50;
    nbEpochs_ = 100;
    learningRate_ = 0.04; // 0.1 original // best 0.05 3epochs
    generateSamplesEveryNMinibatches_ = 200;
    generationInitialization_ = "X";
    seed_ = seed;
    random_ = new Random(seed);
    output_ = null;

    trainingSetIterator_ = new ABCIterator(trainingSet, Charset.forName("ASCII"), batchSize_, random_);
    charToInt_ = trainingSetIterator_.getCharToInt();
    intToChar_ = trainingSetIterator_.getIntToChar();
    exampleLength_ = trainingSetIterator_.getExampleLength();

    int nOut = trainingSetIterator_.totalOutcomes();

    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
            .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT).iterations(1)
            .learningRate(learningRate_)
            .rmsDecay(0.95) // 0.95 original
            .seed(seed_)
            .regularization(true) // true original
            .l2(0.001)
            .weightInit(WeightInit.XAVIER)
            .updater(Updater.RMSPROP)
            .list()
            .layer(0, new GravesLSTM.Builder().nIn(trainingSetIterator_.inputColumns()).nOut(lstmLayerSize_)
                    .activation("tanh").build())
            .layer(1, new GravesLSTM.Builder().nIn(lstmLayerSize_).nOut(lstmLayerSize_)
                    .activation("tanh").build())
            .layer(2, new GravesLSTM.Builder().nIn(lstmLayerSize_).nOut(lstmLayerSize_)
                    .activation("tanh").build())
            .layer(3, new RnnOutputLayer.Builder(LossFunctions.LossFunction.MCXENT).activation("softmax")
                    .nIn(lstmLayerSize_).nOut(nOut).build())
            .backpropType(BackpropType.TruncatedBPTT)
                .tBPTTForwardLength(truncatedBackPropThroughTimeLength_)
                .tBPTTBackwardLength(truncatedBackPropThroughTimeLength_)
            .pretrain(false).backprop(true)
            .build();

    lstmNet_ = new MultiLayerNetwork(conf);
    lstmNet_.init();
    //lstmNet_.setListeners(new ScoreIterationListener(1));
    //lstmNet_.setListeners(new HistogramIterationListener(1));
    UIServer uiServer = UIServer.getInstance();
    StatsStorage statsStorage = new InMemoryStatsStorage();
    uiServer.attach(statsStorage);
    lstmNet_.setListeners(new StatsListener(statsStorage));

    if (ExecutionParameters.verbose) {
        Layer[] layers = lstmNet_.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);
    }
}
 
开发者ID:paveyry,项目名称:LyreLand,代码行数:69,代码来源:LSTMTrainer.java

示例8: build

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
/**
 * Returns a model with the fine tune configuration and specified architecture changes.
 * .init() need not be called. Can be directly fit.
 *
 * @return MultiLayerNetwork
 */
public MultiLayerNetwork build() {

    if (!prepDone) {
        doPrep();
    }

    editedModel = new MultiLayerNetwork(constructConf(), constructParams());
    if (frozenTill != -1) {
        org.deeplearning4j.nn.api.Layer[] layers = editedModel.getLayers();
        for (int i = frozenTill; i >= 0; i--) {
            //Complication here: inner Layer (implementation) NeuralNetConfiguration.layer (config) should keep
            // the original layer config. While network NNC should have the frozen layer, for to/from JSON etc
            NeuralNetConfiguration origNNC = editedModel.getLayerWiseConfigurations().getConf(i);
            NeuralNetConfiguration layerNNC = origNNC.clone();
            editedModel.getLayerWiseConfigurations().getConf(i).resetVariables();
            layers[i].setConf(layerNNC);
            layers[i] = new FrozenLayer(layers[i]);

            if (origNNC.getVariables() != null) {
                List<String> vars = origNNC.variables(true);
                origNNC.clearVariables();
                layerNNC.clearVariables();
                for (String s : vars) {
                    origNNC.variables(false).add(s);
                    origNNC.getL1ByParam().put(s, 0.0);
                    origNNC.getL2ByParam().put(s, 0.0);

                    layerNNC.variables(false).add(s);
                    layerNNC.getL1ByParam().put(s, 0.0);
                    layerNNC.getL2ByParam().put(s, 0.0);
                }
            }



            Layer origLayerConf = editedModel.getLayerWiseConfigurations().getConf(i).getLayer();
            Layer newLayerConf = new org.deeplearning4j.nn.conf.layers.misc.FrozenLayer(origLayerConf);
            newLayerConf.setLayerName(origLayerConf.getLayerName());
            editedModel.getLayerWiseConfigurations().getConf(i).setLayer(newLayerConf);
        }
        editedModel.setLayers(layers);
    }

    return editedModel;
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:52,代码来源:TransferLearning.java

示例9: testCnnSamePaddingMode

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testCnnSamePaddingMode() {
    int nOut = 2;

    int[] minibatchSizes = {1, 3};
    int width = 5;
    int[] heights = new int[]{4, 5, 6}; //Same padding mode: insensitive to exact input size...
    int[] kernelSizes = new int[]{2, 3};
    int[] inputDepths = {1, 2, 4};

    Nd4j.getRandom().setSeed(12345);

    for (int inputDepth : inputDepths) {
        for (int minibatchSize : minibatchSizes) {
            for (int height : heights) {
                for (int k : kernelSizes) {

                    INDArray input = Nd4j.rand(minibatchSize, width * height * inputDepth);
                    INDArray labels = Nd4j.zeros(minibatchSize, nOut);
                    for (int i = 0; i < minibatchSize; i++) {
                        labels.putScalar(new int[]{i, i % nOut}, 1.0);
                    }

                    MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345)
                            .updater(new NoOp())
                            .activation(Activation.TANH).convolutionMode(Same).list()
                            .layer(0, new ConvolutionLayer.Builder().name("layer 0").kernelSize(k, k)
                                    .stride(1, 1).padding(0, 0).nIn(inputDepth).nOut(2).build())
                            .layer(1, new SubsamplingLayer.Builder()
                                    .poolingType(SubsamplingLayer.PoolingType.MAX).kernelSize(k, k)
                                    .stride(1, 1).padding(0, 0).build())
                            .layer(2, new ConvolutionLayer.Builder().nIn(2).nOut(2).kernelSize(k, k)
                                    .stride(1, 1).padding(0, 0).build())
                            .layer(3, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                                    .activation(Activation.SOFTMAX).nOut(nOut).build())
                            .setInputType(InputType.convolutionalFlat(height, width, inputDepth)).build();

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

                    for (int i = 0; i < net.getLayers().length; i++) {
                        System.out.println("nParams, layer " + i + ": " + net.getLayer(i).numParams());
                    }

                    String msg = "Minibatch=" + minibatchSize + ", inDepth=" + inputDepth + ", height=" + height
                            + ", kernelSize=" + k;
                    System.out.println(msg);

                    boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
                            DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);

                    assertTrue(msg, gradOK);
                }
            }
        }
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:58,代码来源:CNNGradientCheckTest.java

示例10: testCnnSamePaddingModeStrided

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testCnnSamePaddingModeStrided() {
    int nOut = 2;

    int[] minibatchSizes = {1, 3};
    int width = 16;
    int height = 16;
    int[] kernelSizes = new int[]{2, 3};
    int[] strides = {1, 2, 3};
    int[] inputDepths = {1, 3};

    Nd4j.getRandom().setSeed(12345);

    for (int inputDepth : inputDepths) {
        for (int minibatchSize : minibatchSizes) {
            for (int stride : strides) {
                for (int k : kernelSizes) {
                    for (boolean convFirst : new boolean[]{true, false}) {

                        INDArray input = Nd4j.rand(minibatchSize, width * height * inputDepth);
                        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
                        for (int i = 0; i < minibatchSize; i++) {
                            labels.putScalar(new int[]{i, i % nOut}, 1.0);
                        }

                        Layer convLayer = new ConvolutionLayer.Builder().name("layer 0").kernelSize(k, k)
                                .stride(stride, stride).padding(0, 0).nIn(inputDepth).nOut(2).build();

                        Layer poolLayer = new SubsamplingLayer.Builder()
                                .poolingType(SubsamplingLayer.PoolingType.MAX).kernelSize(k, k)
                                .stride(stride, stride).padding(0, 0).build();

                        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder().seed(12345)
                                .updater(new NoOp())
                                .activation(Activation.TANH).convolutionMode(Same).list()
                                .layer(0, convFirst ? convLayer : poolLayer)
                                .layer(1, convFirst ? poolLayer : convLayer)
                                .layer(2, new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                                        .activation(Activation.SOFTMAX).nOut(nOut).build())
                                .setInputType(InputType.convolutionalFlat(height, width, inputDepth))
                                .build();

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

                        for (int i = 0; i < net.getLayers().length; i++) {
                            System.out.println("nParams, layer " + i + ": " + net.getLayer(i).numParams());
                        }

                        String msg = "Minibatch=" + minibatchSize + ", inDepth=" + inputDepth + ", height=" + height
                                + ", kernelSize=" + k + ", stride = " + stride + ", convLayer first = "
                                + convFirst;
                        System.out.println(msg);

                        boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
                                DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input,
                                labels);

                        assertTrue(msg, gradOK);
                    }
                }
            }
        }
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:66,代码来源:CNNGradientCheckTest.java

示例11: testDeconvolution2D

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testDeconvolution2D() {
    int nOut = 2;

    int[] minibatchSizes = new int[]{1, 3, 1, 3, 1, 3, 1, 3};
    int[] kernelSizes = new int[]{1, 1, 3, 3, 1, 1, 3, 3};
    int[] strides = {1, 1, 1, 1, 2, 2, 2, 2};
    int[] dilation = {1, 2, 2, 1, 1, 1, 2, 2};
    Activation[] activations = new Activation[]{Activation.SIGMOID, Activation.TANH, Activation.TANH, Activation.TANH, Activation.TANH,  Activation.SIGMOID, Activation.SIGMOID, Activation.SIGMOID};
    ConvolutionMode[] cModes = new ConvolutionMode[]{Truncate, Truncate, Truncate, Truncate, Truncate, Truncate, Truncate, Truncate};
    int width = 7;
    int height = 7;
    int inputDepth = 3;

    Nd4j.getRandom().setSeed(12345);

    for (int i = 0; i < minibatchSizes.length; i++) {
        int minibatchSize = minibatchSizes[i];
        int k = kernelSizes[i];
        int s = strides[i];
        int d = dilation[i];
        ConvolutionMode cm = cModes[i];
        Activation act = activations[i];


        int w = d * width;
        int h = d * height;

        INDArray input = Nd4j.rand(minibatchSize, w * h * inputDepth);
        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
        for (int j = 0; j < minibatchSize; j++) {
            labels.putScalar(new int[]{j, j % nOut}, 1.0);
        }

        NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder().seed(12345)
                .updater(new NoOp())
                .activation(act)
                .list()
                .layer(new Deconvolution2D.Builder().name("deconvolution_2D_layer")
                        .kernelSize(k, k)
                        .stride(s, s)
                        .dilation(d, d)
                        .convolutionMode(cm)
                        .nIn(inputDepth).nOut(nOut).build());

        MultiLayerConfiguration conf = b.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                .activation(Activation.SOFTMAX).nOut(nOut).build())
                .setInputType(InputType.convolutionalFlat(h, w, inputDepth)).build();

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

        for (int j = 0; j < net.getLayers().length; j++) {
            System.out.println("nParams, layer " + j + ": " + net.getLayer(j).numParams());
        }

        String msg = " - mb=" + minibatchSize + ", k="
                + k + ", s=" + s + ", d=" + d + ", cm=" + cm;
        System.out.println(msg);

        boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
                DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);

        assertTrue(msg, gradOK);
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:67,代码来源:CNNGradientCheckTest.java

示例12: testSeparableConv2D

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testSeparableConv2D() {
    int nOut = 2;

    int[] minibatchSizes = new int[] {1, 3};
    int width = 8;
    int height = 8;
    int inputDepth = 3;
    int[] kernelSizes = new int[]{2, 3};
    int[] strides = {1, 2};
    int[] dilation = {1, 2};
    ConvolutionMode[] cModes = new ConvolutionMode[]{ConvolutionMode.Truncate};

    Nd4j.getRandom().setSeed(12345);

    int[] ks = new int[]{1,3,1,3,1,3,1,3};
    int[] ss = new int[]{1,1,2,2,1,1,2,2};
    int[] ds = new int[]{1,1,1,1,2,2,2,2};
    ConvolutionMode[] cms = new ConvolutionMode[]{Truncate, Truncate, Truncate, Truncate, Truncate, Truncate, Truncate, Truncate};
    int[] mb = new int[]{1,1,3,3,3,1,3,3};

    for( int t=0; t<ks.length; t++ ){

        int k = ks[t];
        int s = ss[t];
        int d = ds[t];
        ConvolutionMode cm = cms[t];
        int minibatchSize = mb[t];

        //Use larger input with larger dilation values (to avoid invalid config)
        int w = d * width;
        int h = d * height;

        INDArray input = Nd4j.rand(minibatchSize, w * h * inputDepth);
        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
        for (int i = 0; i < minibatchSize; i++) {
            labels.putScalar(new int[]{i, i % nOut}, 1.0);
        }

        NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder().seed(12345)
                .updater(new NoOp())
                .activation(Activation.TANH)
                .convolutionMode(cm)
                .list()
                .layer(new SeparableConvolution2D.Builder().name("Separable conv 2D layer")
                        .kernelSize(k, k)
                        .stride(s, s)
                        .dilation(d, d)
                        .depthMultiplier(3)
                        .nIn(inputDepth).nOut(2).build());

        MultiLayerConfiguration conf = b.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                .activation(Activation.SOFTMAX).nOut(nOut).build())
                .setInputType(InputType.convolutionalFlat(h, w, inputDepth)).build();

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

        for (int i = 0; i < net.getLayers().length; i++) {
            System.out.println("nParams, layer " + i + ": " + net.getLayer(i).numParams());
        }

        String msg = " - mb=" + minibatchSize + ", k="
                + k + ", s=" + s + ", d=" + d + ", cm=" + cm;
        System.out.println(msg);

        boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
                DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);

        assertTrue(msg, gradOK);
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:73,代码来源:CNNGradientCheckTest.java

示例13: testCnnDilated

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void testCnnDilated() {
    int nOut = 2;

    int minibatchSize = 3;
    int width = 8;
    int height = 8;
    int inputDepth = 3;

    Nd4j.getRandom().setSeed(12345);

    boolean[] sub = new boolean[]{true,false,true,false,true,false,true,false};
    int[] stride = new int[]{1,1,2,2,1,1,2,2};
    int[] kernel = new int[]{2,2,2,2,3,3,3,3};
    int[] ds = new int[]{2,3,3,2,2,3,3,2};
    ConvolutionMode[] cms = new ConvolutionMode[]{Same, Same, Same, Truncate, Truncate, Truncate, Same, Truncate};


    for(int t=0; t<sub.length; t++ ){

        boolean subsampling = sub[t];
        int s = stride[t];
        int k = kernel[t];
        int d = ds[t];
        ConvolutionMode cm = cms[t];

        //Use larger input with larger dilation values (to avoid invalid config)
        int w = d * width;
        int h = d * height;

        INDArray input = Nd4j.rand(minibatchSize, w * h * inputDepth);
        INDArray labels = Nd4j.zeros(minibatchSize, nOut);
        for (int i = 0; i < minibatchSize; i++) {
            labels.putScalar(new int[]{i, i % nOut}, 1.0);
        }

        NeuralNetConfiguration.ListBuilder b = new NeuralNetConfiguration.Builder().seed(12345)
                .updater(new NoOp())
                .activation(Activation.TANH).convolutionMode(cm).list()
                .layer(new ConvolutionLayer.Builder().name("layer 0")
                        .kernelSize(k, k)
                        .stride(s, s)
                        .dilation(d, d)
                        .nIn(inputDepth).nOut(2).build());
        if (subsampling) {
            b.layer(new SubsamplingLayer.Builder()
                    .poolingType(SubsamplingLayer.PoolingType.MAX)
                    .kernelSize(k, k)
                    .stride(s, s)
                    .dilation(d, d)
                    .build());
        } else {
            b.layer(new ConvolutionLayer.Builder().nIn(2).nOut(2)
                    .kernelSize(k, k)
                    .stride(s, s)
                    .dilation(d, d)
                    .build());
        }

        MultiLayerConfiguration conf = b.layer(new OutputLayer.Builder(LossFunctions.LossFunction.MCXENT)
                .activation(Activation.SOFTMAX).nOut(nOut).build())
                .setInputType(InputType.convolutionalFlat(h, w, inputDepth)).build();

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

        for (int i = 0; i < net.getLayers().length; i++) {
            System.out.println("nParams, layer " + i + ": " + net.getLayer(i).numParams());
        }

        String msg = (subsampling ? "subsampling" : "conv") + " - mb=" + minibatchSize + ", k="
                + k + ", s=" + s + ", d=" + d + ", cm=" + cm;
        System.out.println(msg);

        boolean gradOK = GradientCheckUtil.checkGradients(net, DEFAULT_EPS, DEFAULT_MAX_REL_ERROR,
                DEFAULT_MIN_ABS_ERROR, PRINT_RESULTS, RETURN_ON_FIRST_FAILURE, input, labels);

        assertTrue(msg, gradOK);
    }
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:81,代码来源:CNNGradientCheckTest.java

示例14: simpleFineTune

import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; //导入方法依赖的package包/类
@Test
public void simpleFineTune() {

    long rng = 12345L;
    DataSet randomData = new DataSet(Nd4j.rand(10, 4), Nd4j.rand(10, 3));
    //original conf
    NeuralNetConfiguration.Builder confToChange =
                    new NeuralNetConfiguration.Builder().seed(rng).optimizationAlgo(OptimizationAlgorithm.LBFGS)
                                    .updater(new Nesterovs(0.01, 0.99));

    MultiLayerNetwork modelToFineTune = new MultiLayerNetwork(confToChange.list()
                    .layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
                    .layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                    LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                    .build())
                    .build());
    modelToFineTune.init();

    //model after applying changes with transfer learning
    MultiLayerNetwork modelNow =
            new TransferLearning.Builder(modelToFineTune)
                    .fineTuneConfiguration(new FineTuneConfiguration.Builder().seed(rng)
                            .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                            .updater(new RmsProp(0.5)) //Intent: override both weight and bias LR, unless bias LR is manually set also
                            .l2(0.4).build())
                    .build();

    for (org.deeplearning4j.nn.api.Layer l : modelNow.getLayers()) {
        BaseLayer bl = ((BaseLayer) l.conf().getLayer());
        assertEquals(new RmsProp(0.5), bl.getIUpdater());
    }


    NeuralNetConfiguration.Builder confSet = new NeuralNetConfiguration.Builder().seed(rng)
                    .optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
                    .updater(new RmsProp(0.5)).l2(0.4);

    MultiLayerNetwork expectedModel = new MultiLayerNetwork(confSet.list()
                    .layer(0, new DenseLayer.Builder().nIn(4).nOut(3).build())
                    .layer(1, new org.deeplearning4j.nn.conf.layers.OutputLayer.Builder(
                                    LossFunctions.LossFunction.MCXENT).activation(Activation.SOFTMAX).nIn(3).nOut(3)
                                                    .build())
                    .build());
    expectedModel.init();
    expectedModel.setParams(modelToFineTune.params().dup());

    assertEquals(expectedModel.params(), modelNow.params());

    //Check json
    MultiLayerConfiguration expectedConf = expectedModel.getLayerWiseConfigurations();
    assertEquals(expectedConf.toJson(), modelNow.getLayerWiseConfigurations().toJson());

    //Check params after fit
    modelNow.fit(randomData);
    expectedModel.fit(randomData);

    assertEquals(modelNow.score(), expectedModel.score(), 1e-6);
    INDArray pExp = expectedModel.params();
    INDArray pNow = modelNow.params();
    assertEquals(pExp, pNow);
}
 
开发者ID:deeplearning4j,项目名称:deeplearning4j,代码行数:62,代码来源:TransferLearningMLNTest.java


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