File size: 37,281 Bytes
1f5470c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 |
"""Commonly used math operations not included in NumPy."""
from keras.src import backend
from keras.src.api_export import keras_export
from keras.src.backend import KerasTensor
from keras.src.backend import any_symbolic_tensors
from keras.src.ops.operation import Operation
from keras.src.ops.operation_utils import reduce_shape
def _segment_reduce_validation(data, segment_ids):
data_shape = data.shape
segment_ids_shape = segment_ids.shape
if len(segment_ids_shape) > 1:
raise ValueError(
"Argument `segment_ids` should be an 1-D vector, got shape: "
f"{len(segment_ids_shape)}. Consider either flatten input with "
"segment_ids.reshape((-1)) and "
"data.reshape((-1, ) + data.shape[len(segment_ids.shape):]) or "
"vectorize with vmap."
)
if (
segment_ids_shape[0] is not None
and data_shape[0] is not None
and segment_ids_shape[0] != data_shape[0]
):
raise ValueError(
"Argument `segment_ids` and `data` should have same leading "
f"dimension. Got {segment_ids_shape} v.s. "
f"{data_shape}."
)
class SegmentReduction(Operation):
def __init__(self, num_segments=None, sorted=False):
super().__init__()
self.num_segments = num_segments
self.sorted = sorted
def compute_output_spec(self, data, _):
output_shape = (self.num_segments,) + tuple(data.shape[1:])
return KerasTensor(shape=output_shape, dtype=data.dtype)
class SegmentSum(SegmentReduction):
def call(self, data, segment_ids):
_segment_reduce_validation(data, segment_ids)
return backend.math.segment_sum(
data,
segment_ids,
num_segments=self.num_segments,
sorted=self.sorted,
)
@keras_export("keras.ops.segment_sum")
def segment_sum(data, segment_ids, num_segments=None, sorted=False):
"""Computes the sum of segments in a tensor.
Args:
data: Input tensor.
segment_ids: A N-D tensor containing segment indices for each
element in `data`. Num dims for segment ids should be strictly
smaller or equal to number of dims in data.
num_segments: An integer representing the total number of
segments. If not specified, it is inferred from the maximum
value in `segment_ids`.
sorted: A boolean indicating whether `segment_ids` is sorted.
Defaults to `False`.
Returns:
A tensor containing the sum of segments, where each element
represents the sum of the corresponding segment in `data`.
Example:
>>> data = keras.ops.convert_to_tensor([1, 2, 10, 20, 100, 200])
>>> segment_ids = keras.ops.convert_to_tensor([0, 0, 1, 1, 2, 2])
>>> num_segments = 3
>>> keras.ops.segment_sum(data, segment_ids,num_segments)
array([3, 30, 300], dtype=int32)
"""
_segment_reduce_validation(data, segment_ids)
if any_symbolic_tensors((data,)):
return SegmentSum(num_segments, sorted).symbolic_call(data, segment_ids)
return backend.math.segment_sum(
data, segment_ids, num_segments=num_segments, sorted=sorted
)
class SegmentMax(SegmentReduction):
def call(self, data, segment_ids):
_segment_reduce_validation(data, segment_ids)
return backend.math.segment_max(
data,
segment_ids,
num_segments=self.num_segments,
sorted=self.sorted,
)
@keras_export("keras.ops.segment_max")
def segment_max(data, segment_ids, num_segments=None, sorted=False):
"""Computes the max of segments in a tensor.
Args:
data: Input tensor.
segment_ids: A N-D tensor containing segment indices for each
element in `data`. data.shape[:len(segment_ids.shape)] should match.
num_segments: An integer representing the total number of
segments. If not specified, it is inferred from the maximum
value in `segment_ids`.
sorted: A boolean indicating whether `segment_ids` is sorted.
Defaults to `False`.
Returns:
A tensor containing the max of segments, where each element
represents the max of the corresponding segment in `data`.
Example:
>>> data = keras.ops.convert_to_tensor([1, 2, 10, 20, 100, 200])
>>> segment_ids = keras.ops.convert_to_tensor([0, 0, 1, 1, 2, 2])
>>> num_segments = 3
>>> keras.ops.segment_max(data, segment_ids, num_segments)
array([2, 20, 200], dtype=int32)
"""
_segment_reduce_validation(data, segment_ids)
if any_symbolic_tensors((data,)):
return SegmentMax(num_segments, sorted).symbolic_call(data, segment_ids)
return backend.math.segment_max(
data, segment_ids, num_segments=num_segments, sorted=sorted
)
class TopK(Operation):
def __init__(self, k, sorted=False):
super().__init__()
self.k = k
self.sorted = sorted
def compute_output_spec(self, x):
output_shape = list(x.shape)
output_shape[-1] = self.k
# Return a tuple (values, indices).
return (
KerasTensor(shape=output_shape, dtype=x.dtype),
KerasTensor(shape=output_shape, dtype="int32"),
)
def call(self, x):
return backend.math.top_k(x, self.k, self.sorted)
@keras_export("keras.ops.top_k")
def top_k(x, k, sorted=True):
"""Finds the top-k values and their indices in a tensor.
Args:
x: Input tensor.
k: An integer representing the number of top elements to retrieve.
sorted: A boolean indicating whether to sort the output in
descending order. Defaults to `True`.
Returns:
A tuple containing two tensors. The first tensor contains the
top-k values, and the second tensor contains the indices of the
top-k values in the input tensor.
Example:
>>> x = keras.ops.convert_to_tensor([5, 2, 7, 1, 9, 3])
>>> values, indices = top_k(x, k=3)
>>> print(values)
array([9 7 5], shape=(3,), dtype=int32)
>>> print(indices)
array([4 2 0], shape=(3,), dtype=int32)
"""
if any_symbolic_tensors((x,)):
return TopK(k, sorted).symbolic_call(x)
return backend.math.top_k(x, k, sorted)
class InTopK(Operation):
def __init__(self, k):
super().__init__()
self.k = k
def compute_output_spec(self, targets, predictions):
return KerasTensor(shape=targets.shape, dtype="bool")
def call(self, targets, predictions):
return backend.math.in_top_k(targets, predictions, self.k)
@keras_export("keras.ops.in_top_k")
def in_top_k(targets, predictions, k):
"""Checks if the targets are in the top-k predictions.
Args:
targets: A tensor of true labels.
predictions: A tensor of predicted labels.
k: An integer representing the number of predictions to consider.
Returns:
A boolean tensor of the same shape as `targets`, where each element
indicates whether the corresponding target is in the top-k predictions.
Example:
>>> targets = keras.ops.convert_to_tensor([2, 5, 3])
>>> predictions = keras.ops.convert_to_tensor(
... [[0.1, 0.4, 0.6, 0.9, 0.5],
... [0.1, 0.7, 0.9, 0.8, 0.3],
... [0.1, 0.6, 0.9, 0.9, 0.5]])
>>> in_top_k(targets, predictions, k=3)
array([ True False True], shape=(3,), dtype=bool)
"""
if any_symbolic_tensors((targets, predictions)):
return InTopK(k).symbolic_call(targets, predictions)
return backend.math.in_top_k(targets, predictions, k)
class Logsumexp(Operation):
def __init__(self, axis=None, keepdims=False):
super().__init__()
self.axis = axis
self.keepdims = keepdims
def compute_output_spec(self, x):
output_shape = reduce_shape(x.shape, self.axis, self.keepdims)
return KerasTensor(shape=output_shape)
def call(self, x):
return backend.math.logsumexp(x, axis=self.axis, keepdims=self.keepdims)
@keras_export("keras.ops.logsumexp")
def logsumexp(x, axis=None, keepdims=False):
"""Computes the logarithm of sum of exponentials of elements in a tensor.
Args:
x: Input tensor.
axis: An integer or a tuple of integers specifying the axis/axes
along which to compute the sum. If `None`, the sum is computed
over all elements. Defaults to `None`.
keepdims: A boolean indicating whether to keep the dimensions of
the input tensor when computing the sum. Defaults to `False`.
Returns:
A tensor containing the logarithm of the sum of exponentials of
elements in `x`.
Example:
>>> x = keras.ops.convert_to_tensor([1., 2., 3.])
>>> logsumexp(x)
3.407606
"""
if any_symbolic_tensors((x,)):
return Logsumexp(axis, keepdims).symbolic_call(x)
return backend.math.logsumexp(x, axis=axis, keepdims=keepdims)
class ExtractSequences(Operation):
def __init__(self, sequence_length, sequence_stride):
super().__init__()
self.sequence_length = sequence_length
self.sequence_stride = sequence_stride
def compute_output_spec(self, x):
if len(x.shape) < 1:
raise ValueError(
f"Input should have rank >= 1. "
f"Received: input.shape = {x.shape}"
)
if x.shape[-1] is not None:
num_sequences = (
1 + (x.shape[-1] - self.sequence_length) // self.sequence_stride
)
else:
num_sequences = None
new_shape = x.shape[:-1] + (num_sequences, self.sequence_length)
return KerasTensor(shape=new_shape, dtype=x.dtype)
def call(self, x):
return backend.math.extract_sequences(
x,
sequence_length=self.sequence_length,
sequence_stride=self.sequence_stride,
)
@keras_export("keras.ops.extract_sequences")
def extract_sequences(x, sequence_length, sequence_stride):
"""Expands the dimension of last axis into sequences of `sequence_length`.
Slides a window of size `sequence_length` over the last axis of the input
with a stride of `sequence_stride`, replacing the last axis with
`[num_sequences, sequence_length]` sequences.
If the dimension along the last axis is N, the number of sequences can be
computed by:
`num_sequences = 1 + (N - sequence_length) // sequence_stride`
Args:
x: Input tensor.
sequence_length: An integer representing the sequences length.
sequence_stride: An integer representing the sequences hop size.
Returns:
A tensor of sequences with shape [..., num_sequences, sequence_length].
Example:
>>> x = keras.ops.convert_to_tensor([1, 2, 3, 4, 5, 6])
>>> extract_sequences(x, 3, 2)
array([[1, 2, 3],
[3, 4, 5]])
"""
if any_symbolic_tensors((x,)):
return ExtractSequences(sequence_length, sequence_stride).symbolic_call(
x
)
return backend.math.extract_sequences(x, sequence_length, sequence_stride)
class FFT(Operation):
def __init__(self, axis=-1):
super().__init__()
self.axis = axis
def compute_output_spec(self, x):
if not isinstance(x, (tuple, list)) or len(x) != 2:
raise ValueError(
"Input `x` should be a tuple of two tensors - real and "
f"imaginary. Received: x={x}"
)
real, imag = x
# Both real and imaginary parts should have the same shape.
if real.shape != imag.shape:
raise ValueError(
"Input `x` should be a tuple of two tensors - real and "
"imaginary. Both the real and imaginary parts should have the "
f"same shape. Received: x[0].shape = {real.shape}, "
f"x[1].shape = {imag.shape}"
)
# We are calculating 1D FFT. Hence, rank >= 1.
if len(real.shape) < 1:
raise ValueError(
f"Input should have rank >= 1. "
f"Received: input.shape = {real.shape}"
)
# The axis along which we are calculating FFT should be fully-defined.
m = real.shape[-1]
if m is None:
raise ValueError(
f"Input should have its {self.axis}th axis fully-defined. "
f"Received: input.shape = {real.shape}"
)
return (
KerasTensor(shape=real.shape, dtype=real.dtype),
KerasTensor(shape=imag.shape, dtype=imag.dtype),
)
def call(self, x):
return backend.math.fft(x)
@keras_export("keras.ops.fft")
def fft(x):
"""Computes the Fast Fourier Transform along last axis of input.
Args:
x: Tuple of the real and imaginary parts of the input tensor. Both
tensors in the tuple should be of floating type.
Returns:
A tuple containing two tensors - the real and imaginary parts of the
output tensor.
Example:
>>> x = (
... keras.ops.convert_to_tensor([1., 2.]),
... keras.ops.convert_to_tensor([0., 1.]),
... )
>>> fft(x)
(array([ 3., -1.], dtype=float32), array([ 1., -1.], dtype=float32))
"""
if any_symbolic_tensors(x):
return FFT().symbolic_call(x)
return backend.math.fft(x)
class FFT2(Operation):
def __init__(self):
super().__init__()
self.axes = (-2, -1)
def compute_output_spec(self, x):
if not isinstance(x, (tuple, list)) or len(x) != 2:
raise ValueError(
"Input `x` should be a tuple of two tensors - real and "
f"imaginary. Received: x={x}"
)
real, imag = x
# Both real and imaginary parts should have the same shape.
if real.shape != imag.shape:
raise ValueError(
"Input `x` should be a tuple of two tensors - real and "
"imaginary. Both the real and imaginary parts should have the "
f"same shape. Received: x[0].shape = {real.shape}, "
f"x[1].shape = {imag.shape}"
)
# We are calculating 2D FFT. Hence, rank >= 2.
if len(real.shape) < 2:
raise ValueError(
f"Input should have rank >= 2. "
f"Received: input.shape = {real.shape}"
)
# The axes along which we are calculating FFT should be fully-defined.
m = real.shape[self.axes[0]]
n = real.shape[self.axes[1]]
if m is None or n is None:
raise ValueError(
f"Input should have its {self.axes} axes fully-defined. "
f"Received: input.shape = {real.shape}"
)
return (
KerasTensor(shape=real.shape, dtype=real.dtype),
KerasTensor(shape=imag.shape, dtype=imag.dtype),
)
def call(self, x):
return backend.math.fft2(x)
@keras_export("keras.ops.fft2")
def fft2(x):
"""Computes the 2D Fast Fourier Transform along the last two axes of input.
Args:
x: Tuple of the real and imaginary parts of the input tensor. Both
tensors in the tuple should be of floating type.
Returns:
A tuple containing two tensors - the real and imaginary parts of the
output.
Example:
>>> x = (
... keras.ops.convert_to_tensor([[1., 2.], [2., 1.]]),
... keras.ops.convert_to_tensor([[0., 1.], [1., 0.]]),
... )
>>> fft2(x)
(array([[ 6., 0.],
[ 0., -2.]], dtype=float32), array([[ 2., 0.],
[ 0., -2.]], dtype=float32))
"""
if any_symbolic_tensors(x):
return FFT2().symbolic_call(x)
return backend.math.fft2(x)
class IFFT2(Operation):
def __init__(self):
super().__init__()
self.axes = (-2, -1)
def compute_output_spec(self, x):
if not isinstance(x, (tuple, list)) or len(x) != 2:
raise ValueError(
"Input `x` should be a tuple of two tensors - real and "
f"imaginary. Received: x={x}"
)
real, imag = x
# Both real and imaginary parts should have the same shape.
if real.shape != imag.shape:
raise ValueError(
"Input `x` should be a tuple of two tensors - real and "
"imaginary. Both the real and imaginary parts should have the "
f"same shape. Received: x[0].shape = {real.shape}, "
f"x[1].shape = {imag.shape}"
)
# We are calculating 2D IFFT. Hence, rank >= 2.
if len(real.shape) < 2:
raise ValueError(
f"Input should have rank >= 2. "
f"Received: input.shape = {real.shape}"
)
# The axes along which we are calculating IFFT should be fully-defined.
m = real.shape[self.axes[0]]
n = real.shape[self.axes[1]]
if m is None or n is None:
raise ValueError(
f"Input should have its {self.axes} axes fully-defined. "
f"Received: input.shape = {real.shape}"
)
return (
KerasTensor(shape=real.shape, dtype=real.dtype),
KerasTensor(shape=imag.shape, dtype=imag.dtype),
)
def call(self, x):
return backend.math.ifft2(x)
@keras_export("keras.ops.ifft2")
def ifft2(x):
"""Computes the 2D Inverse Fast Fourier Transform along the last two axes of
input.
Args:
x: Tuple of the real and imaginary parts of the input tensor. Both
tensors in the tuple should be of floating type.
Returns:
A tuple containing two tensors - the real and imaginary parts of the
output.
Example:
>>> x = (
... keras.ops.convert_to_tensor([[1., 2.], [2., 1.]]),
... keras.ops.convert_to_tensor([[0., 1.], [1., 0.]]),
... )
>>> ifft2(x)
(array([[ 6., 0.],
[ 0., -2.]], dtype=float32), array([[ 2., 0.],
[ 0., -2.]], dtype=float32))
"""
if any_symbolic_tensors(x):
return IFFT2().symbolic_call(x)
return backend.math.ifft2(x)
class RFFT(Operation):
def __init__(self, fft_length=None):
super().__init__()
self.fft_length = fft_length
def compute_output_spec(self, x):
# We are calculating 1D RFFT. Hence, rank >= 1.
if len(x.shape) < 1:
raise ValueError(
f"Input should have rank >= 1. "
f"Received: input.shape = {x.shape}"
)
if self.fft_length is not None:
new_last_dimension = self.fft_length // 2 + 1
else:
if x.shape[-1] is not None:
new_last_dimension = x.shape[-1] // 2 + 1
else:
new_last_dimension = None
new_shape = x.shape[:-1] + (new_last_dimension,)
return (
KerasTensor(shape=new_shape, dtype=x.dtype),
KerasTensor(shape=new_shape, dtype=x.dtype),
)
def call(self, x):
return backend.math.rfft(x, fft_length=self.fft_length)
@keras_export("keras.ops.rfft")
def rfft(x, fft_length=None):
"""Real-valued Fast Fourier Transform along the last axis of the input.
Computes the 1D Discrete Fourier Transform of a real-valued signal over the
inner-most dimension of input.
Since the Discrete Fourier Transform of a real-valued signal is
Hermitian-symmetric, RFFT only returns the `fft_length / 2 + 1` unique
components of the FFT: the zero-frequency term, followed by the
`fft_length / 2` positive-frequency terms.
Along the axis RFFT is computed on, if `fft_length` is smaller than the
corresponding dimension of the input, the dimension is cropped. If it is
larger, the dimension is padded with zeros.
Args:
x: Input tensor.
fft_length: An integer representing the number of the fft length. If not
specified, it is inferred from the length of the last axis of `x`.
Defaults to `None`.
Returns:
A tuple containing two tensors - the real and imaginary parts of the
output.
Examples:
>>> x = keras.ops.convert_to_tensor([0.0, 1.0, 2.0, 3.0, 4.0])
>>> rfft(x)
(array([10.0, -2.5, -2.5]), array([0.0, 3.4409548, 0.81229924]))
>>> rfft(x, 3)
(array([3.0, -1.5]), array([0.0, 0.8660254]))
"""
if any_symbolic_tensors((x,)):
return RFFT(fft_length).symbolic_call(x)
return backend.math.rfft(x, fft_length)
class IRFFT(Operation):
def __init__(self, fft_length=None):
super().__init__()
self.fft_length = fft_length
def compute_output_spec(self, x):
if not isinstance(x, (tuple, list)) or len(x) != 2:
raise ValueError(
"Input `x` should be a tuple of two tensors - real and "
f"imaginary. Received: x={x}"
)
real, imag = x
# Both real and imaginary parts should have the same shape.
if real.shape != imag.shape:
raise ValueError(
"Input `x` should be a tuple of two tensors - real and "
"imaginary. Both the real and imaginary parts should have the "
f"same shape. Received: x[0].shape = {real.shape}, "
f"x[1].shape = {imag.shape}"
)
# We are calculating 1D IRFFT. Hence, rank >= 1.
if len(real.shape) < 1:
raise ValueError(
f"Input should have rank >= 1. "
f"Received: input.shape = {real.shape}"
)
if self.fft_length is not None:
new_last_dimension = self.fft_length
else:
if real.shape[-1] is not None:
new_last_dimension = 2 * (real.shape[-1] - 1)
else:
new_last_dimension = None
new_shape = real.shape[:-1] + (new_last_dimension,)
return KerasTensor(shape=new_shape, dtype=real.dtype)
def call(self, x):
return backend.math.irfft(x, fft_length=self.fft_length)
@keras_export("keras.ops.irfft")
def irfft(x, fft_length=None):
"""Inverse real-valued Fast Fourier transform along the last axis.
Computes the inverse 1D Discrete Fourier Transform of a real-valued signal
over the inner-most dimension of input.
The inner-most dimension of the input is assumed to be the result of RFFT:
the `fft_length / 2 + 1` unique components of the DFT of a real-valued
signal. If `fft_length` is not provided, it is computed from the size of the
inner-most dimension of the input `(fft_length = 2 * (inner - 1))`. If the
FFT length used to compute is odd, it should be provided since it cannot
be inferred properly.
Along the axis IRFFT is computed on, if `fft_length / 2 + 1` is smaller than
the corresponding dimension of the input, the dimension is cropped. If it is
larger, the dimension is padded with zeros.
Args:
x: Tuple of the real and imaginary parts of the input tensor. Both
tensors in the tuple should be of floating type.
fft_length: An integer representing the number of the fft length. If not
specified, it is inferred from the length of the last axis of `x`.
Defaults to `None`.
Returns:
A tensor containing the inverse real-valued Fast Fourier Transform
along the last axis of `x`.
Examples:
>>> real = keras.ops.convert_to_tensor([0.0, 1.0, 2.0, 3.0, 4.0])
>>> imag = keras.ops.convert_to_tensor([0.0, 1.0, 2.0, 3.0, 4.0])
>>> irfft((real, imag))
array([0.66666667, -0.9106836, 0.24401694])
>>> irfft(rfft(real, 5), 5)
array([0.0, 1.0, 2.0, 3.0, 4.0])
"""
if any_symbolic_tensors(x):
return IRFFT(fft_length).symbolic_call(x)
return backend.math.irfft(x, fft_length)
class STFT(Operation):
def __init__(
self,
sequence_length,
sequence_stride,
fft_length,
window="hann",
center=True,
):
super().__init__()
self.sequence_length = sequence_length
self.sequence_stride = sequence_stride
self.fft_length = fft_length
self.window = window
self.center = center
def compute_output_spec(self, x):
if x.shape[-1] is not None:
padded = 0 if self.center is False else (self.fft_length // 2) * 2
num_sequences = (
1
+ (x.shape[-1] + padded - self.fft_length)
// self.sequence_stride
)
else:
num_sequences = None
new_shape = x.shape[:-1] + (num_sequences, self.fft_length // 2 + 1)
return (
KerasTensor(shape=new_shape, dtype=x.dtype),
KerasTensor(shape=new_shape, dtype=x.dtype),
)
def call(self, x):
return backend.math.stft(
x,
sequence_length=self.sequence_length,
sequence_stride=self.sequence_stride,
fft_length=self.fft_length,
window=self.window,
center=self.center,
)
@keras_export("keras.ops.stft")
def stft(
x, sequence_length, sequence_stride, fft_length, window="hann", center=True
):
"""Short-Time Fourier Transform along the last axis of the input.
The STFT computes the Fourier transform of short overlapping windows of the
input. This giving frequency components of the signal as they change over
time.
Args:
x: Input tensor.
sequence_length: An integer representing the sequence length.
sequence_stride: An integer representing the sequence hop size.
fft_length: An integer representing the size of the FFT to apply. If not
specified, uses the smallest power of 2 enclosing `sequence_length`.
window: A string, a tensor of the window or `None`. If `window` is a
string, available values are `"hann"` and `"hamming"`. If `window`
is a tensor, it will be used directly as the window and its length
must be `sequence_length`. If `window` is `None`, no windowing is
used. Defaults to `"hann"`.
center: Whether to pad `x` on both sides so that the t-th sequence is
centered at time `t * sequence_stride`. Otherwise, the t-th sequence
begins at time `t * sequence_stride`. Defaults to `True`.
Returns:
A tuple containing two tensors - the real and imaginary parts of the
STFT output.
Example:
>>> x = keras.ops.convert_to_tensor([0.0, 1.0, 2.0, 3.0, 4.0])
>>> stft(x, 3, 2, 3)
(array([[0.75, -0.375],
[3.75, -1.875],
[5.25, -2.625]]), array([[0.0, 0.64951905],
[0.0, 0.64951905],
[0.0, -0.64951905]]))
"""
if any_symbolic_tensors((x,)):
return STFT(
sequence_length=sequence_length,
sequence_stride=sequence_stride,
fft_length=fft_length,
window=window,
center=center,
).symbolic_call(x)
return backend.math.stft(
x,
sequence_length=sequence_length,
sequence_stride=sequence_stride,
fft_length=fft_length,
window=window,
center=center,
)
class ISTFT(Operation):
def __init__(
self,
sequence_length,
sequence_stride,
fft_length,
length=None,
window="hann",
center=True,
):
super().__init__()
self.sequence_length = sequence_length
self.sequence_stride = sequence_stride
self.fft_length = fft_length
self.length = length
self.window = window
self.center = center
def compute_output_spec(self, x):
if not isinstance(x, (tuple, list)) or len(x) != 2:
raise ValueError(
"Input `x` should be a tuple of two tensors - real and "
f"imaginary. Received: x={x}"
)
real, imag = x
# Both real and imaginary parts should have the same shape.
if real.shape != imag.shape:
raise ValueError(
"Input `x` should be a tuple of two tensors - real and "
"imaginary. Both the real and imaginary parts should have the "
f"same shape. Received: x[0].shape = {real.shape}, "
f"x[1].shape = {imag.shape}"
)
if len(real.shape) < 2:
raise ValueError(
f"Input should have rank >= 2. "
f"Received: input.shape = {real.shape}"
)
if real.shape[-2] is not None:
output_size = (
real.shape[-2] - 1
) * self.sequence_stride + self.fft_length
if self.length is not None:
output_size = self.length
elif self.center:
output_size = output_size - (self.fft_length // 2) * 2
else:
output_size = None
new_shape = real.shape[:-2] + (output_size,)
return KerasTensor(shape=new_shape, dtype=real.dtype)
def call(self, x):
return backend.math.istft(
x,
sequence_length=self.sequence_length,
sequence_stride=self.sequence_stride,
fft_length=self.fft_length,
length=self.length,
window=self.window,
center=self.center,
)
@keras_export("keras.ops.istft")
def istft(
x,
sequence_length,
sequence_stride,
fft_length,
length=None,
window="hann",
center=True,
):
"""Inverse Short-Time Fourier Transform along the last axis of the input.
To reconstruct an original waveform, the parameters should be the same in
`stft`.
Args:
x: Tuple of the real and imaginary parts of the input tensor. Both
tensors in the tuple should be of floating type.
sequence_length: An integer representing the sequence length.
sequence_stride: An integer representing the sequence hop size.
fft_length: An integer representing the size of the FFT that produced
`stft`. Should be of type `int32`.
length: An integer representing the output is clipped to exactly length.
If not specified, no padding or clipping take place. Defaults to
`None`.
window: A string, a tensor of the window or `None`. If `window` is a
string, available values are `"hann"` and `"hamming"`. If `window`
is a tensor, it will be used directly as the window and its length
must be `sequence_length`. If `window` is `None`, no windowing is
used. Defaults to `"hann"`.
center: Whether `x` was padded on both sides so that the t-th sequence
is centered at time `t * sequence_stride`. Defaults to `True`.
Returns:
A tensor containing the inverse Short-Time Fourier Transform along the
last axis of `x`.
Example:
>>> x = keras.ops.convert_to_tensor([0.0, 1.0, 2.0, 3.0, 4.0])
>>> istft(stft(x, 1, 1, 1), 1, 1, 1)
array([0.0, 1.0, 2.0, 3.0, 4.0])
"""
if any_symbolic_tensors(x):
return ISTFT(
sequence_length=sequence_length,
sequence_stride=sequence_stride,
fft_length=fft_length,
window=window,
center=center,
).symbolic_call(x)
return backend.math.istft(
x,
sequence_length=sequence_length,
sequence_stride=sequence_stride,
fft_length=fft_length,
length=length,
window=window,
center=center,
)
class Rsqrt(Operation):
def call(self, x):
x = backend.convert_to_tensor(x)
return backend.math.rsqrt(x)
def compute_output_spec(self, x):
return KerasTensor(x.shape, dtype=x.dtype)
@keras_export("keras.ops.rsqrt")
def rsqrt(x):
"""Computes reciprocal of square root of x element-wise.
Args:
x: input tensor
Returns:
A tensor with the same dtype as `x`.
Example:
>>> x = keras.ops.convert_to_tensor([1.0, 10.0, 100.0])
>>> keras.ops.rsqrt(x)
array([1.0, 0.31622776, 0.1], dtype=float32)
"""
if any_symbolic_tensors((x,)):
return Rsqrt().symbolic_call(x)
x = backend.convert_to_tensor(x)
return backend.math.rsqrt(x)
class Erf(Operation):
def compute_output_spec(self, x):
return KerasTensor(shape=x.shape, dtype=x.dtype)
def call(self, x):
return backend.math.erf(x)
@keras_export("keras.ops.erf")
def erf(x):
"""Computes the error function of `x`, element-wise.
Args:
x: Input tensor.
Returns:
A tensor with the same dtype as `x`.
Example:
>>> x = np.array([-3.0, -2.0, -1.0, 0.0, 1.0])
>>> keras.ops.erf(x)
array([-0.99998 , -0.99532, -0.842701, 0., 0.842701], dtype=float32)
"""
if any_symbolic_tensors((x,)):
return Erf().symbolic_call(x)
x = backend.convert_to_tensor(x)
return backend.math.erf(x)
class Erfinv(Operation):
def compute_output_spec(self, x):
return KerasTensor(shape=x.shape, dtype=x.dtype)
def call(self, x):
return backend.math.erfinv(x)
@keras_export("keras.ops.erfinv")
def erfinv(x):
"""Computes the inverse error function of `x`, element-wise.
Args:
x: Input tensor.
Returns:
A tensor with the same dtype as `x`.
Example:
>>> x = np.array([-0.5, -0.2, -0.1, 0.0, 0.3])
>>> keras.ops.erfinv(x)
array([-0.47694, -0.17914, -0.08886, 0. , 0.27246], dtype=float32)
"""
if any_symbolic_tensors((x,)):
return Erfinv().symbolic_call(x)
x = backend.convert_to_tensor(x)
return backend.math.erfinv(x)
class Logdet(Operation):
def __init__(self):
super().__init__()
def call(self, x):
return backend.math.logdet(x)
def compute_output_spec(self, x):
return KerasTensor(x.shape[:-2], dtype=x.dtype)
@keras_export(["keras.ops.logdet"])
def logdet(x):
"""Computes log of the determinant of a hermitian positive definite matrix.
Args:
x: Input matrix. It must 2D and square.
Returns:
The natural log of the determinant of matrix.
"""
if any_symbolic_tensors((x,)):
return Logdet().symbolic_call(x)
return backend.math.logdet(x)
class ViewAsComplex(Operation):
def call(self, x):
x = backend.convert_to_tensor(x)
if len(x.shape) < 1 or x.shape[-1] != 2:
raise ValueError(
"Input tensor's last dimension must be 2 (real and imaginary)."
)
return x[..., 0] + 1j * x[..., 1]
def compute_output_spec(self, x):
return KerasTensor(shape=x.shape[:-1], dtype="complex64")
class ViewAsReal(Operation):
def call(self, x):
x = backend.convert_to_tensor(x)
real_part = backend.numpy.real(x)
imag_part = backend.numpy.imag(x)
return backend.numpy.stack((real_part, imag_part), axis=-1)
def compute_output_spec(self, x):
return KerasTensor(shape=x.shape + (2,), dtype="float32")
@keras_export("keras.ops.view_as_complex")
def view_as_complex(x):
"""Converts a real tensor with shape `(..., 2)` to a complex tensor,
where the last dimension represents the real and imaginary components
of a complex tensor.
Args:
x: A real tensor with last dimension of size 2.
Returns:
A complex tensor with shape `x.shape[:-1]`.
Example:
```
>>> import numpy as np
>>> from keras import ops
>>> real_imag = np.array([[1.0, 2.0], [3.0, 4.0]])
>>> complex_tensor = ops.view_as_complex(real_imag)
>>> complex_tensor
array([1.+2.j, 3.+4.j])
```
"""
if any_symbolic_tensors((x,)):
return ViewAsComplex().symbolic_call(x)
x = backend.convert_to_tensor(x)
if len(x.shape) < 1 or x.shape[-1] != 2:
raise ValueError(
"Last dimension of input must be size 2 (real and imaginary). "
f"Received shape: {x.shape}"
)
real_part = x[..., 0]
imag_part = x[..., 1]
return backend.cast(real_part, dtype="complex64") + 1j * backend.cast(
imag_part, dtype="complex64"
)
@keras_export("keras.ops.view_as_real")
def view_as_real(x):
"""Converts a complex tensor to a real tensor with shape `(..., 2)`,
where the last dimension represents the real and imaginary components.
Args:
x: A complex tensor.
Returns:
A real tensor where the last dimension contains the
real and imaginary parts.
Example:
```
>>> import numpy as np
>>> from keras import ops
>>> complex_tensor = np.array([1 + 2j, 3 + 4j])
>>> real = ops.view_as_real(complex_tensor)
>>> real
array([[1., 2.],
[3., 4.]])
```
"""
if any_symbolic_tensors((x,)):
return ViewAsReal().symbolic_call(x)
x = backend.convert_to_tensor(x)
real_part = backend.numpy.real(x)
imag_part = backend.numpy.imag(x)
return backend.numpy.stack((real_part, imag_part), axis=-1)
|