import pytest
import numpy as np
from numpy.testing import assert_allclose

from keras import backend as K
from keras import activations


def get_standard_values():
    '''
    These are just a set of floats used for testing the activation
    functions, and are useful in multiple tests.
    '''
    return np.array([[0, 0.1, 0.5, 0.9, 1.0]], dtype=K.floatx())


def test_softmax():
    '''
    Test using a reference implementation of softmax
    '''
    def softmax(values):
        m = np.max(values)
        e = np.exp(values - m)
        return e / np.sum(e)

    x = K.placeholder(ndim=2)
    f = K.function([x], [activations.softmax(x)])
    test_values = get_standard_values()

    result = f([test_values])[0]
    expected = softmax(test_values)
    assert_allclose(result, expected, rtol=1e-05)


def test_time_distributed_softmax():
    x = K.placeholder(shape=(1, 1, 5))
    f = K.function([x], [activations.softmax(x)])
    test_values = get_standard_values()
    test_values = np.reshape(test_values, (1, 1, np.size(test_values)))
    f([test_values])[0]


def test_softplus():
    '''
    Test using a reference softplus implementation
    '''
    def softplus(x):
        return np.log(np.ones_like(x) + np.exp(x))

    x = K.placeholder(ndim=2)
    f = K.function([x], [activations.softplus(x)])
    test_values = get_standard_values()

    result = f([test_values])[0]
    expected = softplus(test_values)
    assert_allclose(result, expected, rtol=1e-05)


def test_softsign():
    '''
    Test using a reference softsign implementation
    '''
    def softsign(x):
        return np.divide(x, np.ones_like(x) + np.absolute(x))

    x = K.placeholder(ndim=2)
    f = K.function([x], [activations.softsign(x)])
    test_values = get_standard_values()

    result = f([test_values])[0]
    expected = softsign(test_values)
    assert_allclose(result, expected, rtol=1e-05)


def test_sigmoid():
    '''
    Test using a numerically stable reference sigmoid implementation
    '''
    def ref_sigmoid(x):
        if x >= 0:
            return 1 / (1 + np.exp(-x))
        else:
            z = np.exp(x)
            return z / (1 + z)
    sigmoid = np.vectorize(ref_sigmoid)

    x = K.placeholder(ndim=2)
    f = K.function([x], [activations.sigmoid(x)])
    test_values = get_standard_values()

    result = f([test_values])[0]
    expected = sigmoid(test_values)
    assert_allclose(result, expected, rtol=1e-05)


def test_hard_sigmoid():
    '''
    Test using a reference hard sigmoid implementation
    '''
    def ref_hard_sigmoid(x):
        '''
        Reference hard sigmoid with slope and shift values from theano, see
        https://github.com/Theano/Theano/blob/master/theano/tensor/nnet/sigm.py
        '''
        x = (x * 0.2) + 0.5
        z = 0.0 if x <= 0 else (1.0 if x >= 1 else x)
        return z
    hard_sigmoid = np.vectorize(ref_hard_sigmoid)

    x = K.placeholder(ndim=2)
    f = K.function([x], [activations.hard_sigmoid(x)])
    test_values = get_standard_values()

    result = f([test_values])[0]
    expected = hard_sigmoid(test_values)
    assert_allclose(result, expected, rtol=1e-05)


def test_relu():
    '''
    Relu implementation doesn't depend on the value being
    a theano variable. Testing ints, floats and theano tensors.
    '''
    x = K.placeholder(ndim=2)
    f = K.function([x], [activations.relu(x)])

    test_values = get_standard_values()
    result = f([test_values])[0]

    # because no negatives in test values
    assert_allclose(result, test_values, rtol=1e-05)


def test_elu():
    x = K.placeholder(ndim=2)
    f = K.function([x], [activations.elu(x, 0.5)])

    test_values = get_standard_values()
    result = f([test_values])[0]

    # because no negatives in test values
    assert_allclose(result, test_values, rtol=1e-05)

    negative_values = np.array([[-1, -2]], dtype=K.floatx())
    result = f([negative_values])[0]
    true_result = (np.exp(negative_values) - 1) / 2

    assert_allclose(result, true_result)


def test_tanh():
    test_values = get_standard_values()

    x = K.placeholder(ndim=2)
    exp = activations.tanh(x)
    f = K.function([x], [exp])

    result = f([test_values])[0]
    expected = np.tanh(test_values)
    assert_allclose(result, expected, rtol=1e-05)


def test_linear():
    '''
    This function does no input validation, it just returns the thing
    that was passed in.
    '''
    xs = [1, 5, True, None, 'foo']
    for x in xs:
        assert(x == activations.linear(x))


