-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
174 lines (143 loc) · 5.28 KB
/
main.cpp
File metadata and controls
174 lines (143 loc) · 5.28 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
*
* A program to test a Time Delay Neural Network
* Author: Brandon Trabucco
* Date: 2016/07/27
*
*/
#include "TimeDelayNetwork.h"
#include "DatasetAdapter.h"
#include "OutputTarget.h"
#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
long long getMSec() {
struct timeval tp;
gettimeofday(&tp, NULL);
return tp.tv_sec * 1000 + tp.tv_usec / 1000;
}
struct tm *getDate() {
time_t t = time(NULL);
struct tm *timeObject = localtime(&t);
return timeObject;
}
int main(int argc, char *argv[]) {
cout << "Program initializing" << endl;
if (argc < 4) {
cout << argv[0] << " <learning rate> <decay rate> <window> <size ...>" << endl;
return -1;
}
int frameWindow = atoi(argv[3]);
int updatePoints = 100;
int savePoints = 10;
int maxEpoch = 100;
int trainingSize = 500;
int sumNeurons = 0;
double errorBound = 0.01;
double mse = 0;
double learningRate = atof(argv[1]), decayRate = atof(argv[2]);
long long networkStart, networkEnd, sumTime = 0, iterationStart;
const int _day = getDate()->tm_mday;
/**
*
* Open file streams to save data samples from Neural Network
* This data can be plotted via GNUPlot
*
*/
ostringstream errorDataFileName;
errorDataFileName << "/u/trabucco/Desktop/Sequential_Convergence_Data_Files/" <<
(getDate()->tm_year + 1900) << "-" << (getDate()->tm_mon + 1) << "-" << _day <<
"_Multicore-TDNN-Error_" << learningRate <<
"-learning_" << decayRate << "-decay.csv";
ofstream errorData(errorDataFileName.str(), ios::app);
if (!errorData.is_open()) return -1;
ostringstream timingDataFileName;
timingDataFileName << "/u/trabucco/Desktop/Sequential_Convergence_Data_Files/" <<
(getDate()->tm_year + 1900) << "-" << (getDate()->tm_mon + 1) << "-" << _day <<
"_Multicore-TDNN-Timing_" << learningRate <<
"-learning_" << decayRate << "-decay.csv";
ofstream timingData(timingDataFileName.str(), ios::app);
if (!timingData.is_open()) return -1;
ostringstream accuracyDataFileName;
accuracyDataFileName << "/u/trabucco/Desktop/Sequential_Convergence_Data_Files/" <<
(getDate()->tm_year + 1900) << "-" << (getDate()->tm_mon + 1) << "-" << _day <<
"_Multicore-TDNN-Accuracy_" << learningRate <<
"-learning_" << decayRate << "-decay.csv";
ofstream accuracyData(accuracyDataFileName.str(), ios::app);
if (!accuracyData.is_open()) return -1;
ostringstream outputDataFileName;
outputDataFileName << "/u/trabucco/Desktop/Sequential_Convergence_Data_Files/" <<
(getDate()->tm_year + 1900) << "-" << (getDate()->tm_mon + 1) << "-" << _day <<
"_Multicore-TDNN-Output_" << learningRate <<
"-learning_" << decayRate << "-decay.csv";
ofstream outputData(outputDataFileName.str(), ios::app);
if (!outputData.is_open()) return -1;
outputData << endl << endl;
networkStart = getMSec();
DatasetAdapter dataset = DatasetAdapter();
networkEnd = getMSec();
cout << "Language Dataset loaded in " << (networkEnd - networkStart) << "msecs" << endl;
TimeDelayNetwork network = TimeDelayNetwork(dataset.getCharSize(), frameWindow, learningRate, decayRate);
OutputTarget target = OutputTarget(dataset.getCharSize(), dataset.getCharSize());
for (int i = 0; i < (argc - 4); i++) {
network.addLayer(atoi(argv[4 + i]));
sumNeurons += atoi(argv[4 + i]);
} network.addLayer(dataset.getCharSize());
int totalIterations = 0;
bool converged = false;
for (int e = 0; (e < maxEpoch)/* && (!e || (((mse1 + mse2)/2) > errorBound))*/; e++) {
int c = 0, n = 0;
vector<double> error, output;
networkStart = getMSec();
for (int i = 0; i < trainingSize && dataset.nextChar(); i++) {
DatasetExample data = dataset.getChar();
network.pushTimeStep(target.getOutputFromTarget(data.current));
error = network.train(target.getOutputFromTarget(data.next));
}
network.clearTimeSteps();
dataset.reset();
for (int i = 0; i < trainingSize && dataset.nextChar(); i++) {
DatasetExample data = dataset.getChar();
network.pushTimeStep(target.getOutputFromTarget(data.current));
output = network.classify();
n++;
if (target.getTargetFromOutput(output) == (int)data.next) c++;
} networkEnd = getMSec();
sumTime += (networkEnd - networkStart);
totalIterations += 1;
mse = 0;
for (int i = 0; i < error.size(); i++)
mse += error[i] * error[i];
mse /= error.size() * 2;
if (((e + 1) % (maxEpoch / updatePoints)) == 0) {
cout << "Epoch " << e << " completed in " << (networkEnd - networkStart) << "msecs" << endl;
cout << "Error[" << e << "] = " << mse << endl;
cout << "Accuracy[" << e << "] = " << (100.0 * (float)c / (float)n) << endl << endl;
} errorData << e << ", " << mse << endl;
accuracyData << e << ", " << (100.0 * (float)c / (float)n) << endl;
dataset.reset();
}
network.clearTimeSteps();
vector<vector<double> > seed;
seed.push_back(target.getOutputFromTarget((int)'I'));
for (int i = 0; i < 500; i++) {
network.pushTimeStep(seed[i]);
vector<double> output = network.classify();
seed.push_back(output);
char text = (char)target.getTargetFromOutput(output);
outputData << text;
}
timingData << sumNeurons << ", " << sumTime << ", " << totalIterations << endl;
timingData.close();
errorData.close();
accuracyData.close();
outputData.close();
cout << "Program finished" << endl;
return 0;
}