NameError when building docs

I am trying to build the documentation of my module using nbdev_build_docs. In the index.ipynb file I have the following cell in the beginning:

# local
# hide
import torch
import torch.nn as nn
import pandas as pd

from torch.utils.data import Dataset, DataLoader

The hide flag is declared as a tst_flags in the settings.ini file. Normally the cell should not be executed. Following, I have another cell down the line that uses the nn imported module.

# local
net = SimpleLSTM(len(symbols)//2, len(symbols), 32, out_size=2, activation=nn.Identity())

When running nbdev_build_docs I get a name error for this line:

---> 10 net = SimpleLSTM(len(symbols)//2, len(symbols), 32, out_size=2, activation=nn.Identity())

NameError: name 'nn' is not defined

Any ideas?

What else is in this cell? When exporting to html only cells with show_docs or some imports are executed normally.

Thanks for the reply @sgugger

Here is the whole cell that causes the problem:

# local
import string
from astraea.spelling import SimpleLSTM, SpellingEncoder

letters = string.ascii_letters
numbers = string.digits
punctuation = string.punctuation
symbols = letters + numbers + punctuation + ' '

net = SimpleLSTM(len(symbols)//2, len(symbols), 32, out_size=2, activation=nn.Identity())
spelling_encoder = SpellingEncoder(net, symbols)

What do you mean with “some imports”?

You should put

import string
from astraea.spelling import SimpleLSTM, SpellingEncoder

in a separate cell. I imagine your module is called astraea, which is why triggers the execution of that cell (nbdev needs to execute the imports to be able to execute the show_doc it sees basically).

1 Like

Yes you are right. That solved the issue! Thanks @sgugger!

I got the following error on building docs:

----> 1 show_doc(nn.Module.to_representation, default_cls_level=2)
NameError: name 'nn' is not defined

My cell is like this:

#export
@patch
def to_representation(self:nn.Module, *xb):
    "Gets a summary of `self` using `xb`"
    sample_inputs,infos = layer_info(self, *xb)
    ...

At the top of the notebook I have this cell:

#export
from fastai2.vision.all import *

What should I do?