aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/python/keras
diff options
context:
space:
mode:
authorGravatar A. Unique TensorFlower <gardener@tensorflow.org>2018-07-24 17:07:15 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-07-24 17:11:24 -0700
commit0cf2c612e5e6ff8c5026011e8186056801def747 (patch)
tree12793c95ad2aaa21bcddb466904ebd936feb326b /tensorflow/python/keras
parent4c161d7306eb934232e3fe65de2c31c3bb7cf875 (diff)
Keras ReLU Consolidation
Consolidate functionality of ThresholdedReLU and LeakyReLU layers into ReLU layer PiperOrigin-RevId: 205917439
Diffstat (limited to 'tensorflow/python/keras')
-rw-r--r--tensorflow/python/keras/activations.py22
-rw-r--r--tensorflow/python/keras/backend.py32
-rw-r--r--tensorflow/python/keras/backend_test.py61
-rw-r--r--tensorflow/python/keras/layers/advanced_activations.py37
-rw-r--r--tensorflow/python/keras/layers/advanced_activations_test.py8
5 files changed, 141 insertions, 19 deletions
diff --git a/tensorflow/python/keras/activations.py b/tensorflow/python/keras/activations.py
index f608dea430..99645de736 100644
--- a/tensorflow/python/keras/activations.py
+++ b/tensorflow/python/keras/activations.py
@@ -128,20 +128,26 @@ def softsign(x):
@tf_export('keras.activations.relu')
-def relu(x, alpha=0., max_value=None):
+def relu(x, alpha=0., max_value=None, threshold=0):
"""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: Input tensor.
- alpha: Slope of the negative part. Defaults to zero.
- max_value: Maximum value for the output.
+ x: A tensor or variable.
+ alpha: A scalar, slope of negative section (default=`0.`).
+ max_value: float. Saturation threshold.
+ threshold: float. Threshold value for thresholded activation.
Returns:
- The (leaky) rectified linear unit activation: `x` if `x > 0`,
- `alpha * x` if `x < 0`. If `max_value` is defined, the result
- is truncated to this value.
+ A tensor.
"""
- return K.relu(x, alpha=alpha, max_value=max_value)
+ return K.relu(x, alpha=alpha, max_value=max_value, threshold=threshold)
@tf_export('keras.activations.tanh')
diff --git a/tensorflow/python/keras/backend.py b/tensorflow/python/keras/backend.py
index 333f927d2f..38794f1612 100644
--- a/tensorflow/python/keras/backend.py
+++ b/tensorflow/python/keras/backend.py
@@ -3372,26 +3372,48 @@ def in_test_phase(x, alt, training=None):
@tf_export('keras.backend.relu')
-def relu(x, alpha=0., max_value=None):
+def relu(x, alpha=0., max_value=None, threshold=0):
"""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.
alpha: A scalar, slope of negative section (default=`0.`).
- max_value: Saturation threshold.
+ max_value: float. Saturation threshold.
+ threshold: float. Threshold value for thresholded activation.
Returns:
A tensor.
"""
+ clip_max = max_value is not None
+
if alpha != 0.:
- negative_part = nn.relu(-x)
- x = nn.relu(x)
- if max_value is not None:
+ if threshold != 0:
+ negative_part = nn.relu(-x + threshold)
+ else:
+ negative_part = nn.relu(-x)
+
+ if threshold != 0:
+ # computes x for x > threshold else 0
+ x = x * math_ops.cast(math_ops.greater(x, threshold), floatx())
+ elif max_value == 6:
+ # if no threshold, then can use nn.relu6 native TF op for performance
+ x = nn.relu6(x)
+ clip_max = False
+ else:
+ x = nn.relu(x)
+
+ if clip_max:
max_value = _to_tensor(max_value, x.dtype.base_dtype)
zero = _to_tensor(0., x.dtype.base_dtype)
x = clip_ops.clip_by_value(x, zero, max_value)
+
if alpha != 0.:
alpha = _to_tensor(alpha, x.dtype.base_dtype)
x -= alpha * negative_part
diff --git a/tensorflow/python/keras/backend_test.py b/tensorflow/python/keras/backend_test.py
index 36478ea089..40e7910061 100644
--- a/tensorflow/python/keras/backend_test.py
+++ b/tensorflow/python/keras/backend_test.py
@@ -23,6 +23,7 @@ import scipy.sparse
from tensorflow.python import keras
from tensorflow.python.framework import dtypes
+from tensorflow.python.framework import ops
from tensorflow.python.framework import sparse_tensor
from tensorflow.python.ops import variables
from tensorflow.python.platform import test
@@ -490,6 +491,66 @@ class BackendLinearAlgebraTest(test.TestCase):
input_shape_a=(4, 7),
input_shape_b=(4, 7))
+ def test_relu(self):
+ x = ops.convert_to_tensor([[-4, 0], [2, 7]], 'float32')
+ with self.test_session():
+ # standard relu
+ relu_op = keras.backend.relu(x)
+ self.assertAllClose(keras.backend.eval(relu_op), [[0, 0], [2, 7]])
+
+ # alpha
+ relu_op = keras.backend.relu(x, alpha=0.5)
+ self.assertAllClose(keras.backend.eval(relu_op), [[-2, 0], [2, 7]])
+
+ # max_value < some elements
+ relu_op = keras.backend.relu(x, max_value=5)
+ self.assertAllClose(keras.backend.eval(relu_op), [[0, 0], [2, 5]])
+
+ # nn.relu6 used
+ relu_op = keras.backend.relu(x, max_value=6)
+ self.assertTrue('Relu6' in relu_op.name) # uses tf.nn.relu6
+ self.assertAllClose(keras.backend.eval(relu_op), [[0, 0], [2, 6]])
+
+ # max value > 6
+ relu_op = keras.backend.relu(x, max_value=10)
+ self.assertAllClose(keras.backend.eval(relu_op), [[0, 0], [2, 7]])
+
+ # max value is float
+ relu_op = keras.backend.relu(x, max_value=4.3)
+ self.assertAllClose(keras.backend.eval(relu_op), [[0, 0], [2, 4.3]])
+
+ # max value == 0
+ relu_op = keras.backend.relu(x, max_value=0)
+ self.assertAllClose(keras.backend.eval(relu_op), [[0, 0], [0, 0]])
+
+ # alpha and max_value
+ relu_op = keras.backend.relu(x, alpha=0.25, max_value=3)
+ self.assertAllClose(keras.backend.eval(relu_op), [[-1, 0], [2, 3]])
+
+ # threshold
+ relu_op = keras.backend.relu(x, threshold=3)
+ self.assertAllClose(keras.backend.eval(relu_op), [[0, 0], [0, 7]])
+
+ # threshold is float
+ relu_op = keras.backend.relu(x, threshold=1.5)
+ self.assertAllClose(keras.backend.eval(relu_op), [[0, 0], [2, 7]])
+
+ # threshold is negative
+ relu_op = keras.backend.relu(x, threshold=-5)
+ self.assertAllClose(keras.backend.eval(relu_op), [[-4, 0], [2, 7]])
+
+ # threshold and max_value
+ relu_op = keras.backend.relu(x, threshold=3, max_value=5)
+ self.assertAllClose(keras.backend.eval(relu_op), [[0, 0], [0, 5]])
+
+ # threshold and alpha
+ relu_op = keras.backend.relu(x, alpha=0.25, threshold=4)
+ self.assertAllClose(keras.backend.eval(relu_op), [[-2, -1], [-0.5, 7]])
+
+ # threshold, alpha, and max_value
+ relu_op = keras.backend.relu(x, alpha=0.25, threshold=4, max_value=5)
+ self.assertAllClose(keras.backend.eval(relu_op), [[-2, -1], [-0.5, 5]])
+
class BackendShapeOpsTest(test.TestCase):
diff --git a/tensorflow/python/keras/layers/advanced_activations.py b/tensorflow/python/keras/layers/advanced_activations.py
index eba10da6f3..61ab69c16f 100644
--- a/tensorflow/python/keras/layers/advanced_activations.py
+++ b/tensorflow/python/keras/layers/advanced_activations.py
@@ -284,6 +284,13 @@ class Softmax(Layer):
class ReLU(Layer):
"""Rectified Linear Unit activation function.
+ 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) = negative_slope * (x - threshold)` otherwise.
+
Input shape:
Arbitrary. Use the keyword argument `input_shape`
(tuple of integers, does not include the samples axis)
@@ -294,21 +301,39 @@ class ReLU(Layer):
Arguments:
max_value: float >= 0. Maximum activation value.
+ negative_slope: float >= 0. Negative slope coefficient.
+ threshold: float. Threshold value for thresholded activation.
"""
- def __init__(self, max_value=None, **kwargs):
+ def __init__(self, max_value=None, negative_slope=0, threshold=0, **kwargs):
super(ReLU, self).__init__(**kwargs)
- self.support_masking = True
- self.max_value = K.cast_to_floatx(max_value)
- if self.max_value < 0.:
+ if max_value is not None and max_value < 0.:
raise ValueError('max_value of Relu layer '
'cannot be negative value: ' + str(max_value))
+ if negative_slope < 0.:
+ raise ValueError('negative_slope of Relu layer '
+ 'cannot be negative value: ' + str(negative_slope))
+
+ self.support_masking = True
+ self.max_value = K.cast_to_floatx(max_value)
+ self.negative_slope = K.cast_to_floatx(negative_slope)
+ self.threshold = K.cast_to_floatx(threshold)
def call(self, inputs):
- return activations.relu(inputs, max_value=self.max_value)
+ # alpha is used for leaky relu slope in activations instead of
+ # negative_slope.
+ return activations.relu(
+ inputs,
+ alpha=self.negative_slope,
+ max_value=self.max_value,
+ threshold=self.threshold)
def get_config(self):
- config = {'max_value': self.max_value}
+ config = {
+ 'max_value': self.max_value,
+ 'negative_slope': self.negative_slope,
+ 'threshold': self.threshold
+ }
base_config = super(ReLU, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
diff --git a/tensorflow/python/keras/layers/advanced_activations_test.py b/tensorflow/python/keras/layers/advanced_activations_test.py
index 9e1f15b1bc..53c1baa2bb 100644
--- a/tensorflow/python/keras/layers/advanced_activations_test.py
+++ b/tensorflow/python/keras/layers/advanced_activations_test.py
@@ -75,6 +75,14 @@ class AdvancedActivationsTest(test.TestCase):
testing_utils.layer_test(keras.layers.ReLU,
kwargs={'max_value': -10},
input_shape=(2, 3, 4))
+ with self.assertRaisesRegexp(
+ ValueError,
+ 'negative_slope of Relu layer cannot be negative value: -2'):
+ with self.test_session():
+ testing_utils.layer_test(
+ keras.layers.ReLU,
+ kwargs={'negative_slope': -2},
+ input_shape=(2, 3, 4))
if __name__ == '__main__':