-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMachSeq.cpp
271 lines (227 loc) · 8.06 KB
/
MachSeq.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* This file is part of the continuous space language and translation model toolkit
* for statistical machine translation and large vocabulary speech recognition.
*
* Copyright 2015, Holger Schwenk, LIUM, University of Le Mans, France
*
* The CSLM toolkit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3 as
* published by the Free Software Foundation
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
*
*/
using namespace std;
#include <iostream>
#include "Tools.h"
#include "MachSeq.h"
MachSeq::MachSeq()
: MachMulti()
{
debug0("*** constructor MachSeq\n");
}
MachSeq::MachSeq(const MachSeq &m)
: MachMulti(m)
{
debug0("*** copy constructor MachSeq\n");
}
MachSeq::~MachSeq()
{
debug1("*** destructor MachSeq %lx\n", (luint) this);
data_out=grad_in=NULL; // prevent delete[] by ~Mach()
}
MachSeq *MachSeq::Clone()
{
MachSeq *m = new MachSeq(*this);
if (m != NULL)
m->CloneSubmachs(*this);
return m;
}
// set pointer of input data
void MachSeq::SetDataIn(REAL *data)
{
data_in=data;
if (machs.size() > 0) machs[0]->SetDataIn(data);
}
// set pointer of output gradient
void MachSeq::SetGradOut(REAL *data)
{
grad_out=data;
if (machs.size() > 0) machs.back()->SetGradOut(data);
}
void MachSeq::MachAdd(Mach *new_mach)
{
if (machs.empty()) {
debug0("*** add first element to sequential machine\n");
machs.push_back(new_mach);
// think about freeing memory
idim=new_mach->GetIdim();
bsize=new_mach->GetBsize();
data_in=new_mach->GetDataIn();
grad_in=new_mach->GetGradIn();
}
else {
debug0("*** add new element to sequential machine\n");
Mach *last_mach=machs.back();
if (last_mach->GetOdim()!=new_mach->GetIdim()) {
cout << "Current sequential machine:" << endl; Info(false);
cout << "Newly added machine:" << endl; new_mach->Info(false);
Error("input dimension of new sequential machine does not match");
}
if (bsize!=new_mach->GetBsize()) {
cout << "Current sequential machine:" << endl; Info(false);
cout << "Newly added machine:" << endl; new_mach->Info(false);
Error("bunch size of new sequential machine does not match");
}
machs.push_back(new_mach);
// connect new last machine to the previous one
new_mach->SetDataIn(last_mach->GetDataOut());
last_mach->SetGradOut(new_mach->GetGradIn());
}
activ_forw.push_back(true);
activ_backw.push_back(true);
// connect last machine to the outside world
odim=new_mach->GetOdim();
data_out=new_mach->GetDataOut();
grad_out=new_mach->GetGradOut();
debug4("*** data_in=%p, grad_in=%p, data_out=%p, grad_out=%p\n", data_in, grad_in, data_out, grad_out);
}
Mach *MachSeq::MachDel()
{
if (machs.empty()) {
Error("impossible to delete element from sequential machine: is already empty");
}
Mach *del_mach=machs.back();
machs.pop_back();
if (machs.empty()) {
idim=odim=bsize=0;
data_in=data_out=grad_in=grad_out=NULL;
}
else {
Mach *last_mach=machs.back();
// connect new last machine to the outside world
odim=last_mach->GetOdim();
data_out=last_mach->GetDataOut();
grad_out=last_mach->GetGradOut();
}
activ_forw.pop_back();
activ_backw.pop_back();
return del_mach;
}
//-----------------------------------------------
// Insert a machine a an arbitrary position
//-----------------------------------------------
void MachSeq::MachInsert(Mach *new_mach, size_t pos)
{
if (machs.empty())
Error("MachSeq::MachInsert() can't insert machine into empty sequence");
if (pos<1 || pos>=machs.size())
ErrorN("MachSeq::MachInsert() position must be in [%d,%zu], %zu was requested\n",1,machs.size(),pos);
debug2("*** add new element at pos %lu to sequential machine with %lu\n",pos,machs.size());
Mach *prev_mach=machs[pos-1];
Mach *next_mach=machs[pos];
debug2("prev=%p, next=%p\n",prev_mach,next_mach);
if (prev_mach->GetOdim()!=new_mach->GetIdim()) {
cout << "Current sequential machine:" << endl; Info(false);
cout << "Newly added machine:" << endl; new_mach->Info(false);
Error("input dimension of new sequential machine does not match");
}
if (next_mach->GetIdim()!=new_mach->GetOdim()) {
cout << "Current sequential machine:" << endl; Info(false);
cout << "Newly added machine:" << endl; new_mach->Info(false);
Error("output dimension of new sequential machine does not match");
}
if (bsize!=new_mach->GetBsize()) {
cout << "Current sequential machine:" << endl; Info(false);
cout << "Newly added machine:" << endl; new_mach->Info(false);
Error("bunch size of new sequential machine does not match");
}
machs.insert(machs.begin()+pos,new_mach);
// connect new machine to the previous one
new_mach->SetDataIn(prev_mach->GetDataOut());
prev_mach->SetGradOut(new_mach->GetGradIn());
// connect new machine to the next one
next_mach->SetDataIn(new_mach->GetDataOut());
new_mach->SetGradOut(next_mach->GetGradIn());
activ_forw.insert(activ_forw.begin()+pos,true);
activ_backw.insert(activ_backw.begin()+pos,true);
}
//-----------------------------------------------
// File input
//-----------------------------------------------
void MachSeq::ReadData(istream &inpf, size_t s, int bs)
{
debug0("*** read data of MachSeq\n");
MachMulti::ReadData(inpf, s, bs);
debug0("*** rebuild data structures\n");
int nbm=machs.size();
idim = machs[0]->GetIdim();
bsize = machs[0]->GetBsize();
odim = machs[nbm-1]->GetOdim();
// connect first to the outside world
data_in=machs[0]->GetDataIn();
grad_in=machs[0]->GetGradIn();
// forward chain the data
for (int m=1; m<nbm; m++) machs[m]->SetDataIn(machs[m-1]->GetDataOut());
// backward chain the gradients
for (int m=nbm-1; m>0; m--) machs[m-1]->SetGradOut(machs[m]->GetGradIn());
// connect last machine to the outside world
data_out=machs[nbm-1]->GetDataOut();
grad_out=machs[nbm-1]->GetGradOut();
}
//
// Tools
//
void MachSeq::Info(bool detailed, char *txt)
{
if (detailed) {
cout << "Information on sequential machine" << endl;
MachMulti::Info(detailed,txt);
}
else {
printf("%sSequential machine [%u] %d- .. -%d, bs=%d, passes=%lu/%lu", txt, (uint) machs.size(), idim, odim, bsize, nb_forw, nb_backw);
//printf(", this=%p",this);
tm.disp(", ");
tbackw.disp(" + back: ");
printf("\n");
debug5("*** %s data: %p -> %p, grad %p <- %p\n", txt, (void*)data_in, (void*)data_out, (void*)grad_in, (void*)grad_out);
char ntxt[512];
sprintf(ntxt,"%s ", txt);
for (unsigned int i=0; i<machs.size(); i++) machs[i]->Info(detailed, ntxt);
}
printf("%stotal number of parameters: %lu (%d MBytes)\n", txt, GetNbParams(), (int) (GetNbParams()*sizeof(REAL)/1048576));
}
void MachSeq::Forw(int eff_bsize, bool in_train)
{
debug2("* MachSeq::Forw: %p -> %p\n", (void*) data_in, (void*) data_out);
if (machs.empty())
Error("called Forw() for an empty sequential machine");
tm.start();
for (unsigned int i=0; i<machs.size(); i++) {
if (activ_forw[i]) machs[i]->Forw(eff_bsize, in_train);
}
nb_forw += (eff_bsize<=0) ? bsize : eff_bsize;
tm.stop();
}
void MachSeq::Backw(const float lrate, const float wdecay, int eff_bsize)
{
debug2("* MachSeq::Backw: %p <- %p\n", (void*) grad_in, (void*) grad_out);
if (machs.empty())
Error("called Backw() for an empty sequential machine");
debugMachOutp("MachSeq Grad",grad_out,idim,odim,eff_bsize);
tbackw.start();
for (int i=machs.size()-1; i>=0; i--) {
if (activ_backw[i]) machs[i]->Backw(lrate,wdecay,eff_bsize);
}
nb_backw += (eff_bsize<=0) ? bsize : eff_bsize;
tbackw.stop();
debugMachInp("MachSeq Grad",grad_in,idim,odim,eff_bsize);
}