Esempio n. 1
0
  public static Matrix multiplyAddBias(Matrix A, Matrix B, Matrix Bias) {
    int aRows = A.getHeight();
    int aCols = A.getWidth();
    int bRows = B.getHeight();
    int bCols = B.getWidth();
    int biasRows = Bias.getHeight();
    int biasCols = Bias.getWidth();

    // Cannot multiply matrices whose dimensions do not match.
    // We also need the bias to have the same dimension as the new matrix.
    if (aCols != bRows || aRows != biasRows || bCols != biasCols) {
      return null;
    }

    double[][] newMat = new double[aRows][bCols];

    for (int i = 0; i < aRows; i++) {
      for (int j = 0; j < bCols; j++) {
        for (int k = 0; k < aCols; k++) {
          newMat[i][j] += A.get(i, k) * B.get(k, j);
        }
        newMat[i][j] += Bias.get(i, j);
      }
    }

    return new Matrix(newMat);
  }
Esempio n. 2
0
  public static Matrix multiplyMatrices(Matrix A, Matrix B) {
    int aRows = A.getHeight();
    int aCols = A.getWidth();
    int bRows = B.getHeight();
    int bCols = B.getWidth();

    // Cannot multiply matrices whose dimensions do not match.
    if (aCols != bRows) {
      return null;
    }

    double[][] newMat = new double[aRows][bCols];

    for (int i = 0; i < aRows; i++) {
      for (int j = 0; j < bCols; j++) {
        for (int k = 0; k < aCols; k++) {
          newMat[i][j] += A.get(i, k) * B.get(k, j);
        }
      }
    }

    return new Matrix(newMat);
  }
Esempio n. 3
0
  /**
   * Multiplies two matrices together and then adds the bias matrix in and then logsigs them all.
   */
  public static Matrix multiplyLogsigWithBias(Matrix A, Matrix B, Matrix Bias) {
    int aRows = A.getHeight();
    int aCols = A.getWidth();
    int bRows = B.getHeight();
    int bCols = B.getWidth();
    int biasRows = Bias.getHeight();
    int biasCols = Bias.getWidth();

    // Cannot multiply matrices whose dimensions do not match.
    // We also need the bias to have the same dimension as the new matrix.
    if (aCols != bRows) {
      System.out.println("A does not have the same number of columns as B does rows");
      System.out.println("A Number of cols: " + A.getWidth());
      System.out.println("B Number of rows: " + B.getHeight());
      return null;
    }

    if (aRows != biasRows) {
      System.out.println("Bias does not have the same number of rows as A");
      return null;
    }

    if (bCols != biasCols) {
      System.out.println("Bias does not have the same number of cols as B");
      return null;
    }

    double[][] newMat = new double[aRows][bCols];

    for (int i = 0; i < aRows; i++) {
      for (int j = 0; j < bCols; j++) {
        for (int k = 0; k < aCols; k++) {
          newMat[i][j] += A.get(i, k) * B.get(k, j);
        }
        newMat[i][j] += Bias.get(i, j);
        newMat[i][j] = 1.0 / (1 + Math.exp(-1 * newMat[i][j]));
      }
    }

    return new Matrix(newMat);
  }