ELM极限学习机算法源码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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;
private int NumberofInputNeurons;
private String func;
private int []label;
private DenseMatrix BiasofHiddenNeurons;
private DenseMatrix OutputWeight;
private DenseMatrix testP;
private DenseMatrix testT;
private DenseMatrix Y;
private DenseMatrix T;
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(){
}
public DenseMatrix loadmatrix(String filename) throws IOException{
BufferedReader reader = new BufferedReader(new FileReader(new File(filename)));
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 String []datatrings = firstlineString.split(" "); for (int j = 0; j < n; j++) { matrix.set(i, j, Double.parseDouble(datatrings[j])); } i++; firstlineString = reader.readLine(); } return matrix; } public void train(String TrainingData_File) throws NotConvergedException{ try { train_set = loadmatrix(TrainingData_File); } catch (IOException e) { 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);