home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (BaseLayerTest.java)

This example Java source code file (BaseLayerTest.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

baselayertest, before, hashmap, indarray, layer, map, multilayerconfiguration, multilayernetwork, neuralnetconfiguration, test, util

The BaseLayerTest.java Java example source code

package org.deeplearning4j.nn.layers;

import org.deeplearning4j.nn.api.Layer;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.layers.ConvolutionLayer;
import org.deeplearning4j.nn.conf.layers.DenseLayer;
import org.deeplearning4j.nn.conf.layers.OutputLayer;
import org.deeplearning4j.nn.layers.factory.LayerFactories;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.junit.Before;
import org.junit.Test;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;

import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

 * Created by nyghtowl on 11/15/15.
public class BaseLayerTest {

    protected INDArray weight =  Nd4j.create(new double[]{0.10, -0.20, -0.15, 0.05},
            new int[]{2, 2});
    protected INDArray bias = Nd4j.create(new double[] {0.5,0.5},
            new int[] {1,2});
    protected Map<String, INDArray> paramTable;

    public void doBefore(){
        paramTable = new HashMap<>();
        paramTable.put("W", weight);
        paramTable.put("b", bias);


    public void testSetExistingParamsConvolutionSingleLayer() {
        Layer layer = configureSingleLayer();
        assertNotEquals(paramTable, layer.paramTable());

        assertEquals(paramTable, layer.paramTable());

    public void testSetExistingParamsDenseMultiLayer() {
        MultiLayerNetwork net = configureMultiLayer();

        for(Layer layer: net.getLayers()) {
            assertNotEquals(paramTable, layer.paramTable());
            assertEquals(paramTable, layer.paramTable());

    public Layer configureSingleLayer(){
        int nIn = 2;
        int nOut = 2;

        NeuralNetConfiguration conf = new NeuralNetConfiguration.Builder()
                .layer(new ConvolutionLayer.Builder()

        int numParams = LayerFactories.getFactory(conf).initializer().numParams(conf,true);
        INDArray params = Nd4j.create(1, numParams);
        return LayerFactories.getFactory(conf).create(conf, null, 0, params, true);

    public MultiLayerNetwork configureMultiLayer(){
        int nIn = 2;
        int nOut = 2;

        MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
                .layer(0, new DenseLayer.Builder()
                .layer(1, new OutputLayer.Builder()

        MultiLayerNetwork net = new MultiLayerNetwork(conf);
        return net;


Other Java examples (source code examples)

Here is a short list of links related to this Java BaseLayerTest.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.