Универзитет у Новом Саду Технички Факултет "Михајло Пупин" Зрењанин

Meko računarstvo


15
АПР
2019

Neuronske mreže - Java kodovi

/**
*
*/
package edu.brtkav.ann;

import edu.brtkav.ml.DLogSig;
import edu.brtkav.ml.LogSig;
import edu.brtkav.ml.Matrix;

/**
* @author Vladimir Brtka
*
*/
public class TwoLayerNN extends ANN{

/**
* Constructor
* @param seed This is the seed for random number generator
*/
public TwoLayerNN(long seed){
super(seed);
}

/**
* Constructor
* @param seed This is the seed for random number generator
* @param S1 The number of neurons in first layer
* @param S2 The number of neurons in second layer
* @param R The number of inputs
* @param t The number of training pairs
*/
public TwoLayerNN(long seed, int S1, int S2, int R, int t ){
super(seed);
f1= new LogSig();
f2= new LogSig();
df1=new DLogSig();
df2=new DLogSig();
w11= new double[S1][R];
w21= new double[S2][S1];
b1=new double[S1][1];
b2=new double[S2][1];

inputs = new double[R][t];
targets = new double[S2][t];
outputs = new double[S2][t];
errors = new double[S2][t];
n1= new double[S1][t];
n2= new double[S2][t];
a1= new double[S1][t];
a2= new double[S2][t];

e1=new double[S1][t];
e2=new double[S2][t];

D1=new double[S1][S1];
D2=new double[S2][S2];

dw11=new double[S1][R];
dw21=new double[S2][S1];
}

private void calculateN1(int col){
double[][] p;
p=Matrix.getColumn(inputs, col);
n1=Matrix.setColumn(n1, Matrix.matrixSum(Matrix.matrixProduct(w11, p),b1), col);
}

private void calculateN2(int col){
double[][] p;
p=Matrix.getColumn(a1, col);
n2=Matrix.setColumn(n2, Matrix.matrixSum(Matrix.matrixProduct(w21, p),b2), col);
}

private void calculateA1(int col){
double[][] n;
double[][] a;
n=Matrix.getColumn(n1, col);
a=f1.calculate(n);
a1=Matrix.setColumn(a1, a, col);
}

private void calculateA2(int col){
double[][] n;
double[][] a;
n=Matrix.getColumn(n2, col);
a=f2.calculate(n);
a2=Matrix.setColumn(a2, a, col);
outputs=Matrix.setColumn(outputs, a, col);
}

@Override
public void sim() {
int col=inputs[0].length;
for (int i=0;i<col;i++){
calculateN1(i);
calculateA1(i);
calculateN2(i);
calculateA2(i);
}
}

private void calculateD2(){
D2=Matrix.diagonal(df2.calculate(n2));
}

private void calculateD1(){
D1=Matrix.diagonal(df1.calculate(n1));
}

private double[][] getError(int col){
//double[][] res= new double[inputs.length][1];
double[][] y=Matrix.getColumn(outputs, col);
double[][] t=Matrix.getColumn(targets, col);
/*System.out.println("y=");
Matrix.printMatrix(y);
System.out.println("t=");
Matrix.printMatrix(t);
System.out.println();
Matrix.printMatrix(Matrix.matrixSubtract(t, y));
System.out.println("******************");*/
return Matrix.matrixSubtract(t, y);

}

private void calculateE2(){
for (int i=0;i<outputs[0].length;i++){
e2=Matrix.setColumn(e2,Matrix.matrixProduct(D2, getError(i)), i);
}
}

private void calculateE1(){
for (int i=0;i<outputs[0].length;i++){
e1=Matrix.setColumn(e1, Matrix.matrixProduct(D1, Matrix.matrixProduct(Matrix.transposeMatrix(w21), Matrix.getColumn(e2, i))), i);
}
}

private void calculateDw21(int col){
dw21=Matrix.setColumn(dw21, Matrix.matrixProduct(Matrix.getColumn(e2,col), Matrix.transposeMatrix(Matrix.getColumn(a1,col))), col);
}

private void calculateDw11(int col){
dw11=Matrix.setColumn(dw11, Matrix.matrixProduct(Matrix.getColumn(e1,col), Matrix.transposeMatrix(Matrix.getColumn(inputs, col))), col);
}

@Override
public void train() {
for (int i=0;i<inputs[0].length;i++){
sim();
calculateD2();
calculateE2();
calculateD1();
calculateE1();
calculateDw21(i);
calculateDw11(i);

w21=Matrix.matrixSum(w21, dw21);
b2=Matrix.matrixSum(b2, Matrix.getColumn(e2, i));
w11=Matrix.matrixSum(w11, dw11);
b1=Matrix.matrixSum(b1, Matrix.getColumn(e1, i));
}

/*System.out.println("D2=");
Matrix.printMatrix(D2);
System.out.println("_____________");

System.out.println("e2=");
Matrix.printMatrix(e2);
System.out.println("_____________");

System.out.println("D1=");
Matrix.printMatrix(D1);
System.out.println("_____________");

System.out.println("e1=");
Matrix.printMatrix(e1);
System.out.println("_____________");


System.out.println("dW21=");
Matrix.printMatrix(dw21);
System.out.println("_____________");


System.out.println("dW11=");
Matrix.printMatrix(dw11);
System.out.println("_____________");*/

}
}

 

