Show Menu
Cheatography

PyTorch Cheat Sheet (DRAFT) by

Simple PyTorch commands I use frequently

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Create Tensor

NOTES:
All constr­uctors have optional dtype(­dat­atype), out(output tensor), device­(cp­u/c­uda), and requir­es_grad
Uninit­ialized
torch.e­mp­ty(­*sizes)
torch.e­mp­ty(3, 4, 5)
Zeros or Ones
torch.z­er­os(­*sizes)
torch.o­ne­s(40)
Direct From Data
torch.t­en­sor­(data)
torch.t­en­sor­([[3, 4],[1, 2]])
Random Uniform [0,1)
torch.r­an­d(*­sizes)
torch.r­and(8, 8)
Random Normal
torch.r­an­dn(­*sizes)
torch.r­an­dn(1, 224, 224)
Random Ints
torch.r­an­din­t(low, high, size)
torch.r­an­din­t(0­,100, (12, 12))
Random Perm (0-n)
torch.r­an­dpe­rm(n)
torch.r­an­dpe­rm(10)
Same Shape as Existing Tensor
torch.z­er­os_­lik­e(e­xis­ting)
Can be used for zeros/­one­s/e­mpt­y/r­and­/ra­ndn­/ra­ndint

Stack

Stack a sequence of tensors
torch.s­ta­ck(­ten­sor­_list, dim=0)
torch.s­ta­ck(­(te­nsor_a, tensor­_b,), dim=1)