Package com.github.neuralnetworks.architecture

Examples of com.github.neuralnetworks.architecture.Subsampling2DConnection


  assertEquals(244, o.get(1, 0, 0, 0), 0);
    }

    @Test
    public void testMaxPooling() {
  Subsampling2DConnection c = new Subsampling2DConnection(new Layer(), new Layer(), 4, 4, 2, 2, 2);
  List<Connections> connections = new ArrayList<Connections>();
  connections.add(c);

  ConnectionCalculator calc = new AparapiMaxPooling2D();

  ValuesProvider vp = TensorFactory.tensorProvider(c, 2, true);
  float[] src = new float[] { 0.5f, 1, 1, 2, 1.5f, 3, 2, 4, 2.5f, 5, 3, 6, 3.5f, 7, 4f, 8, 4.5f, 9, 5f, 10, 5.5f, 11, 6f, 12, 6.5f, 13, 7f, 14, 8f, 16, 7.5f, 15, 8.5f, 17, 9f, 18, 9.5f, 19, 10f, 20, 10.5f, 21, 11f, 22, 11.5f, 23, 12f, 24, 12.5f, 25, 13f, 26, 13.5f, 27, 14f, 28, 14.5f, 29, 15f, 30, 16f, 32, 15.5f, 31 };
  System.arraycopy(src, 0, vp.get(c.getInputLayer()).getElements(), vp.get(c.getInputLayer()).getStartIndex(), src.length);

  calc.calculate(connections, vp, c.getOutputLayer());

  Tensor o = vp.get(c.getOutputLayer());

  assertEquals(3, o.get(0, 0, 0, 0), 0);
  assertEquals(4, o.get(0, 0, 1, 0), 0);
  assertEquals(7, o.get(0, 1, 0, 0), 0);
  assertEquals(8, o.get(0, 1, 1, 0), 0);
View Full Code Here


  assertEquals(32, o.get(1, 1, 1, 1), 0);
    }

    @Test
    public void testAveragePooling() {
  Subsampling2DConnection c = new Subsampling2DConnection(new Layer(), new Layer(), 4, 4, 2, 2, 2);
  List<Connections> connections = new ArrayList<Connections>();
  connections.add(c);

  AparapiAveragePooling2D calc = new AparapiAveragePooling2D();

  ValuesProvider vp = TensorFactory.tensorProvider(c, 2, true);
  float[] src = new float[] { 0.5f, 1, 1, 2, 1.5f, 3, 2, 4, 2.5f, 5, 3, 6, 3.5f, 7, 4f, 8, 4.5f, 9, 5f, 10, 5.5f, 11, 6f, 12, 6.5f, 13, 7f, 14, 8f, 16, 7.5f, 15, 8.5f, 17, 9f, 18, 9.5f, 19, 10f, 20, 10.5f, 21, 11f, 22, 11.5f, 23, 12f, 24, 12.5f, 25, 13f, 26, 13.5f, 27, 14f, 28, 14.5f, 29, 15f, 30, 16f, 32, 15.5f, 31 };
  System.arraycopy(src, 0, vp.get(c.getInputLayer()).getElements(), vp.get(c.getInputLayer()).getStartIndex(), src.length);

  calc.calculate(connections, vp, c.getOutputLayer());

  Tensor o = vp.get(c.getOutputLayer());

  assertEquals(1.75, o.get(0, 0, 0, 0), 0);
  assertEquals(2.75, o.get(0, 0, 1, 0), 0);
  assertEquals(5.75, o.get(0, 1, 0, 0), 0);
  assertEquals(6.75, o.get(0, 1, 1, 0), 0);
View Full Code Here

  assertEquals(29.5, o.get(1, 1, 1, 1), 0);
    }

    @Test
    public void testStochasticPooling() {
  Subsampling2DConnection c = new Subsampling2DConnection(new Layer(), new Layer(), 3, 3, 3, 3, 1);
  List<Connections> connections = new ArrayList<Connections>();
  connections.add(c);

  ValuesProvider vp = TensorFactory.tensorProvider(c, 2, true);
  float[] src = new float[] { 1.6f, 1.6f, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.4f, 2.4f };
  System.arraycopy(src, 0, vp.get(c.getInputLayer()).getElements(), vp.get(c.getInputLayer()).getStartIndex(), src.length);

  AparapiStochasticPooling2D calc = new AparapiStochasticPooling2D();
  calc.calculate(connections, vp, c.getOutputLayer());

  Tensor t = vp.get(c.getOutputLayer());

  assertEquals(2.08, t.get(0, 0, 0, 0), 0.01);
  assertEquals(2.08, t.get(0, 0, 0, 1), 0.01);
    }
