This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cc
161 lines (140 loc) · 5.05 KB
/
main.cc
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
// Copyright PinaPL
//
// main.cc
// PinaPL
//
#include <eigen3/Eigen/Dense>
#include <string>
#include <fstream>
#include <stdexcept>
// #include "neuronLayer/weightsNeuron.hh"
// #include "neuronLayer/networkNeuron.hh"
// #include "neuronLayer/layer.hh"
#include "functions.hh"
#include "iostream"
#include "test.hh"
#include "lstmCell/weightsLSTM.hh"
#include "lstmCell/networkLSTM.hh"
#include "lstmCell/cell.hh"
#include "sys/time.h"
int main(int argc, char **argv) {
/*
//
// TESTS LAYER & PROPAGATION
//
int input_size = 5;
int output_size = 20;
Weights* weights = new Weights(input_size, output_size);
Layer* layer = new Layer(weights);
Eigen::VectorXd input(input_size);
Eigen::VectorXd previous_outputs(output_size);
input << 2, 1, -1, 0, 0;
previous_outputs << 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1;
Eigen::VectorXd out = layer->compute(input, previous_outputs);
std::cout << out << '\n';
std::cout << "=======================================" << '\n';
Eigen::VectorXd expected_output(output_size);
expected_output << 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0;
Eigen::VectorXd grad = layer->compute_gradient((expected_output - previous_outputs).cwiseProduct(expected_output - previous_outputs));
std::cout << grad << '\n';
std::cout << weights->delta_weight_input << '\n';
std::cout << "applying gradient" << '\n';
weights->apply_gradient(0.1);
std::cout << "test cost function" << '\n';
Eigen::VectorXd test = costfunction(input, previous_outputs);
std::cout << test << '\n';
std::cout << "creating network" << '\n';
Network network = Network(weights, 5, 5, 20);
std::vector<Eigen::VectorXd> inputs;
inputs.push_back(input);
inputs.push_back(input);
inputs.push_back(input);
std::cout << "starting propagation" << '\n';
std::vector<Eigen::VectorXd> propagation = network.propagate(inputs);
std::cout << "propagation complete" << '\n';
std::cout << propagation.at(0) << '\n';
std::cout << propagation.at(1) << '\n';
std::cout << propagation.at(2) << '\n';
std::cout << "starting backpropagation" << '\n';
std::vector<Eigen::VectorXd> expected_outputs;
expected_outputs.push_back(expected_output);
expected_outputs.push_back(expected_output);
expected_outputs.push_back(expected_output);
network.backpropagate(expected_outputs);
std::cout << "backpropagation complete" << '\n';
*/
/*
//
// TESTS COMPARE
//
Eigen::VectorXd v1(20);
v1 << 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0;
Eigen::VectorXd v2(20);
v2 << 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1;
std::vector<Eigen::VectorXd> va;
std::vector<Eigen::VectorXd> vb;
va.push_back(v1);
va.push_back(v1);
va.push_back(v2);
va.push_back(v1);
vb.push_back(v2);
vb.push_back(v2);
vb.push_back(v2);
vb.push_back(v2);
std::cout << "va,va" << '\n';
std::cout << compare_double(va, va) << '\n';
std::cout << "va,vb" << '\n';
std::cout << compare_double(va, vb) << '\n';
*/
/*
//
// TESTS LSTM CELL
//
WeightsLSTM* weightsLSTM = new WeightsLSTM(7, 10);
Cell* cell = new Cell(weightsLSTM);
Eigen::VectorXd previous_memory = Eigen::VectorXd::Zero(10);
Eigen::VectorXd previous_output = Eigen::VectorXd::Zero(10);
Eigen::VectorXd input(7);
input << 1, 0, 1, 0, 1, 0, 1;
std::vector<Eigen::VectorXd> result = cell->compute(previous_output, previous_memory, input);
std::cout << "===== Cell_out =====" << '\n';
std::cout << result.at(0) << '\n';
std::cout << "===== Cell state =====" << '\n';
std::cout << result.at(1) << '\n';
std::vector<Eigen::VectorXd> result2 = cell->compute(result.at(0), result.at(1), input);
Eigen::VectorXd deltas(10);
Eigen::VectorXd previous_delta_cell_in(10);
Eigen::VectorXd previous_delta_cell_state(10);
std::vector<Eigen::VectorXd> grad = cell->compute_gradient(deltas, previous_delta_cell_in,
previous_delta_cell_state);
std::cout << "===== delta_input ======" << '\n';
std::cout << grad.at(0) << '\n';
std::cout << "====== delta_cell_state ======" << '\n';
std::cout << grad.at(1) << '\n';
*/
//
// TESTS LSTM NETWORK
//
/*
WeightsLSTM* weightsLSTM = new WeightsLSTM(7, 10);
NetworkLSTM* networkLSTM = new NetworkLSTM(weightsLSTM, 7, 5, 10);
Eigen::VectorXd input(7);
input << 1, 0, 1, 0, 1, 0, 1;
std::vector<Eigen::VectorXd> inputs;
inputs.push_back(input);
inputs.push_back(input);
std::vector<Eigen::VectorXd> propagation = networkLSTM->propagate(inputs);
std::cout << "Propagation complete" << '\n';
std::cout << "propagate[0]" << '\n';
std::cout << propagation.at(0) << '\n';
std::cout << "propagate[1]" << '\n';
std::cout << propagation.at(1) << '\n';
*/
timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
// time seed
srand((uint64_t)ts.tv_nsec);
grammar_learn(true);
// grammar_learn_LSTM(false);
return 0;
}