From 71c4ad451a736711d5a8692594d5dcc52041db06 Mon Sep 17 00:00:00 2001 From: kibitzing Date: Mon, 24 Jun 2019 01:48:57 +0900 Subject: [PATCH] Fixed dimension bugs in 12_2_hello_rnn.py Error message was 1) "RuntimeError: dimension specified as 0 but tensor has no dimensions". (line: 24) --> changed the dimension using '.view'(-1, 1)' to make the label dimension to be 1 instead of "no dimensions" 2)"IndexError: invalid index of a 0-dim tensor. Use tensor.item() to convert a 0-dim tensor to a Python number" (line: 80) --> Since the output of the Cross Entropy Loss here has no dimension, deleted the index term '[0]'. It was tested with Pytorch 1.1. --- 12_2_hello_rnn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/12_2_hello_rnn.py b/12_2_hello_rnn.py index c0364e3..476b28c 100644 --- a/12_2_hello_rnn.py +++ b/12_2_hello_rnn.py @@ -21,7 +21,7 @@ # As we have one batch of samples, we will change them to variables only once inputs = Variable(torch.Tensor(x_one_hot)) -labels = Variable(torch.LongTensor(y_data)) +labels = Variable(torch.LongTensor(y_data)).view(-1, 1) num_classes = 5 input_size = 5 # one-hot size @@ -77,7 +77,7 @@ def init_hidden(self): sys.stdout.write(idx2char[idx.data[0]]) loss += criterion(output, label) - print(", epoch: %d, loss: %1.3f" % (epoch + 1, loss.data[0])) + print(", epoch: %d, loss: %1.3f" % (epoch + 1, loss.data)) loss.backward() optimizer.step()