@@ -63,12 +63,12 @@ def softmax(x, axis=-1):
63
63
The input values in are the log-odds of the resulting probability.
64
64
65
65
Args:
66
- x : Input tensor.
67
- axis: Integer, axis along which the softmax normalization is applied.
66
+ x : Input tensor.
67
+ axis: Integer, axis along which the softmax normalization is applied.
68
68
69
69
Returns:
70
- Tensor, output of softmax transformation (all values are non-negative
71
- and sum to 1).
70
+ Tensor, output of softmax transformation (all values are non-negative
71
+ and sum to 1).
72
72
73
73
Examples:
74
74
@@ -84,22 +84,7 @@ def softmax(x, axis=-1):
84
84
>>> layer = tf.keras.layers.Dense(32,
85
85
... activation=tf.keras.activations.softmax)
86
86
"""
87
- if x .shape .rank <= 1 :
88
- raise ValueError (
89
- f"Cannot apply softmax to a tensor that is 1D. Received input: { x } "
90
- )
91
-
92
- if isinstance (axis , int ):
93
- output = tf .nn .softmax (x , axis = axis )
94
- else :
95
- # nn.softmax does not support tuple axis.
96
- numerator = tf .exp (x - tf .reduce_max (x , axis = axis , keepdims = True ))
97
- denominator = tf .reduce_sum (numerator , axis = axis , keepdims = True )
98
- output = numerator / denominator
99
-
100
- # Cache the logits to use for crossentropy loss.
101
- output ._keras_logits = x
102
- return output
87
+ return backend .softmax (x , axis )
103
88
104
89
105
90
@keras_export ("keras.activations.elu" )
@@ -138,11 +123,11 @@ def elu(x, alpha=1.0):
138
123
Args:
139
124
x: Input tensor.
140
125
alpha: A scalar, slope of negative section. `alpha` controls the value
141
- to which an ELU saturates for negative net inputs.
126
+ to which an ELU saturates for negative net inputs.
142
127
143
128
Returns:
144
129
The exponential linear unit (ELU) activation function: `x` if `x > 0`
145
- and `alpha * (exp(x) - 1)` if `x < 0`.
130
+ and `alpha * (exp(x) - 1)` if `x < 0`.
146
131
147
132
148
133
Reference:
@@ -196,9 +181,9 @@ def selu(x):
196
181
197
182
Notes:
198
183
- To be used together with the
199
- `tf.keras.initializers.LecunNormal` initializer.
184
+ `tf.keras.initializers.LecunNormal` initializer.
200
185
- To be used together with the dropout variant
201
- `tf.keras.layers.AlphaDropout` (not regular dropout).
186
+ `tf.keras.layers.AlphaDropout` (not regular dropout).
202
187
203
188
References:
204
189
- [Klambauer et al., 2017](https://arxiv.org/abs/1706.02515)
@@ -275,7 +260,7 @@ def swish(x):
275
260
The swish activation applied to `x` (see reference paper for details).
276
261
277
262
Reference:
278
- - [Ramachandran et al., 2017](https://arxiv.org/abs/1710.05941)
263
+ - [Ramachandran et al., 2017](https://arxiv.org/abs/1710.05941)
279
264
"""
280
265
return tf .nn .silu (x )
281
266
@@ -307,16 +292,16 @@ def relu(x, alpha=0.0, max_value=None, threshold=0.0):
307
292
Args:
308
293
x: Input `tensor` or `variable`.
309
294
alpha: A `float` that governs the slope for values lower than the
310
- threshold.
295
+ threshold.
311
296
max_value: A `float` that sets the saturation threshold (the largest
312
- value the function will return).
297
+ value the function will return).
313
298
threshold: A `float` giving the threshold value of the activation
314
- function below which values will be damped or set to zero.
299
+ function below which values will be damped or set to zero.
315
300
316
301
Returns:
317
- A `Tensor` representing the input tensor,
318
- transformed by the relu activation function.
319
- Tensor will be of the same shape and dtype of input `x`.
302
+ A `Tensor` representing the input tensor, transformed by the relu
303
+ activation function. Tensor will be of the same shape and dtype of
304
+ input `x`.
320
305
"""
321
306
return backend .relu (
322
307
x , alpha = alpha , max_value = max_value , threshold = threshold
@@ -358,8 +343,8 @@ def gelu(x, approximate=False):
358
343
if `approximate` is `False`.
359
344
360
345
Reference:
361
- - [Gaussian Error Linear Units (GELUs)](https://arxiv.org/abs/1606.08415)
362
- """
346
+ - [Gaussian Error Linear Units (GELUs)](https://arxiv.org/abs/1606.08415)
347
+ """ # noqa: E501
363
348
return tf .nn .gelu (x , approximate )
364
349
365
350
@@ -412,10 +397,7 @@ def sigmoid(x):
412
397
Returns:
413
398
Tensor with the sigmoid activation: `1 / (1 + exp(-x))`.
414
399
"""
415
- output = tf .sigmoid (x )
416
- # Cache the logits to use for crossentropy loss.
417
- output ._keras_logits = x
418
- return output
400
+ return backend .sigmoid (x )
419
401
420
402
421
403
@keras_export ("keras.activations.exponential" )
@@ -459,11 +441,11 @@ def hard_sigmoid(x):
459
441
x: Input tensor.
460
442
461
443
Returns:
462
- The hard sigmoid activation, defined as:
444
+ The hard sigmoid activation, defined as:
463
445
464
- - `if x < -2.5: return 0`
465
- - `if x > 2.5: return 1`
466
- - `if -2.5 <= x <= 2.5: return 0.2 * x + 0.5`
446
+ - `if x < -2.5: return 0`
447
+ - `if x > 2.5: return 1`
448
+ - `if -2.5 <= x <= 2.5: return 0.2 * x + 0.5`
467
449
"""
468
450
return backend .hard_sigmoid (x )
469
451
@@ -535,6 +517,8 @@ def serialize(activation, use_legacy_format=False):
535
517
536
518
Args:
537
519
activation : Function object.
520
+ use_legacy_format: Boolean, whether to use the legacy format for
521
+ serialization. Defaults to False.
538
522
539
523
Returns:
540
524
String denoting the name attribute of the input function
@@ -608,9 +592,11 @@ def deserialize(name, custom_objects=None, use_legacy_format=False):
608
592
"""Returns activation function given a string identifier.
609
593
610
594
Args:
611
- name: The name of the activation function.
612
- custom_objects: Optional `{function_name: function_obj}`
613
- dictionary listing user-provided activation functions.
595
+ name: The name of the activation function.
596
+ custom_objects: Optional `{function_name: function_obj}`
597
+ dictionary listing user-provided activation functions.
598
+ use_legacy_format: Boolean, whether to use the legacy format for
599
+ deserialization. Defaults to False.
614
600
615
601
Returns:
616
602
Corresponding activation function.
0 commit comments