/*
* This library is free software;
* The original version is a matlab programmer,I rewrote it in Java
* The original Authors: MR QIN-YU ZHU AND DR GUANG-BIN HUANG,
* The original WEBSITE: http://www.ntu.edu.sg/eee/icis/cv/egbhuang.htm
* */
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import no.uib.cipr.matrix.DenseMatrix;
import no.uib.cipr.matrix.DenseVector;
import no.uib.cipr.matrix.Matrices;
import no.uib.cipr.matrix.NotConvergedException;
public class elm {
private DenseMatrix train_set;
private DenseMatrix test_set;
private int numTrainData;
private int numTestData;
private DenseMatrix InputWeight;
private float TrainingTime;
private float TestingTime;
private double TrainingAccuracy, TestingAccuracy;
private int Elm_Type;
private int NumberofHiddenNeurons;
private int NumberofOutputNeurons; //also the number of classes
private int NumberofInputNeurons; //also the number of attribution
private String func;
private int []label;
//this class label employ a lazy and easy method,any class must written in 0,1,2...so the preprocessing is required
//the blow variables in both train() and test()
private DenseMatrix BiasofHiddenNeurons;
private DenseMatrix OutputWeight;
private DenseMatrix testP;
private DenseMatrix testT;
private DenseMatrix Y;
private DenseMatrix T;
/**
* Construct an ELM
* @param
* elm_type - 0 for regression; 1 for (both binary and multi-classes) classification
* @param
* numberofHiddenNeurons - Number of hidden neurons assigned to the ELM
* @param
* ActivationFunction - Type of activation function:
* 'sig' for Sigmoidal function
* 'sin' for Sine function
* 'hardlim' for Hardlim function
* 'tribas' for Triangular basis function
* 'radbas' for Radial basis function (for additive type of SLFNs instead of RBF type of SLFNs)
* @throws NotConvergedException
*/
public elm(int elm_type, int numberofHiddenNeurons, String ActivationFunction){
Elm_Type = elm_type;
NumberofHiddenNeurons = numberofHiddenNeurons;
func = ActivationFunction;
TrainingTime = 0;
TestingTime = 0;
TrainingAccuracy= 0;
TestingAccuracy = 0;
NumberofOutputNeurons = 1;
}
public elm(){
}
//the first line of dataset file must be the number of rows and columns,and number of classes if neccessary
//the first column is the norminal class value 0,1,2...
//if the class value is 1,2...,number of classes should plus 1
public DenseMatrix loadmatrix(String filename) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));
//FileInputStream
String firstlineString = reader.readLine();
String []strings = firstlineString.split(" ");
int m = Integer.parseInt(strings[0]);
int n = Integer.parseInt(strings[1]);
if(strings.length > 2)
NumberofOutputNeurons = Integer.parseInt(strings[2]);
DenseMatrix matrix = new DenseMatrix(m, n);
firstlineString = reader.readLine();
int i = 0;
while (i<m) {
String []datatrings = firstlineString.split(" ");
for (int j = 0; j < n; j++) {
matrix.set(i, j, Double.parseDouble(datatrings[j]));
}
i++;
firstlineString = reader.readLine();
}
/*
for(int ii = 0; ii<m; ii++)
matrix.add(ii, 0, -1);
*/
return matrix;
}
public void train(String TrainingData_File) throws NotConvergedException{
try {
train_set = loadmatrix(TrainingData_File);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
train();
}
public void train(double [][]traindata) throws NotConvergedException{
//classification require a the number of class
train_set = new DenseMatrix(traindata);
int m = train_set.numRows();
if(Elm_Type == 1){
double maxtag = traindata[0][0];
for (int i = 0; i < m; i++) {
if(traindata[i][0] > maxtag)
maxtag = traindata[i][0];
}
NumberofOutputNeurons = (int)maxtag+1;
}
train();
}
private void train() throws NotConvergedException{
numTrainData = train_set.numRows();
NumberofInputNeurons = train_set.numColumns() - 1;
InputWeight = (DenseMatrix) Matrices.random(NumberofHiddenNeurons, NumberofInputNeurons);
DenseMatrix transT = new DenseMatrix(numTrainData, 1);
DenseMatrix transP = new DenseMatrix(numTrainData, NumberofInputNeurons);
for (int i = 0; i < numTrainData; i++) {
transT.set(i, 0, train_set.get(i, 0));
for (int j = 1; j <= NumberofInputNeurons; j++)
transP.set(i, j-1, train_set.get(i, j));
}
T = new DenseMatrix(1,numTrainData);
DenseMatrix P = new DenseMatrix(NumberofInputNeurons,numTrainData);
transT.transpose(T);
transP.transpose(P);
if(Elm_Type != 0) //CLASSIFIER
{
label = new int[NumberofOutputNeurons];
for (int i = 0; i < NumberofOutputNeurons; i++) {
label[i] = i; //class label starts form 0
}
DenseMatrix tempT = new DenseMatrix(NumberofOutputNeurons,numTrainData);
tempT.zero();
for (int i = 0; i < numTrainData; i++){
int j = 0;
for (j = 0; j < NumberofOutputNeurons; j++){
if (label[j] == T.get(0, i))
break;
}
tempT.set(j, i, 1);
}
T = new DenseMatrix(NumberofOutputNeurons,numTrainData); // T=temp_T*2-1;
for (int i = 0; i < NumberofOutputNeurons; i++){
for (int j = 0; j < numTrainData; j++)
T.set(i, j, tempT.get(i, j)*2-1);
}
transT = new DenseMatrix(numTrainData,NumberofOutputNeurons);
T.transpose(transT);
} //end if CLASSIFIER
long start_time_train = System.currentTimeMillis();
// Random generate input weights InputWeight (w_i) and biases BiasofHiddenNeurons (b_i) of hidden neurons
// InputWeight=rand(NumberofHiddenNeurons,NumberofInputNeurons)*2-1;
BiasofHiddenNeurons = (DenseMatrix) Matrices.random(NumberofHiddenNeurons, 1);
DenseMatrix tempH = new DenseMatrix(NumberofHiddenNeurons, numTrainData);
InputWeight.mult(P, tempH);
//DenseMatrix ind = new DenseMatrix(1, numTrainData);
DenseMatrix BiasMatrix = new DenseMatrix(NumberofHiddenNeurons, numTrainData);
for (int j = 0; j < numTrainData; j++) {
for (int i = 0; i < NumberofHiddenNeurons; i++) {
BiasMatrix.set(i, j, BiasofHiddenNeurons.get(i, 0));
}
}
tempH.add(BiasMatrix);
DenseMatrix H = new DenseMatrix(NumberofHiddenNeurons, numTrainData);
if(func.startsWith("sig")){
for (int j = 0; j < NumberofHiddenNeurons; j++) {
for (int i = 0; i < numTrainData; i++) {
double temp = tempH.get(j, i);
temp = 1.0f/ (1 + Math.exp(-temp));
H.set(j, i, temp);
}
}
}
else if(func.startsWith("sin")){
for (int j = 0; j < NumberofHiddenNeurons; j++) {
for (int i = 0; i < numTrainData; i++) {
double temp = tempH.get(j, i);
temp = Math.sin(temp);
H.set(j, i, temp);
}
}
}
else if(func.startsWith("hardlim")){
//If you need it ,you can absolutely complete it yourself
}
else if(func.startsWith("tribas")){
//If you need it ,you can absolutely complete it yourself
}
else if(func.startsWith("radbas")){
//If you need it ,you can absolutely complete it yourself
}
DenseMatrix Ht = new DenseMatrix(numTrainData,NumberofHiddenNeurons);
H.transpose(Ht);
Inverse invers = new Inverse(Ht);
DenseMatrix pinvHt = invers.getMPInverse(); //NumberofHiddenNeurons*numTrainData
//DenseMatrix pinvHt = invers.getMPInverse(0.000001); //fast method, PLEASE CITE in your pap
评论0