-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNNParam.cpp
199 lines (157 loc) · 5.46 KB
/
RNNParam.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include<cstdio>
#include<string>
#include<unordered_map>
#include<iostream>
#include "RNNParam.h"
#include "MatrixOperations.h"
using namespace std;
#define NUMBER_OF_CLASSES 2
#define WORD_SIZE_VEC 30
RNNParam::RNNParam() {
sentimentWeightsMatrix.clear();
biasSentimentMatrix = getZeros(NUMBER_OF_CLASSES);
weightsMatrix.clear();
vector<double> aux = getZeros(2 * WORD_SIZE_VEC);
for(int i = 0; i < WORD_SIZE_VEC; i++) weightsMatrix.push_back(aux);
vector<double> aux2 = getZeros(WORD_SIZE_VEC);
sentimentWeightsMatrix.push_back(aux2);
sentimentWeightsMatrix.push_back(aux2);
biasWeightMatrix = aux2;
vocabError.clear();
totalError = 0;
}
RNNParam::RNNParam(int dimesion) {
sentimentWeightsMatrix.clear();
weightsMatrix.clear();
biasSentimentMatrix = getZeros(NUMBER_OF_CLASSES);
// Set up weights matrix.
vector<double> aux = getZeros(dimesion * 2);
for(int i = 0; i < dimesion; i++) weightsMatrix.push_back(aux);
// Set up score matrix.
vector<double> aux2 = getZeros(dimesion);
for(int i = 0; i < dimesion; i++) {
sentimentWeightsMatrix.push_back(aux2);
}
biasWeightMatrix = aux2;
vocabError.clear();
totalError = 0;
}
void RNNParam::resetFields() {
sentimentWeightsMatrix.clear();
weightsMatrix.clear();
biasSentimentMatrix = getZeros(NUMBER_OF_CLASSES);
vector<double> aux = getZeros(2 * WORD_SIZE_VEC);
for(int i = 0; i < WORD_SIZE_VEC; i++) weightsMatrix.push_back(aux);
vector<double> aux2 = getZeros(WORD_SIZE_VEC);
sentimentWeightsMatrix.push_back(aux2);
sentimentWeightsMatrix.push_back(aux2);
biasWeightMatrix = aux2;
vocabError.clear();
totalError = 0;
}
void RNNParam::resetVocab() {
vocabError.clear();
}
void RNNParam::setSentimentWeightsMatrix(vector<vector<double>> x) {
this->sentimentWeightsMatrix = x;
}
void RNNParam::setBiasSentimentMatrix(vector<double> x) {
this->biasSentimentMatrix = x;
}
void RNNParam::setBiasWeightMatrix(vector<double> x) {
this->biasWeightMatrix = x;
}
void RNNParam::updateBiasSentimentMatrix(vector<double> x) {
if (x.size() != biasSentimentMatrix.size()) {
cout<<"Couldn't update the biasSentimentMatrix because the given input has different dimension."<<endl;
}
for(int i = 0; i < biasSentimentMatrix.size(); i++) {
biasSentimentMatrix[i] += x[i];
}
}
void RNNParam::updateBiasWeightMatrix(vector<double> x) {
if (x.size() != biasWeightMatrix.size()) {
cout<<"Couldn't update the updateBiasWeightMatrix because the given input has different dimension."<<endl;
}
for(int i = 0; i < biasWeightMatrix.size(); i++) {
biasWeightMatrix[i] += x[i];
}
}
void RNNParam::setTotalError(double x) {
this->totalError = x;
}
void RNNParam::updateSentimentWeightsMatrix(vector<vector<double>> x) {
if (x.size() != sentimentWeightsMatrix.size() || x[0].size() != sentimentWeightsMatrix[0].size()) {
cout<<"Sentiment matrix couldn't be updated due to different dimensions. (updateSentimentWeightsMatrix())."<<endl;
// exit(0);
}
for(int i = 0; i < x.size(); i++) {
for(int j = 0; j < x[0].size(); j++) {
sentimentWeightsMatrix[i][j] += x[i][j];
}
}
}
void RNNParam::setWeightsMatrix(vector<vector<double>> x) {
this->weightsMatrix = x;
}
void RNNParam::updateWeightsMatrix(vector<vector<double>> x) {
if (x.size() != weightsMatrix.size() ||x[0].size() != weightsMatrix[0].size()) {
cout<<"Sentiment matrix couldn't be updated due to different dimensions. (updateWeightsMatrix())."<<endl;
// exit(0);
}
for(int i = 0; i < x.size(); i++) {
for(int j = 0; j < x[0].size(); j++) {
this->weightsMatrix[i][j] += x[i][j];
}
}
}
RNNParam::~RNNParam(){
}
// Search for the word in the map. If an entry has already been created,
// add the error to this word.
void RNNParam::updateVocabError(string word, vector<double> error) {
unordered_map<string, vector<double>>::const_iterator found_iter = vocabError.find(word);
if (found_iter == vocabError.end()) {
vocabError.insert(make_pair(word, error));
return;
}
vector<double> aux = vocabError[word];
for(int i = 0; i < error.size(); i++) {
aux[i] += error[i];
}
vocabError[word] = aux;
}
void RNNParam::updateVocabError(unordered_map<string, vector<double>> left, unordered_map<string, vector<double>> right) {
for (auto it = left.begin(); it != left.end(); ++it ) {
this->updateVocabError(it->first, it->second);
}
for (auto it = right.begin(); it != right.end(); ++it ) {
this->updateVocabError(it->first, it->second);
}
}
void RNNParam::updateVocabError(unordered_map<string, vector<double>> left) {
for (auto it = left.begin(); it != left.end(); ++it ) {
this->updateVocabError(it->first, it->second);
}
}
void RNNParam::updateTotalError(double x) {
this->totalError += x;
}
vector<vector<double>> RNNParam::getSentimentWeightsMatrix() {
return this->sentimentWeightsMatrix;
}
vector<vector<double>> RNNParam::getWeightsMatrix() {
return this->weightsMatrix;
}
double RNNParam::getTotalError() {
return this->totalError;
}
vector<double> RNNParam::getBiasSentimentMatrix() {
return this->biasSentimentMatrix;
}
vector<double> RNNParam::getBiasWeightMatrix() {
return this->biasWeightMatrix;
}
unordered_map<string, vector<double>> RNNParam::getVocabError() {
return this->vocabError;
}