NameError: global name 'self' is not defined

Hi All,

Can someone please help me with the below error.

Python is 2.7 and I am using pytorch in colab.

I am getting below error.

NameError: global name ‘self’ is not defined

The code is bit long so I have given Github link.

Thanks and Regards,
Subho

L1Penalty uses self.l1weight which is unknown. Probably you wanted to use ctx.l1weight instead.

1 Like

Hi ptrblck,

I tried, but now I am getting below error.

RuntimeError: function L1PenaltyBackward returned an incorrect number of gradients (expected 2, got 1)

Colab link

Thanks and Regards,
Subho

Hi All,

Please help me on this.

Thanks and Regards,
Subho

Hi Subho,

have a look at the Extending PyTorch docs.
From these docs:

It should return as many Tensor s as there were inputs, with each of them containing the gradient w.r.t. its corresponding input. If your inputs didn’t require gradient ( needs_input_grad is a tuple of booleans indicating whether each input needs gradient computation), or were non- Tensor objects, you can return None .

As it seems you are working on some kind of lecture assignment, you shouldn’t expect to get a complete solution.
Also, I’m not a big fan of posting repeatedly and would l like to ask you to be a bit more patient :wink:

1 Like

ok sure Thanks ptrblck,

1 Like