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
|
#pragma once
#include "../mblas/matrix.h"
#include "../dl4mt/model.h"
#include "../dl4mt/gru.h"
namespace amunmt {
namespace CPU {
class Encoder {
private:
/////////////////////////////////////////////////////////////////
template <class Weights>
class Embeddings {
public:
Embeddings(const Weights& model)
: w_(model)
{}
void Lookup(mblas::Matrix& Row, size_t i) {
size_t len = w_.E_.columns();
if(i < w_.E_.rows())
Row = blaze::submatrix(w_.E_, i, 0, 1, len);
else
Row = blaze::submatrix(w_.E_, 1, 0, 1, len); // UNK
}
const Weights& w_;
private:
};
/////////////////////////////////////////////////////////////////
template <class Weights>
class RNN {
public:
RNN(const Weights& model)
: gru_(model) {}
void InitializeState(size_t batchSize = 1) {
State_.resize(batchSize, gru_.GetStateLength());
State_ = 0.0f;
}
void GetNextState(mblas::Matrix& NextState,
const mblas::Matrix& State,
const mblas::Matrix& Embd) {
gru_.GetNextState(NextState, State, Embd);
}
template <class It>
void GetContext(It it, It end,
mblas::Matrix& Context, bool invert) {
InitializeState();
size_t n = std::distance(it, end);
size_t i = 0;
while(it != end) {
GetNextState(State_, State_, *it++);
size_t len = gru_.GetStateLength();
if(invert)
blaze::submatrix(Context, n - i - 1, len, 1, len) = State_;
else
blaze::submatrix(Context, i, 0, 1, len) = State_;
++i;
}
}
size_t GetStateLength() const {
return gru_.GetStateLength();
}
private:
// Model matrices
const GRU<Weights> gru_;
mblas::Matrix State_;
};
/////////////////////////////////////////////////////////////////
public:
Encoder(const Weights& model)
: embeddings_(model.encEmbeddings_),
forwardRnn_(model.encForwardGRU_),
backwardRnn_(model.encBackwardGRU_)
{}
void GetContext(const std::vector<size_t>& words,
mblas::Matrix& context);
private:
Embeddings<Weights::Embeddings> embeddings_;
RNN<Weights::GRU> forwardRnn_;
RNN<Weights::GRU> backwardRnn_;
};
}
}
|