MCPcopy Create free account
hub / github.com/pytorch/tutorials / sample

Function sample

intermediate_source/char_rnn_generation_tutorial.py:388–407  ·  view source on GitHub ↗
(category, start_letter='A')

Source from the content-addressed store, hash-verified

386
387# Sample from a category and starting letter
388def sample(category, start_letter='A'):
389 with torch.no_grad(): # no need to track history in sampling
390 category_tensor = categoryTensor(category)
391 input = inputTensor(start_letter)
392 hidden = rnn.initHidden()
393
394 output_name = start_letter
395
396 for i in range(max_length):
397 output, hidden = rnn(category_tensor, input[0], hidden)
398 topv, topi = output.topk(1)
399 topi = topi[0][0]
400 if topi == n_letters - 1:
401 break
402 else:
403 letter = all_letters[topi]
404 output_name += letter
405 input = inputTensor(letter)
406
407 return output_name
408
409# Get multiple samples from one category and multiple starting letters
410def samples(category, start_letters='ABC'):

Callers 1

samplesFunction · 0.85

Calls 3

categoryTensorFunction · 0.85
inputTensorFunction · 0.85
initHiddenMethod · 0.80

Tested by

no test coverage detected