New Usage Example: 1. paddle.Tensor() 2. paddle.Tensor(device="cpu") 3. paddle.Tensor(1,2,3) 4. paddle.Tensor(1,2,3, device="cpu") 5. paddle.Tensor([1,2,3]) 6. paddle.Tensor([1,2,3], device="cpu") 7. paddle.Tensor(data=[1,2,3])
(self, *args, **kwargs)
| 169 | original_init = Tensor.__init__ |
| 170 | |
| 171 | def new_init(self, *args, **kwargs): |
| 172 | """ |
| 173 | New Usage Example: |
| 174 | 1. paddle.Tensor() |
| 175 | 2. paddle.Tensor(device="cpu") |
| 176 | 3. paddle.Tensor(1,2,3) |
| 177 | 4. paddle.Tensor(1,2,3, device="cpu") |
| 178 | 5. paddle.Tensor([1,2,3]) |
| 179 | 6. paddle.Tensor([1,2,3], device="cpu") |
| 180 | 7. paddle.Tensor(data=[1,2,3]) |
| 181 | 8. paddle.Tensor(data=[1,2,3], device="cpu") |
| 182 | Original Usage Example: |
| 183 | 9. paddle.Tensor(value=data, place="cpu", persistable=False, zero_copy=False, name=None, stop_gradient=True) |
| 184 | """ |
| 185 | if 'device' in kwargs: |
| 186 | device = kwargs.pop('device') |
| 187 | else: |
| 188 | device = "cpu" |
| 189 | device = framework._get_paddle_place(device) |
| 190 | if len(args) == 0 and len(kwargs) == 0: # case 1, 2 |
| 191 | original_init( |
| 192 | self, |
| 193 | paddle.empty(shape=[0], dtype='float32', device=device), |
| 194 | place=device, |
| 195 | ) |
| 196 | return |
| 197 | if 'data' in kwargs: # case 7,8 |
| 198 | data = kwargs.pop('data') |
| 199 | original_init( |
| 200 | self, |
| 201 | paddle.tensor(data, dtype='float32', device=device), |
| 202 | place=device, |
| 203 | ) |
| 204 | elif len(args) == 1 and isinstance(args[0], (list, tuple)): |
| 205 | # case 5, 6 |
| 206 | original_init( |
| 207 | self, |
| 208 | paddle.tensor(args[0], dtype='float32', device=device), |
| 209 | place=device, |
| 210 | ) |
| 211 | elif ( |
| 212 | builtins.all(isinstance(arg, builtins.int) for arg in args) |
| 213 | and len(kwargs) == 0 |
| 214 | ): |
| 215 | # case 3, 4 |
| 216 | original_init( |
| 217 | self, |
| 218 | paddle.empty(shape=list(args), dtype='float32', device=device), |
| 219 | place=device, |
| 220 | ) |
| 221 | else: |
| 222 | original_init(self, *args, **kwargs) |
| 223 | |
| 224 | Tensor.__init__ = new_init |
| 225 |