本文實例講述了基于java實現的一層簡單人工神經網絡算法。分享給大家供大家參考,具體如下:
先來看看筆者繪制的算法圖:
2、數據類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import java.util.arrays; public class data { double [] vector; int dimention; int type; public double [] getvector() { return vector; } public void setvector( double [] vector) { this .vector = vector; } public int getdimention() { return dimention; } public void setdimention( int dimention) { this .dimention = dimention; } public int gettype() { return type; } public void settype( int type) { this .type = type; } public data( double [] vector, int dimention, int type) { super (); this .vector = vector; this .dimention = dimention; this .type = type; } public data() { } @override public string tostring() { return "data [vector=" + arrays.tostring(vector) + ", dimention=" + dimention + ", type=" + type + "]" ; } } |
3、簡單人工神經網絡
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
package cn.edu.hbut.chenjie; import java.util.arraylist; import java.util.list; import java.util.random; import org.jfree.chart.chartfactory; import org.jfree.chart.chartframe; import org.jfree.chart.jfreechart; import org.jfree.data.xy.defaultxydataset; import org.jfree.ui.refineryutilities; public class ann2 { private double eta; //學習率 private int n_iter; //權重向量w[]訓練次數 private list<data> exercise; //訓練數據集 private double w0 = 0 ; //閾值 private double x0 = 1 ; //固定值 private double [] weights; //權重向量,其長度為訓練數據維度+1,在本例中數據為2維,故長度為3 private int testsum = 0 ; //測試數據總數 private int error = 0 ; //錯誤次數 defaultxydataset xydataset = new defaultxydataset(); /** * 向圖表中增加同類型的數據 * @param type 類型 * @param a 所有數據的第一個分量 * @param b 所有數據的第二個分量 */ public void add(string type, double [] a, double [] b) { double [][] data = new double [ 2 ][a.length]; for ( int i= 0 ;i<a.length;i++) { data[ 0 ][i] = a[i]; data[ 1 ][i] = b[i]; } xydataset.addseries(type, data); } /** * 畫圖 */ public void draw() { jfreechart jfreechart = chartfactory.createscatterplot( "exercise" , "x1" , "x2" , xydataset); chartframe frame = new chartframe( "訓練數據" , jfreechart); frame.pack(); refineryutilities.centerframeonscreen(frame); frame.setvisible( true ); } public static void main(string[] args) { ann2 ann2 = new ann2( 0.001 , 100 ); //構造人工神經網絡 list<data> exercise = new arraylist<data>(); //構造訓練集 //人工模擬1000條訓練數據 ,分界線為x2=x1+0.5 for ( int i= 0 ;i< 1000000 ;i++) { random rd = new random(); double x1 = rd.nextdouble(); //隨機產生一個分量 double x2 = rd.nextdouble(); //隨機產生另一個分量 double [] da = {x1,x2}; //產生數據向量 data d = new data(da, 2 , x2 > x1+ 0.5 ? 1 : - 1 ); //構造數據 exercise.add(d); //將訓練數據加入訓練集 } int sum1 = 0 ; //記錄類型1的訓練記錄數 int sum2 = 0 ; //記錄類型-1的訓練記錄數 for ( int i = 0 ; i < exercise.size(); i++) { if (exercise.get(i).gettype()== 1 ) sum1++; else if (exercise.get(i).gettype()==- 1 ) sum2++; } double [] x1 = new double [sum1]; double [] y1 = new double [sum1]; double [] x2 = new double [sum2]; double [] y2 = new double [sum2]; int index1 = 0 ; int index2 = 0 ; for ( int i = 0 ; i < exercise.size(); i++) { if (exercise.get(i).gettype()== 1 ) { x1[index1] = exercise.get(i).vector[ 0 ]; y1[index1++] = exercise.get(i).vector[ 1 ]; } else if (exercise.get(i).gettype()==- 1 ) { x2[index2] = exercise.get(i).vector[ 0 ]; y2[index2++] = exercise.get(i).vector[ 1 ]; } } ann2.add( "1" , x1, y1); ann2.add( "-1" , x2, y2); ann2.draw(); ann2.input(exercise); //將訓練集輸入人工神經網絡 ann2.fit(); //訓練 ann2.showweigths(); //顯示權重向量 //人工生成一千條測試數據 for ( int i= 0 ;i< 10000 ;i++) { random rd = new random(); double x1_ = rd.nextdouble(); double x2_ = rd.nextdouble(); double [] da = {x1_,x2_}; data test = new data(da, 2 , x2_ > x1_+ 0.5 ? 1 : - 1 ); ann2.predict(test); //測試 } system.out.println( "總共測試" + ann2.testsum + "條數據,有" + ann2.error + "條錯誤,錯誤率:" + ann2.error * 1.0 /ann2.testsum * 100 + "%" ); } /** * * @param eta 學習率 * @param n_iter 權重分量學習次數 */ public ann2( double eta, int n_iter) { this .eta = eta; this .n_iter = n_iter; } /** * 輸入訓練集到人工神經網絡 * @param exercise */ private void input(list<data> exercise) { this .exercise = exercise; //保存訓練集 weights = new double [exercise.get( 0 ).dimention + 1 ]; //初始化權重向量,其長度為訓練數據維度+1 weights[ 0 ] = w0; //權重向量第一個分量為w0 for ( int i = 1 ; i < weights.length; i++) weights[i] = 0 ; //其余分量初始化為0 } private void fit() { for ( int i = 0 ; i < n_iter; i++) //權重分量調整n_iter次 { for ( int j = 0 ; j < exercise.size(); j++) //對于訓練集中的每條數據進行訓練 { int real_result = exercise.get(j).type; //y int calculate_result = calculateresult(exercise.get(j)); //y' double delta0 = eta * (real_result - calculate_result); //計算閾值更新 w0 += delta0; //閾值更新 weights[ 0 ] = w0; //更新w[0] for ( int k = 0 ; k < exercise.get(j).getdimention(); k++) //更新權重向量其它分量 { double delta = eta * (real_result - calculate_result) * exercise.get(j).vector[k]; //δw=η*(y-y')*x weights[k+ 1 ] += delta; //w=w+δw } } } } private int calculateresult(data data) { double z = w0 * x0; for ( int i = 0 ; i < data.dimention; i++) z += data.vector[i] * weights[i+ 1 ]; //z=w0x0+w1x1+...+wmxm //激活函數 if (z>= 0 ) return 1 ; else return - 1 ; } private void showweigths() { for ( double w : weights) system.out.println(w); } private void predict(data data) { int type = calculateresult(data); if (type == data.gettype()) { //system.out.println("預測正確"); } else { //system.out.println("預測錯誤"); error ++; } testsum ++; } } |
運行結果:
1
2
3
4
|
- 0.22000000000000017 - 0.4416843982815453 0.442444202054685 總共測試 10000 條數據,有 17 條錯誤,錯誤率: 0.16999999999999998 % |
希望本文所述對大家java程序設計有所幫助。
原文鏈接:http://blog.csdn.net/csj941227/article/details/73325695