现在手头没数据集,用之前的几张图片测试了一下。
data 数组前两个是同一种树叶:
没数据集了,用的差不多的一种树叶对比的,就是上面double x[]的特征:
虽然看面积就知道和第一个相似,程序运行结果:
[0.100747, 0.00233442, 0.690329, 1.6875, 0.0483126]:[0.9902157551076237, 0.0030781815284147123, 0.009777284822493251]
[0.104574, 0.0029732, 0.660244, 1.71795, 0.0530419]:[0.9816079000858899, 0.0030103326451449953, 0.018348749704753257]
[0.159876, 0.016334, 0.710315, 2.91176, 0.0400851]:[6.278126573769545E-4, 0.0017362230457877918, 0.9993780509911905]
[0.128983, 0.00860048, 0.72265, 2.39355, 0.0454218]:[0.020148315067172743, 0.0021197880406902316, 0.9798882999520514]
[0.147046, 0.00364964, 0.67903, 1.66344, 0.0540356]:[0.9914561686871933, 0.003028309020178626, 0.008562771909244912]
前四个是训练集,“:”后面是类别,之前是想分八类,所以用了三个输出节点。不影响。
最后一个(第五个)是测试结果,可以看到被分到第一类里了,就是和前两个分一类,占99.14%。
明天去采集数据集,看看效果怎么样。先实现一下4分类吧。。
import java.util.Arrays;
public class BpDeepTest{
public static void main(String[] args){
//初始化神经网络的基本配置
//第一个参数是一个整型数组,表示神经网络的层数和每层节点数,比如{3,10,10,10,10,2}表示输入层是3个节点,输出层是2个节点,中间有4层隐含层,每层10个节点
//第二个参数是学习步长,第三个参数是动量系数
BpDeep bp = new BpDeep(new int[]{5,10,3}, 0.12, 0.8);
//设置样本数据,对应上面的4个二维坐标数据
double[][] data = new double[][]{{0.100747,0.00233442,0.690329,1.6875,0.0483126},{0.104574,0.0029732,0.660244,1.71795,0.0530419},{0.159876,0.016334,0.710315,2.91176,0.0400851},{0.128983,0.00860048,0.72265,2.39355,0.0454218}};
//设置目标数据,对应4个坐标数据的分类
double[][] target = new double[][]{{1,0,0},{1,0,0},{0,0,1},{0,0,1}};
//迭代训练5000次
for(int n=0;n<5000;n++)
for(int i=0;i//根据训练结果来检验样本数据
for(int j=0;jdouble[] result = bp.computeOut(data[j]);
System.out.println(Arrays.toString(data[j])+":"+Arrays.toString(result));
}
//根据训练结果来预测一条新数据的分类
//0.147046,0.00364964,0.67903,1.66344,0.0540356
double[] x = new double[]{0.147046,0.00364964,0.67903,1.66344,0.0540356};
double[] result = bp.computeOut(x);
System.out.println(Arrays.toString(x)+":"+Arrays.toString(result));
}
}
import java.util.Random;
public class BpDeep{
public double[][] layer;//神经网络各层节点
public double[][] layerErr;//神经网络各节点误差
public double[][][] layer_weight;//各层节点权重
public double[][][] layer_weight_delta;//各层节点权重动量
public double mobp;//动量系数
public double rate;//学习系数
public BpDeep(int[] layernum, double rate, double mobp){
this.mobp = mobp;
this.rate = rate;
layer = new double[layernum.length][];
layerErr = new double[layernum.length][];
layer_weight = new double[layernum.length][][];
layer_weight_delta = new double[layernum.length][][];
Random random = new Random();
for(int l=0;lnew double[layernum[l]];
layerErr[l]=new double[layernum[l]];
if(l+1new double[layernum[l]+1][layernum[l+1]];
layer_weight_delta[l]=new double[layernum[l]+1][layernum[l+1]];
for(int j=0;j1;j++)
for(int i=0;i1];i++)
layer_weight[l][j][i]=random.nextDouble();//随机初始化权重
}
}
}
//逐层向前计算输出
public double[] computeOut(double[] in){
for(int l=1;lfor(int j=0;jdouble z=layer_weight[l-1][layer[l-1].length][j];
for(int i=0;i1].length;i++){
layer[l-1][i]=l==1?in[i]:layer[l-1][i];
z+=layer_weight[l-1][i][j]*layer[l-1][i];
}
layer[l][j]=1/(1+Math.exp(-z));
}
}
return layer[layer.length-1];
}
//逐层反向计算误差并修改权重
public void updateWeight(double[] tar){
int l=layer.length-1;
for(int j=0;j1-layer[l][j])*(tar[j]-layer[l][j]);
while(l-->0){
for(int j=0;jdouble z = 0.0;
for(int i=0;i1].length;i++){
z=z+l>0?layerErr[l+1][i]*layer_weight[l][j][i]:0;
layer_weight_delta[l][j][i]= mobp*layer_weight_delta[l][j][i]+rate*layerErr[l+1][i]*layer[l][j];//隐含层动量调整
layer_weight[l][j][i]+=layer_weight_delta[l][j][i];//隐含层权重调整
if(j==layerErr[l].length-1){
layer_weight_delta[l][j+1][i]= mobp*layer_weight_delta[l][j+1][i]+rate*layerErr[l+1][i];//截距动量调整
layer_weight[l][j+1][i]+=layer_weight_delta[l][j+1][i];//截距权重调整
}
}
layerErr[l][j]=z*layer[l][j]*(1-layer[l][j]);//记录误差
}
}
}
public void train(double[] in, double[] tar){
double[] out = computeOut(in);
updateWeight(tar);
}
}