MCPcopy Create free account
hub / github.com/clab/dynet / encode

Method encode

examples/sequence-to-sequence/encdec.h:127–196  ·  view source on GitHub ↗

* \brief Batched encoding * \details Encodes a batch of sentences of the same size (don't forget to pad them) * * \param isents Whole dataset * \param id Index of the start of the batch * \param bsize Batch size * \param chars Number of tokens processed (used to compute loss per characters) * \param cg Computation graph * \return Returns the expression for t

Source from the content-addressed store, hash-verified

125 * \return Returns the expression for the negative (batched) encoding
126 */
127 Expression encode(const vector<vector<int>>& isents,
128 unsigned id,
129 unsigned bsize,
130 unsigned & chars,
131 ComputationGraph & cg) {
132 // Set variables for the input sentence
133 const unsigned islen = isents[id].size();
134 vector<unsigned> x_t(bsize);
135
136 // Forward encoder -------------------------------------------------------------------------
137
138 // Initialize parameters in fwd_enc_builder
139 fwd_enc_builder.new_graph(cg);
140 // Initialize the sequence
141 fwd_enc_builder.start_new_sequence();
142
143 // Run the forward encoder on the batch
144 for (unsigned t = 0; t < islen; ++t) {
145 // Fill x_t with the characters at step t in the batch
146 for (unsigned i = 0; i < bsize; ++i) {
147 x_t[i] = isents[id + i][t];
148 if (x_t[i] != static_cast<unsigned>(*isents[id].rbegin())) chars++; // if x_t is non-EOS, count a char
149 }
150 // Get embedding
151 Expression i_x_t = lookup(cg, p_ec, x_t);
152 // Run a step in the forward encoder
153 fwd_enc_builder.add_input(i_x_t);
154 }
155
156 // Backward encoder ------------------------------------------------------------------------
157 if (bidirectional) {
158 // Initialize parameters in bwd_enc_builder
159 rev_enc_builder.new_graph(cg);
160 // Initialize the sequence
161 rev_enc_builder.start_new_sequence();
162 // Fill x_t with the characters at step t in the batch (in reverse order)
163 for (int t = islen - 1; t >= 0; --t) {
164 for (unsigned i = 0; i < bsize; ++i) {
165 x_t[i] = isents[id + i][t];
166 }
167 // Get embedding (could be mutualized with fwd_enc_builder)
168 Expression i_x_t = lookup(cg, p_ec, x_t);
169 // Run a step in the forward encoder
170 rev_enc_builder.add_input(i_x_t);
171 }
172 }
173
174 // Collect encodings -----------------------------------------------------------------------
175 vector<Expression> to;
176 // Get states from forward encoder
177 for (auto s_l : fwd_enc_builder.final_s()) to.push_back(s_l);
178 // Get states from backward encoder
179 if (bidirectional)
180 for (auto s_l : rev_enc_builder.final_s()) to.push_back(s_l);
181
182 // Put it as a vector (matrix because it's batched)
183 Expression i_combined = concatenate(to);
184 Expression i_nc;

Callers 1

mainFunction · 0.45

Calls 8

lookupFunction · 0.50
concatenateFunction · 0.50
parameterFunction · 0.50
sizeMethod · 0.45
new_graphMethod · 0.45
start_new_sequenceMethod · 0.45
add_inputMethod · 0.45
final_sMethod · 0.45

Tested by

no test coverage detected