Examples of FullyConnected


Examples of com.github.neuralnetworks.architecture.FullyConnected

  // CNN
  NeuralNetworkImpl cnn = NNFactory.convNN(new int[][] { { 2, 1, 1 }, { 1, 1 }, { 4 }, {1} }, false);
  cnn.setLayerCalculator(NNFactory.lcSigmoid(cnn, null));
  NNFactory.lcMaxPooling(cnn);
  FullyConnected cnnfci = (FullyConnected) cnn.getOutputLayer().getConnections().get(0).getInputLayer().getConnections().get(0);
  cnnfci.getWeights().set(0.02f, 0, 0);
  cnnfci.getWeights().set(0.01f, 1, 0);
  cnnfci.getWeights().set(0.03f, 2, 0);
  cnnfci.getWeights().set(0.001f, 3, 0);
  cnnfci.getWeights().set(0.005f, 0, 1);
  cnnfci.getWeights().set(0.04f, 1, 1);
  cnnfci.getWeights().set(0.02f, 2, 1);
  cnnfci.getWeights().set(0.009f, 3, 1);

  FullyConnected cnnfco = (FullyConnected) cnn.getOutputLayer().getConnections().get(0);
  cnnfco.getWeights().set(0.05f, 0, 0);
  cnnfco.getWeights().set(0.08f, 0, 1);

  // MLP
  NeuralNetworkImpl mlp = NNFactory.mlpSigmoid(new int[] { 2, 4, 1 }, false);

  FullyConnected mlpfci = (FullyConnected) mlp.getOutputLayer().getConnections().get(0).getInputLayer().getConnections().get(0);
  mlpfci.getWeights().set(0.02f, 0, 0);
  mlpfci.getWeights().set(0.01f, 1, 0);
  mlpfci.getWeights().set(0.03f, 2, 0);
  mlpfci.getWeights().set(0.001f, 3, 0);
  mlpfci.getWeights().set(0.005f, 0, 1);
  mlpfci.getWeights().set(0.04f, 1, 1);
  mlpfci.getWeights().set(0.02f, 2, 1);
  mlpfci.getWeights().set(0.009f, 3, 1);

  FullyConnected mlpfco = (FullyConnected) mlp.getOutputLayer().getConnections().get(0);
  mlpfco.getWeights().set(0.05f, 0, 0);
  mlpfco.getWeights().set(0.08f, 0, 1);

  // compare bp
  SimpleInputProvider inputProvider = new SimpleInputProvider(new float[][] { {0, 0}, {0, 1}, {1, 0}, {1, 1} }, new float[][] { {0}, {1}, {1}, {0} });

  BackPropagationTrainer<?> mlpbpt = TrainerFactory.backPropagation(mlp, inputProvider, inputProvider, new XorOutputError(), null, 1f, 0f, 0f, 0f, 0f, 1, 1, 10000);
  mlpbpt.train();
  mlpbpt.test();

  BackPropagationTrainer<?> cnnbpt = TrainerFactory.backPropagation(cnn, inputProvider, inputProvider, new XorOutputError(), null, 1f, 0f, 0f, 0f, 0f, 1, 1, 10000);
  cnnbpt.train();
  cnnbpt.test();

  assertEquals(mlpbpt.getOutputError().getTotalNetworkError(), cnnbpt.getOutputError().getTotalNetworkError(), 0);
  assertTrue(Arrays.equals(cnnfco.getWeights().getElements(), mlpfco.getWeights().getElements()));
  assertTrue(Arrays.equals(cnnfci.getWeights().getElements(), mlpfci.getWeights().getElements()));
    }
View Full Code Here

