/
activations.py
793 lines (677 loc) · 20 KB
/
activations.py
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
"""Collection of Ivy activation functions."""
from typing import Union, Optional, Callable, Literal
# local
import ivy
from ivy.utils.backend import current_backend
from ivy.func_wrapper import (
handle_array_function,
handle_out_argument,
to_native_arrays_and_back,
handle_nestable,
handle_array_like_without_promotion,
handle_device,
handle_complex_input,
handle_backend_invalid,
)
from ivy.utils.exceptions import handle_exceptions
def _gelu_jax_like(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
fn_original: Optional[Callable] = None,
approximate: bool = False,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
# We don't have the exact implementation
# cuz the erf function doesn't work on complex numbers
return fn_original(x, approximate=True, out=out)
@handle_exceptions
@handle_backend_invalid
@handle_nestable
@handle_array_like_without_promotion
@handle_out_argument
@to_native_arrays_and_back
@handle_array_function
@handle_device
@handle_complex_input
def gelu(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
approximate: bool = False,
complex_mode: Literal["split", "magnitude", "jax"] = "jax",
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Apply the Gaussian error linear unit (GELU) activation function.
Parameters
----------
x
Input array.
approximate
Whether to approximate, default is ``True``. An approximation is always used if
the input array is complex.
complex_mode
optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
out
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
Returns
-------
ret
The input array with gelu applied element-wise.
Examples
--------
With :class:`ivy.Array` input:
>>> x = ivy.array([-1.2, -0.6, 1.5])
>>> y = ivy.gelu(x)
>>> y
ivy.array([-0.138, -0.165, 1.4])
With :class:`ivy.NativeArray` input:
>>> x = ivy.native_array([-1.3, 3.8, 2.1])
>>> y = ivy.gelu(x)
>>> y
ivy.array([-0.126, 3.8, 2.06])
With :class:`ivy.Container` input:
>>> x = ivy.Container(a=ivy.array([1., 2.]), b=ivy.array([-0.9, -1.]))
>>> y = ivy.gelu(x)
>>> y
{
a: ivy.array([0.841, 1.95]),
b: ivy.array([-0.166, -0.159])
}
"""
return current_backend(x).gelu(x, approximate=approximate, out=out)
gelu.jax_like = _gelu_jax_like
def _leaky_relu_jax_like(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
fn_original: Optional[Callable] = None,
alpha: float = 0.2,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
return ivy.where(
(
ivy.logical_or(
ivy.real(x) < 0, ivy.logical_and(ivy.real(x) == 0, ivy.imag(x) < 0)
)
),
ivy.astype(x * alpha, x.dtype),
x,
)
@handle_exceptions
@handle_backend_invalid
@handle_nestable
@handle_array_like_without_promotion
@handle_out_argument
@to_native_arrays_and_back
@handle_array_function
@handle_device
@handle_complex_input
def leaky_relu(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
alpha: float = 0.2,
complex_mode: Literal["split", "magnitude", "jax"] = "jax",
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Apply the leaky rectified linear unit function element-wise.
If the input is complex, then by default each element is scaled by `alpha` if
either its real part is strictly negative or if its real part is zero and its
imaginary part is negative. This behaviour can be changed by specifying a different
`complex_mode`.
Parameters
----------
x
Input array.
alpha
Negative slope for ReLU.
complex_mode
optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
out
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
Returns
-------
ret
The input array with leaky relu applied element-wise.
Examples
--------
With :class:`ivy.Array` input:
>>> x = ivy.array([0.39, -0.85])
>>> y = ivy.leaky_relu(x)
>>> print(y)
ivy.array([ 0.39, -0.17])
>>> x = ivy.array([1.5, 0.7, -2.4])
>>> y = ivy.zeros(3)
>>> ivy.leaky_relu(x, out=y)
>>> print(y)
ivy.array([ 1.5 , 0.7 , -0.48])
>>> x = ivy.array([[1.1, 2.2, 3.3],
... [-4.4, -5.5, -6.6]])
>>> ivy.leaky_relu(x, out=x)
>>> print(x)
ivy.array([[ 1.1 , 2.2 , 3.3 ],
[-0.88, -1.1 , -1.32]])
With :class:`ivy.Container` input:
>>> x = ivy.Container(a=ivy.array([0.0, -1.2]), b=ivy.array([0.4, -0.2]))
>>> x = ivy.leaky_relu(x, out=x)
>>> print(x)
{
a: ivy.array([0., -0.24000001]),
b: ivy.array([0.40000001, -0.04])
}
"""
return current_backend(x).leaky_relu(x, alpha=alpha, out=out)
leaky_relu.jax_like = _leaky_relu_jax_like
@handle_exceptions
@handle_backend_invalid
@handle_nestable
@handle_array_like_without_promotion
@handle_out_argument
@to_native_arrays_and_back
@handle_array_function
@handle_device
def log_softmax(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
axis: Optional[int] = None,
complex_mode: Literal["split", "magnitude", "jax"] = "jax",
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Apply the log_softmax function element-wise.
Parameters
----------
x
Input array.
axis
The dimension log_softmax would be performed on. The default is ``None``.
complex_mode
optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
out
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
Returns
-------
ret
The output array with log_softmax applied element-wise to input.
Examples
--------
With :class:`ivy.Array` input:
>>> x = ivy.array([-1.0, -0.98])
>>> y = ivy.log_softmax(x)
>>> print(y)
ivy.array([-0.703, -0.683])
>>> x = ivy.array([1.0, 2.0, 3.0])
>>> y = ivy.log_softmax(x)
>>> print(y)
ivy.array([-2.41, -1.41, -0.408])
With :class:`ivy.NativeArray` input:
>>> x = ivy.native_array([1.5, 0.5, 1.0])
>>> y = ivy.log_softmax(x)
>>> print(y)
ivy.array([-0.68, -1.68, -1.18])
With :class:`ivy.Container` input:
>>> x = ivy.Container(a=ivy.array([1.5, 0.5, 1.0]))
>>> y = ivy.log_softmax(x)
>>> print(y)
{
a: ivy.array([-0.68, -1.68, -1.18])
}
>>> x = ivy.Container(a=ivy.array([1.0, 2.0]), b=ivy.array([0.4, -0.2]))
>>> y = ivy.log_softmax(x)
>>> print(y)
{
a: ivy.array([-1.31, -0.313]),
b: ivy.array([-0.437, -1.04])
}
"""
return current_backend(x).log_softmax(x, axis=axis, out=out)
def _relu_jax_like(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
fn_original=None,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
return ivy.where(
(
ivy.logical_or(
ivy.real(x) < 0, ivy.logical_and(ivy.real(x) == 0, ivy.imag(x) < 0)
)
),
ivy.array(0.0, dtype=x.dtype),
x,
)
@handle_exceptions
@handle_backend_invalid
@handle_nestable
@handle_array_like_without_promotion
@handle_out_argument
@to_native_arrays_and_back
@handle_array_function
@handle_device
@handle_complex_input
def relu(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
complex_mode: Literal["split", "magnitude", "jax"] = "jax",
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Apply the rectified linear unit function element-wise.
If the input is complex, then by default each element is set to zero if
either its real part is strictly negative or if its real part is zero and its
imaginary part is negative. This behaviour can be changed by specifying a different
`complex_mode`.
Parameters
----------
x
input array
complex_mode
optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
out
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
Returns
-------
ret
an array containing the rectified linear unit activation of each element in
``x``.
Examples
--------
With :class:`ivy.Array` input:
>>> x = ivy.array([-1., 0., 1.])
>>> y = ivy.relu(x)
>>> print(y)
ivy.array([0., 0., 1.])
>>> x = ivy.array([1.5, 0.7, -2.4])
>>> y = ivy.zeros(3)
>>> ivy.relu(x, out = y)
>>> print(y)
ivy.array([1.5, 0.7, 0.])
With :class:`ivy.Container` input:
>>> x = ivy.Container(a=ivy.array([1.0, -1.2]), b=ivy.array([0.4, -0.2]))
>>> x = ivy.relu(x, out=x)
>>> print(x)
{
a: ivy.array([1., 0.]),
b: ivy.array([0.40000001, 0.])
}
"""
return current_backend(x).relu(x, out=out)
relu.jax_like = _relu_jax_like
@handle_exceptions
@handle_backend_invalid
@handle_nestable
@handle_array_like_without_promotion
@handle_out_argument
@to_native_arrays_and_back
@handle_array_function
@handle_device
@handle_complex_input
def sigmoid(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
complex_mode: Literal["split", "magnitude", "jax"] = "jax",
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Apply the sigmoid function element-wise.
Parameters
----------
x
input array.
complex_mode
optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
out
optional output array, for writing the result to. It must have a shape that the
input broadcast to.
default: None
Returns
-------
ret
an array containing the sigmoid activation of each element in ``x``.
sigmoid activation of x is defined as 1/(1+exp(-x)).
Examples
--------
With :class:`ivy.Array` input:
>>> x = ivy.array([-1.0, 1.0, 2.0])
>>> y = ivy.sigmoid(x)
>>> print(y)
ivy.array([0.2689414 , 0.7310586 , 0.88079703])
>>> x = ivy.array([-1.0, 1.0, 2.0])
>>> y = ivy.zeros(3)
>>> ivy.sigmoid(x, out=y)
>>> print(y)
ivy.array([0.2689414 , 0.7310586 , 0.88079703])
With :class:`ivy.Container` input:
>>> x = ivy.Container(a=ivy.array([0.]),
... b=ivy.Container(c=ivy.array([1.]),
... d=ivy.array([2.])))
>>> y = ivy.sigmoid(x)
>>> print(y)
{
a: ivy.array([0.5]),
b: {
c: ivy.array([0.7310586]),
d: ivy.array([0.88079703])
}
}
>>> x = ivy.Container(a=ivy.array([0.]),
... b=ivy.Container(c=ivy.array([1.]),
... d=ivy.array([2.])))
>>> y = ivy.Container(a=ivy.array([0.]),
... b=ivy.Container(c=ivy.array([0.]),
... d=ivy.array([0.])))
>>> ivy.sigmoid(x, out=y)
>>> print(y)
{
a: ivy.array([0.5]),
b: {
c: ivy.array([0.7310586]),
d: ivy.array([0.88079703])
}
}
"""
return current_backend(x).sigmoid(x, out=out)
@handle_exceptions
@handle_backend_invalid
@handle_nestable
@handle_array_like_without_promotion
@handle_out_argument
@to_native_arrays_and_back
@handle_array_function
@handle_device
@handle_complex_input
def softmax(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
axis: Optional[int] = None,
complex_mode: Literal["split", "magnitude", "jax"] = "jax",
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Apply the softmax function element-wise.
Parameters
----------
x
Input array.
axis
The dimension softmax would be performed on. The default is ``None``.
complex_mode
optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
out
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
Returns
-------
ret
The input array with softmax applied element-wise.
Examples
--------
With :class:`ivy.Array` input:
>>> x = ivy.array([1.0, 0, 1.0])
>>> y = ivy.softmax(x)
>>> print(y)
ivy.array([0.422, 0.155, 0.422])
>>> x = ivy.array([[1.1, 2.2, 3.3],
... [4.4, 5.5, 6.6]])
>>> y = ivy.softmax(x, axis = 1)
>>> print(y)
ivy.array([[0.0768, 0.231 , 0.693 ],
[0.0768, 0.231 , 0.693 ]])
"""
return current_backend(x).softmax(x, axis=axis, out=out)
def _wrap_between(y, a):
"""Wrap y between [-a, a]"""
a = ivy.array(a, dtype=y.dtype)
a2 = ivy.array(2 * a, dtype=y.dtype)
zero = ivy.array(0, dtype=y.dtype)
rem = ivy.remainder(ivy.add(y, a), a2)
rem = ivy.where(rem < zero, rem + a2, rem) - a
return rem
def _softplus_jax_like(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
fn_original=None,
beta: Optional[Union[int, float]] = None,
threshold: Optional[Union[int, float]] = None,
out: Optional[ivy.Array] = None,
):
if beta is not None:
x_beta = ivy.multiply(x, ivy.array(beta, dtype=x.dtype))
else:
x_beta = x
amax = ivy.relu(x_beta)
res = ivy.subtract(x_beta, ivy.multiply(amax, ivy.array(2, dtype=x.dtype)))
res = ivy.add(amax, ivy.log(ivy.add(1, ivy.exp(res))))
res = ivy.real(res) + _wrap_between(ivy.imag(res), ivy.pi).astype(
x.dtype
) * ivy.astype(1j, x.dtype)
if beta is not None:
res = ivy.divide(res, ivy.array(beta, dtype=x.dtype))
if threshold is not None:
res = ivy.where(
ivy.real(x_beta) < threshold,
res,
x,
).astype(x.dtype)
return res
@handle_exceptions
@handle_backend_invalid
@handle_nestable
@handle_array_like_without_promotion
@handle_out_argument
@to_native_arrays_and_back
@handle_array_function
@handle_device
@handle_complex_input
def softplus(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
beta: Optional[Union[int, float]] = None,
threshold: Optional[Union[int, float]] = None,
complex_mode: Literal["split", "magnitude", "jax"] = "jax",
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Apply the softplus function element-wise.
If the input is complex, then by default we apply the softplus operation
`log(1+ exp(x))` to each element
If threshold is set we check if either its real part is strictly negative or
if its real part is zero and its imaginary part is negative then we apply
`input×β > threshold`.
Parameters
----------
x
input array.
beta
The beta value for the softplus formation. Default: ``None``.
threshold
values above this revert to a linear function
If the input is complex, only its real part is considered. Default: ``None``
complex_mode
optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
out
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
Returns
-------
ret
an array containing the softplus activation of each element in ``x``.
Examples
--------
With :class:`ivy.Array` input:
>>> x = ivy.array([-0.3461, -0.6491])
>>> y = ivy.softplus(x)
>>> print(y)
ivy.array([0.535,0.42])
>>> x = ivy.array([-0.3461, -0.6491])
>>> y = ivy.softplus(x, beta=0.5)
>>> print(y)
ivy.array([1.22, 1.09])
>>> x = ivy.array([1., 2., 3.])
>>> y = ivy.softplus(x, threshold=2)
>>> print(y)
ivy.array([1.31, 2.13, 3. ])
"""
return current_backend(x).softplus(x, beta=beta, threshold=threshold, out=out)
softplus.jax_like = _softplus_jax_like
# Softsign
@handle_exceptions
@handle_backend_invalid
@handle_nestable
@handle_array_like_without_promotion
@handle_out_argument
@to_native_arrays_and_back
@handle_array_function
@handle_device
def softsign(
x: Union[ivy.Array, ivy.NativeArray],
/,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Apply the softsign function element-wise.
Parameters
----------
x
Input array.
out
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
Returns
-------
ret
The input array with softsign applied element-wise.
Examples
--------
With :class:`ivy.Array` input:
>>> x = ivy.array([1.0, 2.0, 3.0])
>>> y = ivy.softsign(x)
>>> print(y)
ivy.array([0.5, 0.66666667, 0.75])
"""
return current_backend(x).softsign(x, out=out)
@handle_exceptions
@handle_backend_invalid
@handle_nestable
@handle_array_like_without_promotion
@handle_out_argument
@to_native_arrays_and_back
@handle_array_function
@handle_device
@handle_complex_input
def mish(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
complex_mode: Literal["split", "magnitude", "jax"] = "jax",
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Apply the mish activation function element-wise.
Parameters
----------
x
input array
complex_mode
optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
out
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
Returns
-------
ret
an array containing the mish activation of each element in
``x``.
Examples
--------
With :class:`ivy.Array` input:
>>> x = ivy.array([-1., 0., 1.])
>>> y = ivy.mish(x)
>>> print(y)
ivy.array([-0.30340147, 0. , 0.86509842])
>>> x = ivy.array([1.5, 0.7, -2.4])
>>> y = ivy.zeros(3)
>>> ivy.mish(x, out = y)
>>> print(y)
ivy.array([ 1.40337825, 0.56114835, -0.20788449])
With :class:`ivy.Container` input:
>>> x = ivy.Container(a=ivy.array([1.0, -1.2]), b=ivy.array([0.4, -0.2]))
>>> x = ivy.mish(x)
>>> print(x)
{
a: ivy.array([0.86509842, -0.30883577]),
b: ivy.array([0.28903052, -0.10714479])
}
"""
return current_backend(x).mish(x, out=out)
def _hardswish_jax_like(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
fn_original=None,
out: Optional[ivy.Array] = None,
) -> ivy.Array:
def hard_sigmoid(x):
return ivy.relu6(x + 3.0) / 6
return ivy.multiply(x, hard_sigmoid(x).astype(x.dtype))
@handle_exceptions
@handle_backend_invalid
@handle_nestable
@handle_array_like_without_promotion
@handle_out_argument
@to_native_arrays_and_back
@handle_array_function
@handle_complex_input
def hardswish(
x: Union[ivy.Array, ivy.NativeArray],
/,
*,
complex_mode: Literal["split", "magnitude", "jax"] = "jax",
out: Optional[ivy.Array] = None,
) -> ivy.Array:
"""Apply the hardswish activation function element-wise.
Parameters
----------
x
input array
complex_mode
optional specifier for how to handle complex data types. See
``ivy.func_wrapper.handle_complex_input`` for more detail.
out
optional output array, for writing the result to. It must have a shape that the
inputs broadcast to.
Returns
-------
ret
an array containing the hardswish activation of each element in ``x``.
Examples
--------
With :class:`ivy.Array` input:
>>> x = ivy.array([0., 0., 4.])
>>> y = ivy.hardswish(x)
>>> y
ivy.array([0., 0., 4.])
With :class:`ivy.Container` input:
>>> x = ivy.Container(a=ivy.array([-3., 4., 5.]), b=ivy.array([0., 5.]))
>>> x = ivy.hardswish(x, out=x)
>>> x
{
a: ivy.array([-0., 4., 5.]),
b: ivy.array([0., 5.])
}
"""
return current_backend(x).hardswish(x, out=out)
hardswish.jax_like = _hardswish_jax_like