|
| 1 | +- [PyTorch for Numpy users.](#pytorch-for-numpy-users) |
| 2 | + - [Types](#types) |
| 3 | + - [Constructors](#constructors) |
| 4 | + - [Ones and zeros](#ones-and-zeros) |
| 5 | + - [From existing data](#from-existing-data) |
| 6 | + - [Numerical ranges](#numerical-ranges) |
| 7 | + - [Linear algebra](#linear-algebra) |
| 8 | + - [Building matrices](#building-matrices) |
| 9 | + - [Attributes](#attributes) |
| 10 | + - [Indexing](#indexing) |
| 11 | + - [Shape manipulation](#shape-manipulation) |
| 12 | + - [Item selection and manipulation](#item-selection-and-manipulation) |
| 13 | + - [Calculation](#calculation) |
| 14 | + - [Arithmetic and comparison operations](#arithmetic-and-comparison-operations) |
| 15 | + - [Random numbers](#random-numbers) |
| 16 | + - [Numerical operations](#numerical-operations) |
| 17 | + |
| 18 | + |
| 19 | +# PyTorch for Numpy users. |
| 20 | + |
| 21 | + |
| 22 | +## Types |
| 23 | + |
| 24 | +| Numpy | PyTorch | |
| 25 | +| ---------- | ----------------------------- | |
| 26 | +| np.ndarray | torch.Tensor | |
| 27 | +| np.float32 | torch.float32<br>torch.float | |
| 28 | +| np.float64 | torch.float64<br>torch.double | |
| 29 | +| np.float16 | torch.float16<br>torch.half | |
| 30 | +| np.int8 | torch.int8 | |
| 31 | +| np.uint8 | torch.uint8 | |
| 32 | +| np.int16 | torch.int16<br>torch.short | |
| 33 | +| np.int32 | torch.int32<br>torch.int | |
| 34 | +| np.int64 | torch.int64<br>torch.long | |
| 35 | + |
| 36 | + |
| 37 | + |
| 38 | +## Constructors |
| 39 | + |
| 40 | +### Ones and zeros |
| 41 | + |
| 42 | +| Numpy | PyTorch | |
| 43 | +| ---------------- | ------------------- | |
| 44 | +| np.empty((2, 3)) | torch.empty(2, 3) | |
| 45 | +| np.empty_like(x) | torch.empty_like(x) | |
| 46 | +| np.eye | torch.eye | |
| 47 | +| np.identity | torch.eye | |
| 48 | +| np.ones | torch.ones | |
| 49 | +| np.ones_like | torch.ones_like | |
| 50 | +| np.zeros | torch.zeros | |
| 51 | +| np.zeros_like | torch.zeros_like | |
| 52 | + |
| 53 | + |
| 54 | +### From existing data |
| 55 | + |
| 56 | +| Numpy | PyTorch | |
| 57 | +| ------------------------------------------------------------- | --------------------------------------------- | |
| 58 | +| np.array([[1, 2], [3, 4]]) | torch.tensor([[1, 2], [3, 4]]) | |
| 59 | +| np.array([3.2, 4.3], dtype=np.float16)<br>np.float16([3.2, 4.3]) | torch.tensor([3.2, 4.3], dtype=torch.float16) | |
| 60 | +| x.copy() | x.clone() | |
| 61 | +| np.fromfile(file) | torch.tensor(torch.Storage(file)) | |
| 62 | +| np.frombuffer | | |
| 63 | +| np.fromfunction | | |
| 64 | +| np.fromiter | | |
| 65 | +| np.fromstring | | |
| 66 | +| np.load | torch.load | |
| 67 | +| np.loadtxt | | |
| 68 | +| np.concatenate | torch.cat | |
| 69 | + |
| 70 | + |
| 71 | +### Numerical ranges |
| 72 | + |
| 73 | +| Numpy | PyTorch | |
| 74 | +| -------------------- | ----------------------- | |
| 75 | +| np.arange(10) | torch.arange(10) | |
| 76 | +| np.arange(2, 3, 0.1) | torch.arange(2, 3, 0.1) | |
| 77 | +| np.linspace | torch.linspace | |
| 78 | +| np.logspace | torch.logspace | |
| 79 | + |
| 80 | + |
| 81 | +### Linear algebra |
| 82 | + |
| 83 | +| Numpy | PyTorch | |
| 84 | +| ------ | -------- | |
| 85 | +| np.dot | torch.mm | |
| 86 | + |
| 87 | + |
| 88 | +### Building matrices |
| 89 | + |
| 90 | +| Numpy | PyTorch | |
| 91 | +| ------- | ---------- | |
| 92 | +| np.diag | torch.diag | |
| 93 | +| np.tril | torch.tril | |
| 94 | +| np.triu | torch.triu | |
| 95 | + |
| 96 | + |
| 97 | +### Attributes |
| 98 | + |
| 99 | +| Numpy | PyTorch | |
| 100 | +| --------- | ------------ | |
| 101 | +| x.shape | x.shape | |
| 102 | +| x.strides | x.stride() | |
| 103 | +| x.ndim | x.dim() | |
| 104 | +| x.data | x.data | |
| 105 | +| x.size | x.nelement() | |
| 106 | +| x.dtype | x.dtype | |
| 107 | + |
| 108 | + |
| 109 | +### Indexing |
| 110 | + |
| 111 | +| Numpy | PyTorch | |
| 112 | +| ------------------- | ---------------------------------------- | |
| 113 | +| x[0] | x[0] | |
| 114 | +| x[:, 0] | x[:, 0] | |
| 115 | +| x[indices] | x[indices] | |
| 116 | +| np.take(x, indices) | torch.take(x, torch.LongTensor(indices)) | |
| 117 | +| x[x != 0] | x[x != 0] | |
| 118 | + |
| 119 | + |
| 120 | +### Shape manipulation |
| 121 | + |
| 122 | +| Numpy | PyTorch | |
| 123 | +| -------------------------------------- | ------------------------ | |
| 124 | +| x.reshape | x.reshape; x.view | |
| 125 | +| x.resize() | x.resize_ | |
| 126 | +| | x.resize_as_ | |
| 127 | +| x.transpose | x.transpose<br> x.permute | |
| 128 | +| x.flatten | x.view(-1) | |
| 129 | +| x.squeeze() | x.squeeze() | |
| 130 | +| x[:, np.newaxis]<br>np.expand_dims(x, 1) | x.unsqueeze(1) | |
| 131 | + |
| 132 | + |
| 133 | +### Item selection and manipulation |
| 134 | + |
| 135 | +| Numpy | PyTorch | |
| 136 | +| -------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 137 | +| np.put | - | |
| 138 | +| x.put | x.put_ | |
| 139 | +| x = np.array([1, 2, 3])<br>x.repeat(2)<br># [1, 1, 2, 2, 3, 3] | x = torch.tensor([1, 2, 3])<br>x.repeat(2)<br># [1, 2, 3, 1, 2, 3]<br>x.repeat(2).reshape(2, -1).transpose(1, 0).reshape(-1)<br># [1, 1, 2, 2, 3, 3] | |
| 140 | +| np.tile(x, (3, 2)) | x.repeat(3, 2) | |
| 141 | +| np.choose | | |
| 142 | +| np.sort | sorted, indices = torch.sort(x, [dim]) | |
| 143 | +| np.argsort | sorted, indices = torch.sort(x, [dim]) | |
| 144 | +| np.nonzero | torch.nonzero | |
| 145 | +| np.where | torch.where | |
| 146 | +| x[::-1] | torch.flip(x, [0]) | |
| 147 | + |
| 148 | + |
| 149 | +### Calculation |
| 150 | + |
| 151 | +| Numpy | PyTorch | |
| 152 | +| ------------- | ------------------------------ | |
| 153 | +| x.min | x.min | |
| 154 | +| x.argmin | x.argmin | |
| 155 | +| x.max | x.max | |
| 156 | +| x.argmax | x.argmax | |
| 157 | +| x.clip | x.clamp | |
| 158 | +| x.round | x.round | |
| 159 | +| np.floor(x) | torch.floor(x); x.floor() | |
| 160 | +| np.ceil(x) | torch.ceil(x); x.ceil() | |
| 161 | +| x.trace | x.trace | |
| 162 | +| x.sum | x.sum | |
| 163 | +| x.sum(axis=0) | x.sum(0) | |
| 164 | +| x.cumsum | x.cumsum | |
| 165 | +| x.mean | x.mean | |
| 166 | +| x.std | x.std | |
| 167 | +| x.prod | x.prod | |
| 168 | +| x.cumprod | x.cumprod | |
| 169 | +| x.all | (x == 1).sum() == x.nelement() | |
| 170 | +| x.any | (x == 1).sum() > 0 | |
| 171 | + |
| 172 | + |
| 173 | +### Arithmetic and comparison operations |
| 174 | + |
| 175 | +| Numpy | PyTorch | |
| 176 | +| ---------------- | ------- | |
| 177 | +| np.less | x.lt | |
| 178 | +| np.less_equal | x.le | |
| 179 | +| np.greater | x.gt | |
| 180 | +| np.greater_equal | x.ge | |
| 181 | +| np.equal | x.eq | |
| 182 | +| np.not_equal | x.ne | |
| 183 | + |
| 184 | +### Random numbers |
| 185 | + |
| 186 | +| Numpy | PyTorch | |
| 187 | +| ------------------------ | ----------------- | |
| 188 | +| np.random.seed | torch.manual_seed | |
| 189 | +| np.random.permutation(5) | torch.randperm(5) | |
| 190 | + |
| 191 | + |
| 192 | +### Numerical operations |
| 193 | + |
| 194 | +| Numpy | PyTorch | |
| 195 | +| ------- | ---------- | |
| 196 | +| np.sign | torch.sign | |
| 197 | +| np.sqrt | torch.sqrt | |
| 198 | + |
| 199 | + |
0 commit comments