Duplicate element of tensor while perserving gradient values
So I have a vector that I computed somehow with size k

x = torch.FloatTensor([0.5, 0.3, 0.1, 0.7])
x = x + 2


I want to take its first element x[0] and create a vector of size k-1 filled with the value x[0] so that the gradients that come along with this element are present in the new vector.

I tried using torch.full and filling it up with x[0] but that does not preserve gradients.

Using pytorch 1.4

You could also use .repeat like this (IMO cleaner and more verbose):

# type deduction is automatic
x = torch.tensor([0.5, 0.3, 0.1, 0.7])
x = x + 2
y = x[0].repeat(50)


Gradient will be preserved (gradient history will be copied).
