Package com.github.neuralnetworks.util

Examples of com.github.neuralnetworks.util.Matrix


    @Test
    public void testRBMLayerCalculator1() {
  RBM rbm = NNFactory.rbm(2, 2, false);
  rbm.setLayerCalculator(NNFactory.rbmSigmoidSigmoid(rbm));

  Matrix cg1 = rbm.getMainConnections().getConnectionGraph();
  cg1.set(0.1f, 0, 0);
  cg1.set(0.8f, 0, 1);
  cg1.set(0.4f, 1, 0);
  cg1.set(0.6f, 1, 1);

  RBMLayerCalculator lc = (RBMLayerCalculator) rbm.getLayerCalculator();

  Matrix visible = new Matrix(new float[] { 0.35f, 0.9f }, 1);
  Matrix hidden = new Matrix(2, 1);
  lc.calculateHiddenLayer(rbm, visible, hidden);

  assertEquals(0.68, hidden.get(0, 0), 0.01);
  assertEquals(0.6637, hidden.get(1, 0), 0.01);
    }
View Full Code Here


    @Test
    public void testRBMLayerCalculator2() {
  RBM rbm = NNFactory.rbm(2, 2, false);
  rbm.setLayerCalculator(NNFactory.rbmSigmoidSigmoid(rbm));

  Matrix cg1 = rbm.getMainConnections().getConnectionGraph();
  cg1.set(0.1f, 0, 0);
  cg1.set(0.8f, 1, 0);
  cg1.set(0.4f, 0, 1);
  cg1.set(0.6f, 1, 1);

  RBMLayerCalculator lc = (RBMLayerCalculator) rbm.getLayerCalculator();

  Matrix visible = new Matrix(2, 1);
  Matrix hidden = new Matrix(new float[] { 0.35f, 0.9f }, 1);

  lc.calculateVisibleLayer(rbm, visible, hidden);

  assertEquals(0.68, visible.get(0, 0), 0.01);
  assertEquals(0.6637, visible.get(1, 0), 0.01);
View Full Code Here

    @Test
    public void testRBMLayerCalculator3() {
  RBM rbm = NNFactory.rbm(3, 2, true);
  rbm.setLayerCalculator(NNFactory.rbmSigmoidSigmoid(rbm));

  Matrix cg1 = rbm.getMainConnections().getConnectionGraph();
  cg1.set(0.2f, 0, 0);
  cg1.set(0.4f, 0, 1);
  cg1.set(-0.5f, 0, 2);
  cg1.set(-0.3f, 1, 0);
  cg1.set(0.1f, 1, 1);
  cg1.set(0.2f, 1, 2);

  Matrix cgb1 = rbm.getHiddenBiasConnections().getConnectionGraph();
  cgb1.set(-0.4f, 0, 0);
  cgb1.set(0.2f, 1, 0);

  RBMLayerCalculator lc = (RBMLayerCalculator) rbm.getLayerCalculator();
  Matrix visible = new Matrix(new float[] { 1f, 0f, 1f }, 1);
  Matrix hidden = new Matrix(2, 1);
  lc.calculateHiddenLayer(rbm, visible, hidden);

  assertEquals(0.332, hidden.get(0, 0), 0.001);
  assertEquals(0.525, hidden.get(1, 0), 0.001);
    }
View Full Code Here

    @Test
    public void testRBMLayerCalculator4() {
  RBM rbm = NNFactory.rbm(2, 3, true);
  rbm.setLayerCalculator(NNFactory.rbmSigmoidSigmoid(rbm));

  Matrix cg1 = rbm.getMainConnections().getConnectionGraph();
  cg1.set(0.2f, 0, 0);
  cg1.set(0.4f, 1, 0);
  cg1.set(-0.5f, 2, 0);
  cg1.set(-0.3f, 0, 1);
  cg1.set(0.1f, 1, 1);
  cg1.set(0.2f, 2, 1);

  Matrix cgb1 = rbm.getVisibleBiasConnections().getConnectionGraph();
  cgb1.set(-0.4f, 0, 0);
  cgb1.set(0.2f, 1, 0);

  RBMLayerCalculator lc = (RBMLayerCalculator) rbm.getLayerCalculator();
  Matrix hidden = new Matrix(new float[] { 1f, 0f, 1f }, 1);
  Matrix visible = new Matrix(2, 1);
  lc.calculateVisibleLayer(rbm, visible, hidden);

  assertEquals(0.332, visible.get(0, 0), 0.001);
  assertEquals(0.525, visible.get(1, 0), 0.001);
    }
View Full Code Here

  results = new ValuesProvider();
  intermediateResults = new ValuesProvider();
    }

    public void gibbsSampling(RBM rbm, Matrix posPhaseVisible, Matrix posPhaseHidden, Matrix negPhaseVisible, Matrix negPhaseHidden, int samplingCount, boolean resetNetwork, boolean useIntermediateResults) {
  Matrix hidden, visible;
  calculateHiddenLayer(rbm, posPhaseVisible, posPhaseHidden);

  if (resetNetwork) {
      System.arraycopy(posPhaseHidden.getElements(), 0, negPhaseHidden.getElements(), 0, negPhaseHidden.getElements().length);
  }
View Full Code Here

  super.calculate(rbm, hiddenLayer, calculatedLayers, results);
    }

    private Matrix getLayerResult(Layer layer, Matrix realResult, boolean useIntermediateResults) {
  Matrix result = realResult;
  if (useIntermediateResults) {
      intermediateResults.setColumns(realResult.getColumns());
      result = intermediateResults.getValues(layer, realResult.getRows());
      if (result == null) {
    intermediateResults.addValues(layer, result = new Matrix(realResult));
      }

      System.arraycopy(realResult.getElements(), 0, result.getElements(), 0, result.getElements().length);
  }

  return result;
    }
