Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
testNet().
@param test_inputs A double[][] matrix of network inputs
@return A double[][] matrix of network outputs
*/
public double[][] testNet( double[][] test_inputs ) {
double[][] test_ip,test_out1,test_out2,test_out3;
test_ip = multiply(W1,test_inputs);
test_out1 = logsig (test_ip,b1);
test_ip = multiply(W2,test_out1);
test_out2 = logsig (test_ip,b2);
test_ip = multiply(W3,test_out2);
test_out3 = logsig (test_ip,b3);
return test_out3;
}
/**Establishes what the training inputs and targets are to be,
and prepares the network for subsequent calls to trainNet(). This
method need be called only once.
<p>The inputs[][] variable is set to the first argument, and so it must
have R rows and a number of columns which is at least one. The number of
columns establishes Q, the number of inputs. The targets[][] variable is
set to the second argument and so that must have S3 rows and Q columns.
If the number of columns in the two arguments are not the same, or if
the arguments do not have R and S3 rows respectively, the method returns
false. The individual input vectors and corresponding target vectors
used to train the network are thus the column vectors in the inputs[][]
and targets[][] arrays. All neuron activations in this network are
constrained to the interval [0,1] by a log-sigmoid function.
Inputs to the network are also constrained to this interval; to use
this class with a set of inputs that span a different range, a
scaling algorithm would need to be applied to the inputs and targets
before calling this method or testNet().
@param inputs A double[][] matrix of network inputs
@param targets A double[][] matrix of target network outputs
@return A boolean value indicating successful invocation.
*/
public boolean trainInit( double[][] inputs, double[][] targets ) {
// First initialize inputs and targets.
this.inputs = inputs;
this.targets= targets;
// First create arrays dW1,dW2,dW3 to be same size as W1,W2,W3 arrays.
// Java initializes all elements to zero so we don't need to.
double[][] ip; //used for inner products
dW1
dW2
dW3
db1
db2
db3
=
=
=
=
=
=
new
new
new
new
new
new
double[S1][R];
double[S2][S1];
double[S3][S2];
double[S1][1];
double[S2][1];
double[S3][1];
MC=0;
int[] dim_inputs = getSize(inputs);
int[] dim_targets = getSize(targets);
Q=dim_inputs[1];
errors=new double[S3][Q];
if (dim_targets[1] != Q) {
new_W1[][]
new_W2[][]
new_W3[][]
new_b1[][]
new_b2[][]
new_b3[][]
=
=
=
=
=
=
new
new
new
new
new
new
double[S1][R];
double[S2][S1];
double[S3][S2];
double[S1][1];
double[S2][1];
double[S3][1];
epoch++;
// LEARNING PHASE
// Calculate dW arrays and db arrays
// dW1:
for (int i=0;i<S1;i++) {
for (int j=0;j<R;j++) {
dW1[i][j] *= MC; // momentum term
for (int k=0;k<Q;k++) {
dW1[i][j] += lr * (1-MC) * d1[i][k] *
}
}
}
// db1:
for (int i=0;i<S1;i++) {
db1[i][0] *= MC; // momentum term
for (int k=0;k<Q;k++) {
db1[i][0] += lr * (1-MC) * d1[i][k];
}
}
// dW2:
for (int i=0;i<S2;i++) {
for (int j=0;j<S1;j++) {
dW2[i][j] *= MC; // momentum term
for (int k=0;k<Q;k++) {
dW2[i][j] += lr * (1-MC) * d2[i][k] *
}
}
}
// db2:
for (int i=0;i<S2;i++) {
db2[i][0] *= MC;
for (int k=0;k<Q;k++) {
db2[i][0] += lr * (1-MC) * d2[i][k];
}
}
// dW3:
for (int i=0;i<S3;i++) {
for (int j=0;j<S2;j++) {
dW3[i][j] *= MC; // momentum term
for (int k=0;k<Q;k++) {
dW3[i][j] += lr * (1-MC) * d3[i][k] *
}
}
}
// db3:
for (int i=0;i<S3;i++) {
db3[i][0] *= MC;
for (int k=0;k<Q;k++) {
db3[i][0] += lr * (1-MC) * d3[i][k];
}
}
// Add dW and db matrices to W and b to get
MC=mc;
for (int i=0;i<S1;i++) {
new_b1[i][0] = b1[i][0] + db1[i][0];
for (int j=0;j<R;j++) {
new_W1[i][j] = W1[i][j] + dW1[i][j];
}
}
inputs[j][k];
out1[j][k];
out2[j][k];
*/
public int getepoch() {
return epoch;
}
/**
@param n Integer specifying which layer's weight matrix you want. (1 for
input layer, 2 for hidden layer, 3 for output layer)
@return A double[][] matrix containing the current weight matrix of layer n
(has Sn rows, and S(n-1) columns- or R columns if n=1)
*/
public double[][] getWeightMatrix(int n) {
double[][] W;
switch (n) {
case 1:
W=W1;
break;
case 2:
W=W2;
break;
case 3:
W=W3;
break;
default:
System.out.println("getWeightMatrix: Invalid parameter");
return null;
}
return W;
}
/**
@param n Integer specifying which layer's bias vector you want. (1 for
input layer, 2 for hidden layer, 3 for output layer)
@return double[][] containing the current bias vector of layer n
(has Sn rows, one column)
*/
public double[][] getBiasVector(int n) {
double[][] b;
switch (n) {
case 1:
b=b1;
break;
case 2:
b=b2;
break;
case 3:
b=b3;
break;
default:
System.out.println("getBiasVector: Invalid parameter");
return null;
}
return b;
}
//--------------------------------------------------------------------//-------- END OF PUBLIC API -------- PRIVATE METHODS FOLLOW ---------//--------------------------------------------------------------------/*--------------------------------------------------------------------PRIVATE METHOD: InitWB
return C;
}
/*--------------------------------------------------------------------PRIVATE METHOD: transpose
PARAMETERS: double[][] A
RETURNS: double [][] transpose of matrix argument
ALTERS CLASS VARIABLES: none
READS CLASS VARIABLES: none
This method simply returns the transpose of a matrix. It is called
by the three-argument version of deltalog.
*/
private double[][] transpose(double[][] A) {
// returns transpose of a matrix A
int[] dim_A = getSize(A);
int m=dim_A[0];
int n=dim_A[1];
double[][] At = new double[n][m];
for (int i=0;i<m;i++) {
for (int j=0;j<n;j++) {
At[j][i]=A[i][j];
}
}
return At;
}
/*--------------------------------------------------------------------PRIVATE METHOD: getSize
PARAMETERS: double[][] A
RETURNS: int[] containing dimensions of A
ALTERS CLASS VARIABLES: none
READS CLASS VARIABLES: none
Java's array "length" field only returns the number of rows in a
2-D array- there is no easy way to get the number of columns.
This method will return a two-element int array containing the
dimension of its 2-D double[][] argument, i.e. number of rows and
columns contained in a RECTANGULAR double[][] array A. It searches
for the lowest array column index that will generate an
ArrayIndexOutOfBoundsException. This method only scans the length
of the first row. It is not expecting funny arrays that are triangular
or otherwise irregularly shaped but it will still work on them.
*/
private int[] getSize(double[][] A) {
double temp;
boolean gotCols;
int[] dims=new int[2];
int stepsize,Cols;
dims[0]=A.length; //The # of rows is the easy part!
stepsize=1024;
Cols=stepsize;
gotCols=false;
while (gotCols==false) {
try {
temp=A[0][Cols];
//Executed normally... Cols is within bounds of array
Cols += stepsize;
}
catch (java.lang.ArrayIndexOutOfBoundsException e) {