-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblender.cc
146 lines (114 loc) · 3.59 KB
/
blender.cc
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
/* blender.cc
Jeremy Barnes, 30 September 2009
Copyright (c) 2009 Jeremy Barnes. All rights reserved.
Implementation of the blender class.
*/
#include "blender.h"
#include "boosting_blender.h"
#include "gated_blender.h"
#include "deep_net_blender.h"
#include "multiple_regression_blender.h"
#include "classifier_blender.h"
using namespace ML;
using namespace std;
__thread float correct_prediction = 0.0;
/*****************************************************************************/
/* BLENDER */
/*****************************************************************************/
Blender::
Blender()
{
}
Blender::
~Blender()
{
}
std::string
Blender::
explain(const ML::distribution<float> & models) const
{
return "";
}
/*****************************************************************************/
/* LINEAR_BLENDER */
/*****************************************************************************/
Linear_Blender::
Linear_Blender()
{
}
Linear_Blender::
~Linear_Blender()
{
}
void
Linear_Blender::
configure(const ML::Configuration & config_,
const std::string & name,
int random_seed,
Target target)
{
Configuration config(config_, name, Configuration::PREFIX_APPEND);
config.require(mode, "mode");
config.require(num_models, "num_models");
}
void
Linear_Blender::
init(const Data & data,
const ML::distribution<float> & example_weights)
{
model_weights.clear();
model_weights.resize(data.nm(), 0.0);
if (mode == "best_n" || mode == "best_n_weighted") {
if (num_models > data.nm())
num_models = data.nm();
for (unsigned i = 0; i < num_models; ++i) {
int m = data.model_ranking[i];
cerr << "chose model " << m << " " << data.model_names[m] << endl;
float score
= (mode == "best_n_weighted")
? data.models[m].score
: 1.0;
model_weights[m] = score;
}
model_weights.normalize();
}
else throw Exception("unknown mode " + mode);
}
float
Linear_Blender::
predict(const ML::distribution<float> & models) const
{
return models.dotprod(model_weights);
}
/*****************************************************************************/
/* UTILITY FUNCTIONS */
/*****************************************************************************/
boost::shared_ptr<Blender>
get_blender(const ML::Configuration & config_,
const std::string & name,
const Data & data,
const ML::distribution<float> & example_weights,
int random_seed,
Target target)
{
Configuration config(config_, name, Configuration::PREFIX_APPEND);
string type;
config.require(type, "type");
boost::shared_ptr<Blender> result;
if (type == "linear")
result.reset(new Linear_Blender());
else if (type == "boosting")
result.reset(new Boosting_Blender());
else if (type == "gated")
result.reset(new Gated_Blender());
else if (type == "deep_net")
result.reset(new Deep_Net_Blender());
else if (type == "multiple_regression")
result.reset(new Multiple_Regression_Blender());
else if (type == "classifier")
result.reset(new Classifier_Blender());
else throw Exception("Blender of type " + type + " doesn't exist");
result->configure(config_, name, random_seed, target);
result->init(data, example_weights);
return result;
}