I want to get a 2-D torch.Tensor with size [a,b] filled with values from a uniform distribution (in range [r1,r2]) in PyTorch. Answer If U is a random variable uniformly distributed on [0, 1], then (r1 – r2) * U + r2 is uniformly distributed on [r1, r2]. Thus, you just need: Alternatively, you can simply use: To fully explain
Tag: pytorch
L1/L2 regularization in PyTorch
How do I add L1/L2 regularization in PyTorch without manually computing it? Answer See the documentation. Add a weight_decay parameter to the optimizer for L2 regularization.
What does .view() do in PyTorch?
What does .view() do to a tensor x? What do negative values mean? Answer view() reshapes the tensor without copying memory, similar to numpy’s reshape(). Given a tensor a with 16 elements: To reshape this tensor to make it a 4 x 4 tensor, use: Now a will be a 4 x 4 tensor. Note that after the reshape the