Repeat data of a tensor Args: repeats(int or a sequence): the number that the tensor need to repeat for axis (int):the axis to do repeat If it is None, then the repeated tensor will be flattened.If it isn't None, the repe
(self, repeats, axis)
| 378 | return _call_singa_func(self.data.Clone) |
| 379 | |
| 380 | def repeat(self, repeats, axis): |
| 381 | '''Repeat data of a tensor |
| 382 | |
| 383 | Args: |
| 384 | repeats(int or a sequence): the number that the tensor need to repeat for |
| 385 | axis (int):the axis to do repeat |
| 386 | If it is None, then the repeated tensor will be flattened.If it isn't None, |
| 387 | the repeats could be sequence, but it's size should match the axis's shape |
| 388 | |
| 389 | Returns: |
| 390 | the tensor which has been repeated |
| 391 | |
| 392 | ''' |
| 393 | t = Tensor() |
| 394 | t_ndim = self.ndim() |
| 395 | if isinstance(repeats, int) or isinstance(repeats, complex): |
| 396 | if repeats < 0: |
| 397 | raise ValueError( |
| 398 | "'repeats' should not be negative: {}".format(repeats)) |
| 399 | if axis != None and axis < 0: |
| 400 | axis += t_ndim |
| 401 | # broadcast = True |
| 402 | if axis is None: |
| 403 | axis = 9999 |
| 404 | t.shape = (product(self.shape) * repeats,) |
| 405 | Repeats = [ |
| 406 | repeats, |
| 407 | ] |
| 408 | t.data = self.data.Repeat(Repeats, axis) |
| 409 | elif axis >= 0: |
| 410 | t_shape = list(self.shape) |
| 411 | t_shape[axis] = self.shape[axis] * repeats |
| 412 | t.shape = tuple(t_shape) |
| 413 | Repeats = [ |
| 414 | repeats, |
| 415 | ] |
| 416 | t.data = self.data.Repeat(Repeats, axis) |
| 417 | |
| 418 | elif isinstance(repeats, tuple) or isinstance(repeats, list): |
| 419 | for rep in repeats: |
| 420 | if rep < 0: |
| 421 | raise ValueError( |
| 422 | "'repeats' should be int or sequence: {}".format( |
| 423 | repeats)) |
| 424 | |
| 425 | if axis != None and axis < 0: |
| 426 | axis += t_ndim |
| 427 | if axis is None: |
| 428 | raise ValueError( |
| 429 | "when axis us None, 'repeats' should be int: {}".format( |
| 430 | repeats)) |
| 431 | elif axis >= 0: |
| 432 | t_shape = list(self.shape) |
| 433 | t_shape[axis] = sum(repeats) |
| 434 | t.shape = tuple(t_shape) |
| 435 | t.data = self.data.Repeat(list(repeats), axis) |
| 436 | else: |
| 437 | raise ValueError('repeats should be int or sequence') |