Examples of com.github.neuralnetworks.architecture.FullyConnected

    @Test
    public void testConnectionFactory() {
  Environment.getInstance().setUseWeightsSharedMemory(true);
  ConnectionFactory f = new ConnectionFactory();
  FullyConnected fc1 = f.fullyConnected(null, null, 2, 3);
  FullyConnected fc2 = f.fullyConnected(null, null, 5, 2);

  assertTrue(fc1.getWeights().getElements() == fc2.getWeights().getElements());
  assertEquals(16, fc1.getWeights().getElements().length, 0);
  assertEquals(0, fc1.getWeights().getStartOffset(), 0);
  assertEquals(3, fc1.getWeights().getRows(), 0);
  assertEquals(2, fc1.getWeights().getColumns(), 0);
  fc1.getWeights().set(3, 1, 1);
  assertEquals(3, fc1.getWeights().get(1, 1), 0);

  assertEquals(6, fc2.getWeights().getStartOffset(), 0);
  assertEquals(2, fc2.getWeights().getRows(), 0);
  assertEquals(5, fc2.getWeights().getColumns(), 0);
  fc2.getWeights().set(5, 1, 1);
  assertEquals(5, fc2.getWeights().get(1, 1), 0);

  Conv2DConnection c = f.conv2d(null, null, 3, 3, 3, 2, 2, 3, 1);
  assertEquals(52, c.getWeights().getElements().length, 0);
  assertEquals(36, c.getWeights().getSize(), 0);
  assertEquals(16, c.getWeights().getStartOffset(), 0);
View Full Code Here

Examples of com.github.neuralnetworks.architecture.FullyConnected

    protected void init(Layer visibleLayer, Layer hiddenLayer, int visibleUnitCount, int hiddenUnitCount, boolean addVisibleBias, boolean addHiddenBias) {
  addLayer(visibleLayer);
  addLayer(hiddenLayer);

  new FullyConnected(visibleLayer, hiddenLayer, visibleUnitCount, hiddenUnitCount);

  if (addVisibleBias) {
      Layer visibleBiasLayer = new Layer();
      addLayer(visibleBiasLayer);
      new FullyConnected(visibleBiasLayer, visibleLayer, 1, visibleUnitCount);
  }

  if (addHiddenBias) {
      Layer hiddenBiasLayer = new Layer();
      addLayer(hiddenBiasLayer);
      new FullyConnected(hiddenBiasLayer, hiddenLayer, 1, hiddenUnitCount);
  }
    }
View Full Code Here

Examples of com.github.neuralnetworks.architecture.FullyConnected

    LayerCalculatorImpl lc = (LayerCalculatorImpl) nn.getLayerCalculator();
    nn.getConnections().stream().filter(c -> c instanceof FullyConnected && c.getInputLayer() != nn.getInputLayer() && !Util.isBias(c.getInputLayer())).forEach(c -> {
        ConnectionCalculatorFullyConnected cc = (ConnectionCalculatorFullyConnected) lc.getConnectionCalculator(c.getOutputLayer());
        cc.setDropoutRate(0);
        FullyConnected fc = (FullyConnected) c;
        fc.getWeights().forEach(i -> fc.getWeights().getElements()[i] = fc.getWeights().getElements()[i] * (1 - dropoutRate));
    });
      }
  }
    }
View Full Code Here

Examples of com.github.neuralnetworks.architecture.FullyConnected

  l = l.getConnections().get(1).getOutputLayer();
  assertEquals(1, cc.getOutputFeatureMapRows(), 0);
  assertEquals(1, cc.getOutputFeatureMapColumns(), 0);
  assertEquals(120, cc.getOutputFilters(), 0);

  FullyConnected cg = (FullyConnected) l.getConnections().get(2);
  assertEquals(84, cg.getWeights().getRows(), 0);

  FullyConnected cg2 = (FullyConnected) cg.getOutputLayer().getConnections().get(2);
  assertEquals(10, cg2.getWeights().getRows(), 0);
    }
View Full Code Here

Examples of com.github.neuralnetworks.architecture.FullyConnected

  Environment.getInstance().setUseWeightsSharedMemory(true);
  NeuralNetworkImpl nn = NNFactory.convNN(new int[][] { {2, 1, 1}, {1, 1}, {2}, {2}, {1} }, false);
  nn.setLayerCalculator(NNFactory.lcSigmoid(nn, null));
  NNFactory.lcMaxPooling(nn);

  FullyConnected c1 = (FullyConnected) nn.getInputLayer().getConnections().get(0).getOutputLayer().getConnections().get(1).getOutputLayer().getConnections().get(1);
  Matrix cg1 = c1.getWeights();
  cg1.set(0.1f, 0, 0);
  cg1.set(0.8f, 0, 1);
  cg1.set(0.4f, 1, 0);
  cg1.set(0.6f, 1, 1);

  FullyConnected c2 = (FullyConnected) nn.getOutputLayer().getConnections().iterator().next();
  Matrix cg2 = c2.getWeights();
  cg2.set(0.3f, 0, 0);
  cg2.set(0.9f, 0, 1);

  BackPropagationTrainer<?> bpt = TrainerFactory.backPropagation(nn, new SimpleInputProvider(new float[][] { { 0.35f, 0.9f } }, new float[][] { { 0.5f } }), new SimpleInputProvider(new float[][] { { 0.35f, 0.9f } }, new float[][] { { 0.5f } }), null, null, 1f, 0f, 0f, 0f, 0f, 1, 1, 1);
  bpt.train();
