29 using namespace PhysicsTools;
38 ProcMLP(
const char *
name,
43 virtual void configure(ConfIterator iter,
unsigned int n);
44 virtual void eval(ValueIterator iter,
unsigned int n)
const;
45 virtual std::vector<double> deriv(
46 ValueIterator iter,
unsigned int n)
const;
51 Layer(
const Layer &orig) :
53 coeffs(orig.coeffs), sigmoid(orig.sigmoid) {}
57 std::vector<double> coeffs;
61 std::vector<Layer> layers;
74 coeffs.resize(neurons * (
inputs + 1));
75 std::vector<double>::iterator inserter = coeffs.begin();
77 for(std::vector<Neuron>::const_iterator iter = calib.first.begin();
78 iter != calib.first.end(); iter++) {
79 *inserter++ = iter->first;
81 if (iter->second.size() !=
inputs)
83 <<
"ProcMLP neuron layer inconsistent."
86 inserter =
std::copy(iter->second.begin(), iter->second.end(),
91 ProcMLP::ProcMLP(
const char *
name,
98 std::back_inserter(layers));
100 for(
size_t i = 0;
i < layers.size();
i++) {
101 maxTmp = std::max<unsigned int>(maxTmp, layers[
i].neurons);
102 if (
i > 0 && layers[
i - 1].neurons != layers[
i].
inputs)
104 <<
"ProcMLP neuron layers do not connect "
105 "properly." << std::endl;
109 void ProcMLP::configure(ConfIterator iter,
unsigned int n)
111 if (n != layers.front().inputs)
114 for(
unsigned int i = 0;
i <
n;
i++)
115 iter++(Variable::FLAG_NONE);
117 for(
unsigned int i = 0;
i < layers.back().neurons;
i++)
118 iter << Variable::FLAG_NONE;
121 void ProcMLP::eval(ValueIterator iter,
unsigned int n)
const
123 double *
tmp = (
double*)alloca(2 * maxTmp *
sizeof(
double));
126 for(
double *
pos = tmp; iter; iter++,
pos++)
130 for(std::vector<Layer>::const_iterator layer = layers.begin();
131 layer != layers.end(); layer++, flip = !flip) {
132 const double *
input = &tmp[flip ? maxTmp : 0];
133 output = &tmp[flip ? 0 : maxTmp];
134 std::vector<double>::const_iterator coeff =
135 layer->coeffs.begin();
136 for(
unsigned int i = 0;
i < layer->neurons;
i++) {
137 double sum = *coeff++;
138 for(
unsigned int j = 0;
j < layer->inputs;
j++)
139 sum += input[
j] * *coeff++;
150 std::vector<double> ProcMLP::deriv(ValueIterator iter,
unsigned int n)
const
152 std::vector<double> prevValues, nextValues;
153 std::vector<double> prevMatrix, nextMatrix;
156 nextValues.push_back(*iter++);
158 unsigned int size = nextValues.size();
159 nextMatrix.resize(size * size);
160 for(
unsigned int i = 0;
i <
size;
i++)
161 nextMatrix[
i * size +
i] = 1.;
163 for(std::vector<Layer>::const_iterator layer = layers.begin();
164 layer != layers.end(); layer++) {
170 std::vector<double>::const_iterator coeff =
171 layer->coeffs.begin();
172 for(
unsigned int i = 0;
i < layer->neurons;
i++) {
173 double sum = *coeff++;
174 for(
unsigned int j = 0;
j < layer->inputs;
j++)
175 sum += prevValues[
j] * *coeff++;
178 if (layer->sigmoid) {
180 sum = 1.0 / (e + 1.0);
181 deriv = 1.0 / (e + 1.0/e + 2.0);
185 nextValues.push_back(sum);
187 for(
unsigned int k = 0;
k <
size;
k++) {
189 coeff -= layer->inputs;
190 for(
unsigned int j = 0;
j < layer->inputs;
j++)
191 sum += prevMatrix[
j * size +
k] *
193 nextMatrix.push_back(sum * deriv);
detail::ThreadSafeRegistry< ParameterSetID, ParameterSet, ProcessParameterSetIDCache > Registry
U second(std::pair< T, U > const &p)
MVATrainerComputer * calib
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
std::vector< std::vector< double > > tmp
tuple size
Write out results.