Generate a random vector that have a small distance between it and another one

Hi,
let’s say I have a random vector z1=torch.randn(1,128,requires_grad = True). Now I would like to generate another vector z2 such that ||z1-z2||<epsilon. How should I do this in pytorch?
Any help and suggestions would be appreciated, thanks in advance.

Perhaps generate z2 as z1 with some uniform noise added to it? With uniform noise you could guarantee the difference will not exceed epsilon, if you set the lower and upper bound correctly.

z1=torch.randn(1,128,requires_grad = True)

epsilon = torch.tensor([0.5])

bound = (epsilon/2)/(z1.numel())

distribution = torch.distributions.uniform.Uniform(-bound, bound)
z2 = z1 + distribution.sample((1, 128))

Not sure if I computed the bounds optimally, but I hope the idea helps.

1 Like