-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMachMulti.cpp
250 lines (215 loc) · 6.42 KB
/
MachMulti.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
/*
* 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 "MachMulti.h"
/*******************************************
*
********************************************/
MachMulti::MachMulti()
: Mach(0,0,0)
{
debug0("** constructor MachMulti\n");
machs.clear();
}
MachMulti::MachMulti(const MachMulti &m)
: Mach(m)
{
debug0("** copy constructor MachMulti\n");
machs.clear();
}
/*******************************************
*
********************************************/
MachMulti *MachMulti::Clone()
{
debug1("** MachMulti::Clone %p\n", this);
MachMulti *m = new MachMulti(*this);
if (m != NULL)
m->CloneSubmachs(*this);
return m;
}
void MachMulti::CloneSubmachs(const MachMulti &mm)
{
debug1("** MachMulti::CloneSubmachs %p\n", &mm);
for (unsigned int m=0; m<mm.machs.size(); m++) {
debug2("** - clone machine %d at %p\n",m,mm.machs[m]);
this->MachAdd( mm.machs[m]->Clone() );
if (!activ_forw.empty())
activ_forw.back() = mm.activ_forw[m];
if (!activ_backw.empty())
activ_backw.back() = mm.activ_backw[m];
}
}
/*******************************************
*
********************************************/
MachMulti::~MachMulti()
{
debug1("** destructor MachMulti %lx\n", (luint) this);
MachMulti::Delete();
machs.clear();
}
Mach* MachMulti::MachGet(size_t i)
{
if (i<0 || i>=machs.size())
Error("MachMulti: accessing inexistent machine");
return machs[i];
}
void MachMulti::Delete()
{
for (unsigned int m=0; m<machs.size(); m++) delete machs[m];
}
void MachMulti::MachAdd(Mach *new_mach)
{
Error("MachAdd not defined for abstract multiple machine");
}
Mach *MachMulti::MachDel()
{
Error("MachDel not defined for abstract multiple machine");
return NULL;
}
ulong MachMulti::GetNbParams() {
ulong sum=0;
for (vector<Mach*>::iterator it = machs.begin(); it!=machs.end(); ++it) {
sum += (*it)->GetNbParams();
}
return sum;
}
//-----------------------------------------------
// File output
//-----------------------------------------------
void MachMulti::WriteParams(ostream &of) {
debug0("* write params of MachMulti\n");
Mach::WriteParams(of);
int nbm=machs.size();
of.write((char*) &nbm, sizeof(int));
}
void MachMulti::WriteData(ostream &outf) {
debug0("* writing data of multiple machine to file\n");
int nbm=machs.size(), s=sizeof(REAL);
outf.write((char*) &nbm, sizeof(int));
outf.write((char*) &s, sizeof(int));
for (vector<Mach*>::iterator it = machs.begin(); it!=machs.end(); ++it) {
(*it)->Write(outf);
}
}
//-----------------------------------------------
// File input
//-----------------------------------------------
void MachMulti::ReadParams(istream &inpf, bool with_alloc)
{
debug0("* read params of type MachMulti\n");
if (machs.size() > 0)
Error("Trying to read multiple machine into non empty data structures\n");
Mach::ReadParams(inpf, false);
int nbm;
inpf.read((char*) &nbm, sizeof(int));
if (nbm<1) Error("illegal number of machines");
machs.clear();
for (int i=0; i<nbm; i++) {
machs.push_back(NULL);
activ_forw.push_back(true);
activ_backw.push_back(true);
}
}
void MachMulti::ReadData(istream &inpf, size_t s, int bs)
{
debug0("* read data of MachMulti\n");
if (s!=machs.size())
ErrorN("data block of multiple machine has %zu machines (%zu were expected)", s, machs.size());
for (vector<Mach*>::iterator it = machs.begin(); it!=machs.end(); ++it) {
(*it) = Mach::Read(inpf, bs);
}
}
//
// Tools
//
void MachMulti::SetBsize(int bs)
{
if (bs<1) Error("wrong value in SetBsize()");
Mach::SetBsize(bs);
for (uint i=0; i<machs.size(); i++) machs[i]->SetBsize(bs);
}
void MachMulti::SetNbEx(ulong nf, ulong nb)
{
Mach::SetNbEx(nf, nb);
for (uint i=0; i<machs.size(); i++) machs[i]->SetNbEx(nf, nb);
}
void MachMulti::Info(bool detailed, char *txt)
{
if (detailed) {
if (machs.size()) {
Mach::Info();
for (unsigned int i=0; i<machs.size(); i++) {
cout << "MACHINE " << i << ": " << endl;
machs[i]->Info();
}
}
else
cout << " *** empty ***" << endl;
}
else {
printf("%sMultiple machine %d- .. -%d, bs=%d, passes=%lu/%lu", txt, idim, odim, bsize, nb_forw, nb_backw);
tm.disp(", ");
printf("\n");
char ntxt[256];
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));
}
bool MachMulti::CopyParams(Mach* mach)
{
MachMulti* machmulti = static_cast<MachMulti*>(mach);
size_t nb_machs = this->machs.size();
if ( Mach::CopyParams(mach)
&& (machmulti->machs.size() == nb_machs) ) {
this->activ_forw = machmulti->activ_forw;
this->activ_backw = machmulti->activ_backw;
for (size_t i = 0 ; i < nb_machs ; i++)
if (!(this->machs[i]->CopyParams(machmulti->machs[i])))
return false;
return true;
}
else
return false;
}
void MachMulti::Forw(int eff_bsize, bool in_train)
{
if (machs.empty())
Error("called Forw() for an empty multiple machine");
else
Error("call to Forw() not defined for an abstract multiple machine");
}
void MachMulti::Backw(const float lrate, const float wdecay, int eff_bsize)
{
Error("call to Backw() not defined for an abstract multiple machine");
}
void MachMulti::Activate(int nb, bool do_forw, bool do_backw)
{
if (nb<0 || nb>=(int)machs.size())
Error("MachMulti::Activate: wrong machine number\n");
activ_forw[nb] = do_forw;
activ_backw[nb] = do_backw;
}