ELM极限学习机算法源码.docx

上传人:小飞机 文档编号:3156365 上传时间:2023-03-11 格式:DOCX 页数:18 大小:39.69KB
返回 下载 相关 举报
ELM极限学习机算法源码.docx_第1页
第1页 / 共18页
ELM极限学习机算法源码.docx_第2页
第2页 / 共18页
ELM极限学习机算法源码.docx_第3页
第3页 / 共18页
ELM极限学习机算法源码.docx_第4页
第4页 / 共18页
ELM极限学习机算法源码.docx_第5页
第5页 / 共18页
亲,该文档总共18页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《ELM极限学习机算法源码.docx》由会员分享,可在线阅读,更多相关《ELM极限学习机算法源码.docx(18页珍藏版)》请在三一办公上搜索。

1、ELM极限学习机算法源码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

2、.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, TestingAcc

3、uracy; 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

4、; 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; N

5、umberofOutputNeurons = 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(strings0);

6、 int n = Integer.parseInt(strings1); if(strings.length 2) NumberofOutputNeurons = Integer.parseInt(strings2); DenseMatrix matrix = new DenseMatrix(m, n); firstlineString = reader.readLine; int i = 0; while (im) String datatrings = firstlineString.split(); for (int j = 0; j n; j+) matrix.set(i, j, Do

7、uble.parseDouble(datatringsj); 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 NotC

8、onvergedException /classification require a the number of class train_set = new DenseMatrix(traindata); int m = train_set.numRows; if(Elm_Type = 1) double maxtag = traindata00; for (int i = 0; i maxtag) maxtag = traindatai0; NumberofOutputNeurons = (int)maxtag+1; train; private void train throws Not

9、ConvergedException 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, Nu

10、mberofInputNeurons); 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.transpo

