I’m following the PyTorch official tutorial on how to viz model via TensorBoard 1 in my notebook here 2.
(Note that I copied the exact code from the tutorial webpage)

However, I continued to get the following error on the section to write image to tensorboard, although obviously “trainloader” has been defined in the first cell.

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-1-6d95240dcdaa> in <module>
      1 # get some random training images
----> 2 dataiter = iter(trainloader)
      3 images, labels = dataiter.next()
      4 
      5 # create grid of images

NameError: name 'trainloader' is not defined


After I re-ran the first cell to create the trainloader twice, the error points to the writer object, which obviously has been created in previous cell.

NameError: name 'writer' is not defined
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-5-6d95240dcdaa> in <module>
     10 
     11 # write to tensorboard
---> 12 writer.add_image('four_fashion_mnist_images', img_grid)
NameError: name 'writer' is not defined

Appreciate if you can advise what could have caused this problem.