Package com.github.neuralnetworks.tensor

Examples of com.github.neuralnetworks.tensor.Matrix


      this.sharedWeights = new float[0];
  }
    }

    public FullyConnected fullyConnected(Layer inputLayer, Layer outputLayer, int inputUnitCount, int outputUnitCount) {
  Matrix weights = null;
  if (useSharedWeights()) {
      int l = sharedWeights.length;
      sharedWeights = Arrays.copyOf(sharedWeights, l + inputUnitCount * outputUnitCount);
      updateSharedWeights();
      weights = TensorFactory.tensor(sharedWeights, l, outputUnitCount, inputUnitCount);
View Full Code Here


  reset();
    }

    @Override
    public void addItem(Tensor newtorkOutput, Tensor targetOutput) {
  Matrix target = (Matrix) targetOutput;
  Matrix actual = (Matrix) newtorkOutput;
 
  if (dim == -1) {
      dim = target.getRows();
  }

  if (!Arrays.equals(actual.getDimensions(), target.getDimensions())) {
      throw new IllegalArgumentException("Dimensions don't match");
  }

  for (int i = 0; i < target.getColumns(); i++) {
      boolean hasDifferentValues = false;
      for (int j = 0; j < actual.getRows(); j++) {
    if (actual.get(j, i) != actual.get(0, i)) {
        hasDifferentValues = true;
        break;
    }
      }

      if (hasDifferentValues) {
    int targetPos = 0;
    for (int j = 0; j < target.getRows(); j++) {
        if (target.get(j, i) == 1) {
      targetPos = j;
      break;
        }
    }

    int outputPos = 0;
    float max = actual.get(0, i);
    for (int j = 0; j < actual.getRows(); j++) {
        if (actual.get(j, i) > max) {
      max = actual.get(j, i);
      outputPos = j;
        }
    }

    tuples.add(new OutputTargetTuple(outputPos, targetPos));
View Full Code Here

      // log test results
      if (isTesting && logTestResults) {
    MiniBatchFinishedEvent mbe = (MiniBatchFinishedEvent) event;
    if (mbe.getResults() != null) {
        Matrix input = (Matrix) mbe.getData().getInput();
        Matrix target = (Matrix) mbe.getData().getTarget();
        Trainer<?> t = (Trainer<?>) mbe.getSource();
        Matrix networkOutput = (Matrix) mbe.getResults().get(t.getNeuralNetwork().getOutputLayer());

        for (int i = 0; i < input.getColumns(); i++) {
      sb.append(s);
      sb.append("Input:  ");

      for (int j = 0; j < input.getRows(); j++) {
          sb.append(input.get(j, i)).append("  ");
      }

      sb.append(s);
      sb.append("Output: ");
      for (int j = 0; j < networkOutput.getRows(); j++) {
          sb.append(networkOutput.get(j, i)).append("  ");
      }

      sb.append(s);
      sb.append("Target: ");
      for (int j = 0; j < target.getRows(); j++) {
View Full Code Here

  FullyConnected c1 = new FullyConnected(il1, ol, TensorFactory.tensor(weights, new int[][]{{0, 0, 0}, {0, 1, 2}}));
  FullyConnected c2 = new FullyConnected(il2, ol, TensorFactory.tensor(weights, new int[][]{{1, 0, 0}, {1, 1, 2}}));
  FullyConnected bc = new FullyConnected(new Layer(), ol, 1, 2);

  Matrix cg = c1.getWeights();
  cg.set(1, 0, 0);
  cg.set(2, 0, 1);
  cg.set(3, 0, 2);
  cg.set(4, 1, 0);
  cg.set(5, 1, 1);
  cg.set(6, 1, 2);

  cg = c2.getWeights();
  cg.set(1, 0, 0);
  cg.set(2, 0, 1);
  cg.set(3, 0, 2);
  cg.set(4, 1, 0);
  cg.set(5, 1, 1);
  cg.set(6, 1, 2);

  Matrix bcg = bc.getWeights();
  bcg.set(0.1f, 0, 0);
  bcg.set(0.2f, 1, 0);

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

  NeuralNetworkImpl nn = new NeuralNetworkImpl();
  nn.addConnections(connections.toArray(new Connections[connections.size()]));

  ValuesProvider vp = TensorFactory.tensorProvider(nn, 2, true);

  Matrix i1 = vp.get(nn.getInputLayer());
  i1.set(1, 0, 0);
  i1.set(2, 1, 0);
  i1.set(3, 2, 0);
  i1.set(4, 0, 1);
  i1.set(5, 1, 1);
  i1.set(6, 2, 1);

  ConnectionCalculatorFullyConnected aws = new AparapiWeightedSumConnectionCalculator();
  aws.calculate(connections, vp, ol);

  // most simple case
  Matrix o = vp.get(nn.getOutputLayer());
  assertEquals(14, o.get(0, 0), 0);
  assertEquals(32, o.get(0, 1), 0);
  assertEquals(32, o.get(1, 0), 0);
  assertEquals(77, o.get(1, 1), 0);

  // with bias
  connections = new ArrayList<>();
  connections.add(c1);
  connections.add(bc);

  nn = new NeuralNetworkImpl();
  nn.addConnections(connections.toArray(new Connections[connections.size()]));
  vp = TensorFactory.tensorProvider(nn, 2, true);

  i1 = vp.get(nn.getInputLayer());
  i1.set(1, 0, 0);
  i1.set(2, 1, 0);
  i1.set(3, 2, 0);
  i1.set(4, 0, 1);
  i1.set(5, 1, 1);
  i1.set(6, 2, 1);

  aws = new AparapiWeightedSumConnectionCalculator();
  aws.calculate(connections, vp, ol);

  o = vp.get(nn.getOutputLayer());
  assertEquals(14.1, o.get(0, 0), 0.01);
  assertEquals(32.1, o.get(0, 1), 0.01);
  assertEquals(32.2, o.get(1, 0), 0.01);
  assertEquals(77.2, o.get(1, 1), 0.01);

  // combined layers
  connections = new ArrayList<>();
  connections.add(c1);
  connections.add(c2);
  connections.add(bc);
  nn = new NeuralNetworkImpl();
  nn.addConnections(connections.toArray(new Connections[connections.size()]));
  vp = TensorFactory.tensorProvider(nn, 2, true);

  i1 = vp.get(il1);
  i1.set(1, 0, 0);
  i1.set(2, 1, 0);
  i1.set(3, 2, 0);
  i1.set(4, 0, 1);
  i1.set(5, 1, 1);
  i1.set(6, 2, 1);

  Matrix i2 = vp.get(il2);
  i2.set(1, 0, 0);
  i2.set(2, 1, 0);
  i2.set(3, 2, 0);
  i2.set(4, 0, 1);
  i2.set(5, 1, 1);
  i2.set(6, 2, 1);

  aws = new AparapiWeightedSumConnectionCalculator();
  aws.calculate(connections, vp, ol);

  o = vp.get(nn.getOutputLayer());
View Full Code Here

  DBN dbn = NNFactory.dbn(new int [] {3, 2, 2}, true);
  dbn.setLayerCalculator(NNFactory.lcSigmoid(dbn, null));

  RBM firstRBM = dbn.getFirstNeuralNetwork();

  Matrix cg1 = firstRBM.getMainConnections().getWeights();
  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 = firstRBM.getVisibleBiasConnections().getWeights();
  cgb1.set(0f, 0, 0);
  cgb1.set(0f, 1, 0);
  cgb1.set(0f, 2, 0);

  Matrix cgb2 = firstRBM.getHiddenBiasConnections().getWeights();
  cgb2.set(-0.4f, 0, 0);
  cgb2.set(0.2f, 1, 0);

  SimpleInputProvider inputProvider = new SimpleInputProvider(new float[][] { { 1, 0, 1 } }, null);

  AparapiCDTrainer firstTrainer = TrainerFactory.cdSigmoidTrainer(firstRBM, null, null, null, null, 1f, 0f, 0f, 0f, 1, 1, 1, true);

  RBM secondRBM = dbn.getLastNeuralNetwork();

  AparapiCDTrainer secondTrainer = TrainerFactory.cdSigmoidTrainer(secondRBM, null, null, null, null, 1f, 0f, 0f, 0f, 1, 1, 1, true);

  Map<NeuralNetwork, OneStepTrainer<?>> layerTrainers = new HashMap<>();
  layerTrainers.put(firstRBM, firstTrainer);
  layerTrainers.put(secondRBM, secondTrainer);

  DNNLayerTrainer trainer = TrainerFactory.dnnLayerTrainer(dbn, layerTrainers, inputProvider, null, null);
  trainer.train();
 
  assertEquals(0.2 + 0.13203661, cg1.get(0, 0), 0.00001);
  assertEquals(0.4 - 0.22863509,  cg1.get(0, 1), 0.00001);
  assertEquals(-0.5 + 0.12887852, cg1.get(0, 2), 0.00001);
  assertEquals(-0.3 + 0.26158813, cg1.get(1, 0), 0.00001);
  assertEquals(0.1 - 0.3014404,  cg1.get(1, 1), 0.00001);
  assertEquals(0.2 + 0.25742438, cg1.get(1, 2), 0.00001);

  assertEquals(0.52276707, cgb1.get(0, 0), 0.00001);
  assertEquals(- 0.54617375, cgb1.get(1, 0), 0.00001);
  assertEquals(0.51522285, cgb1.get(2, 0), 0.00001);

  assertEquals(-0.4 - 0.08680013, cgb2.get(0, 0), 0.00001);
  assertEquals(0.2 - 0.02693379, cgb2.get(1, 0), 0.00001);
    }
View Full Code Here

  RBM firstRBM = dbn.getFirstNeuralNetwork();

  LayerCalculatorImpl lc = (LayerCalculatorImpl) dbn.getLayerCalculator();
  lc.addConnectionCalculator(firstRBM.getHiddenLayer(), new AparapiWeightedSumConnectionCalculator());

  Matrix m1 = firstRBM.getMainConnections().getWeights();
  m1.set(1, 0, 0);
  m1.set(0, 0, 1);
  m1.set(0, 0, 2);
  m1.set(0, 1, 0);
  m1.set(1, 1, 1);
  m1.set(0, 1, 2);
  m1.set(0, 2, 0);
  m1.set(0, 2, 1);
  m1.set(1, 2, 2);

  RBM secondRBM = dbn.getLastNeuralNetwork();
 
  Matrix cg1 = secondRBM.getMainConnections().getWeights();
  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 = secondRBM.getVisibleBiasConnections().getWeights();
  cgb1.set(0f, 0, 0);
  cgb1.set(0f, 1, 0);
  cgb1.set(0f, 2, 0);
 
  Matrix cgb2 = secondRBM.getHiddenBiasConnections().getWeights();
  cgb2.set(-0.4f, 0, 0);
  cgb2.set(0.2f, 1, 0);

  SimpleInputProvider inputProvider = new SimpleInputProvider(new float[][] { { 1, 0, 1 } }, null);

  AparapiCDTrainer firstTrainer = TrainerFactory.cdSigmoidTrainer(firstRBM, null, null, null, null, 0f, 0f, 0f, 0f, 0, 1, 1, true);

  AparapiCDTrainer secondTrainer = TrainerFactory.cdSigmoidTrainer(secondRBM, null, null, null, null, 1f, 0f, 0f, 0f, 1, 1, 1, true);

  Map<NeuralNetwork, OneStepTrainer<?>> layerTrainers = new HashMap<>();
  layerTrainers.put(firstRBM, firstTrainer);
  layerTrainers.put(secondRBM, secondTrainer);

  DNNLayerTrainer trainer = TrainerFactory.dnnLayerTrainer(dbn, layerTrainers, inputProvider, null, null);
  trainer.train();
 
  assertEquals(0.2 + 0.13203661, cg1.get(0, 0), 0.00001);
  assertEquals(0.4 - 0.22863509,  cg1.get(0, 1), 0.00001);
  assertEquals(-0.5 + 0.12887852, cg1.get(0, 2), 0.00001);
  assertEquals(-0.3 + 0.26158813, cg1.get(1, 0), 0.00001);
  assertEquals(0.1 - 0.3014404,  cg1.get(1, 1), 0.00001);
  assertEquals(0.2 + 0.25742438, cg1.get(1, 2), 0.00001);

  assertEquals(0.52276707, cgb1.get(0, 0), 0.00001);
  assertEquals(- 0.54617375, cgb1.get(1, 0), 0.00001);
  assertEquals(0.51522285, cgb1.get(2, 0), 0.00001);
 
  assertEquals(-0.4 - 0.08680013, cgb2.get(0, 0), 0.00001);
  assertEquals(0.2 - 0.02693379, cgb2.get(1, 0), 0.00001);
    }
View Full Code Here

  NNFactory.addFullyConnectedLayer(nn, h, cf, 2, 3, true);
  NNFactory.addFullyConnectedLayer(nn, o, cf, 4, 1, true);

  ValuesProvider tp = TensorFactory.tensorProvider(nn, 2, true);

  Matrix im = tp.get(nn.getInputLayer());
  Matrix hm1 = tp.get(h, 3, 2);
  Matrix hm2 = tp.get(h, 4, 2);

  Tensor om = tp.get(o);

  assertTrue(im == tp.get(i, 2, 2));
  assertTrue(im == tp.get(i));
View Full Code Here

  // simple mlp test
  Environment.getInstance().setUseWeightsSharedMemory(true);
  NeuralNetworkImpl nn = NNFactory.mlp(new int[] { 3, 4, 2 }, true);
  ValuesProvider tp = TensorFactory.tensorProvider(nn, 2, false);

  Matrix in = tp.get(nn.getInputLayer());
  Matrix hidden = tp.get(nn.getLayers().stream().filter(l -> l != nn.getInputLayer() && l != nn.getOutputLayer() && !Util.isBias(l)).findFirst().get());
  Matrix out = tp.get(nn.getOutputLayer());

  assertEquals(6, in.getElements().length, 0);
  assertEquals(3, in.getRows(), 0);
  assertEquals(2, in.getColumns(), 0);
  assertEquals(8, hidden.getElements().length, 0);
  assertEquals(4, hidden.getRows(), 0);
  assertEquals(2, hidden.getColumns(), 0);
  assertEquals(4, out.getElements().length, 0);
  assertEquals(2, out.getRows(), 0);
  assertEquals(2, out.getColumns(), 0);

  hidden.set(2, 1, 0);
  hidden.set(3, 2, 0);
  hidden.set(4, 2, 1);
  hidden.set(5, 3, 1);
  assertEquals(2, hidden.get(1, 0), 0);
  assertEquals(3, hidden.get(2, 0), 0);
  assertEquals(4, hidden.get(2, 1), 0);
  assertEquals(5, hidden.get(3, 1), 0);

  out.set(8, 1, 0);
  out.set(9, 1, 1);
  assertEquals(8, out.get(1, 0), 0);
  assertEquals(9, out.get(1, 1), 0);
    }
View Full Code Here

    @Test
    public void testSoftMax() {
  Tensor t = TensorFactory.tensor(5, 4, 2);
  IntStream.range(0, t.getElements().length).forEach(i -> t.getElements()[i] = i + 1);
  Matrix m = TensorFactory.tensor(t, new int[][] { { 1, 1, 1 }, { 3, 2, 1 } });
  m.set(1, 0, 0);
  m.set(2, 1, 0);
  m.set(3, 2, 0);
  m.set(4, 0, 1);
  m.set(5, 1, 1);
  m.set(6, 2, 1);

  SoftmaxFunction sf = new SoftmaxFunction();
  Environment.getInstance().setExecutionMode(EXECUTION_MODE.SEQ);
  sf.value(m);

  assertEquals(1 / 6f, m.get(0, 0), 0);
  assertEquals(2 / 6f, m.get(1, 0), 0);
  assertEquals(3 / 6f, m.get(2, 0), 0);
  assertEquals(4 / 15f, m.get(0, 1), 0);
  assertEquals(5 / 15f, m.get(1, 1), 0);
  assertEquals(6 / 15f, m.get(2, 1), 0);
    }
View Full Code Here

  assertEquals(13, tarr[1].get(1, 1), 0);
    }

    @Test
    public void testMatrix() {
  Matrix m = TensorFactory.tensor(5, 6);

  assertEquals(5, m.getRows(), 0);
  assertEquals(6, m.getColumns(), 0);
  assertEquals(6, m.getDimensionElementsDistance(0), 0);
  assertEquals(1, m.getDimensionElementsDistance(1), 0);
  assertEquals(5, m.getDimensions()[0], 0);
  assertEquals(6, m.getDimensions()[1], 0);

  for (int i = 0; i < m.getElements().length; i++) {
      m.getElements()[i] = i + 1;
  }

  assertEquals(2, m.get(0, 1), 0);
  assertEquals(15, m.get(2, 2), 0);

  m = TensorFactory.tensor(1, 6);
  for (int i = 0; i < m.getElements().length; i++) {
      m.getElements()[i] = i + 1;
  }

  assertEquals(2, m.get(0, 1), 0);
  assertEquals(6, m.get(0, 5), 0);

  m = TensorFactory.tensor(6, 1);
  for (int i = 0; i < m.getElements().length; i++) {
      m.getElements()[i] = i + 1;
  }

  assertEquals(2, m.get(1, 0), 0);
  assertEquals(6, m.get(5, 0), 0);

  // submatrix
  Tensor t = TensorFactory.tensor(5, 5, 5);
  float[] elements = t.getElements();

  for (int i = 0; i < elements.length; i++) {
      elements[i] = i + 1;
  }

  m = TensorFactory.tensor(t, new int[][] { { 1, 0, 0 }, { 1, 4, 4 } });
  assertEquals(26, m.get(0, 0), 0);
  assertEquals(27, m.get(0, 1), 0);
  assertEquals(36, m.get(2, 0), 0);
  assertEquals(38, m.get(2, 2), 0);

  m = TensorFactory.tensor(t, new int[][] { { 1, 0, 0 }, { 1, 4, 4 } });
  assertEquals(26, m.get(0, 0), 0);
  assertEquals(27, m.get(0, 1), 0);
  assertEquals(36, m.get(2, 0), 0);
  assertEquals(38, m.get(2, 2), 0);

  m = TensorFactory.tensor(t, new int[][] { { 0, 0, 1 }, { 4, 4, 1 } });
  assertEquals(2, m.get(0, 0), 0);
  assertEquals(7, m.get(0, 1), 0);
  assertEquals(12, m.get(0, 2), 0);
  assertEquals(27, m.get(1, 0), 0);
  assertEquals(32, m.get(1, 1), 0);
  assertEquals(37, m.get(1, 2), 0);

  m = TensorFactory.tensor(t, new int[][] { { 2, 2, 1 }, { 3, 3, 1 } });
  assertEquals(62, m.get(0, 0), 0);
  assertEquals(67, m.get(0, 1), 0);
  assertEquals(92, m.get(1, 1), 0);
  Iterator<Integer> it = m.iterator();

  assertEquals(62, m.getElements()[it.next()], 0);
  assertEquals(67, m.getElements()[it.next()], 0);
  it.next();
  assertEquals(92, m.getElements()[it.next()], 0);

  it = m.iterator(new int[][] { { 1, 0 }, { 1, 1 } });
  it.next();
  assertEquals(92, m.getElements()[it.next()], 0);

  m = TensorFactory.tensor(4, 4);
  for (int i = 0; i < m.getElements().length; i++) {
      m.getElements()[i] = i + 1;
  }

  Matrix m2 = TensorFactory.tensor(m, new int[][] { { 1, 1 }, { 2, 2 } });
  assertEquals(6, m2.get(0, 0), 0);
  assertEquals(7, m2.get(0, 1), 0);
  assertEquals(10, m2.get(1, 0), 0);
  assertEquals(11, m2.get(1, 1), 0);
    }
View Full Code Here

TOP

Related Classes of com.github.neuralnetworks.tensor.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.