How to replace the use of python's attributes in Julia

Viewed 51

I have some functions in Python form fast.ai course:

def mse_grad(inp, targ): 
    # grad of loss with respect to output of previous layer
    inp.g = 2. * (inp.squeeze() - targ).unsqueeze(-1) / inp.shape[0]

def lin_grad(inp, out, w, b):
    # grad of matmul with respect to input
    inp.g = out.g @ w.t()
    w.g = (inp.unsqueeze(-1) * out.g.unsqueeze(1)).sum(0)
    b.g = out.g.sum(0)

def forward_and_backward(inp, targ):
    # backward pass:
    mse_grad(out, targ)
    lin_grad(l2, out, w2, b2)

It uses python's attributes. How do I replace this inp.g and out.g in Julia so i can use those functions in forward_and_backward function so they have access to each other gradients?

1 Answers
Related