View Full Code Here

  if (!values.containsKey(targetLayer)) {
      values.put(targetLayer, new HashSet<Matrix>());
  }

  Set<Matrix> set = values.get(targetLayer);
  Matrix result = set.stream().filter(m -> m.getRows() == rows && m.getColumns() == getColumns()).findFirst().orElse(null);

  if (result == null) {
      set.add(result = new Matrix(rows, getColumns()));
  }

  return result;
    }
View Full Code Here

  this.weightsInitialStep = new int[series];
  this.weightsStep = new int[series];

  int totalInputSize = 0, totalWeightSize = 0, i = 0;
  for (java.util.Map.Entry<GraphConnections, Integer> e : inputConnections.entrySet()) {
      Matrix cg = e.getKey().getConnectionGraph();

      inputStartPositions[i] = totalInputSize;
      totalInputSize += e.getValue();
      weightStartPositions[i] = totalWeightSize;
      totalWeightSize += e.getKey().getConnectionGraph().getElements().length;

      // depending on the direction of the calculation
      if (e.getKey().getOutputLayer() == targetLayer) {
    weightsDimension[i] = cg.getColumns();
    weightsInitialStep[i] = cg.getColumns();
    weightsStep[i] = 1;
      } else {
    weightsDimension[i] = cg.getRows();
    weightsInitialStep[i] = 1;
    weightsStep[i] = cg.getColumns();
      }

      i++;
  }
View Full Code Here

    public FullyConnected(Layer inputLayer, Layer outputLayer, int inputUnitCount, int outputUnitCount) {
  super(inputLayer, outputLayer);

  // connection graph is initialized depending on the size of the input/output layers
  connectionGraph = new Matrix(new float[inputUnitCount * outputUnitCount], inputUnitCount);
    }
View Full Code Here

  }

  public void setInputOutput(Matrix inputOutput) {
      this.input = inputOutput;
      if (target == null || target.getElements().length != input.getElements().length) {
    target = new Matrix(input.getRows(), input.getColumns());
      }

      System.arraycopy(input.getElements(), 0, target.getElements(), 0, target.getElements().length);
  }
View Full Code Here

TOP

Related Classes of com.github.neuralnetworks.util.Matrix

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.