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

Method sample

examples/batching/rnnlm-batch.py:72–95  ·  view source on GitHub ↗
(self, first=1, nchars=0, stop=-1)

Source from the content-addressed store, hash-verified

70
71
72 def sample(self, first=1, nchars=0, stop=-1):
73 res = [first]
74 dy.renew_cg()
75 state = self.builder.initial_state()
76
77 R = dy.parameter(self.R)
78 bias = dy.parameter(self.bias)
79 cw = first
80 while True:
81 x_t = dy.lookup(self.lookup, cw)
82 state = state.add_input(x_t)
83 y_t = state.output()
84 r_t = bias + (R * y_t)
85 ydist = dy.softmax(r_t)
86 dist = ydist.vec_value()
87 rnd = random.random()
88 for i,p in enumerate(dist):
89 rnd -= p
90 if rnd <= 0: break
91 res.append(i)
92 cw = i
93 if cw == stop: break
94 if nchars and len(res) > nchars: break
95 return res
96
97if __name__ == '__main__':
98 parser = argparse.ArgumentParser()

Callers 1

rnnlm-batch.pyFile · 0.45

Calls 5

initial_stateMethod · 0.80
vec_valueMethod · 0.80
appendMethod · 0.80
add_inputMethod · 0.45
outputMethod · 0.45

Tested by

no test coverage detected