Package com.github.neuralnetworks.architecture

Examples of com.github.neuralnetworks.architecture.ConnectionFactory


    @Test
    public void testParallelNetworks() {
  Environment.getInstance().setExecutionMode(EXECUTION_MODE.SEQ);

  Environment.getInstance().setUseWeightsSharedMemory(true);
  ConnectionFactory cf = new ConnectionFactory();
  NeuralNetworkImpl mlp = new NeuralNetworkImpl();
  Layer input = new Layer();
  Layer leaf1 = new Layer();
  Layer leaf2 = new Layer();
  Layer output = new Layer();

  mlp.addLayer(input);

  FullyConnected fc1 = cf.fullyConnected(input, leaf1, 2, 3);
  fc1.getWeights().forEach(i -> fc1.getWeights().getElements()[i] = 0.1f);
  mlp.addConnections(fc1);

  FullyConnected fc2 = cf.fullyConnected(input, leaf2, 2, 3);
  fc2.getWeights().forEach(i -> fc2.getWeights().getElements()[i] = 0.2f);
  mlp.addConnections(fc2);

  FullyConnected fc3 = cf.fullyConnected(leaf1, output, 3, 1);
  fc3.getWeights().forEach(i -> fc3.getWeights().getElements()[i] = 0.3f);
  mlp.addConnections(fc3);
  FullyConnected fc4 = cf.fullyConnected(leaf2, output, 3, 1);
  fc4.getWeights().forEach(i -> fc4.getWeights().getElements()[i] = 0.4f);
  mlp.addConnections(fc4);

  mlp.setLayerCalculator(NNFactory.lcWeightedSum(mlp, null));
View Full Code Here


  if (layers[0].length != 3) {
      throw new IllegalArgumentException("first layer must be convolutional");
  }

  NeuralNetworkImpl result = new NeuralNetworkImpl();
  ConnectionFactory cf = new ConnectionFactory();
  result.setProperties(new Properties());
  result.getProperties().setParameter(Constants.CONNECTION_FACTORY, cf);

  Layer prev = null;
  int prevUnitCount = layers[0][0] * layers[0][1] * layers[0][2];
  result.addLayer(prev = new Layer());
  for (int i = 1; i < layers.length; i++) {
      int[] l = layers[i];
      Layer newLayer = null;
      Layer biasLayer = null;
      if (l.length == 1) {
    cf.fullyConnected(prev, newLayer = new Layer(), prevUnitCount, l[0]);
    if (addBias) {
        cf.fullyConnected(biasLayer = new Layer(), newLayer, 1, l[0]);
    }

    prevUnitCount = l[0];
      } else if (l.length == 4 || l.length == 2) {
    Integer inputFMRows = null;
    Integer inputFMCols = null;
    Integer filters = null;
    if (i == 1) {
        inputFMRows = layers[0][0];
        inputFMCols = layers[0][1];
        filters = layers[0][2];
    } else {
        for (Connections c : prev.getConnections()) {
      if (c.getOutputLayer() == prev) {
          if (c instanceof Conv2DConnection) {
        Conv2DConnection cc = (Conv2DConnection) c;
        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);
View Full Code Here

     * @param useSharedMemory - whether all network weights will be part of single array
     * @return
     */
    public static NeuralNetworkImpl mlp(int[] layers, boolean addBias) {
  NeuralNetworkImpl result = new NeuralNetworkImpl();
  mlp(result, new ConnectionFactory(), layers, addBias);
  return result;
    }
View Full Code Here

  return result;
    }

    public static Autoencoder autoencoder(int visibleCount, int hiddenCount, boolean addBias) {
  Autoencoder result = new Autoencoder();
  mlp(result, new ConnectionFactory(), new int[] {visibleCount, hiddenCount, visibleCount}, addBias);
  return result;
    }
View Full Code Here

  return ae;
    }

    public static RBM rbm(int visibleCount, int hiddenCount, boolean addBias) {
  RBM result = new RBM();
  ConnectionFactory cf = new ConnectionFactory();
  result.addConnections(cf.fullyConnected(new Layer(), new Layer(), visibleCount, hiddenCount));

  if (addBias) {
      result.addConnections(cf.fullyConnected(new Layer(), result.getVisibleLayer(), 1, visibleCount));
      result.addConnections(cf.fullyConnected(new Layer(), result.getHiddenLayer(), 1, hiddenCount));
  }

  return result;
    }
View Full Code Here

     * @return Weight update tensors
     */
    public static Map<Connections, Tensor> weightUpdates(NeuralNetworkImpl nn) {
  Map<Connections, Tensor> result = new HashMap<>();

  ConnectionFactory cf = nn.getProperties().getParameter(Constants.CONNECTION_FACTORY);

  List<Connections> connections = cf.getConnections().stream().filter(c -> c instanceof WeightsConnections).collect(Collectors.toList());
  float[] elements = cf.useSharedWeights() ? new float[((WeightsConnections) connections.get(0)).getWeights().getElements().length] : null;
  IntStream.range(0, connections.size()).forEach(i -> result.put(connections.get(i), TensorFactory.duplicate(((WeightsConnections) connections.get(i)).getWeights(), elements)));

  return result;
    }
View Full Code Here

  if (layers.length <= 1) {
      throw new IllegalArgumentException("more than one layer is required");
  }

  DBN result = new DBN();
  ConnectionFactory cf = new ConnectionFactory();
  result.setProperties(new Properties());
  result.getProperties().setParameter(Constants.CONNECTION_FACTORY, cf);

  result.addLayer(new Layer());
  for (int i = 1; i < layers.length; i++) {
      RBM rbm = new RBM();
      rbm.setProperties(new Properties());
      rbm.getProperties().setParameter(Constants.CONNECTION_FACTORY, cf);

      rbm.addConnections(cf.fullyConnected(result.getOutputLayer(), new Layer(), layers[i - 1], layers[i]));

      if (addBias) {
    rbm.addConnections(cf.fullyConnected(new Layer(), rbm.getVisibleLayer(), 1, layers[i - 1]));
    rbm.addConnections(cf.fullyConnected(new Layer(), rbm.getHiddenLayer(), 1, layers[i]));
      }

      result.addNeuralNetwork(rbm);
  }
View Full Code Here

    public static StackedAutoencoder sae(int[] layers, boolean addBias) {
  if (layers == null || layers.length <= 1) {
      throw new IllegalArgumentException("more than one layer is required");
  }

  ConnectionFactory cf = new ConnectionFactory();
  Properties properties = new Properties();
  properties.setParameter(Constants.CONNECTION_FACTORY, cf);
  StackedAutoencoder result = new StackedAutoencoder(new Layer());
  result.setProperties(properties);
View Full Code Here

  Layer o = new Layer();

  nn.addLayer(i);

  Environment.getInstance().setUseWeightsSharedMemory(true);
  ConnectionFactory cf = new ConnectionFactory();
  NNFactory.addFullyConnectedLayer(nn, h, cf, 2, 3, true);
  NNFactory.addFullyConnectedLayer(nn, o, cf, 4, 1, true);

  ValuesProvider tp = TensorFactory.tensorProvider(nn, 2, true);
View Full Code Here

    }

    @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);
  assertEquals(4, c.getWeights().getDimensions().length, 0);
    }
View Full Code Here

TOP

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

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.