Equivalent of Numpy's list.append() for differentiable Variables

Hi, I was wondering what is the equivalent of:

    var = []
    for i in range(0, num):
        newstuff = #dostuff
        var.append(newstuff)

I’m trying to use torch.cat but it needs a starting Variable to concatenate against. Is there any other way?

var = [] 
for I in range(0, num):
    new_stuff = # do stuff 
    var.append(new_stuff.unsqueeze(0))
var_tensor = torch.cat(var)

Should work if new_stuff is already a tensor

3 Likes

Hi, i think the last row shoule be var_tensor = torch.stack(var).

Example see here: How to turn a list of tensor to tensor? - #4 by dmadeka1

1 Like