/**
*
*/
package edu.brtkav.ann;

import java.util.Random;

import edu.brtkav.ml.Matrix;
import edu.brtkav.ml.TransferFunction;
import edu.brtkav.rand.Randomizer;

/**
* @author Vladimir Brtka
*
*/
public abstract class ANN {

private long seed;
protected Randomizer dice;

protected double alpha;
protected double[][] inputs; // p
protected double[][] targets; // t
protected double[][] outputs; // y
protected double[][] errors; // e

protected double[][] w11;
protected double[][] w21;
protected double[][] w32;

protected double[][] b1;
protected double[][] b2;
protected double[][] b3;

protected double[][] n1;
protected double[][] n2;
protected double[][] n3;

protected double[][] a1;
protected double[][] a2;
protected double[][] a3;

protected double[][] e1;
protected double[][] e2;
protected double[][] e3;

protected double[][] D1;
protected double[][] D2;
protected double[][] D3;

protected TransferFunction f1;
protected TransferFunction f2;
protected TransferFunction f3;

protected TransferFunction df1;
protected TransferFunction df2;
protected TransferFunction df3;

protected double[][] dw11;
protected double[][] dw21;
protected double[][] dw32;

public ANN(long seed){
super();
dice=new Randomizer(seed);
}


public abstract void sim();

public abstract void train();

private String getColString(int cols){
String s="{";
for (int i=0;i<cols;i++) s+="c";
return s+"}";
}

public String toLaTex(double[][] m){
int rows=m.length;
int cols=m[0].length;
String ss="\\left[\\begin{array}"+getColString(cols)+"\n";
String s="";
for (int i=0;i<rows;i++){
s="";
for (int j=0;j<cols;j++){
s+=m[i][j]+" &";
}
ss=ss+s+"\\\\\\\n";
}
ss+="\\end{array}\\right]";

return ss+"\n";
}

/**
* The learning rate from [0,1] interval
* @return the alpha
*/
public double getAlpha() {
return alpha;
}

/**
* The learning rate from [0,1] interval
* @param alpha the alpha to set
*/
public void setAlpha(double alpha) {
this.alpha = alpha;
}

/**
* @return the inputs
*/
public double[][] getInputs() {
return inputs;
}

/**
* @param inputs the inputs to set
*/
public void setInputs(double[][] inputs) {
this.inputs = inputs;
}

/**
* @return the targets
*/
public double[][] getTargets() {
return targets;
}

/**
* @param targets the targets to set
*/
public void setTargets(double[][] targets) {
this.targets = targets;
}


/**
* @return the seed
*/
public long getSeed() {
return seed;
}


/**
* @param seed the seed to set
*/
public void setSeed(long seed) {
this.seed = seed;
}


/**
* @return the dice
*/
public Randomizer getDice() {
return dice;
}


/**
* @param dice the dice to set
*/
public void setDice(Randomizer dice) {
this.dice = dice;
}


/**
* @return the w11
*/
public double[][] getW11() {
return w11;
}


/**
* @param w11 the w11 to set
*/
public void setW11(double[][] w11) {
this.w11 = w11;
}


/**
* @return the w21
*/
public double[][] getW21() {
return w21;
}


/**
* @param w21 the w21 to set
*/
public void setW21(double[][] w21) {
this.w21 = w21;
}


/**
* @return the w32
*/
public double[][] getW32() {
return w32;
}


/**
* @param w32 the w32 to set
*/
public void setW32(double[][] w32) {
this.w32 = w32;
}


/**
* @return the f1
*/
public TransferFunction getF1() {
return f1;
}


/**
* @param f1 the f1 to set
*/
public void setF1(TransferFunction f1) {
this.f1 = f1;
}


/**
* @return the f2
*/
public TransferFunction getF2() {
return f2;
}


/**
* @param f2 the f2 to set
*/
public void setF2(TransferFunction f2) {
this.f2 = f2;
}


/**
* @return the f3
*/
public TransferFunction getF3() {
return f3;
}


/**
* @param f3 the f3 to set
*/
public void setF3(TransferFunction f3) {
this.f3 = f3;
}


/**
* @return the b1
*/
public double[][] getB1() {
return b1;
}


/**
* @param b1 the b1 to set
*/
public void setB1(double[][] b1) {
this.b1 = b1;
}


/**
* @return the b2
*/
public double[][] getB2() {
return b2;
}


/**
* @param b2 the b2 to set
*/
public void setB2(double[][] b2) {
this.b2 = b2;
}


/**
* @return the b3
*/
public double[][] getB3() {
return b3;
}


/**
* @param b3 the b3 to set
*/
public void setB3(double[][] b3) {
this.b3 = b3;
}


/**
* @return the outputs
*/
public double[][] getOutputs() {
return outputs;
}


/**
* @return the errors
*/
public double[][] getErrors() {
return errors;
}

}

 

 

