| 209 | return dy.esum(errs); |
| 210 | } |
| 211 | public void RandomSample(int maxLen = 200) { |
| 212 | // Renew the computation graph |
| 213 | dy.RenewCG(); |
| 214 | |
| 215 | // hidden -> word rep parameter |
| 216 | Expression R = dy.parameter(p_R); |
| 217 | // word bias |
| 218 | Expression bias = dy.parameter(p_bias); |
| 219 | |
| 220 | Random r = new Random(); |
| 221 | // Start with an <s> |
| 222 | RNNState state = builder.GetInitialState().AddInput(lp[d["<s>"]]); |
| 223 | int cur = d["<s>"], len = 0; |
| 224 | while (len < maxLen) { |
| 225 | // Regular softmax |
| 226 | Expression u_t = dy.affine_transform(bias, R, state.Output()); |
| 227 | Expression dist_expr = dy.softmax(u_t); |
| 228 | float[] dist = dist_expr.VectorValue(); |
| 229 | // Get a random between 0->1, sample the next item |
| 230 | double p = r.NextDouble(); |
| 231 | for (cur = 0; cur < dist.Length; cur++) { |
| 232 | p -= dist[cur]; |
| 233 | if (p < 0) break; |
| 234 | } |
| 235 | if (cur == dist.Length) cur = d["</s>"]; |
| 236 | // Are we at the end? |
| 237 | if (cur == d["</s>"]) |
| 238 | break; |
| 239 | len++; |
| 240 | // Output the chracter |
| 241 | Console.Write((len == 1 ? "" : " ") + di2W[cur]); |
| 242 | }// next prediction |
| 243 | Console.WriteLine(); |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | } |