-
Notifications
You must be signed in to change notification settings - Fork 0
/
computationalModel.cpp
144 lines (124 loc) · 5.46 KB
/
computationalModel.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "computationalModel.h"
#include "layers.h"
#include "computationalNode.h"
#include <iostream>
#include "mathFunc.h"
#include <tuple>
#include "orderedData.h"
#include "weights.h"
#include "architecture.h"
using namespace std;
void computationalModel::AddNode(int from, int to, computationalNode* node){
computationList.push_back(std::make_tuple(from, to, node));
}
void computationalModel::SetModel(architecture * arch, layers* layersData, layers* deltas, weights* weightsData, weights* gradient,
activityLayers* layersActivity, bool primalWeightOwner){
Nlayers = arch->Nlayers;
for(int j=0; j<arch->Nnodes; ++j){
AddNode(arch->from[j], arch->to[j], arch->computation_list[j]->New());
}
this->Compile(layersData, deltas, weightsData, gradient, layersActivity, primalWeightOwner);
}
void computationalModel::SetModel(architecture * arch, layers* layersData, layers* deltas, weights* weightsData, weights* gradient,
activityLayers* layersActivity, bool primalWeightOwner, computationalModel * primalCM){
this->SetModel(arch, layersData, deltas, weightsData, gradient, layersActivity, primalWeightOwner);
computationalNode * node;
for(unsigned int j=0; j<computationList.size(); ++j){
node = get<2>(computationList[j]);
if (node->NeedsUnification())
node->Unify(get<2>(primalCM->computationList[j]));
}
}
void computationalModel::SetModel(layers* layersData, layers* deltas, weights* weightsData, weights* gradient, activityLayers* layersActivity, bool primalWeightOwner){
Nlayers=layersData->Nlayers;
AddNode(0, 0, new StairsFullBottleneckBalancedDrop(0, 1, 3, 10, 10, 10));
AddNode(0, 1, new FullAveragePoolingBalancedDrop());
AddNode(1, 1, new StairsFullBottleneckBalancedDrop(2, 3, 203, 10, 10, 10));
AddNode(1, 2, new FullAveragePoolingBalancedDrop());
AddNode(2, 2, new StairsFullBottleneckBalancedDrop(4, 5, 403, 10, 10, 10));
AddNode(2, 3, new FullAveragePoolingBalancedDrop());
AddNode(3, 4, new FullyConnectedSoftMax(6));
this->Compile(layersData, deltas, weightsData, gradient, layersActivity, primalWeightOwner);
}
void computationalModel::SetModel(layers* layersData, layers* deltas, weights* weightsData, weights* gradient,
activityLayers* layersActivity, bool primalWeightOwner, computationalModel * primalCM){
this->SetModel(layersData, deltas, weightsData, gradient, layersActivity, primalWeightOwner);
computationalNode * node;
for(unsigned int j=0; j<computationList.size(); ++j){
node = get<2>(computationList[j]);
if (node->NeedsUnification())
node->Unify(get<2>(primalCM->computationList[j]));
}
}
void computationalModel::UpdateBalancedDropParameters(float alpha_, float pDrop_, float pNotDrop_){
computationalNode * node;
for(unsigned int j=0; j<computationList.size(); ++j){
node = get<2>(computationList[j]);
if (node->UsesBalancedDrop())
node->UpdateBalancedDropParameters(alpha_, pDrop_, pNotDrop_);
}
}
void computationalModel::Compile(layers* layersData, layers* deltas, weights* weightsData, weights* gradient, activityLayers* layersActivity, bool primalWeightOwner){
hasBottomWeightDependency.resize(Nlayers, 0);
computationalNode * node;
int from, to;
for(unsigned int j=0; j<computationList.size(); ++j){
from = get<0>(computationList[j]);
to = get<1>(computationList[j]);
node = get<2>(computationList[j]);
if (node->HasWeightsDependency() || hasBottomWeightDependency[from])
hasBottomWeightDependency[to] = 1;
node->Initiate(layersData, deltas, weightsData, gradient, layersActivity, from, to, primalWeightOwner);
}
}
//assuming input is set
void computationalModel::ForwardPass(){
computationalNode* node;
for(unsigned int j=0; j<computationList.size(); ++j){
node = get<2>(computationList[j]);
node->ForwardPass();
}
}
//assuming last layer deltas are set
void computationalModel::BackwardPass(int trueClass){
computationalNode* node;
int from;
for(int j=computationList.size()-1; j>=0; --j){
from = get<0>(computationList[j]);
node = get<2>(computationList[j]);
node->BackwardPass(hasBottomWeightDependency[from], trueClass);
}
}
void computationalModel::WriteCoefficientsToFile(){
computationalNode* node;
for(unsigned int j=0; j<computationList.size(); ++j){
node = get<2>(computationList[j]);
if (node->HasWeightsDependency())
node->WriteStructuredWeightsToFile();
}
// for(vector<pair<int, int> >::iterator it=computationList.begin(); it!=computationList.end(); ++it){
// if (computationTable[it->first][it->second]->HasWeightsDependency())
// computationTable[it->first][it->second]->WriteStructuredWeightsToFile();
// }
}
void computationalModel::SetToTrainingMode(){
computationalNode* node;
for(unsigned int j=0; j<computationList.size(); ++j){
node = get<2>(computationList[j]);
node->SetToTrainingMode();
}
}
void computationalModel::SetToTestMode(){
computationalNode* node;
for(unsigned int j=0; j<computationList.size(); ++j){
node = get<2>(computationList[j]);
node->SetToTestMode();
}
}
computationalModel::~computationalModel(){
computationalNode* node;
for(unsigned int j=0; j<computationList.size(); ++j){
node = get<2>(computationList[j]);
delete node;
}
}