MCPcopy Create free account
hub / github.com/pytorch/pytorch / GenerateText

Method GenerateText

caffe2/python/examples/char_rnn.py:217–242  ·  view source on GitHub ↗
(self, num_characters, ch)

Source from the content-addressed store, hash-verified

215 last_n_iter = 0
216
217 def GenerateText(self, num_characters, ch):
218 # Given a starting symbol we feed a fake sequence of size 1 to
219 # our RNN num_character times. After each time we use output
220 # probabilities to pick a next character to feed to the network.
221 # Same character becomes part of the output
222 CreateNetOnce(self.forward_net)
223
224 text = '' + ch
225 for _i in range(num_characters):
226 workspace.FeedBlob(
227 "seq_lengths", np.array([1] * self.batch_size, dtype=np.int32))
228 workspace.RunNet(self.prepare_state.Name())
229
230 input = np.zeros([1, self.batch_size, self.D]).astype(np.float32)
231 input[0][0][self.char_to_idx[ch]] = 1
232
233 workspace.FeedBlob("input_blob", input)
234 workspace.RunNet(self.forward_net.Name())
235
236 p = workspace.FetchBlob(self.predictions)
237 next = np.random.choice(self.D, p=p[0][0])
238
239 ch = self.idx_to_char[next]
240 text += ch
241
242 print(text)
243
244
245@utils.debug

Callers 1

TrainModelMethod · 0.95

Calls 5

CreateNetOnceFunction · 0.85
astypeMethod · 0.80
rangeFunction · 0.50
NameMethod · 0.45
zerosMethod · 0.45

Tested by

no test coverage detected