-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDualPerceptron.java
More file actions
198 lines (182 loc) · 5.95 KB
/
DualPerceptron.java
File metadata and controls
198 lines (182 loc) · 5.95 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package cs475;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.*;
import java.math.*;
import java.util.Map.Entry;
public class DualPerceptron extends Predictor {
private double online_learning_rate = 1;
private String preName = "perceptron_linear_kernel";
private int online_training_iterations = 1;
private boolean isLinear = false;
private boolean isPoly = false;
private double d = 2;
private List<Instance> trainingset = new ArrayList<Instance>();
Double[][] gram;
HashMap<Integer,Double> alphaVector = new HashMap<Integer,Double>();
public DualPerceptron(int online_training_iterations, double online_learning_rate,
boolean isLinear, boolean isPoly, double d, List<Instance> instances){
this.online_learning_rate = online_learning_rate;
this.online_training_iterations = online_training_iterations;
this.d = d;
this.isLinear = isLinear;
this.isPoly = isPoly;
if((online_learning_rate>1)||(online_learning_rate<0)){
System.out.println("The online_learning_rate must in (0,1]");
}
if(isLinear){
gram = this.getLinearGram(instances);
}
else if(isPoly){
gram = this.getPolyGram(instances,d);
}
}
public void train(List<Instance> instances){
// System.out.println(online_training_iterations);
for(int i = 0 ;i <instances.size();i++){
alphaVector.put(i, 0.0);
}
for(int i = 0; i< instances.size(); i++){
this.trainingset.add(instances.get(i));
}
// System.out.println("trainingset: " + trainingset.size());
for(int k = 0; k <online_training_iterations;k++){
// System.out.println("---------"+ k+"th Iteration---------");
for(int i = 0;i<instances.size();i++){
double wx = .0;
double alpha_i = alphaVector.get(i);
ClassificationLabel labeli = (ClassificationLabel) instances.get(i).getLabel();
int yi = (labeli.getLabelValue() == 0 ? -1 : 1);
for(int j = 0;j<instances.size();j++){
double alpha_j = alphaVector.get(j);
ClassificationLabel label = (ClassificationLabel) instances.get(j).getLabel();
int yj = label.getLabelValue();
if(yj == 0){
yj = -1;
}
wx += alpha_j*yj*gram[i][j];
}
if(wx*yi < 1){
alpha_i +=1;
alphaVector.put(i, alpha_i);
}
}
}
}
private Double[][] getLinearGram(List<Instance> instances){
// System.out.println("Computing G Matrix...");
Double[][] gram = new Double[instances.size()][instances.size()];
int numFtre = numAllFtre(instances);
for(int i = 0; i < instances.size(); i++){
Double[] row = new Double[instances.size()];
for(int j = 0; j< instances.size(); j++){
double value = 0.0;
Instance tmpi = instances.get(i);
Instance tmpj = instances.get(j);
for(Entry<Integer,Double> entry:tmpi.getFeatureVector().getVector().entrySet()){
value += entry.getValue()*tmpj.getFeatureVector().get(entry.getKey());
}
// for(int k = 1; k<=numFtre; k++){
// value += tmpi.getFeatureVector().get(k)*tmpj.getFeatureVector().get(k);
// }
row[j]=(value);
}
gram[i] =(row);
}
// System.out.println("Finished computing");
return gram;
}
public int numAllFtre(List<Instance> instances){
int allFtre = 0;
Iterator<Instance> it = instances.iterator();
while(it.hasNext()){
Instance tmp = it.next();
int m = Collections.max(tmp.getFeatureVector().getVector().keySet());
if (m>allFtre){
allFtre = m; // get the how many features for a instance
}
}
return allFtre;
}
private Double[][] getPolyGram(List<Instance> instances, double d){
// value = Math.pow((1+xx), d);
// System.out.println("Computing G Matrix...");
Double[][] lgram = this.getLinearGram(instances);
Double[][] gram = new Double[instances.size()][instances.size()];
int numFtre = numAllFtre(instances);
for(int i = 0; i < instances.size(); i++){
Double[] row = new Double[instances.size()];
for(int j = 0; j< instances.size(); j++){
double value = 0.0;
value = Math.pow((1+lgram[i][j]), d);
row[j] = value;
}
gram[i] = row;
}
return gram;
}
public ClassificationLabel predict(Instance instance,
boolean isLinear, boolean isPoly, double d
){
List<Instance> instances = this.trainingset;
int preLabel = 0;
double tmp = .0;
int allFtre = this.numAllFtre(instances);
Double[] gramp = new Double[instances.size()];
if(isLinear){
// System.out.println("Computin gram...");
for(int i = 0; i<instances.size();i++){
double value = 0.0;
Instance ins = instances.get(i);
// for(int j =1;j<=allFtre;j++){
// value += ins.getFeatureVector().get(j)*instance.getFeatureVector().get(j);
// }
for(Entry<Integer,Double> entry:instance.getFeatureVector().getVector().entrySet()){
value += entry.getValue()*ins.getFeatureVector().get(entry.getKey());
}
gramp[i] = value;
}
}
else if(isPoly){
for(int i = 0; i<instances.size();i++){
double value = 0.0;
Instance ins = instances.get(i);
for(Entry<Integer,Double> entry:instance.getFeatureVector().getVector().entrySet()){
value += entry.getValue()*ins.getFeatureVector().get(entry.getKey());
}
value = Math.pow((1+value), d);
gramp[i] = value;
}
}
for (int i = 0; i< instances.size();i++){
ClassificationLabel label = (ClassificationLabel)instances.get(i).getLabel();
int yi = label.getLabelValue();
if(yi ==0){
yi = -1;
}
tmp += this.alphaVector.get(i)*gramp[i]*yi;
}
if(tmp >= 0){
preLabel = 1;
}
else preLabel = 0;
ClassificationLabel Label = new ClassificationLabel(preLabel);
return Label;
}
public Label predict(Instance instance){
return null;
}
public Label getLabel(){
return null;
}
public String getpreName(){
return this.preName;
}
public boolean isLinear(){
return this.isLinear;
}
public double dvalue(){
return this.d;
}
}