-
Notifications
You must be signed in to change notification settings - Fork 1
/
MotionLearn.cpp
129 lines (108 loc) · 3.52 KB
/
MotionLearn.cpp
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
// MotionLearn.cpp : Defines the entry point for the console application.
#include "MIO.h"
#include "lib/Eigen/Core"
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
/**
* prototypes
**/
static void ShowUsage(void);
static void CheckOption(char *option, int argc, int minargc);
bool verbose = false;
int main(int argc, char *argv[]) {
// first argument is program name
argv++, argc--;
// look for help
for (int i = 0; i < argc; i++) {
if (!strcmp(argv[i], "-help")) {
ShowUsage();
}
}
// apply options
for (int i = 0; i < argc; i++) {
if (!strcmp(*argv, "-v")) {
verbose = true;
argv += 1, argc -= 1;
}
}
// no argument case
if (argc == 0) {
ShowUsage();
}
// parse arguments
while (argc > 0) {
if (**argv == '-') {
if (!strcmp(*argv, "-loadData")) {
int numopts = 1;
// numopts+1 because parameter name itself counts
CheckOption(*argv, argc, numopts + 1);
// TODO: call network function
std::cout << "Attempting to read file " << argv[1] << std::endl;
Eigen::MatrixXd rawData = matrixFromFile(argv[1], 0, ',');
std::cout << "Read in " << rawData.rows() << "x" << rawData.cols()
<< "raw file " << std::endl;
Eigen::MatrixXd data = rawData.topRightCorner(100, 784);
std::cout << "Read in " << data.rows() << " x " << data.cols()
<< "matrix." << std::endl;
argv += numopts + 1, argc -= numopts + 1;
int nHidden = 100;
int nClasses = 10;
Eigen::MatrixXd inputToHidden = Eigen::MatrixXd::Random(784, nHidden);
Eigen::MatrixXd hiddenToOutput =
Eigen::MatrixXd::Random(nHidden, nClasses);
// for first 10 rows
for (int i = 0; i < 5; i++) {
Eigen::VectorXd hiddenLayer = (data.row(i) * inputToHidden);
// apply RELU
for (int j = 0; j < hiddenLayer.cols(); j++) {
hiddenLayer[j] = hiddenLayer[j] < 0 ? 0 : hiddenLayer[j];
}
Eigen::VectorXd output = (hiddenLayer.transpose() * hiddenToOutput);
output =
output - Eigen::VectorXd::Ones(output.size()) * output.maxCoeff();
softmax(output);
// prediction is whatever class has max value
std::cout << "row " << i << " prediction:" << std::endl;
std::cout << output.transpose() << std::endl;
std::cout << "ground truth: " << rawData(i, 0) << std::endl;
}
} else if (!strcmp(*argv, "-forwardProp")) {
int numopts = 0;
// numopts+1 because parameter name itself counts
CheckOption(*argv, argc, numopts + 1);
// TODO: call network function
argv += numopts + 1, argc -= numopts + 1;
} else {
fprintf(stderr, "invalid option: %s\n", *argv);
ShowUsage();
}
} else {
fprintf(stderr, "DeepNav: invalid option (2): %s\n", *argv);
ShowUsage();
}
}
return EXIT_SUCCESS;
}
/**
* ShowUsage
**/
static char options[] = "-help (show this message)\n"
"-v verbose output\n"
"- forwardProp\n";
static void ShowUsage(void) {
fprintf(stderr, "Usage: DeepNav [-option [arg ...] ...] -output \n");
fprintf(stderr, "%s", options);
exit(EXIT_FAILURE);
}
/**
* CheckOption
**/
static void CheckOption(char *option, int argc, int minargc) {
if (argc < minargc) {
fprintf(stderr, "Too few arguments for %s, expected %d, received %d\n",
option, minargc, argc);
ShowUsage();
}
}