def regularizer_test(reg, np_fn, np_input=np_input, debug=False): ndim = len(np_input.shape) x = B.placeholder(ndim=ndim) f = B.function([x], [reg(x)]) if debug: print("Expected Output Shape:\n{}".format(np_fn(np_input))) print("Output:\n{}".format(f([np_input])[0])) assert_allclose(f([np_input])[0], np_fn(np_input), rtol=1e-05)
def activation_test(k_fn, np_fn, np_input=np_input, debug=False): ndim = len(np_input.shape) x = B.placeholder(ndim=ndim) f = B.function([x], [k_fn(x)]) if debug: print("Expected Output Shape:\n{}".format(np_fn(np_input))) print("Output:\n{}".format(f([np_input])[0])) assert_allclose(f([np_input])[0], np_fn(np_input).astype(B._FLOATX), rtol=float_test_rtol)
def test_softmax(): def softmax(values): e = np.max(values, axis=-1, keepdims=True) s = np.exp(values - e) return s / np.sum(s) activation_test(activations.softmax, softmax) # Testing cases of 3D input activation_test(activations.softmax, softmax, np_input=np.linspace(-1, 1, 5)[None, None, :]) with pytest.raises(KError): # softmax accepts only 2D or 3D x = B.placeholder(ndim=1) B.function([x], [activations.softmax(x)])