1# Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Tests for advanced activation layers.""" 16 17from __future__ import absolute_import 18from __future__ import division 19from __future__ import print_function 20 21import numpy as np 22 23from tensorflow.python import keras 24from tensorflow.python.eager import context 25from tensorflow.python.keras import keras_parameterized 26from tensorflow.python.keras import testing_utils 27from tensorflow.python.platform import test 28 29 30@keras_parameterized.run_all_keras_modes 31class AdvancedActivationsTest(keras_parameterized.TestCase): 32 33 def test_leaky_relu(self): 34 for alpha in [0., .5, -1.]: 35 testing_utils.layer_test(keras.layers.LeakyReLU, 36 kwargs={'alpha': alpha}, 37 input_shape=(2, 3, 4)) 38 39 def test_prelu(self): 40 testing_utils.layer_test(keras.layers.PReLU, kwargs={}, 41 input_shape=(2, 3, 4)) 42 43 def test_prelu_share(self): 44 testing_utils.layer_test(keras.layers.PReLU, 45 kwargs={'shared_axes': 1}, 46 input_shape=(2, 3, 4)) 47 48 def test_elu(self): 49 for alpha in [0., .5, -1.]: 50 testing_utils.layer_test(keras.layers.ELU, 51 kwargs={'alpha': alpha}, 52 input_shape=(2, 3, 4)) 53 54 def test_thresholded_relu(self): 55 testing_utils.layer_test(keras.layers.ThresholdedReLU, 56 kwargs={'theta': 0.5}, 57 input_shape=(2, 3, 4)) 58 59 def test_softmax(self): 60 testing_utils.layer_test(keras.layers.Softmax, 61 kwargs={'axis': 1}, 62 input_shape=(2, 3, 4)) 63 64 def test_relu(self): 65 testing_utils.layer_test(keras.layers.ReLU, 66 kwargs={'max_value': 10}, 67 input_shape=(2, 3, 4)) 68 x = keras.backend.ones((3, 4)) 69 if not context.executing_eagerly(): 70 # Test that we use `leaky_relu` when appropriate in graph mode. 71 self.assertTrue( 72 'LeakyRelu' in keras.layers.ReLU(negative_slope=0.2)(x).name) 73 # Test that we use `relu` when appropriate in graph mode. 74 self.assertTrue('Relu' in keras.layers.ReLU()(x).name) 75 # Test that we use `relu6` when appropriate in graph mode. 76 self.assertTrue('Relu6' in keras.layers.ReLU(max_value=6)(x).name) 77 78 def test_relu_with_invalid_arg(self): 79 with self.assertRaisesRegexp( 80 ValueError, 'max_value of Relu layer cannot be negative value: -10'): 81 testing_utils.layer_test(keras.layers.ReLU, 82 kwargs={'max_value': -10}, 83 input_shape=(2, 3, 4)) 84 with self.assertRaisesRegexp( 85 ValueError, 86 'negative_slope of Relu layer cannot be negative value: -2'): 87 with self.cached_session(): 88 testing_utils.layer_test( 89 keras.layers.ReLU, 90 kwargs={'negative_slope': -2}, 91 input_shape=(2, 3, 4)) 92 93 @keras_parameterized.run_with_all_model_types 94 def test_layer_as_activation(self): 95 layer = keras.layers.Dense(1, activation=keras.layers.ReLU()) 96 model = testing_utils.get_model_from_layers([layer], input_shape=(10,)) 97 model.compile('sgd', 'mse', run_eagerly=testing_utils.should_run_eagerly()) 98 model.fit(np.ones((10, 10)), np.ones((10, 1)), batch_size=2) 99 100 101if __name__ == '__main__': 102 test.main() 103