Fixing PyTorch NotImplementedError in forward

PyTorch NotImplementedError in forward is an error which occurs in python when you use the wrong indentation.

Today I try to explain why this error takes place and how to solve it, I will also add other solutions that could solve the error if possible.

Exploring PyTorch NotImplementedError in forward

This is is an error which occurs in python when you use the wrong indentation.

The error should look like this. Double check in order to avoid mixing between errors.

                                                                       #
    main()
    outputs = model(images)
.....
    result = self.forward(*input, **kwargs)
.....
    raise NotImplementedError
NotImplementedError
                                                                       #

Bellow we will take care of the error using multiple possible solutions according to your needs.

Solution : Fix the the indentation of the __init__ function and the indentation of the forward method

The problem is caused by you using the wrong indentation, if you are new to python you should understand that unlike other programming languages indentation in python is not only for esthetics.

Indentation is taken very seriously in python, the forward method should be its own method and not part of the __init__ function.

look at these examples bellow, one of them will cause the error and the other wont.

The code bellow will cause the error, since here the forward method is part of the init function.

                                                                       #
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.layer = nn.Sequential(
        .....

        self.fc = nn.Sequential(
        .....
        )

        def forward(self, x):
        .....
        .....
                                                                       #

This code will return no error, since the init function and the forward method have the same indentation, meaning they are vertically on the same level. Now we can say that forward is an independent method.

                                                                       #
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.layer = nn.Sequential(
        .....

        self.fc = nn.Sequential(
        .....
        )

    def forward(self, x):
    .....
    .....
                                                                       #

This solution should be enough to make the error go.

If you want to learn more about the rules of indentation in python, please visit the link bellow.

https://www.w3schools.com/python/gloss_python_indentation.asp

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

I hope this article has helped you achieve your objective, If you like the effort we did here, please consider donating to our Kofi account located at the top of this page. Keep coding, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/