11、se(T); transP.transpose(P); if(Elm_Type != 0) /CLASSIFIER label = new intNumberofOutputNeurons; for (int i = 0; i NumberofOutputNeurons; i+) labeli = i; DenseMatrix tempT = new DenseMatrix(NumberofOutputNeurons,numTrainData); tempT.zero; for (int i = 0; i numTrainData; i+) int j = 0; for (j = 0; j N

12、umberofOutputNeurons; j+) if (labelj = 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(numTrainD

13、ata,NumberofOutputNeurons); T.transpose(transT); long start_time_train = System.currentTimeMillis; BiasofHiddenNeurons = (DenseMatrix) Matrices.random(NumberofHiddenNeurons, 1); DenseMatrix tempH = new DenseMatrix(NumberofHiddenNeurons, numTrainData); InputWeight.mult(P, tempH); /DenseMatrix ind = n

14、ew 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(

15、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+

16、) 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) else if(func.startsWith(tribas) else if(func.startsWith(radbas) DenseMatrix Ht = new DenseMatrix(numTrainData,NumberofHiddenNeurons); H.transpose(Ht); Inver

17、se invers = new Inverse(Ht); DenseMatrix pinvHt = invers.getMPInverse; OutputWeight = new DenseMatrix(NumberofHiddenNeurons, NumberofOutputNeurons); pinvHt.mult(transT, OutputWeight); long end_time_train = System.currentTimeMillis; TrainingTime = (end_time_train - start_time_train)*1.0f/1000; DenseM

18、atrix Yt = new DenseMatrix(numTrainData,NumberofOutputNeurons); Ht.mult(OutputWeight,Yt); Y = new DenseMatrix(NumberofOutputNeurons,numTrainData); Yt.transpose(Y); if(Elm_Type = 0) double MSE = 0; for (int i = 0; i numTrainData; i+) MSE += (Yt.get(i, 0) - transT.get(i, 0)*(Yt.get(i, 0) - transT.get(

19、i, 0); TrainingAccuracy = Math.sqrt(MSE/numTrainData); else if(Elm_Type = 1) float MissClassificationRate_Training=0; for (int i = 0; i numTrainData; i+) double maxtag1 = Y.get(0, i); int tag1 = 0; double maxtag2 = T.get(0, i); int tag2 = 0; for (int j = 1; j maxtag1) maxtag1 = Y.get(j, i); tag1 = j

20、; if(T.get(j, i) maxtag2) maxtag2 = T.get(j, i); tag2 = j; if(tag1 != tag2) MissClassificationRate_Training +; TrainingAccuracy = 1 - MissClassificationRate_Training*1.0f/numTrainData; public void test(String TestingData_File) try test_set = loadmatrix(TestingData_File); catch (IOException e) / TODO

21、 Auto-generated catch block e.printStackTrace; numTestData = test_set.numRows; DenseMatrix ttestT = new DenseMatrix(numTestData, 1); DenseMatrix ttestP = new DenseMatrix(numTestData, NumberofInputNeurons); for (int i = 0; i numTestData; i+) ttestT.set(i, 0, test_set.get(i, 0); for (int j = 1; j = Nu

22、mberofInputNeurons; j+) ttestP.set(i, j-1, test_set.get(i, j); testT = new DenseMatrix(1,numTestData); testP = new DenseMatrix(NumberofInputNeurons,numTestData); ttestT.transpose(testT); ttestP.transpose(testP); long start_time_test = System.currentTimeMillis; DenseMatrix tempH_test = new DenseMatri

23、x(NumberofHiddenNeurons, numTestData); InputWeight.mult(testP, tempH_test); DenseMatrix BiasMatrix2 = new DenseMatrix(NumberofHiddenNeurons, numTestData); for (int j = 0; j numTestData; j+) for (int i = 0; i NumberofHiddenNeurons; i+) BiasMatrix2.set(i, j, BiasofHiddenNeurons.get(i, 0); tempH_test.a

24、dd(BiasMatrix2); DenseMatrix H_test = new DenseMatrix(NumberofHiddenNeurons, numTestData); if(func.startsWith(sig) for (int j = 0; j NumberofHiddenNeurons; j+) for (int i = 0; i numTestData; i+) double temp = tempH_test.get(j, i); temp = 1.0f/ (1 + Math.exp(-temp); H_test.set(j, i, temp); else if(fu

25、nc.startsWith(sin) for (int j = 0; j NumberofHiddenNeurons; j+) for (int i = 0; i numTestData; i+) double temp = tempH_test.get(j, i); temp = Math.sin(temp); H_test.set(j, i, temp); else if(func.startsWith(hardlim) else if(func.startsWith(tribas) else if(func.startsWith(radbas) DenseMatrix transH_te

26、st = new DenseMatrix(numTestData,NumberofHiddenNeurons); H_test.transpose(transH_test); DenseMatrix Yout = new DenseMatrix(numTestData,NumberofOutputNeurons); transH_test.mult(OutputWeight,Yout); DenseMatrix testY = new DenseMatrix(NumberofOutputNeurons,numTestData); Yout.transpose(testY); long end_

27、time_test = System.currentTimeMillis; TestingTime = (end_time_test - start_time_test)*1.0f/1000; /REGRESSION if(Elm_Type = 0) double MSE = 0; for (int i = 0; i numTestData; i+) MSE += (Yout.get(i, 0) - testT.get(0,i)*(Yout.get(i, 0) - testT.get(0,i); TestingAccuracy = Math.sqrt(MSE/numTestData); /CL

28、ASSIFIER else if(Elm_Type = 1) DenseMatrix temptestT = new DenseMatrix(NumberofOutputNeurons,numTestData); for (int i = 0; i numTestData; i+) int j = 0; for (j = 0; j NumberofOutputNeurons; j+) if (labelj = testT.get(0, i) break; temptestT.set(j, i, 1); testT = new DenseMatrix(NumberofOutputNeurons,

29、numTestData); for (int i = 0; i NumberofOutputNeurons; i+) for (int j = 0; j numTestData; j+) testT.set(i, j, temptestT.get(i, j)*2-1); float MissClassificationRate_Testing=0; for (int i = 0; i numTestData; i+) double maxtag1 = testY.get(0, i); int tag1 = 0; double maxtag2 = testT.get(0, i); int tag

30、2 = 0; for (int j = 1; j maxtag1) maxtag1 = testY.get(j, i); tag1 = j; if(testT.get(j, i) maxtag2) maxtag2 = testT.get(j, i); tag2 = j; if(tag1 != tag2) MissClassificationRate_Testing +; TestingAccuracy = 1 - MissClassificationRate_Testing*1.0f/numTestData; public double testOut(double inpt) test_se

31、t = new DenseMatrix(inpt); return testOut; public double testOut(double inpt) test_set = new DenseMatrix(new DenseVector(inpt); return testOut; /Output numTestData*NumberofOutputNeurons private double testOut numTestData = test_set.numRows; NumberofInputNeurons = test_set.numColumns-1; DenseMatrix t

32、testT = new DenseMatrix(numTestData, 1); DenseMatrix ttestP = new DenseMatrix(numTestData, NumberofInputNeurons); for (int i = 0; i numTestData; i+) ttestT.set(i, 0, test_set.get(i, 0); for (int j = 1; j = NumberofInputNeurons; j+) ttestP.set(i, j-1, test_set.get(i, j); testT = new DenseMatrix(1,num

33、TestData); testP = new DenseMatrix(NumberofInputNeurons,numTestData); ttestT.transpose(testT); ttestP.transpose(testP); DenseMatrix tempH_test = new DenseMatrix(NumberofHiddenNeurons, numTestData); InputWeight.mult(testP, tempH_test); DenseMatrix BiasMatrix2 = new DenseMatrix(NumberofHiddenNeurons,

34、numTestData); for (int j = 0; j numTestData; j+) for (int i = 0; i NumberofHiddenNeurons; i+) BiasMatrix2.set(i, j, BiasofHiddenNeurons.get(i, 0); tempH_test.add(BiasMatrix2); DenseMatrix H_test = new DenseMatrix(NumberofHiddenNeurons, numTestData); if(func.startsWith(sig) for (int j = 0; j Numberof

35、HiddenNeurons; j+) for (int i = 0; i numTestData; i+) double temp = tempH_test.get(j, i); temp = 1.0f/ (1 + Math.exp(-temp); H_test.set(j, i, temp); else if(func.startsWith(sin) for (int j = 0; j NumberofHiddenNeurons; j+) for (int i = 0; i numTestData; i+) double temp = tempH_test.get(j, i); temp =

36、 Math.sin(temp); H_test.set(j, i, temp); else if(func.startsWith(hardlim) else if(func.startsWith(tribas) else if(func.startsWith(radbas) DenseMatrix transH_test = new DenseMatrix(numTestData,NumberofHiddenNeurons); H_test.transpose(transH_test); DenseMatrix Yout = new DenseMatrix(numTestData,Number

37、ofOutputNeurons); transH_test.mult(OutputWeight,Yout); double result = new doublenumTestData; if(Elm_Type = 0) for (int i = 0; i numTestData; i+) resulti = Yout.get(i, 0); else if(Elm_Type = 1) for (int i = 0; i numTestData; i+) int tagmax = 0; double tagvalue = Yout.get(i, 0); for (int j = 1; j tag

38、value) tagvalue = Yout.get(i, j); tagmax = j; resulti = tagmax; return result; public float getTrainingTime return TrainingTime; public double getTrainingAccuracy return TrainingAccuracy; public float getTestingTime return TestingTime; public double getTestingAccuracy return TestingAccuracy; public

39、int getNumberofInputNeurons return NumberofInputNeurons; public int getNumberofHiddenNeurons return NumberofHiddenNeurons; public int getNumberofOutputNeurons return NumberofOutputNeurons; public DenseMatrix getInputWeight return InputWeight; public DenseMatrix getBiasofHiddenNeurons return BiasofHi

40、ddenNeurons; public DenseMatrix getOutputWeight return OutputWeight; /for predicting a data file based on a trained model. public void testgetoutput(String filename) throws IOException try test_set = loadmatrix(filename); catch (IOException e) / TODO Auto-generated catch block e.printStackTrace; numTestData = test_set.numRows; NumberofInputNeurons = test_set.numColumns - 1; double rsum = 0; double actual = new doublenumTestData; double data = new doublenumTestDataNumberofInputNeurons;

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号