Convert variable to list of variables without breaking Autograd?

Hi there,

do you know if there is a way to convert variable to list of variables without breaking Autograd?

for example, I have variable T whose shape is (3,5), and T.require_grad is True. Now I want to convert T to a list of list. I know I could use T.data.tolist(), but this will break the autograd computation graph. I also do not want to do two for loops to get the elements from T and put them into the list one by one since the efficiency would be very bad.

Is there anyone know how to do this efficiently without breaking the Autograd?

Thanks!

You are looking for torch.chunk or torch.split. For example, you have tensor x of size [8, 2, 2], you want to convert it into two tensor of size [4, 2, 2] along the dim 0, you can use the following:

torch.split(x, 4, dim=0)
# or you can use torch.chunk(x, 2, dim=0)

Thanks! It works for me.