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)
| 409 | return _call_singa_func(self.data.Clone) |
| 410 | |
| 411 | def repeat(self, repeats, axis): |
| 412 | '''Repeat data of a tensor |
| 413 | |
| 414 | Args: |
| 415 | repeats(int or a sequence): the number that the tensor need to repeat for |
| 416 | axis (int):the axis to do repeat |
| 417 | If it is None, then the repeated tensor will be flattened.If it isn't None, |
| 418 | the repeats could be sequence, but it's size should match the axis's shape |
| 419 | |
| 420 | Returns: |
| 421 | the tensor which has been repeated |
| 422 | |
| 423 | ''' |
| 424 | t = Tensor() |
| 425 | t_ndim = self.ndim() |
| 426 | if isinstance(repeats, int) or isinstance(repeats, complex): |
| 427 | if repeats < 0: |
| 428 | raise ValueError( |
| 429 | "'repeats' should not be negative: {}".format(repeats)) |
| 430 | if axis != None and axis < 0: |
| 431 | axis += t_ndim |
| 432 | # broadcast = True |
| 433 | if axis is None: |
| 434 | axis = 9999 |
| 435 | t.shape = (product(self.shape) * repeats,) |
| 436 | Repeats = [ |
| 437 | repeats, |
| 438 | ] |
| 439 | t.data = self.data.Repeat(Repeats, axis) |
| 440 | elif axis >= 0: |
| 441 | t_shape = list(self.shape) |
| 442 | t_shape[axis] = self.shape[axis] * repeats |
| 443 | t.shape = tuple(t_shape) |
| 444 | Repeats = [ |
| 445 | repeats, |
| 446 | ] |
| 447 | t.data = self.data.Repeat(Repeats, axis) |
| 448 | |
| 449 | elif isinstance(repeats, tuple) or isinstance(repeats, list): |
| 450 | for rep in repeats: |
| 451 | if rep < 0: |
| 452 | raise ValueError( |
| 453 | "'repeats' should be int or sequence: {}".format( |
| 454 | repeats)) |
| 455 | |
| 456 | if axis != None and axis < 0: |
| 457 | axis += t_ndim |
| 458 | if axis is None: |
| 459 | raise ValueError( |
| 460 | "when axis us None, 'repeats' should be int: {}".format( |
| 461 | repeats)) |
| 462 | elif axis >= 0: |
| 463 | t_shape = list(self.shape) |
| 464 | t_shape[axis] = sum(repeats) |
| 465 | t.shape = tuple(t_shape) |
| 466 | t.data = self.data.Repeat(list(repeats), axis) |
| 467 | else: |
| 468 | raise ValueError('repeats should be int or sequence') |