Skip to content

nn.NLLLoss example throws error #8840

@ptrblck

Description

@ptrblck

nn.NLLLoss example does not work in current stable and master doc.

Reported in this thread.

The second example of nn.NLLLoss from the docs throws an error on creating the target tensor:

# 2D loss example (used, for example, with image inputs)
N, C = 5, 4
loss = nn.NLLLoss()
# input is of size N x C x height x width
data = torch.randn(N, 16, 10, 10)
m = nn.Conv2d(16, C, (3, 3))
# each element in target has to have 0 <= value < C
target = torch.tensor(N, 8, 8).random_(0, C)
> TypeError: tensor() takes 1 positional argument but 3 were given
output = loss(m(data), target)
output.backward()

Tested under 0.4.0 and 0.5.0a0+e62c3a4.

The fix could be:

target = torch.empty(N, 8, 8, dtype=torch.long).random_(0, C)

I'm creating a pull request to fix this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions