This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathLinear.h
executable file
·174 lines (138 loc) · 4.81 KB
/
Linear.h
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
/*
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/
#ifndef _LINEAR_
#define _LINEAR_
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <vector>
#include "common.h"
#include "Vec.h"
#include "utils.h"
namespace rnn {
// Linear struct:
struct Linear{
public:
/*** Constructors ***/
Linear(){};
explicit Linear(const my_int& si, const my_int& so) :
_data(so, si, 0),
_gradient(so, si, 0) {};
Linear(const Linear& rhs) {
this->_data = rhs._data;
this->_gradient = rhs._gradient;
this->_gradient.zeros();
}
/*** methods ***/
my_int ncol() const {
return this->_data.ncol();
}
my_int nrow() const {
return this->_data.nrow();
}
void initialize(){
for(my_int i = 0; i < _data.size(); i++)
_data[i] = random(-0.1, 0.1)+random(-0.1, 0.1)+random(-0.1, 0.1);
}
my_int size() { return this->_data.size();}
my_int sizeIn() { return this->_data.ncol();}
my_int sizeOut() { return this->_data.nrow();}
void zeros(){ this->_data.zeros();};
/*** forward methods ***/
void forward(const my_int& idx, Vec& out){
assert(out.size() == _data.nrow());
for(my_int x = 0; x < out.size(); x++)
out[x] += _data(x, idx);
};
void forward_transpose(const my_int& idx, Vec& out){
assert(out.size() == _data.ncol());
for(my_int x = 0; x < out.size(); x++)
out[x] += _data(idx, x);
};
void forward_transpose(const my_int& idx, Vec& out,
const my_int& obegin, const my_int& oend){
assert(out.size() == _data.ncol());
for(my_int x = obegin; x < oend; x++)
out[x] += _data(idx, x);
};
void forward(const Vec& in, Vec& out,
const my_int& ibegin, const my_int& iend,
const my_int& obegin, const my_int& oend){
assert(obegin >= 0);
assert(oend <= _data.nrow());
assert(oend <= out.size());
assert(ibegin >= 0);
assert(iend <= _data.ncol());
assert(iend <= in.size());
matrixXvector(out, in, this->_data, obegin, oend, ibegin, iend, 0);
}
void forward(const Vec& in, Vec& out){
matrixXvector(out, in, this->_data, 0, out.size(), 0, in.size(), 0);
};
/*** backward methods ***/
void backward(Vec& in, const Vec& out){
matrixXvector(in, out, this->_data, 0, out.size(), 0, in.size(), 1);
};
void backward(Vec& in, const Vec& out,
const my_int& ibegin, const my_int& iend,
const my_int& obegin, const my_int& oend){
assert(obegin >= 0);
assert(oend <= _data.nrow());
assert(oend <= out.size());
assert(ibegin >= 0);
assert(iend <= _data.ncol());
assert(iend <= in.size());
matrixXvector(in, out, this->_data, obegin, oend, ibegin, iend, 1);
}
/*** gradient methods ***/
void resetGradient(){
this->_gradient.zeros();
};
void computeGradient(const my_int& idx ,const Vec& out){
for(my_int i = 0; i < _gradient.nrow(); i++)
_gradient(i, idx) += out[i]; // gradient += out * in';
};
void computeGradient_transpose(const my_int& idx ,const Vec& out){
for(my_int i = 0; i < _gradient.ncol(); i++)
_gradient(idx,i) += out[i]; // gradient += out * in';
};
void computeGradient_transpose(const my_int& idx ,const Vec& out,
const my_int& obegin, const my_int& oend){
for(my_int i = obegin; i < oend; i++)
_gradient(idx,i) += out[i]; // gradient += out * in';
};
void computeGradient(const Vec& in ,const Vec& out){
computeGradient(in, out, 0, in.size(), 0, out.size());
};
void computeGradient(const Vec& in, const Vec& out,
const my_int& ibegin, const my_int& iend,
const my_int& obegin, const my_int& oend){
assert(obegin >= 0);
assert(oend <= _gradient.nrow());
assert(oend <= out.size());
assert(ibegin >= 0);
assert(iend <= _gradient.ncol());
assert(iend <= in.size());
for(my_int o = obegin; o < oend; o++){
for(my_int i = ibegin; i < iend; i++){
_gradient(o, i) += out[o] * in[i];
}
}
}
void update(const my_real& lr){
for(my_int i =0; i < this->size(); i++)
this->_data[i] += lr * this->_gradient[i];
}
// TODO make that private
Vec2D _data;
Vec2D _gradient;
};
}// end namespace rnn
#endif