This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
/
OnlineRegressionUDTF.java
354 lines (304 loc) · 12.5 KB
/
OnlineRegressionUDTF.java
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* Hivemall: Hive scalable Machine Learning Library
*
* Copyright (C) 2013
* National Institute of Advanced Industrial Science and Technology (AIST)
* Registration Number: H25PRO-1520
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package hivemall.regression;
import hivemall.common.FeatureValue;
import hivemall.common.HivemallConstants;
import hivemall.common.PredictionResult;
import hivemall.common.WeightValue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentException;
import org.apache.hadoop.hive.ql.exec.UDFArgumentTypeException;
import org.apache.hadoop.hive.ql.metadata.HiveException;
import org.apache.hadoop.hive.ql.udf.generic.GenericUDTF;
import org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorUtils;
import org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.FloatObjectInspector;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
import org.apache.hadoop.hive.serde2.objectinspector.primitive.WritableConstantStringObjectInspector;
import org.apache.hadoop.io.FloatWritable;
import org.apache.hadoop.io.Text;
public abstract class OnlineRegressionUDTF extends GenericUDTF {
protected ListObjectInspector featureListOI;
protected ObjectInspector featureInputOI;
protected FloatObjectInspector targetOI;
protected boolean parseX;
protected boolean feature_hashing;
protected float bias;
protected Object biasKey;
protected Map<Object, WeightValue> weights;
protected int count;
@Override
public StructObjectInspector initialize(ObjectInspector[] argOIs) throws UDFArgumentException {
if(argOIs.length < 2) {
throw new UDFArgumentException(getClass().getSimpleName()
+ " takes 2 arguments: List<Int|BigInt|Text> features, float target [, constant string options]");
}
this.featureInputOI = processFeaturesOI(argOIs[0]);
this.targetOI = (FloatObjectInspector) argOIs[1];
processOptions(argOIs);
ObjectInspector featureOutputOI = featureInputOI;
if(parseX && feature_hashing) {
featureOutputOI = PrimitiveObjectInspectorFactory.javaIntObjectInspector;
}
if(bias != 0.f) {
this.biasKey = (featureOutputOI.getTypeName() == HivemallConstants.INT_TYPE_NAME) ? HivemallConstants.BIAS_CLAUSE_INT
: new Text(HivemallConstants.BIAS_CLAUSE);
} else {
this.biasKey = null;
}
ArrayList<String> fieldNames = new ArrayList<String>();
ArrayList<ObjectInspector> fieldOIs = new ArrayList<ObjectInspector>();
fieldNames.add("feature");
ObjectInspector featureOI = ObjectInspectorUtils.getStandardObjectInspector(featureOutputOI);
fieldOIs.add(featureOI);
fieldNames.add("weight");
fieldOIs.add(PrimitiveObjectInspectorFactory.writableFloatObjectInspector);
this.weights = new HashMap<Object, WeightValue>(8192);
this.count = 1;
return ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldOIs);
}
protected ObjectInspector processFeaturesOI(ObjectInspector arg)
throws UDFArgumentTypeException {
this.featureListOI = (ListObjectInspector) arg;
ObjectInspector featureRawOI = featureListOI.getListElementObjectInspector();
String keyTypeName = featureRawOI.getTypeName();
if(keyTypeName != HivemallConstants.STRING_TYPE_NAME
&& keyTypeName != HivemallConstants.INT_TYPE_NAME
&& keyTypeName != HivemallConstants.BIGINT_TYPE_NAME) {
throw new UDFArgumentTypeException(0, "1st argument must be Map of key type [Int|BitInt|Text]: "
+ keyTypeName);
}
this.parseX = (keyTypeName == HivemallConstants.STRING_TYPE_NAME);
return featureRawOI;
}
protected Options getOptions() {
Options opts = new Options();
opts.addOption("fh", "fhash", false, "Enable feature hashing (only used when feature is TEXT type) [default: off]");
opts.addOption("b", "bias", true, "Bias clause [default 1.0, 0.0 to disable]");
return opts;
}
private final CommandLine parseOptions(String optionValue) throws UDFArgumentException {
String[] args = optionValue.split("\\s+");
Options opts = getOptions();
BasicParser parser = new BasicParser();
final CommandLine cl;
try {
cl = parser.parse(opts, args);
} catch (ParseException e) {
throw new UDFArgumentException(e);
}
return cl;
}
protected CommandLine processOptions(ObjectInspector[] argOIs) throws UDFArgumentException {
boolean fhashFlag = false;
float biasValue = 0.f;
CommandLine cl = null;
if(argOIs.length >= 3) {
String rawArgs = ((WritableConstantStringObjectInspector) argOIs[2]).getWritableConstantValue().toString();
cl = parseOptions(rawArgs);
if(cl.hasOption("fh")) {
fhashFlag = true;
}
String biasStr = cl.getOptionValue("b");
if(biasStr != null) {
biasValue = Float.parseFloat(biasStr);
}
}
this.feature_hashing = fhashFlag;
this.bias = biasValue;
return cl;
}
@Override
public void process(Object[] args) throws HiveException {
List<?> features = (List<?>) featureListOI.getList(args[0]);
float target = targetOI.get(args[1]);
checkTargetValue(target);
train(features, target);
count++;
}
protected void checkTargetValue(float target) throws UDFArgumentException {}
protected void train(final Collection<?> features, final float target) {
float p = predict(features);
update(features, target, p);
}
protected float predict(final Collection<?> features) {
final ObjectInspector featureInspector = this.featureInputOI;
final boolean parseX = this.parseX;
float score = 0.f;
for(Object f : features) {// a += w[i] * x[i]
if(f == null) {
continue;
}
final Object k;
final float v;
if(parseX) {
FeatureValue fv = FeatureValue.parse(f, feature_hashing);
k = fv.getFeature();
v = fv.getValue();
} else {
k = ObjectInspectorUtils.copyToStandardObject(f, featureInspector);
v = 1.f;
}
WeightValue old_w = weights.get(k);
if(old_w != null) {
score += (old_w.get() * v);
}
}
if(biasKey != null) {
WeightValue biasWeight = weights.get(biasKey);
if(biasWeight != null) {
score += (biasWeight.get() * bias);
}
}
return score;
}
protected PredictionResult calcScoreAndNorm(Collection<?> features) {
final ObjectInspector featureInspector = this.featureInputOI;
final boolean parseX = this.parseX;
float score = 0.f;
float squared_norm = 0.f;
for(Object f : features) {// a += w[i] * x[i]
if(f == null) {
continue;
}
final Object k;
final float v;
if(parseX) {
FeatureValue fv = FeatureValue.parse(f, feature_hashing);
k = fv.getFeature();
v = fv.getValue();
} else {
k = ObjectInspectorUtils.copyToStandardObject(f, featureInspector);
v = 1.f;
}
WeightValue old_w = weights.get(k);
if(old_w != null) {
score += (old_w.get() * v);
}
squared_norm += (v * v);
}
if(biasKey != null) {
WeightValue biasWeight = weights.get(biasKey);
if(biasWeight != null) {
score += (biasWeight.get() * bias);
}
squared_norm += (bias * bias); // REVIEWME
}
return new PredictionResult(score).squaredNorm(squared_norm);
}
protected PredictionResult calcScoreAndVariance(Collection<?> features) {
final ObjectInspector featureInspector = featureListOI.getListElementObjectInspector();
final boolean parseX = this.parseX;
float score = 0.f;
float variance = 0.f;
for(Object f : features) {// a += w[i] * x[i]
if(f == null) {
continue;
}
final Object k;
final float v;
if(parseX) {
FeatureValue fv = FeatureValue.parse(f, feature_hashing);
k = fv.getFeature();
v = fv.getValue();
} else {
k = ObjectInspectorUtils.copyToStandardObject(f, featureInspector);
v = 1.f;
}
WeightValue old_w = weights.get(k);
if(old_w == null) {
variance += (1.f * v * v);
} else {
score += (old_w.getValue() * v);
variance += (old_w.getCovariance() * v * v);
}
}
if(biasKey != null) {
WeightValue biasWeight = weights.get(biasKey);
if(biasWeight == null) {
variance += (1.f * bias * bias);
} else {
score += (biasWeight.getValue() * bias);
variance += (biasWeight.getCovariance() * bias * bias);
}
}
return new PredictionResult(score).variance(variance);
}
protected void update(Collection<?> features, float target, float predicted) {
float d = dloss(target, predicted);
update(features, d);
}
protected float dloss(float target, float predicted) {
throw new IllegalStateException();
}
protected void update(Collection<?> features, float coeff) {
final ObjectInspector featureInspector = this.featureInputOI;
for(Object f : features) {// w[i] += y * x[i]
if(f == null) {
continue;
}
final Object x;
final float xi;
if(parseX) {
FeatureValue fv = FeatureValue.parse(f, feature_hashing);
x = fv.getFeature();
xi = fv.getValue();
} else {
x = ObjectInspectorUtils.copyToStandardObject(f, featureInspector);
xi = 1.f;
}
WeightValue old_w = weights.get(x);
float new_w = (old_w == null) ? coeff * xi : old_w.get() + (coeff * xi);
weights.put(x, new WeightValue(new_w));
}
if(biasKey != null) {
WeightValue old_bias = weights.get(biasKey);
float new_bias = (old_bias == null) ? coeff * bias : old_bias.get() + (coeff * bias);
weights.put(biasKey, new WeightValue(new_bias));
}
}
@Override
public void close() throws HiveException {
if(weights != null) {
final Object[] forwardMapObj = new Object[2];
for(Map.Entry<Object, WeightValue> e : weights.entrySet()) {
Object k = e.getKey();
WeightValue v = e.getValue();
FloatWritable fv = new FloatWritable(v.get());
forwardMapObj[0] = k;
forwardMapObj[1] = fv;
forward(forwardMapObj);
}
this.weights = null;
}
}
}