How to retain gradient after converting tensor->numpy->tensor

I am working on Graph Convolutional neural networks using PyTorch. The input vectors are fed into series of GCN layers where it accumulates its neighbor information and generates an embedding vector for each node. Until here I am using PyTorch tensors for tracking gradients. But now for the prediction, I should feed these embedding vectors individually into a downstream model(XGBoost in my case). But as tensors don’t work on XGBoost I need to convert them to NumPy, make prediction, compute loss, and backpropagate through the model until the beginning of GCN layers. But as I have converted the tensor to numpy I will lose the gradients after that. Is there a way I could retain my gradients including the operations on numpy array.


We cannot automatically track gradients for numpy Tensors I’m afraid. You will have to either stick with pytorch Tensor or write the backward computation explicitly as described here: Extending PyTorch — PyTorch 1.8.0 documentation