public Layer(Matrix weightMatrix, Vector biasVector, ActivationFunction af) { activationFunction = af; this.weightMatrix = weightMatrix; lastWeightUpdateMatrix = new Matrix(weightMatrix.getRowDimension(), weightMatrix.getColumnDimension()); penultimateWeightUpdateMatrix = new Matrix(weightMatrix .getRowDimension(), weightMatrix.getColumnDimension()); this.biasVector = biasVector; lastBiasUpdateVector = new Vector(biasVector.getRowDimension()); penultimateBiasUpdateVector = new Vector(biasVector.getRowDimension()); }
public Layer(int numberOfNeurons, int numberOfInputs, double lowerLimitForWeights, double upperLimitForWeights, ActivationFunction af) { activationFunction = af; this.weightMatrix = new Matrix(numberOfNeurons, numberOfInputs); lastWeightUpdateMatrix = new Matrix(weightMatrix.getRowDimension(), weightMatrix.getColumnDimension()); penultimateWeightUpdateMatrix = new Matrix(weightMatrix .getRowDimension(), weightMatrix.getColumnDimension()); this.biasVector = new Vector(numberOfNeurons); lastBiasUpdateVector = new Vector(biasVector.getRowDimension()); penultimateBiasUpdateVector = new Vector(biasVector.getRowDimension()); initializeMatrix(weightMatrix, lowerLimitForWeights, upperLimitForWeights); initializeVector(biasVector, lowerLimitForWeights, upperLimitForWeights); }
/** * Solve A*X = B * * @param B * A Matrix with as many rows as A and any number of columns. * @return X so that L*U*X = B(piv,:) * @exception IllegalArgumentException * Matrix row dimensions must agree. * @exception ApplicationException * Matrix is singular. */ public Matrix solve(Matrix B) { if (B.getRowDimension() != m) { throw new ArgumentException( "Matrix row dimensions must agree."); } if (!this.isNonsingular()) { throw new ApplicationException("Matrix is singular."); } // Copy right hand side with pivoting int nx = B.getColumnDimension(); Matrix Xmat = B.getMatrix(piv, 0, nx - 1); double[][] X = Xmat.getArray(); // Solve L*Y = B(piv,:) for (int k = 0; k < n; k++) { for (int i = k + 1; i < n; i++) { for (int j = 0; j < nx; j++) { X[i][j] -= X[k][j] * LU[i][k]; } } } // Solve U*X = Y; for (int k = n - 1; k >= 0; k--) { for (int j = 0; j < nx; j++) { X[k][j] /= LU[k][k]; } for (int i = 0; i < k; i++) { for (int j = 0; j < nx; j++) { X[i][j] -= X[k][j] * LU[i][k]; } } } return(Xmat); }
/* * ------------------------ Constructor ------------------------ */ /** * LU Decomposition, a structure to access L, U and piv. * * @param A * Rectangular matrix */ public LUDecomposition(Matrix A) { // Use a "left-looking", dot-product, Crout/Doolittle algorithm. LU = A.getArrayCopy(); m = A.getRowDimension(); n = A.getColumnDimension(); piv = new int[m]; for (int i = 0; i < m; i++) { piv[i] = i; } pivsign = 1; double[] LUrowi; double[] LUcolj = new double[m]; // Outer loop. for (int j = 0; j < n; j++) { // Make a copy of the j-th column to localize references. for (int i = 0; i < m; i++) { LUcolj[i] = LU[i][j]; } // Apply previous transformations. for (int i = 0; i < m; i++) { LUrowi = LU[i]; // Most of the time is spent in the following dot product. int kmax = Math.Min(i, j); double s = 0.0; for (int k = 0; k < kmax; k++) { s += LUrowi[k] * LUcolj[k]; } LUrowi[j] = LUcolj[i] -= s; } // Find pivot and exchange if necessary. int p = j; for (int i = j + 1; i < m; i++) { if (Math.Abs(LUcolj[i]) > Math.Abs(LUcolj[p])) { p = i; } } if (p != j) { for (int k = 0; k < n; k++) { double t = LU[p][k]; LU[p][k] = LU[j][k]; LU[j][k] = t; } int k2 = piv[p]; piv[p] = piv[j]; piv[j] = k2; pivsign = -pivsign; } // Compute multipliers. if (j < m & LU[j][j] != 0.0) { for (int i = j + 1; i < m; i++) { LU[i][j] /= LU[j][j]; } } } }
/** * Solve A*X = B * * @param B * A Matrix with as many rows as A and any number of columns. * @return X so that L*U*X = B(piv,:) * @exception IllegalArgumentException * Matrix row dimensions must agree. * @exception ApplicationException * Matrix is singular. */ public Matrix solve(Matrix B) { if (B.getRowDimension() != m) { throw new ArgumentException( "Matrix row dimensions must agree."); } if (!this.isNonsingular()) { throw new ApplicationException("Matrix is singular."); } // Copy right hand side with pivoting int nx = B.getColumnDimension(); Matrix Xmat = B.getMatrix(piv, 0, nx - 1); double[][] X = Xmat.getArray(); // Solve L*Y = B(piv,:) for (int k = 0; k < n; k++) { for (int i = k + 1; i < n; i++) { for (int j = 0; j < nx; j++) { X[i][j] -= X[k][j] * LU[i][k]; } } } // Solve U*X = Y; for (int k = n - 1; k >= 0; k--) { for (int j = 0; j < nx; j++) { X[k][j] /= LU[k][k]; } for (int i = 0; i < k; i++) { for (int j = 0; j < nx; j++) { X[i][j] -= X[k][j] * LU[i][k]; } } } return Xmat; }
// // PRIVATE METHODS // private static void initializeMatrix(Matrix aMatrix, double lowerLimit, double upperLimit) { for (int i = 0; i < aMatrix.getRowDimension(); i++) { for (int j = 0; j < aMatrix.getColumnDimension(); j++) { double random = Util.generateRandomDoubleBetween(lowerLimit, upperLimit); aMatrix.set(i, j, random); } } }