This is a draft cheat sheet. It is a work in progress and is not finished yet.
Create Tensor
NOTES: |
All constructors have optional dtype(datatype), out(output tensor), device(cpu/cuda), and requires_grad |
Uninitialized |
torch.empty(*sizes) |
torch.empty(3, 4, 5) |
Zeros or Ones |
torch.zeros(*sizes) |
torch.ones(40) |
Direct From Data |
torch.tensor(data) |
torch.tensor([[3, 4],[1, 2]]) |
Random Uniform [0,1) |
torch.rand(*sizes) |
torch.rand(8, 8) |
Random Normal |
torch.randn(*sizes) |
torch.randn(1, 224, 224) |
Random Ints |
torch.randint(low, high, size) |
torch.randint(0,100, (12, 12)) |
Random Perm (0-n) |
torch.randperm(n) |
torch.randperm(10) |
Same Shape as Existing Tensor |
torch.zeros_like(existing) |
Can be used for zeros/ones/empty/rand/randn/randint |
Stack
Stack a sequence of tensors |
torch.stack(tensor_list, dim=0) |
torch.stack((tensor_a, tensor_b,), dim=1) |
|