I get a 'None' error, but am not sure what I'm not returning in my code. I'm using jupyter notebook and have the following code (I separated the code blocks the way they are in the notebook)
The cause is the line variable, but I'm not sure how to troubleshoot why it return's None.
Edit: The None value is coming from the line variable, that's as far as I can trace it. I'm trying to follow the code at the following site (https://pytorch.org/tutorials/intermediate/char_rnn_classification_tutorial.html), to correlate hostnames to groupids.
def randomChoice(l):
return l[random.randint(0, len(l) - 1)]
def randomTrainingExample():
category = randomChoice(all_categories)
line = randomChoice(category_lines[category])
category_tensor = torch.tensor([all_categories.index(category)], dtype=torch.long)
line_tensor = lineToTensor(line)
return category, line, category_tensor, line_tensor
for i in range(10):
category, line, category_line, line_tensor = randomTrainingExample()
print('category =', category, '/ line =', line)
This is the output I get from above, which runs without errors:
category = [groupid] / line = [hostname]
This is the code that produces the error:
criterion = nn.NLLLoss()
learning_rate = 0.005 #If set too high, it might explode, too low, it might not learn
def train(category_tensor, line_tensor):
hidden = rnn.initHidden()
rnn.zero_grad()
for i in range(line_tensor.size()[0]:
output, hidden = rnn(line_tensor[i], hidden)
loss = criterion(output, category_tensor)
loss.backward()
#Add parameters' gradients to their values, multiplied by learning rate
for p in rnn.parameters():
p.data.add_(p.grad.data, alpha=-learning_rate)
return output, loss.item()
n_iters = 100000
print_every = 5000
plot_every = 1000
#keep track of losses for plotting
current_loss = 0
all_losses = []
def timeSince(since):
now = time.time()
s = now - since
m = math.floor(s / 60)
s -= m * 60
return '%dm %ds' % (m, s)
start = time.time()
for iter in range(1, n_iters + 1):
category, line, category_tensor, line_tensor = randomTrainingExample()
output, loss = train(category_tensor, line_tensor)
current_loss += loss
# Print iter number, loss, name and guess
if iter % print_every == 0:
guess, guess_i = categoryFromOutput(output)
correct = 'Y' if guess == category else 'N (%s)' % category
print('%d %d%% (%s) %.4f %s / %s %s' % (iter, iter / n_iters * 100, timeSince(start), loss, line, guess, correct))
#Add current loss avg to list of losses
if iter % plot_every == 0:
all_losses.append(current_loss / plot_every)
current_loss = 0
Here's the Error, notice that randomTrainingExample returns None for the line variable:
KeyError Traceback (most recent call last)
<ipython-input-38-9d873b1d00f6> in <module>
19
20 for iter in range(1, n_iters + 1):
---> 21 category, line, category_tensor, line_tensor = randomTrainingExample()
22 output, loss = train(category_tensor, line_tensor)
23 current_loss += loss
<ipython-input-35-94e77eed2f29> in randomTrainingExample()
4 def randomTrainingExample():
5 category = randomChoice(all_categories)
----> 6 line = randomChoice(category_lines[category])
7 category_tensor = torch.tensor([all_categories.index(category)], dtype=torch.long)
8 line_tensor = lineToTensor(line)
KeyError: None
Aucun commentaire:
Enregistrer un commentaire