if __name__ == '__main__':
    pytest.main([__file__])

[((575, 596), 'keras.backend.placeholder', 'K.placeholder', ([], {'ndim': '(2)'}), '(ndim=2)\n', (588, 596), True, 'from keras import backend as K\n'), ((761, 806), 'numpy.testing.assert_allclose', 'assert_allclose', (['result', 'expected'], {'rtol': '(1e-05)'}), '(result, expected, rtol=1e-05)\n', (776, 806), False, 'from numpy.testing import assert_allclose\n'), ((854, 884), 'keras.backend.placeholder', 'K.placeholder', ([], {'shape': '(1, 1, 5)'}), '(shape=(1, 1, 5))\n', (867, 884), True, 'from keras import backend as K\n'), ((1242, 1263), 'keras.backend.placeholder', 'K.placeholder', ([], {'ndim': '(2)'}), '(ndim=2)\n', (1255, 1263), True, 'from keras import backend as K\n'), ((1430, 1475), 'numpy.testing.assert_allclose', 'assert_allclose', (['result', 'expected'], {'rtol': '(1e-05)'}), '(result, expected, rtol=1e-05)\n', (1445, 1475), False, 'from numpy.testing import assert_allclose\n'), ((1658, 1679), 'keras.backend.placeholder', 'K.placeholder', ([], {'ndim': '(2)'}), '(ndim=2)\n', (1671, 1679), True, 'from keras import backend as K\n'), ((1846, 1891), 'numpy.testing.assert_allclose', 'assert_allclose', (['result', 'expected'], {'rtol': '(1e-05)'}), '(result, expected, rtol=1e-05)\n', (1861, 1891), False, 'from numpy.testing import assert_allclose\n'), ((2167, 2192), 'numpy.vectorize', 'np.vectorize', (['ref_sigmoid'], {}), '(ref_sigmoid)\n', (2179, 2192), True, 'import numpy as np\n'), ((2202, 2223), 'keras.backend.placeholder', 'K.placeholder', ([], {'ndim': '(2)'}), '(ndim=2)\n', (2215, 2223), True, 'from keras import backend as K\n'), ((2388, 2433), 'numpy.testing.assert_allclose', 'assert_allclose', (['result', 'expected'], {'rtol': '(1e-05)'}), '(result, expected, rtol=1e-05)\n', (2403, 2433), False, 'from numpy.testing import assert_allclose\n'), ((2859, 2889), 'numpy.vectorize', 'np.vectorize', (['ref_hard_sigmoid'], {}), '(ref_hard_sigmoid)\n', (2871, 2889), True, 'import numpy as np\n'), ((2899, 2920), 'keras.backend.placeholder', 'K.placeholder', ([], {'ndim': '(2)'}), '(ndim=2)\n', (2912, 2920), True, 'from keras import backend as K\n'), ((3095, 3140), 'numpy.testing.assert_allclose', 'assert_allclose', (['result', 'expected'], {'rtol': '(1e-05)'}), '(result, expected, rtol=1e-05)\n', (3110, 3140), False, 'from numpy.testing import assert_allclose\n'), ((3306, 3327), 'keras.backend.placeholder', 'K.placeholder', ([], {'ndim': '(2)'}), '(ndim=2)\n', (3319, 3327), True, 'from keras import backend as K\n'), ((3496, 3544), 'numpy.testing.assert_allclose', 'assert_allclose', (['result', 'test_values'], {'rtol': '(1e-05)'}), '(result, test_values, rtol=1e-05)\n', (3511, 3544), False, 'from numpy.testing import assert_allclose\n'), ((3571, 3592), 'keras.backend.placeholder', 'K.placeholder', ([], {'ndim': '(2)'}), '(ndim=2)\n', (3584, 3592), True, 'from keras import backend as K\n'), ((3765, 3813), 'numpy.testing.assert_allclose', 'assert_allclose', (['result', 'test_values'], {'rtol': '(1e-05)'}), '(result, test_values, rtol=1e-05)\n', (3780, 3813), False, 'from numpy.testing import assert_allclose\n'), ((3970, 4006), 'numpy.testing.assert_allclose', 'assert_allclose', (['result', 'true_result'], {}), '(result, true_result)\n', (3985, 4006), False, 'from numpy.testing import assert_allclose\n'), ((4075, 4096), 'keras.backend.placeholder', 'K.placeholder', ([], {'ndim': '(2)'}), '(ndim=2)\n', (4088, 4096), True, 'from keras import backend as K\n'), ((4107, 4126), 'keras.activations.tanh', 'activations.tanh', (['x'], {}), '(x)\n', (4123, 4126), False, 'from keras import activations\n'), ((4135, 4157), 'keras.backend.function', 'K.function', (['[x]', '[exp]'], {}), '([x], [exp])\n', (4145, 4157), True, 'from keras import backend as K\n'), ((4207, 4227), 'numpy.tanh', 'np.tanh', (['test_values'], {}), '(test_values)\n', (4214, 4227), True, 'import numpy as np\n'), ((4232, 4277), 'numpy.testing.assert_allclose', 'assert_allclose', (['result', 'expected'], {'rtol': '(1e-05)'}), '(result, expected, rtol=1e-05)\n', (4247, 4277), False, 'from numpy.testing import assert_allclose\n'), ((4537, 4560), 'pytest.main', 'pytest.main', (['[__file__]'], {}), '([__file__])\n', (4548, 4560), False, 'import pytest\n'), ((491, 505), 'numpy.max', 'np.max', (['values'], {}), '(values)\n', (497, 505), True, 'import numpy as np\n'), ((518, 536), 'numpy.exp', 'np.exp', (['(values - m)'], {}), '(values - m)\n', (524, 536), True, 'import numpy as np\n'), ((351, 361), 'keras.backend.floatx', 'K.floatx', ([], {}), '()\n', (359, 361), True, 'from keras import backend as K\n'), ((556, 565), 'numpy.sum', 'np.sum', (['e'], {}), '(e)\n', (562, 565), True, 'import numpy as np\n'), ((622, 644), 'keras.activations.softmax', 'activations.softmax', (['x'], {}), '(x)\n', (641, 644), False, 'from keras import activations\n'), ((910, 932), 'keras.activations.softmax', 'activations.softmax', (['x'], {}), '(x)\n', (929, 932), False, 'from keras import activations\n'), ((1024, 1044), 'numpy.size', 'np.size', (['test_values'], {}), '(test_values)\n', (1031, 1044), True, 'import numpy as np\n'), ((1289, 1312), 'keras.activations.softplus', 'activations.softplus', (['x'], {}), '(x)\n', (1309, 1312), False, 'from keras import activations\n'), ((1705, 1728), 'keras.activations.softsign', 'activations.softsign', (['x'], {}), '(x)\n', (1725, 1728), False, 'from keras import activations\n'), ((2112, 2121), 'numpy.exp', 'np.exp', (['x'], {}), '(x)\n', (2118, 2121), True, 'import numpy as np\n'), ((2249, 2271), 'keras.activations.sigmoid', 'activations.sigmoid', (['x'], {}), '(x)\n', (2268, 2271), False, 'from keras import activations\n'), ((2946, 2973), 'keras.activations.hard_sigmoid', 'activations.hard_sigmoid', (['x'], {}), '(x)\n', (2970, 2973), False, 'from keras import activations\n'), ((3353, 3372), 'keras.activations.relu', 'activations.relu', (['x'], {}), '(x)\n', (3369, 3372), False, 'from keras import activations\n'), ((3618, 3641), 'keras.activations.elu', 'activations.elu', (['x', '(0.5)'], {}), '(x, 0.5)\n', (3633, 3641), False, 'from keras import activations\n'), ((3864, 3874), 'keras.backend.floatx', 'K.floatx', ([], {}), '()\n', (3872, 3874), True, 'from keras import backend as K\n'), ((3932, 3955), 'numpy.exp', 'np.exp', (['negative_values'], {}), '(negative_values)\n', (3938, 3955), True, 'import numpy as np\n'), ((4481, 4502), 'keras.activations.linear', 'activations.linear', (['x'], {}), '(x)\n', (4499, 4502), False, 'from keras import activations\n'), ((1204, 1219), 'numpy.ones_like', 'np.ones_like', (['x'], {}), '(x)\n', (1216, 1219), True, 'import numpy as np\n'), ((1222, 1231), 'numpy.exp', 'np.exp', (['x'], {}), '(x)\n', (1228, 1231), True, 'import numpy as np\n'), ((1615, 1630), 'numpy.ones_like', 'np.ones_like', (['x'], {}), '(x)\n', (1627, 1630), True, 'import numpy as np\n'), ((1633, 1647), 'numpy.absolute', 'np.absolute', (['x'], {}), '(x)\n', (1644, 1647), True, 'import numpy as np\n'), ((2070, 2080), 'numpy.exp', 'np.exp', (['(-x)'], {}), '(-x)\n', (2076, 2080), True, 'import numpy as np\n')]