Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'ctx' and 'self' in python?

Tags:

python

pytorch

While using the deep learning library PyTorch I came across definitions like this. Does ctx have the same behavior as self?

class LinearFunction(Function):

    @staticmethod
    def forward(ctx, input, weight, bias=None):
        ctx.save_for_backward(input, weight, bias)
        output = input.mm(weight.t())
        if bias is not None:
            output += bias.unsqueeze(0).expand_as(output)
        return output

    @staticmethod
    def backward(ctx, grad_output):
        input, weight, bias = ctx.saved_variables
        grad_input = grad_weight = grad_bias = None
        if ctx.needs_input_grad[0]:
            grad_input = grad_output.mm(weight)
        if ctx.needs_input_grad[1]:
            grad_weight = grad_output.t().mm(input)
        if bias is not None and ctx.needs_input_grad[2]:
            grad_bias = grad_output.sum(0).squeeze(0)

        return grad_input, grad_weight, grad_bias
like image 569
Peri Javia Avatar asked Mar 27 '18 14:03

Peri Javia


1 Answers

A static method (@staticmethod) is called using the class type directly, not an instance of this class:

LinearFunction.backward(x, y)

Since you have no instance, it does not make sense to use self in a static method.

Here, ctx is just a regular argument that you'll have to pass when calling your methods.

like image 67
Ronan Boiteau Avatar answered Nov 16 '22 02:11

Ronan Boiteau