CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
MLP.cc
Go to the documentation of this file.
1 #include <iostream>
2 #include <cstdlib>
3 #include <cstring>
4 #include <sstream>
5 #include <string>
6 #include <vector>
7 
9 
10 #include "MLP.h"
11 
12 #include "mlp_gen.h"
13 
14 namespace PhysicsTools {
15 
16 bool MLP::inUse = false;
17 
18 static std::vector<std::string> split(const std::string line, char delim)
19 {
20  const char *str = line.c_str();
21  const char *p = str;
22 
23  std::vector<std::string> tokens;
24 
25  if (line[0] == '\0')
26  return tokens;
27 
28  while(p) {
29  const char *q = std::strchr(p, delim);
30 
31  if (!q) {
32  tokens.push_back(std::string(p));
33  p = 0;
34  } else {
35  tokens.push_back(std::string(p, q - p));
36  p = q + 1;
37  }
38  }
39 
40  return tokens;
41 }
42 
43 MLP::MLP(unsigned int nIn, unsigned int nOut, const std::string layout_) :
44  initialized(false), layers(0), layout(0), epoch(0)
45 {
46  if (inUse)
47  throw cms::Exception("MLP")
48  << "mlpfit doesn't support more than one instance."
49  << std::endl;
50 
51  std::vector<std::string> parsed = split(layout_, ':');
52  if (parsed.size() < 1)
53  throw cms::Exception("MLP")
54  << "Invalid layout." << std::endl;
55 
56  layout = new int[parsed.size() + 2];
57 
58  layers = parsed.size();
59  layout[0] = (int)nIn;
60  for(int i = 0; i < layers; i++) {
61  std::istringstream ss(parsed[i]);
62  int nodes;
63  ss >> nodes;
64  if (nodes < 1)
65  throw cms::Exception("MLP")
66  << "Invalid layout." << std::endl;
67 
68  layout[i + 1] = nodes;
69  }
70  layout[layers + 1] = (int)nOut;
71  layers += 2;
72 
73  inUse = true;
74 
75  MLP_SetNet(&layers, layout);
76  setLearn();
77  LearnAlloc();
78  InitWeights();
79 }
80 
82 {
83  clear();
84 
85  LearnFree();
86  inUse = false;
87  delete[] layout;
88 }
89 
90 void MLP::clear()
91 {
92  if (!initialized)
93  return;
94  initialized = false;
95 
96  FreePatterns(0);
97  free(PAT.Rin);
98  free(PAT.Rans);
99  free(PAT.Pond);
100 }
101 
102 void MLP::setLearn(void)
103 {
104  LEARN.Meth = 7;
105  LEARN.Nreset = 50;
106  LEARN.Tau = 1.5;
107  LEARN.Decay = 1.0;
108  LEARN.eta = 0.1;
109  LEARN.Lambda = 1.0;
110  LEARN.delta = 0.0;
111  LEARN.epsilon = 0.2;
112 }
113 
114 void MLP::setNPattern(unsigned int size)
115 {
116  PAT.Npat[0] = (int)size;
117  PAT.Npat[1] = 0;
118  PAT.Nin = layout[0];
119  PAT.Nout = layout[layers - 1];
120 }
121 
122 void MLP::init(unsigned int rows)
123 {
124  setNPattern(rows);
125  AllocPatterns(0, rows, layout[0], layout[layers - 1], 0);
126  initialized = true;
127 }
128 
129 void MLP::set(unsigned int row, double *data, double *target, double weight)
130 {
131  int nIn = layout[0];
132  int nOut = layout[layers - 1];
133 
134  std::memcpy(&PAT.vRin[0][row*(nIn + 1) + 1], data, sizeof(double) * nIn);
135  std::memcpy(&PAT.Rans[0][row][0], target, sizeof(double) * nOut);
136  PAT.Pond[0][row] = weight;
137 }
138 
139 double MLP::train()
140 {
141  double alpMin;
142  int nTest;
143 
144  return MLP_Epoch(++epoch, &alpMin, &nTest);
145 }
146 
147 const double *MLP::eval(double *data) const
148 {
149  MLP_Out_T(data);
150 
151  return &NET.Outn[layers - 1][0];
152 }
153 
154 void MLP::save(const std::string file) const
155 {
156  if (SaveWeights(const_cast<char*>(file.c_str()), (int)epoch) < 0)
157  throw cms::Exception("MLP")
158  << "Error opening \"" << file << "\"." << std::endl;
159 }
160 
162 {
163  int epoch_ = 0;
164  if (LoadWeights(const_cast<char*>(file.c_str()), &epoch_) < 0)
165  throw cms::Exception("MLP")
166  << "Error opening \"" << file << "\"." << std::endl;
167  epoch = (unsigned int)epoch_;
168 }
169 
170 } // namespace PhysicsTools
dbl MLP_Epoch(int iepoch, dbl *alpmin, int *Ntest)
Definition: mlp_gen.cc:706
int i
Definition: DBlmapReader.cc:9
std::vector< LayerSetAndLayers > layers(const SeedingLayerSetsHits &sets)
Definition: LayerTriplets.cc:4
const double * eval(double *data) const
Definition: MLP.cc:147
void LearnFree()
Definition: mlp_gen.cc:2640
void InitWeights()
Definition: mlp_gen.cc:2147
void setNPattern(unsigned int size)
Definition: MLP.cc:114
int SaveWeights(char *filename, int iepoch)
Definition: mlp_gen.cc:2972
int MLP_SetNet(int *nl, int *nn)
Definition: mlp_gen.cc:3639
#define PAT
Definition: mlp_gen.h:45
static bool inUse
Definition: MLP.h:32
void load(const std::string file)
Definition: MLP.cc:161
unsigned int epoch
Definition: MLP.h:31
MLP(unsigned int nIn, unsigned int nOut, const std::string layout)
Definition: MLP.cc:43
#define NET
Definition: mlp_gen.h:25
int layers
Definition: MLP.h:28
bool initialized
Definition: MLP.h:27
void MLP_Out_T(type_pat *rrin)
Definition: mlp_gen.cc:113
int LearnAlloc()
Definition: mlp_gen.cc:2685
void set(unsigned int row, double *data, double *target, double weight=1.0)
Definition: MLP.cc:129
void setLearn(void)
Definition: MLP.cc:102
int LoadWeights(char *filename, int *iepoch)
Definition: mlp_gen.cc:3022
int AllocPatterns(int ifile, int npat, int nin, int nout, int iadd)
Definition: mlp_gen.cc:3077
#define LEARN
Definition: mlp_gen.h:36
int * layout
Definition: MLP.h:29
void clear()
Definition: MLP.cc:90
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
static std::vector< std::string > split(const std::string line, char delim)
Definition: MLP.cc:18
double train()
Definition: MLP.cc:139
void save(const std::string file) const
Definition: MLP.cc:154
volatile std::atomic< bool > shutdown_flag false
int weight
Definition: histoStyle.py:50
int FreePatterns(int ifile)
Definition: mlp_gen.cc:3232
void init(unsigned int rows)
Definition: MLP.cc:122
tuple size
Write out results.