Skip to content

Conversation

@linhoangce
Copy link

PyTorch now throws an error for this code:

`def sample(model, starting_str,
len_generated_text=500,
scale_factor=1.0):

encoded_input = torch.tensor([char2int[s] for s in starting_str])
encoded_input = torch.reshape(encoded_input, (1, -1))

generated_str = starting_str

model.eval()
hidden, cell = model.init_hidden(1)
hidden = hidden.to(device)
cell = cell.to(device)
for c in range(len(starting_str)-1):
    _, hidden, cell = model(encoded_input[:, c].view(1), hidden, cell) 

last_char = encoded_input[:, -1]
for i in range(len_generated_text):
    logits, hidden, cell = model(last_char.view(1), hidden, cell) 
    logits = torch.squeeze(logits, 0)
    scaled_logits = logits * scale_factor
    m = Categorical(logits=scaled_logits)
    last_char = m.sample()
    generated_str += str(char_array[last_char])
    
return generated_str

torch.manual_seed(1)
model.to('cpu')
print(sample(model, starting_str='The island'))`

Issue comes from encoded_input[:, c].view(1) and last_char.view(1) being not on the same device and the model. Same for hidden and cell, but these have been corrected.

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant