public ClassifierTheta(int FeatureLength, int CatSize) { CatSize--; this.FeatureLength = FeatureLength; this.CatSize = CatSize; W = DoubleMatrix.rand(FeatureLength, CatSize).subi(0.5); b = DoubleMatrix.rand(CatSize, 1).subi(0.5); Theta = new double[FeatureLength * CatSize + CatSize]; flatten(); }
public static void main(String argv[]) { modshogun.init_shogun_with_defaults(); int dim = 7; DoubleMatrix data = DoubleMatrix.rand(dim, dim); RealFeatures feats = new RealFeatures(data); DoubleMatrix data_T = data.transpose(); DoubleMatrix symdata = data.add(data_T); int cols = (1 + dim) * dim / 2; DoubleMatrix lowertriangle = DoubleMatrix.zeros(1, cols); int count = 0; for (int i = 0; i < dim; i++) { for (int j = 0; j < dim; j++) { if (j <= i) { lowertriangle.put(0, count++, symdata.get(i, j)); } } } CustomKernel kernel = new CustomKernel(); kernel.set_triangle_kernel_matrix_from_triangle(lowertriangle); DoubleMatrix km_triangletriangle = kernel.get_kernel_matrix(); kernel.set_triangle_kernel_matrix_from_full(symdata); DoubleMatrix km_fulltriangle = kernel.get_kernel_matrix(); kernel.set_full_kernel_matrix_from_full(data); DoubleMatrix km_fullfull = kernel.get_kernel_matrix(); modshogun.exit_shogun(); }
protected void applyDropOutIfNecessary(DoubleMatrix input) { if (dropOut > 0) { this.doMask = DoubleMatrix.rand(input.rows, input.columns).gt(dropOut); } else this.doMask = DoubleMatrix.ones(input.rows, input.columns); // actually apply drop out input.muli(doMask); }
/** 训练模型 */ private void train() { printInfo("训练数据载入完毕,开始训练模型………………训练数据规模" + train_set.rows + "x" + train_set.columns); // 输入矩阵初始化 横向即每列为一组输入 /* * 4 8 …… N * * 1 5 …… N * 2 6 …… N * 3 7 …… N */ P = new DoubleMatrix(NumberofInputNeurons, train_data_Count); // 测试机输出矩阵 横向 T = new DoubleMatrix(NumberofOutputNeurons, train_data_Count); // 初始化 for (int i = 0; i < train_data_Count; i++) { for (int j = 0; j < totalColCount; j++) { if (j < NumberofInputNeurons) { // 输入部分 P.put(j, i, train_set.get(i, j)); } else { T.put(j - NumberofInputNeurons, i, train_set.get(i, j)); } } } printInfo("训练矩阵初始化完毕………………开始隐含层神经元权重与阈值"); // end 初始化 long start_time_train = System.currentTimeMillis(); // 随机初始化输入权值矩阵 // 行数为隐含层神经元个数,列数为输入层神经元 /** * W11 W12 W13(第一个隐含神经元对应各输入的权值) W21 W22 W23(第二个隐含神经元对应各输入的权值) ………………………………(第N个隐含神经元对应各输入的权值) */ InputWeight = DoubleMatrix.rand(NumberofHiddenNeurons, NumberofInputNeurons); // 初始化阈值 BiasofHiddenNeurons = DoubleMatrix.rand(NumberofHiddenNeurons, 1); printInfo("隐含层神经元权重与阈值初始化完毕………………开始计算输入权重"); DoubleMatrix tmpH = new DoubleMatrix(NumberofHiddenNeurons, train_data_Count); tmpH = InputWeight.mmul(P); printInfo("输入矩阵计算权重完毕………………开始计算输入权重"); // 加阈值 // 注意横向问题 for (int i = 0; i < NumberofHiddenNeurons; i++) { for (int j = 0; j < train_data_Count; j++) { tmpH.put(i, j, tmpH.get(i, j) + BiasofHiddenNeurons.get(i, 0)); } } printInfo("输入矩阵计算阈值完毕………………开始计算输入激活函数"); // 输出矩阵 DoubleMatrix H = new DoubleMatrix(NumberofHiddenNeurons, train_data_Count); if (func.startsWith("sig")) { for (int i = 0; i < NumberofHiddenNeurons; i++) { for (int j = 0; j < train_data_Count; j++) { double tmp = tmpH.get(i, j); tmp = 1.0f / (1 + Math.exp(-tmp)); H.put(i, j, tmp); } } } else { throw new IllegalArgumentException("不支持的激活函数类型"); } printInfo("输出矩阵计算激活函数完毕,开始求解广义逆………………矩阵规模" + H.columns + "x" + H.rows); // 将H转置 DoubleMatrix Ht = H.transpose(); // 求广义逆 DoubleMatrix Ht_MP = getMPMatrix(Ht); printInfo("输出矩阵广义逆求解完毕………………开始求解输出矩阵权重"); // 隐含层输出权重矩阵= Ht_MP * Tt OutputWeight = Ht_MP.mmul(T.transpose()); printInfo("输出矩阵权重求解完毕………………"); long end_time_train = System.currentTimeMillis(); TrainingTime = (end_time_train - start_time_train) * 1.0f / 1000; printInfo("模型训练完毕………………开始评估模型"); // 误差评估 DoubleMatrix Yt = new DoubleMatrix(train_data_Count, NumberofOutputNeurons); Yt = Ht.mmul(OutputWeight); double MSE = 0; printInfo("共有" + NumberofOutputNeurons + "个输出值"); for (int out = 0; out < NumberofOutputNeurons; out++) { printInfo("计算第" + (out + 1) + "个输出值"); double mseTem = 0.0; for (int i = 0; i < train_data_Count; i++) { System.out.println(Yt.get(i, out) + " " + T.get(out, i)); mseTem += (Yt.get(i, out) - T.get(out, i)) * (Yt.get(i, out) - T.get(out, i)); } printInfo( "第" + NumberofOutputNeurons + "个输出值计算完毕,MSE=" + Math.sqrt(mseTem / train_data_Count)); MSE += mseTem; } TrainingAccuracy = Math.sqrt(MSE / train_data_Count); printInfo("模型评估完毕………………"); }