import torch
T = torch.FloatTensor(range(0,10 ** 6)) # 1M
#case 1:
torch.save(T, 'junk.pt')
# results in a 4 MB file size
#case 2:
torch.save(T[-20:], 'junk2.pt')
# results in a 4 MB file size
#case 3:
torch.save(torch.FloatTensor(T[-20:]), 'junk3.pt')
# results in a 4 MB file size
#case 4:
torch.save(torch.FloatTensor(T[-20:].tolist()), 'junk4.pt')
# results in a 405 Bytes file size
My questions are:
In case 3 the resulting file size seems surprising as we are creating a new tensor. Why is this new tensor not just the slice?
Is case 4, the optimal method for saving just part (slice) of a tensor?
More generally, if I want to 'trim' a very large 1-dimensional tensor by removing the first half of its values in order to save memory, do I have to proceed as in case 4, or is there a more direct and less computationally costly way that does not involve creating a python list.