Rectified linear unit. With default values, it returns element-wise `max(x, 0)`. Otherwise, it follows: `f(x) = max_value` for `x >= max_value`, `f(x) = x` for `threshold <= x < max_value`, `f(x) = alpha * (x - threshold)` otherwise. Arguments: x: A tensor or variable. alp
(x, alpha=0., max_value=None, threshold=0)
| 4208 | |
| 4209 | @keras_export('keras.backend.relu') |
| 4210 | def relu(x, alpha=0., max_value=None, threshold=0): |
| 4211 | """Rectified linear unit. |
| 4212 | |
| 4213 | With default values, it returns element-wise `max(x, 0)`. |
| 4214 | |
| 4215 | Otherwise, it follows: |
| 4216 | `f(x) = max_value` for `x >= max_value`, |
| 4217 | `f(x) = x` for `threshold <= x < max_value`, |
| 4218 | `f(x) = alpha * (x - threshold)` otherwise. |
| 4219 | |
| 4220 | Arguments: |
| 4221 | x: A tensor or variable. |
| 4222 | alpha: A scalar, slope of negative section (default=`0.`). |
| 4223 | max_value: float. Saturation threshold. |
| 4224 | threshold: float. Threshold value for thresholded activation. |
| 4225 | |
| 4226 | Returns: |
| 4227 | A tensor. |
| 4228 | """ |
| 4229 | |
| 4230 | if alpha != 0.: |
| 4231 | if max_value is None and threshold == 0: |
| 4232 | return nn.leaky_relu(x, alpha=alpha) |
| 4233 | |
| 4234 | if threshold != 0: |
| 4235 | negative_part = nn.relu(-x + threshold) |
| 4236 | else: |
| 4237 | negative_part = nn.relu(-x) |
| 4238 | |
| 4239 | clip_max = max_value is not None |
| 4240 | |
| 4241 | if threshold != 0: |
| 4242 | # computes x for x > threshold else 0 |
| 4243 | x = x * math_ops.cast(math_ops.greater(x, threshold), floatx()) |
| 4244 | elif max_value == 6: |
| 4245 | # if no threshold, then can use nn.relu6 native TF op for performance |
| 4246 | x = nn.relu6(x) |
| 4247 | clip_max = False |
| 4248 | else: |
| 4249 | x = nn.relu(x) |
| 4250 | |
| 4251 | if clip_max: |
| 4252 | max_value = _constant_to_tensor(max_value, x.dtype.base_dtype) |
| 4253 | zero = _constant_to_tensor(0., x.dtype.base_dtype) |
| 4254 | x = clip_ops.clip_by_value(x, zero, max_value) |
| 4255 | |
| 4256 | if alpha != 0.: |
| 4257 | alpha = _to_tensor(alpha, x.dtype.base_dtype) |
| 4258 | x -= alpha * negative_part |
| 4259 | return x |
| 4260 | |
| 4261 | |
| 4262 | @keras_export('keras.backend.elu') |