loss = criterion(output, category_tensor) optimizer.zero_grad() loss.backward() optimizer.step() return output, loss.item() current_loss = 0 all_losses = [] plot_steps, print_steps = 1000, 5000 n_iter = 100000 for i in range(n_iter): category, line, category_tensor, line_tensor = random_training_example( category_lines, all_categories) output, loss = train(line_tensor, category_tensor) current_loss += loss if (i + 1) % plot_steps == 0: all_losses.append(current_loss / plot_steps) current_loss = 0 if (i + 1) % print_steps == 0: guess = category_from_output(output) correct = "CORRECT" if guess == category else f"WRONG ({category})" print(f"{i} {i / n_iter * 100} {loss:.4f} {line}/ {guess} {correct}") plt.figure() plt.plot(all_losses)
loss = 0 # sum of losses for an epoch total_loss = 0 # now! start = time.time() print() print("----------------") print("training starts!") print("----------------") print() for i in range(1, n_iter + 1): output, loss = train(*random_training_example()) total_loss += loss if i % print_every == 0: avg_loss = total_loss / plot_every sys.stdout.write( "%d %d%% (%s) %.4f\n" % (i, i / n_iter * 100, time_since(start), avg_loss)) losses.append(avg_loss) total_loss = 0 plt.figure() plt.plot(losses) plt.show() sys.stdout.write("train finished\n")