Hello All,

I have come across a situation where i have a function written in matlab and i call it in my pytorch model to perform final computation on my model output. But during back propagation, my model parameter’s gradients are not calculated.

I reproduced the issue with a toy example for better understanding:

import torch

import numpy as np

def b_func(x):

abc = torch.tensor([[1]],dtype = torch.float)

x = abc + x

return x

X = torch.tensor([[2]],dtype = torch.float, requires_grad = True)

W = X ** 2

Y = W ** 3

print(Y)

Z = b_func(Y)

print(Z)

Z.backward(retain_graph=True)

print(X.grad)

This is the expected behavior of pytorch and there is no issue here. However, when i perform the below code:

def a_func(x):

abc = np.array([[1]])

x = x.detach().numpy()

x = abc + x

x = torch.tensor(x, requires_grad = True)

return x

XX = torch.tensor([[2]],dtype = torch.float, requires_grad = True)

WW = X ** 2

YY = W ** 3

print(Y)

ZZ = a_func(Y)

print(ZZ)

ZZ.backward(retain_graph=True)

print(XX.grad)

What changes shall i make for gradients to flow in this scenario. Apart from using pytorch tensors.