View Full Code Here

    }

    @Test
    public void testMaxPoolingBackpropagation() {
  Environment.getInstance().setExecutionMode(EXECUTION_MODE.SEQ);
  Subsampling2DConnection c = new Subsampling2DConnection(new Layer(), new Layer(), 4, 4, 2, 2, 2);

  List<Connections> connections = new ArrayList<Connections>();
  connections.add(c);

  // max pooling
  ValuesProvider activations = TensorFactory.tensorProvider(c, 2, true);
  float[] src = new float[] { 0.5f, 1, 1, 2, 1.5f, 3, 2, 4, 2.5f, 5, 3, 6, 3.5f, 7, 4f, 8, 4.5f, 9, 5f, 10, 5.5f, 11, 6f, 12, 6.5f, 13, 7f, 14, 8f, 16, 7.5f, 15, 8.5f, 17, 9f, 18, 9.5f, 19, 10f, 20, 10.5f, 21, 11f, 22, 11.5f, 23, 12f, 24, 12.5f, 25, 13f, 26, 13.5f, 27, 14f, 28, 14.5f, 29, 15f, 30, 16f, 32, 15.5f, 31 };
  System.arraycopy(src, 0, activations.get(c.getInputLayer()).getElements(), activations.get(c.getInputLayer()).getStartIndex(), src.length);

  ConnectionCalculator calc = new AparapiMaxPooling2D();
  calc.calculate(connections, activations, c.getOutputLayer());

  ValuesProvider vp = TensorFactory.tensorProvider(c, 2, true);
  TensorFactory.copy(activations.get(c.getOutputLayer()), vp.get(c.getOutputLayer()));

  BackpropagationMaxPooling2D bp = new BackpropagationMaxPooling2D();
  bp.setActivations(activations);
  bp.calculate(connections, vp, c.getInputLayer());

  Tensor a = activations.get(c.getInputLayer());
  Tensor bpo = vp.get(c.getInputLayer());

  assertEquals(true, bpo.get(0, 1, 1, 0) == a.get(0, 1, 1, 0));
  assertEquals(true, bpo.get(0, 1, 3, 0) == a.get(0, 1, 3, 0));
  assertEquals(true, bpo.get(0, 3, 1, 0) == a.get(0, 3, 1, 0));
  assertEquals(true, bpo.get(0, 3, 2, 0) == a.get(0, 3, 2, 0));
View Full Code Here

  assertEquals(true, bpo.get(1, 3, 2, 1) == a.get(1, 3, 2, 1));
    }

    @Test
    public void testAveragePoolingBackpropagation() {
  Subsampling2DConnection c = new Subsampling2DConnection(new Layer(), new Layer(), 4, 4, 2, 2, 2);

  // average pooling
  ConnectionCalculator calc = new AparapiAveragePooling2D();

  List<Connections> connections = new ArrayList<Connections>();
  connections.add(c);

  ValuesProvider activations = TensorFactory.tensorProvider(c, 2, true);
  float[] src = new float[] { 0.5f, 1, 1, 2, 1.5f, 3, 2, 4, 2.5f, 5, 3, 6, 3.5f, 7, 4f, 8, 4.5f, 9, 5f, 10, 5.5f, 11, 6f, 12, 6.5f, 13, 7f, 14, 8f, 16, 7.5f, 15, 8.5f, 17, 9f, 18, 9.5f, 19, 10f, 20, 10.5f, 21, 11f, 22, 11.5f, 23, 12f, 24, 12.5f, 25, 13f, 26, 13.5f, 27, 14f, 28, 14.5f, 29, 15f, 30, 16f, 32, 15.5f, 31 };
  System.arraycopy(src, 0, activations.get(c.getInputLayer()).getElements(), activations.get(c.getInputLayer()).getStartIndex(), src.length);

  calc.calculate(connections, activations, c.getOutputLayer());

  BackpropagationAveragePooling2D bp = new BackpropagationAveragePooling2D();
  bp.setActivations(activations);

  ValuesProvider vp = TensorFactory.tensorProvider(c, 2, true);
  TensorFactory.copy(activations.get(c.getOutputLayer()), vp.get(c.getOutputLayer()));

  bp.calculate(connections, vp, c.getInputLayer());

  Tensor o = activations.get(c.getOutputLayer());
  Tensor bpo = vp.get(c.getInputLayer());
  assertEquals(true, bpo.get(0, 0, 0, 0) == o.get(0, 0, 0, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(0, 0, 2, 0) == o.get(0, 0, 1, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(0, 2, 0, 0) == o.get(0, 1, 0, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(0, 2, 2, 0) == o.get(0, 1, 1, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(1, 0, 0, 0) == o.get(1, 0, 0, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(1, 0, 2, 0) == o.get(1, 0, 1, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(1, 2, 0, 0) == o.get(1, 1, 0, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(1, 2, 2, 0) == o.get(1, 1, 1, 0) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(0, 0, 0, 1) == o.get(0, 0, 0, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(0, 0, 2, 1) == o.get(0, 0, 1, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(0, 2, 0, 1) == o.get(0, 1, 0, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(0, 2, 2, 1) == o.get(0, 1, 1, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(1, 0, 0, 1) == o.get(1, 0, 0, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(1, 0, 2, 1) == o.get(1, 0, 1, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(1, 2, 0, 1) == o.get(1, 1, 0, 1) / c.getSubsamplingRegionLength());
  assertEquals(true, bpo.get(1, 2, 2, 1) == o.get(1, 1, 1, 1) / c.getSubsamplingRegionLength());
    }
View Full Code Here

        inputFMRows = cc.getOutputFeatureMapRows();
        inputFMCols = cc.getOutputFeatureMapColumns();
        filters = cc.getOutputFilters();
        break;
          } else if (c instanceof Subsampling2DConnection) {
        Subsampling2DConnection sc = (Subsampling2DConnection) c;
        inputFMRows = sc.getOutputFeatureMapRows();
        inputFMCols = sc.getOutputFeatureMapColumns();
        filters = sc.getFilters();
        break;
          }
      }
        }
    }

    if (l.length == 4) {
        Conv2DConnection c = cf.conv2d(prev, newLayer = new Layer(), inputFMRows, inputFMCols, filters, l[0], l[1], l[2], l[3]);
        if (addBias) {
      cf.conv2d(biasLayer = new Layer(), newLayer, c.getOutputFeatureMapRows(), c.getOutputFeatureMapColumns(), 1, 1, 1, l[2], l[3]);
        }

        prevUnitCount = c.getOutputUnitCount();
    } else if (l.length == 2) {
        Subsampling2DConnection c = cf.subsampling2D(prev, newLayer = new Layer(), inputFMRows, inputFMCols, l[0], l[1], filters);
        prevUnitCount = c.getOutputUnitCount();
    }
      }

      result.addLayer(newLayer);
      if (biasLayer != null) {
View Full Code Here

  }

  if (hasFullyConnected) {
      result = new int[] {targetLayer.getUnitCount(connections), miniBatchSize };
  } else if (hasSubsampling) {
      Subsampling2DConnection c = (Subsampling2DConnection) connections.iterator().next();
      if (c.getOutputLayer() == targetLayer) {
    result = new int[] { c.getFilters(), c.getOutputFeatureMapRows(), c.getOutputFeatureMapColumns(), miniBatchSize };
      } else if (c.getInputLayer() == targetLayer) {
    result = new int[] { c.getFilters(), c.getInputFeatureMapRows(), c.getInputFeatureMapColumns(), miniBatchSize };
      }
  } else if (hasConvolutional) {
      Conv2DConnection c = (Conv2DConnection) connections.iterator().next();
      if (c.getOutputLayer() == targetLayer) {
    result = new int[] { c.getOutputFilters(), c.getOutputFeatureMapRows(), c.getOutputFeatureMapColumns(), miniBatchSize };
      } else if (c.getInputLayer() == targetLayer) {
    result = new int[] { c.getInputFilters(), c.getInputFeatureMapRows(), c.getInputFeatureMapColumns(), miniBatchSize };
      }
  }

  return result;
    }
View Full Code Here

    }
    if (!dout.stream().anyMatch(i -> Arrays.equals(i, outputDim))) {
        dout.add(outputDim);
    }
      } else if (c instanceof Subsampling2DConnection) {
    Subsampling2DConnection cc = (Subsampling2DConnection) c;
    int[] inputDim = new int[] { cc.getFilters(), cc.getInputFeatureMapRows(), cc.getInputFeatureMapColumns(), miniBatchSize };
    int[] outputDim = new int[] { cc.getFilters(), cc.getOutputFeatureMapRows(), cc.getOutputFeatureMapColumns(), miniBatchSize };
    if (!din.stream().anyMatch(i -> Arrays.equals(i, inputDim))) {
        din.add(inputDim);
    }
    if (!dout.stream().anyMatch(i -> Arrays.equals(i, outputDim))) {
        dout.add(outputDim);
View Full Code Here

TOP

Related Classes of com.github.neuralnetworks.architecture.Subsampling2DConnection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.