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

Method BuildLMGraph

examples/tensorboard/rnnlm-batch.py:30–71  ·  view source on GitHub ↗
(self, sents)

Source from the content-addressed store, hash-verified

28
29 # Build the language model graph
30 def BuildLMGraph(self, sents):
31 dy.renew_cg()
32 # initialize the RNN
33 init_state = self.builder.initial_state()
34 # parameters -> expressions
35 R = dy.parameter(self.R)
36 bias = dy.parameter(self.bias)
37
38 S = vocab.w2i["<s>"]
39 # get the cids and masks for each step
40 tot_chars = 0
41 cids = []
42 masks = []
43
44 for i in range(len(sents[0])):
45 cids.append([(vocab.w2i[sent[i]] if len(sent) > i else S) for sent in sents])
46 mask = [(1 if len(sent)>i else 0) for sent in sents]
47 masks.append(mask)
48 tot_chars += sum(mask)
49
50 # start the rnn with "<s>"
51 init_ids = cids[0]
52 s = init_state.add_input(lookup_batch(self.lookup, init_ids))
53
54 losses = []
55
56 # feed char vectors into the RNN and predict the next char
57 for cid, mask in zip(cids[1:], masks[1:]):
58 score = dy.affine_transform([bias, R, s.output()])
59 loss = dy.pickneglogsoftmax_batch(score, cid)
60 # mask the loss if at least one sentence is shorter
61 if mask[-1] != 1:
62 mask_expr = dy.inputVector(mask)
63 mask_expr = dy.reshape(mask_expr, (1,), len(sents))
64 loss = loss * mask_expr
65
66 losses.append(loss)
67 # update the state of the RNN
68 cemb = dy.lookup_batch(self.lookup, cid)
69 s = s.add_input(cemb)
70
71 return dy.sum_batches(dy.esum(losses)), tot_chars
72
73
74 def sample(self, first=1, nchars=0, stop=-1):

Callers 1

rnnlm-batch.pyFile · 0.45

Calls 6

sumFunction · 0.85
lookup_batchFunction · 0.85
initial_stateMethod · 0.80
appendMethod · 0.80
add_inputMethod · 0.45
outputMethod · 0.45

Tested by

no test coverage detected