HOG2
LinearRegression.h
Go to the documentation of this file.
1 /*
2  * LinearRegression.h
3  * hog2
4  *
5  * Created by Nathan Sturtevant on 6/1/06.
6  * Copyright 2006 Nathan Sturtevant. All rights reserved.
7  *
8  */
9 
10 #ifndef LinearRegression_H
11 #define LinearRegression_H
12 
13 #include "FPUtil.h"
14 #include "FunctionApproximator.h"
15 #include <cassert>
16 
17 class updateData {
18 public:
19  updateData() :mean(0), S(0), totErr(0), n(0) {}
20  void reset() { mean = 0.0; S = 0.0; totErr = 0.0; n = 0; }
21  double mean;
22  double S;
23  double totErr;
24  int n;
25 };
26 
28 public:
29  LinearRegression(int _inputs, int _outputs, double learnrate);
31  LinearRegression(char *);
34 
35  void setUseBinary(bool binary) { useBinary = binary; }
36 
37  void resizeInputs(int newSize, double newVal);
38  void resizeInputs(int newSize);
39 
40  void save(const char *);
41  void save(FILE *);
42  void load(const char *);
43  void load(FILE *);
44  void load(const FunctionApproximator *fa) { load((LinearRegression*)fa); }
45  void load(const LinearRegression *);
46  static bool validSaveFile(char *fname);
47 
48  double train(std::vector<double> &input, std::vector<double> &output2);
49  double *test(const std::vector<double> &input);
50  double train(std::vector<unsigned int> &input, std::vector<double> &output2);
51  double *test(const std::vector<unsigned int> &input);
52 
53  void Print();
54 
55  int getNumInputs() { return inputs; }
56  void setInputWeight(double value, unsigned int weightNum, unsigned int whichOutput=0);
57  double getInputWeight(int inp, int outp=0) { return weight[outp][inp]; }
58  void getWeightUpdateVariance(std::vector<double> &var, unsigned int which=0);
59  double getWeightUpdateVariance(unsigned int weightNum, unsigned int whichOutput=0);
60 
61  void getWeightUpdateAverage(std::vector<double> &var, unsigned int which=0);
62  double getWeightUpdateAverage(unsigned int weightNum, unsigned int whichOutput=0);
63 
64  void getWeightUpdateSum(std::vector<double> &var, unsigned int which=0);
65  double getWeightUpdateSum(unsigned int weightNum, unsigned int whichOutput=0);
66  void resetWeightVariance(unsigned int weightNum, unsigned int whichOutput=0);
67  void resetWeightVariance();
68 
69  int getWeightFrequency(unsigned int weightNum, unsigned int whichOutput=0);
70 
71  double GetInput(std::vector<double> &input, const std::vector<double> &target)
72  { assert(!"Unimplemented"); }
73 
74 private:
75  void allocateMemory();
76  void freeMemory();
77 
78  double g(double a);
79  double dg(double a);
80  double outputerr(std::vector<double> &output, std::vector<double> &expected, int which);
81 
82  double error(double* output);
83  std::vector<std::vector<double> > weight;
84  std::vector<std::vector<updateData> > updates;
85  //std::vector<double > weight;
86  std::vector<double> output;
87  //double** weight;
88  //double* output;
90  bool useBinary;
91 };
92 
93 #endif
LinearRegression::setInputWeight
void setInputWeight(double value, unsigned int weightNum, unsigned int whichOutput=0)
Definition: LinearRegression.cpp:501
LinearRegression::getWeightUpdateSum
void getWeightUpdateSum(std::vector< double > &var, unsigned int which=0)
Definition: LinearRegression.cpp:462
LinearRegression::Print
void Print()
Definition: LinearRegression.cpp:507
LinearRegression::validSaveFile
static bool validSaveFile(char *fname)
Definition: LinearRegression.cpp:265
LinearRegression::getWeightUpdateVariance
void getWeightUpdateVariance(std::vector< double > &var, unsigned int which=0)
Definition: LinearRegression.cpp:412
LinearRegression::test
double * test(const std::vector< double > &input)
Definition: LinearRegression.cpp:399
LinearRegression::resetWeightVariance
void resetWeightVariance()
Definition: LinearRegression.cpp:485
LinearRegression::error
double error(double *output)
FunctionApproximator.h
LinearRegression::dg
double dg(double a)
updateData::totErr
double totErr
Definition: LinearRegression.h:23
LinearRegression::useBinary
bool useBinary
Definition: LinearRegression.h:90
FPUtil.h
updateData::n
int n
Definition: LinearRegression.h:24
LinearRegression::LinearRegression
LinearRegression(int _inputs, int _outputs, double learnrate)
Definition: LinearRegression.cpp:23
LinearRegression::load
void load(const char *)
Definition: LinearRegression.cpp:168
LinearRegression::setUseBinary
void setUseBinary(bool binary)
Definition: LinearRegression.h:35
updateData::updateData
updateData()
Definition: LinearRegression.h:19
LinearRegression::resizeInputs
void resizeInputs(int newSize, double newVal)
Definition: LinearRegression.cpp:80
FunctionApproximator
Definition: FunctionApproximator.h:25
LinearRegression
Definition: LinearRegression.h:27
LinearRegression::outputs
int outputs
Definition: LinearRegression.h:89
LinearRegression::getInputWeight
double getInputWeight(int inp, int outp=0)
Definition: LinearRegression.h:57
updateData::mean
double mean
Definition: LinearRegression.h:21
LinearRegression::getWeightUpdateAverage
void getWeightUpdateAverage(std::vector< double > &var, unsigned int which=0)
Definition: LinearRegression.cpp:437
LinearRegression::g
double g(double a)
LinearRegression::weight
std::vector< std::vector< double > > weight
Definition: LinearRegression.h:83
LinearRegression::allocateMemory
void allocateMemory()
Definition: LinearRegression.cpp:138
LinearRegression::getNumInputs
int getNumInputs()
Definition: LinearRegression.h:55
LinearRegression::freeMemory
void freeMemory()
Definition: LinearRegression.cpp:154
LinearRegression::outputerr
double outputerr(std::vector< double > &output, std::vector< double > &expected, int which)
Definition: LinearRegression.cpp:328
LinearRegression::GetInput
double GetInput(std::vector< double > &input, const std::vector< double > &target)
Definition: LinearRegression.h:71
updateData::S
double S
Definition: LinearRegression.h:22
updateData::reset
void reset()
Definition: LinearRegression.h:20
LinearRegression::updates
std::vector< std::vector< updateData > > updates
Definition: LinearRegression.h:84
updateData
Definition: LinearRegression.h:17
LinearRegression::~LinearRegression
~LinearRegression()
Definition: LinearRegression.cpp:75
LinearRegression::load
void load(const FunctionApproximator *fa)
Definition: LinearRegression.h:44
LinearRegression::output
std::vector< double > output
Definition: LinearRegression.h:86
LinearRegression::train
double train(std::vector< double > &input, std::vector< double > &output2)
Definition: LinearRegression.cpp:360
LinearRegression::inputs
int inputs
Definition: LinearRegression.h:89
LinearRegression::save
void save(const char *)
Definition: LinearRegression.cpp:288
LinearRegression::getWeightFrequency
int getWeightFrequency(unsigned int weightNum, unsigned int whichOutput=0)
Definition: LinearRegression.cpp:496