'''
    This file contains test cases for tflearn
'''

import tensorflow.compat.v1 as tf
import tflearn
import unittest

class TestActivations(unittest.TestCase):
    '''
        This class contains test cases for the functions in tflearn/activations.py
    '''
    PLACES = 4 # Number of places to match when testing floating point values

    def test_linear(self):
        f = tflearn.linear

        # Case 1
        x = tf.placeholder(tf.float32, shape=())
        self.assertEqual(f(x), x)

        # Case 2
        x = tf.placeholder(tf.int64, shape=())
        self.assertEqual(f(x), x)

    def test_tanh(self):
        f = tflearn.tanh
        x = tf.placeholder(tf.float32, shape=())
        
        with tf.Session() as sess:
            # Case 1
            self.assertEqual(sess.run(f(x), feed_dict={x:0}), 0)

            # Case 2
            self.assertAlmostEqual(sess.run(f(x), feed_dict={x:0.5}),
                0.4621, places=TestActivations.PLACES)

            # Case 3
            self.assertAlmostEqual(sess.run(f(x), feed_dict={x:-0.25}),
                -0.2449, places=TestActivations.PLACES)

    def test_leaky_relu(self):
        f = lambda x: tflearn.leaky_relu(x, alpha=0.2)
        x = tf.placeholder(tf.float32, shape=())

        with tf.Session() as sess:
            # Case 1
            self.assertEqual(sess.run(f(x), feed_dict={x:0}), 0)

            # Case 2
            self.assertAlmostEqual(sess.run(f(x), feed_dict={x:1}),
                1, places=TestActivations.PLACES)

            # Case 3
            self.assertAlmostEqual(sess.run(f(x), feed_dict={x:-1}),
                -0.2, places=TestActivations.PLACES)

            # Case 4
            self.assertAlmostEqual(sess.run(f(x), feed_dict={x:-5}),
                -1, places=TestActivations.PLACES)

    def test_apply_activation(self):
        lrelu_02 = lambda x: tflearn.leaky_relu(x, alpha=0.2)
        x = tf.constant(-0.25, tf.float32)

        with tf.Session() as sess:
            # Case 1: 'linear'
            self.assertEqual(
                sess.run(tflearn.activation(x, 'linear')),
                -0.25)

            # Case 2: 'relu'
            self.assertEqual(
                sess.run(tflearn.activation(x, 'relu')),
                0)

            # Case 3: 'leaky_relu'
            self.assertAlmostEqual(
                sess.run(tflearn.activation(x, 'leaky_relu')),
                -0.025, places=TestActivations.PLACES)

            # Case 4: 'tanh'
            self.assertAlmostEqual(
                sess.run(tflearn.activation(x, 'tanh')),
                -0.2449, places=TestActivations.PLACES)

            # Case 5: lrelu_02 (callable)
            self.assertAlmostEqual(
                sess.run(tflearn.activation(x, lrelu_02)),
                -0.05, places=TestActivations.PLACES)

if __name__ == "__main__":
    unittest.main()
[((2857, 2872), 'unittest.main', 'unittest.main', ([], {}), '()\n', (2870, 2872), False, 'import unittest\n'), ((425, 461), 'tensorflow.compat.v1.placeholder', 'tf.placeholder', (['tf.float32'], {'shape': '()'}), '(tf.float32, shape=())\n', (439, 461), True, 'import tensorflow.compat.v1 as tf\n'), ((526, 560), 'tensorflow.compat.v1.placeholder', 'tf.placeholder', (['tf.int64'], {'shape': '()'}), '(tf.int64, shape=())\n', (540, 560), True, 'import tensorflow.compat.v1 as tf\n'), ((658, 694), 'tensorflow.compat.v1.placeholder', 'tf.placeholder', (['tf.float32'], {'shape': '()'}), '(tf.float32, shape=())\n', (672, 694), True, 'import tensorflow.compat.v1 as tf\n'), ((1221, 1257), 'tensorflow.compat.v1.placeholder', 'tf.placeholder', (['tf.float32'], {'shape': '()'}), '(tf.float32, shape=())\n', (1235, 1257), True, 'import tensorflow.compat.v1 as tf\n'), ((1918, 1948), 'tensorflow.compat.v1.constant', 'tf.constant', (['(-0.25)', 'tf.float32'], {}), '(-0.25, tf.float32)\n', (1929, 1948), True, 'import tensorflow.compat.v1 as tf\n'), ((717, 729), 'tensorflow.compat.v1.Session', 'tf.Session', ([], {}), '()\n', (727, 729), True, 'import tensorflow.compat.v1 as tf\n'), ((1176, 1208), 'tflearn.leaky_relu', 'tflearn.leaky_relu', (['x'], {'alpha': '(0.2)'}), '(x, alpha=0.2)\n', (1194, 1208), False, 'import tflearn\n'), ((1272, 1284), 'tensorflow.compat.v1.Session', 'tf.Session', ([], {}), '()\n', (1282, 1284), True, 'import tensorflow.compat.v1 as tf\n'), ((1873, 1905), 'tflearn.leaky_relu', 'tflearn.leaky_relu', (['x'], {'alpha': '(0.2)'}), '(x, alpha=0.2)\n', (1891, 1905), False, 'import tflearn\n'), ((1963, 1975), 'tensorflow.compat.v1.Session', 'tf.Session', ([], {}), '()\n', (1973, 1975), True, 'import tensorflow.compat.v1 as tf\n'), ((2071, 2102), 'tflearn.activation', 'tflearn.activation', (['x', '"""linear"""'], {}), "(x, 'linear')\n", (2089, 2102), False, 'import tflearn\n'), ((2213, 2242), 'tflearn.activation', 'tflearn.activation', (['x', '"""relu"""'], {}), "(x, 'relu')\n", (2231, 2242), False, 'import tflearn\n'), ((2361, 2396), 'tflearn.activation', 'tflearn.activation', (['x', '"""leaky_relu"""'], {}), "(x, 'leaky_relu')\n", (2379, 2396), False, 'import tflearn\n'), ((2545, 2574), 'tflearn.activation', 'tflearn.activation', (['x', '"""tanh"""'], {}), "(x, 'tanh')\n", (2563, 2574), False, 'import tflearn\n'), ((2737, 2768), 'tflearn.activation', 'tflearn.activation', (['x', 'lrelu_02'], {}), '(x, lrelu_02)\n', (2755, 2768), False, 'import tflearn\n')]