Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'nn' is not defined

Tags:

pytorch

Several times when I copy PyTorch code I get this error:

NameError: name 'nn' is not defined

What is missing? What is nn?


To reproduce:

class SLL(nn.Module):
    "single linear layer"
    def __init__(self):
        super().__init__()
        self.l1 = nn.Linear(10,100)        

    def forward(self)->None: 
        print("SLL:forward")

m1 = SLL()
like image 885
prosti Avatar asked Jun 17 '19 14:06

prosti


1 Answers

If you got this error you can fix it with the following code:

import torch
import torch.nn as nn

You need to include both lines, since if you set just the second one it may not work if the torch package is not imported.

Where torch and torch.nn (or just nn) are two of the main PyTorch packages. You can help(torch.nn) to confirm this.

It is not uncommon when you include nn to include the functional interface as F like this:

import torch
import torch.nn as nn
import torch.nn.functional as F

To bring you the hints what you imported or what is inside the nn package I provided the list:

['AdaptiveAvgPool1d', 'AdaptiveAvgPool2d', 'AdaptiveAvgPool3d', 'AdaptiveLogSoftmaxWithLoss', 'AdaptiveMaxPool1d', 'AdaptiveMaxPool2d', 'AdaptiveMaxPool3d', 'AlphaDropout', 'AvgPool1d', 'AvgPool2d', 'AvgPool3d', 'BCELoss', 'BCEWithLogitsLoss', 'BatchNorm1d', 'BatchNorm2d', 'BatchNorm3d', 'Bilinear', 'CELU', 'CTCLoss', 'ConstantPad1d', 'ConstantPad2d', 'ConstantPad3d', 'Container', 'Conv1d', 'Conv2d', 'Conv3d', 'ConvTranspose1d', 'ConvTranspose2d', 'ConvTranspose3d', 'CosineEmbeddingLoss', 'CosineSimilarity', 'CrossEntropyLoss', 'CrossMapLRN2d', 'DataParallel', 'Dropout', 'Dropout2d', 'Dropout3d', 'ELU', 'Embedding', 'EmbeddingBag', 'FeatureAlphaDropout', 'Fold', 'FractionalMaxPool2d', 'GLU', 'GRU', 'GRUCell', 'GroupNorm', 'Hardshrink', 'Hardtanh', 'HingeEmbeddingLoss', 'InstanceNorm1d', 'InstanceNorm2d', 'InstanceNorm3d', 'KLDivLoss', 'L1Loss', 'LPPool1d', 'LPPool2d', 'LSTM', 'LSTMCell', 'LayerNorm', 'LeakyReLU', 'Linear', 'LocalResponseNorm', 'LogSigmoid', 'LogSoftmax', 'MSELoss', 'MarginRankingLoss', 'MaxPool1d', 'MaxPool2d', 'MaxPool3d', 'MaxUnpool1d', 'MaxUnpool2d', 'MaxUnpool3d', 'Module', 'ModuleDict', 'ModuleList', 'MultiLabelMarginLoss', 'MultiLabelSoftMarginLoss', 'MultiMarginLoss', 'NLLLoss', 'NLLLoss2d', 'PReLU', 'PairwiseDistance', 'Parameter', 'ParameterDict', 'ParameterList', 'PixelShuffle', 'PoissonNLLLoss', 'RNN', 'RNNBase', 'RNNCell', 'RNNCellBase', 'RReLU', 'ReLU', 'ReLU6', 'ReflectionPad1d', 'ReflectionPad2d', 'ReplicationPad1d', 'ReplicationPad2d', 'ReplicationPad3d', 'SELU', 'Sequential', 'Sigmoid', 'SmoothL1Loss', 'SoftMarginLoss', 'Softmax', 'Softmax2d', 'Softmin', 'Softplus', 'Softshrink', 'Softsign', 'Tanh', 'Tanhshrink', 'Threshold', 'TripletMarginLoss', 'Unfold', 'Upsample', 'UpsamplingBilinear2d', 'UpsamplingNearest2d', 'ZeroPad2d', '_VF', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '_functions', '_reduction', 'backends', 'functional', 'grad', 'init', 'modules', 'parallel', 'parameter', 'utils']

Containing many classes where probable the most fundamental one is the PyTorch class nn.Module.

Do not confuse PyTorch class nn.Module with the Python modules.


To fix the SLL model from the question you just have to add the first two lines:

import torch
import torch.nn as nn

class SLL(nn.Module):
    "single linear layer"
    def __init__(self):
        super().__init__()
        self.l1 = nn.Linear(10,100)        

    def forward(self)->None: 
        print("SLL:forward")

# we create a module instance m1
m1 = SLL()

And you will get the output:

SLL(
  (l1): Linear(in_features=10, out_features=100, bias=True)
)
like image 96
prosti Avatar answered Oct 18 '22 22:10

prosti