View Full Code Here

Examples of com.github.neuralnetworks.architecture.FullyConnected

  // CNN
  NeuralNetworkImpl cnn = NNFactory.convNN(new int[][] { { 2, 1, 1 }, { 1, 1 }, {1} }, false);
  cnn.setLayerCalculator(NNFactory.lcSigmoid(cnn, null));
  NNFactory.lcMaxPooling(cnn);
  FullyConnected cnnfc = (FullyConnected) cnn.getOutputLayer().getConnections().get(0);
  cnnfc.getWeights().set(0.05f, 0, 0);
  cnnfc.getWeights().set(0.08f, 0, 1);
  ValuesProvider cnnvp = TensorFactory.tensorProvider(cnn, 1, Environment.getInstance().getUseDataSharedMemory());
  Tensor cnnin = cnnvp.get(cnn.getInputLayer());
  cnnin.set(0.2f, 0, 0, 0, 0);
  cnnin.set(0.6f, 0, 1, 0, 0);

  // MLP
  NeuralNetworkImpl mlp = NNFactory.mlpSigmoid(new int[] { 2, 1 }, false);
  FullyConnected mlpfc = (FullyConnected) mlp.getOutputLayer().getConnections().get(0);
  mlpfc.getWeights().set(0.05f, 0, 0);
  mlpfc.getWeights().set(0.08f, 0, 1);
  ValuesProvider mlpvp = TensorFactory.tensorProvider(mlp, 1, Environment.getInstance().getUseDataSharedMemory());
  Tensor mlpin = mlpvp.get(mlp.getInputLayer());
  mlpin.set(0.2f, 0, 0);
  mlpin.set(0.6f, 1, 0);
View Full Code Here

Examples of com.github.neuralnetworks.architecture.FullyConnected

     * @param nn
     * @param layer
     * @param addBias
     */
    public static FullyConnected addFullyConnectedLayer(NeuralNetworkImpl nn, Layer layer, ConnectionFactory cf, int inputUnitCount, int outputUnitCount, boolean addBias) {
  FullyConnected result = null;
  if (nn.addLayer(layer) && nn.getOutputLayer() != layer) {
      result = cf.fullyConnected(nn.getOutputLayer(), layer, inputUnitCount, outputUnitCount);
  }

  if (addBias && nn.getInputLayer() != layer) {
View Full Code Here

Examples of com.github.neuralnetworks.architecture.FullyConnected

      if (dout == null) {
    result.put(c.getOutputLayer(), dout = new HashSet<>());
      }

      if (c instanceof FullyConnected) {
    FullyConnected fc = (FullyConnected) c;
    int[] inputDim = new int[] { fc.getInputUnitCount(), miniBatchSize };
    int[] outputDim = new int[] { fc.getOutputUnitCount(), miniBatchSize };
    inputDim[0] = fc.getInputUnitCount();
    outputDim[0] = fc.getOutputUnitCount();

    if (!din.stream().anyMatch(i -> Arrays.equals(i, inputDim))) {
        din.add(inputDim);
    }
    if (!dout.stream().anyMatch(i -> Arrays.equals(i, outputDim))) {
View Full Code Here

Examples of com.github.neuralnetworks.architecture.FullyConnected

    public void initialize(NeuralNetwork nn) {
  List<ConnectionCandidate> ccs = new BreadthFirstOrderStrategy(nn, nn.getInputLayer()).order();
  for (ConnectionCandidate cc : ccs) {
      if (cc.connection instanceof FullyConnected) {
    FullyConnected fc = (FullyConnected) cc.connection;
    if (Util.isBias(fc.getInputLayer())) {
        if (biasDefaultValue != null) {
      fc.getWeights().forEach(i -> fc.getWeights().getElements()[i] = biasDefaultValue);
        } else if (biasRandomInitializer != null) {
      biasRandomInitializer.initialize(fc.getWeights());
        } else {
      randomInitializer.initialize(fc.getWeights());
        }
    } else {
        randomInitializer.initialize(fc.getWeights());
    }
      } else if (cc.connection instanceof Conv2DConnection) {
    Conv2DConnection c = (Conv2DConnection) cc.connection;
    if (Util.isBias(c.getInputLayer())) {
        if (biasDefaultValue != null) {
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.