Yes it is a bit tricky.
To be clearer the change I propose is the following, hope this is easy enough to do in your code.
# before
placeholder = torch.zeros(1000, 5, 5)
for idx in range(1000):
    some_tensor = torch.rand(5, 5)
    placeholder[idx].copy_(some_tensor)
return placeholder

# after
results = []
for idx in range(1000):
    results.append(torch.rand(5, 5))
return torch.stack(results, 0)

Note that because of the custom allocator used by pytorch, the second one won’t be slower by any significant amount compared to the first one  even if you create the big tensor only once and reuse it in the first case !