/**
*
*/
package edu.brtkav.ml;

import java.util.Random;

//import edu.brtkav.rand.Randomizer;

/**
* Standard matrix operators
* @author Vladimir Brtka
*
*/
public class Matrix {

private static double decimalFactor=100.00;
public long seed;

/**
* Matrix product
* @param A left matrix
* @param B right matrix
* @return A*B
*/
public static double[][] matrixProduct(double[][] A, double[][] B){
int rowsCountA=A.length;
int colsCountA=A[0].length;
int rowsCountB=B.length;
int colsCountB=B[0].length;

if (colsCountA != rowsCountB) {
throw new IllegalArgumentException("Incompatible matrix dimensions error.");
}

double[][] C=new double[rowsCountA][colsCountB];

for (int aRow = 0; aRow < rowsCountA; aRow++) { // aRow
for (int bCol = 0; bCol < colsCountB; bCol++) { // bColumn
for (int k = 0; k < colsCountA; k++) { // aColumn
C[aRow][bCol] += A[aRow][k] * B[k][bCol];
}
}
}

return C;
}

/**
* Scalar matrix product
* @param A matrix
* @param scalar the real number
* @return scalar*A
*/
public static double[][] scalarProduct(double[][] A, double scalar){
int rows=A.length;
int cols=A[0].length;

double[][] C=Matrix.setMatrix(A);

for (int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
C[i][j]=scalar*A[i][j];

return C;

}

/**
* Matrix subtraction
* @param A matrix
* @param B matrix
* @return A-B
*/
public static double[][] matrixSubtract(double[][] A, double[][] B){
int rowsCountA=A.length;
int colsCountA=A[0].length;
int rowsCountB=B.length;
int colsCountB=B[0].length;

if ((rowsCountA != rowsCountB)||(colsCountA != colsCountB)) {
throw new IllegalArgumentException("Incompatible matrix dimensions error.");
}

double[][] C=new double[rowsCountA][colsCountB];

for (int aRow=0;aRow<rowsCountA;aRow++)
for (int aCol=0;aCol<colsCountA;aCol++)
C[aRow][aCol]=A[aRow][aCol]-B[aRow][aCol];


return C;
}

/**
* The sum of two matrices
* @param A matrix
* @param B matrix
* @return A+B
*/
public static double[][] matrixSum(double[][] A, double[][] B){
int rowsCountA=A.length;
int colsCountA=A[0].length;
int rowsCountB=B.length;
int colsCountB=B[0].length;

if ((rowsCountA != rowsCountB)||(colsCountA != colsCountB)) {
throw new IllegalArgumentException("Incompatible matrix dimensions error.");
}

double[][] C=new double[rowsCountA][colsCountB];

for (int aRow=0;aRow<rowsCountA;aRow++)
for (int aCol=0;aCol<colsCountA;aCol++)
C[aRow][aCol]=A[aRow][aCol]+B[aRow][aCol];
return C;
}

/**
* Prints matrix elements to the standard output (console)
* @param A matrix
*/
public static void printMatrix(double[][] A){
for (int i=0;i<A.length;i++){
for (int j=0;j<A[0].length;j++){
System.out.print(A[i][j]+" ");
}
System.out.println();
}
}

/**
* Matrix transpose
* @param A matrix
* @return transposed matrix A, switch rows and columns
*/
public static double[][] transposeMatrix(double[][] A){
int row=A.length;
int col=A[0].length;
double[][] C=new double[col][row];

for (int i=0;i<row;i++)
for (int j=0;j<col;j++)
C[j][i]=A[i][j];

return C;
}

/**
* Generates a new matrix with random elements between 0 and 1.
* @param rows the number of rows
* @param cols the number of columns
* @return matrix
*/
public static double[][] randomMatrix(long seed, int rows, int cols){
double[][] C= new double[rows][cols];
Random dice=new Random(seed);

for (int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
C[i][j]=Math.round(dice.nextDouble()*decimalFactor)/decimalFactor;

return C;
}

/**
* Matrix with random values between rangeMin and rangeMax
* @param rows
* @param cols
* @param rangeMin
* @param rangeMax
* @return
*/
public static double[][] randomMatrix(long seed,int rows, int cols, double rangeMin, double rangeMax){
double[][] C= new double[rows][cols];
Random dice=new Random(seed);

for (int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
C[i][j]=Math.round((rangeMin+(rangeMax-rangeMin)*dice.nextDouble())*decimalFactor)/decimalFactor;

return C;
}

public static double[][] random01Matrix(long seed, int rows, int cols){
double[][] C= new double[rows][cols];
Random dice=new Random(seed);
double d;

for (int i=0;i<rows;i++)
for (int j=0;j<cols;j++){
d=dice.nextDouble();
if (d>=0.5) C[i][j]=1.0; else C[i][j]=0.0;
}

return C;
}

/**
* Generates a new matrix with zero elements.
* @param rows the number of rows
* @param cols the number of columns
* @return matrix
*/
public static double[][] zeroMatrix(int rows, int cols){
double[][] C= new double[rows][cols];

for (int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
C[i][j]=0.0;

return C;
}

public static double[][] eyeMatrix(int rows, int cols){
double[][] C=new double[rows][cols];

for (int i=0;i<rows;i++)
for (int j=0; j<cols;j++)
if (i==j) C[i][j]=1; else C[i][j]=0;

return C;
}

/**
* Gets specific column of the matrix
* @param A matrix
* @param col integer, the index of the column
* @return column vector
*/
public static double[][] getColumn(double[][] A, int col){
int rows=A.length;
int cols=A[0].length;

if ((col>=cols)||(col<0)) {
throw new IllegalArgumentException("Matrix dimension error.");
}

double[][] C= new double[rows][1];

for (int i=0;i<rows;i++)
C[i][0]=A[i][col];

return C;
}

/**
* Gets specific row of the matrix
* @param A matrix
* @param row integer, the index of the row
* @return row vector
*/
public static double[][] getRow(double[][] A, int row){
int rows=A.length;
int cols=A[0].length;

if ((row>=rows)||(row<0)) {
throw new IllegalArgumentException("Matrix dimension error.");
}

double[][] C= new double[1][cols];

for (int i=0;i<cols;i++)
C[0][i]=A[row][i];

return C;
}

/**
* Assignment operator
* @param A matrix
* @return A
*/
public static double[][] setMatrix(double[][] A){
int rows=A.length;
int cols=A[0].length;

double[][] C= new double[rows][cols];

for (int i=0;i<rows;i++)
for (int j=0;j<cols;j++)
C[i][j]=A[i][j];

return C;
}

/**
* Assigns new scalar value to each element of the specific row
* @param A matrix
* @param row row index
* @param value scalar
* @return matrix
*/
public static double[][] setRowValue(double[][] A, int row, double value){
int rows=A.length;
int cols=A[0].length;

if ((row>=rows)||(row<0)) {
throw new IllegalArgumentException("Matrix dimension error.");
}

double[][] C= setMatrix(A);

for (int i=0;i<cols;i++)
C[row][i]=value;

return C;
}

/**
* Assigns new scalar value to each element of the specific column
* @param A matrix
* @param col column index
* @param value scalar
* @return matrix
*/
public static double[][] setColumnValue(double[][] A, int col, double value){
int rows=A.length;
int cols=A[0].length;

if ((col>=cols)||(col<0)) {
throw new IllegalArgumentException("Matrix dimension error.");
}

double[][] C= setMatrix(A);

for (int i=0;i<rows;i++)
C[i][col]=value;

return C;
}

/**
* Sets entire row of the matrix
* @param A matrix
* @param rowVector row vector compatible with the matrix
* @param row row index
* @return matrix
*/
public static double[][] setRow(double[][] A, double[][] rowVector, int row){
//int rowsA=A.length;
int colsA=A[0].length;
//int rowsB=rowVector.length;
int colsB=rowVector[0].length;

if (colsA != colsB) {
throw new IllegalArgumentException("Incompatible matrix dimensions error.");
}

double[][] C=Matrix.setMatrix(A);
for (int i=0;i<colsA;i++)
C[row][i]=rowVector[0][i];

return C;
}

/**
* Sets entire column of the matrix
* @param A matrix
* @param colVector column vector compatible with the matrix
* @param col column index
* @return matrix
*/
public static double[][] setColumn(double[][] A, double[][] colVector, int col){
int rowsA=A.length;
//int colsA=A[0].length;
int rowsB=colVector.length;
//int colsB=colVector[0].length;

if (rowsA != rowsB) {
throw new IllegalArgumentException("Incompatible matrix dimensions error.");
}

double[][] C=Matrix.setMatrix(A);
for (int i=0;i<rowsA;i++)
C[i][col]=colVector[i][0];

return C;
}

/**
* Matrix equality test
* @param A matrix
* @param B matrix
* @return true or false
*/
public static boolean isEqual(double[][]A, double[][] B){
int rowsA=A.length;
int colsA=A[0].length;
int rowsB=B.length;
int colsB=B[0].length;

if ((rowsA != rowsB)||(colsA != colsB)) {
throw new IllegalArgumentException("Incompatible matrix dimensions error.");
}

for (int i=0;i<rowsA;i++)
for (int j=0;j<colsA;j++)
if (A[i][j]!=B[i][j]) return false;

return true;

}

/**
* Returns true if matrix A is zero matrix, otherwise returns false.
* @param A
* @return
*/
public static boolean isZero(double[][] A){
boolean zero=true;
int rowsA=A.length;
int colsA=A[0].length;

for (int i=0;i<rowsA;i++)
for (int j=0;j<colsA;j++)
if (A[i][j]!=0.0) zero=false;

return zero;
}

public static double[][] diagonal(double[][] vector){
int s=vector.length;
double[][] res = new double[s][s];
for (int i=0;i<s;i++)
for (int j=0;j<s;j++)
if (i==j) res[i][j]=vector[i][0];else res[i][j]=0;

return res;
}
}

 

 

package edu.brtkav.ml;

public class DLogSig extends TransferFunction{

/**
* Calculates the value of the derivative of the logsig function for argument x
* @param x
* @return
*/
public double calculate(double x){
double a=Math.exp(-x);
double b=1+a;
double c=b*b;
//return Math.exp(-x)/((1+Math.exp(-x))*(1+Math.exp(-x)));
return a/c;
}

}

 

 

/**
*
*/
package edu.brtkav.ml;

/**
* @author Vladimir Brtka
*
*/
public class LogSig extends TransferFunction{

/**
* Constructor
*/
public LogSig(){
super();
}

/**
* Logarithmic sigmoid transfer function
* Matlab syntax: 1/(1+exp(-n))
*/
@Override
public double calculate(double x) {
return 1/(1+Math.exp(-x));
}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Download

Kolokvijum-primer1.pdf

Kolokvijum-primer2.pdf

14
МАР
2012

Spisak ispitnih pitanja

Spisak ispitnih pitanja možete preizeti ovde

14
МАР
2012

Primeri rešenih zadataka za kolokvijum

 

Primer 1

Primer 2

Primer 3