def test_dense_z_b_theano(): back_dense_fn = create_back_dense_fn('z_b',min_in=-2, max_in=4) out_relevances = np.array([[5,4,1]], dtype=np.float32) weights = np.array([[1,-2,4], [0,1,2]], dtype=np.float32) in_activations = np.array([[3,2]], dtype=np.float32) in_relevances = back_dense_fn(out_relevances,in_activations,weights) assert np.allclose([5 + 4/3.0 + 5/7.0,8/3.0 + 2/7.0], in_relevances)
def test_dense_z_plus_theano(): back_dense_fn = create_back_dense_fn('z_plus') out_relevances = np.array([[0,2,1]], dtype=np.float32) weights = np.array([[1,1,1], [2,2.25,0]], dtype=np.float32) in_activations = np.array([[1,4]], dtype=np.float32) in_relevances = back_dense_fn(out_relevances,in_activations,weights) assert np.allclose([1.2,1.8], in_relevances) assert np.allclose([ 3.44895196, 3.20214176, 1.3489064 ], back_dense_fn(np.array([[2,1,1,3,1]], dtype=np.float32), np.array([[1,4,3]], dtype=np.float32), np.array([[1,1,2,3,4], [9,3,4,-5,1],[1,2,5,-2,2]], dtype=np.float32))) # different from numpy due to NaNs being replaced by zeros instead of # being replaced by 1 / #input units assert np.allclose([[ 4.4000001 , 0. , 0.60000002]], back_dense_fn(np.array([[2,1,0,3,1]], dtype=np.float32), np.array([[1,0,3]], dtype=np.float32), np.array([[0,1,2,3,4], [9,3,4,-5,1],[0,0,5,-2,2]], dtype=np.float32))) # Only regression test, values are computed by hand, # so could be incorrect assert np.allclose([[ 3.44895196, 3.202142 , 1.34890628], [ 4.04285717, 0. , 2.95714283]], back_dense_fn(np.array([[2,1,1,3,1], [2,1,0,3,1]], dtype=np.float32), np.array([[1,4,3], [1,0,3]], dtype=np.float32), np.array([[1,1,2,3,4], [9,3,4,-5,1],[1,2,5,-2,2]], dtype=np.float32)))
def test_dense_z_plus_theano(): back_dense_fn = create_back_dense_fn('z_plus') out_relevances = np.array([[0, 2, 1]], dtype=np.float32) weights = np.array([[1, 1, 1], [2, 2.25, 0]], dtype=np.float32) in_activations = np.array([[1, 4]], dtype=np.float32) in_relevances = back_dense_fn(out_relevances, in_activations, weights) assert np.allclose([1.2, 1.8], in_relevances) assert np.allclose( [3.44895196, 3.20214176, 1.3489064], back_dense_fn( np.array([[2, 1, 1, 3, 1]], dtype=np.float32), np.array([[1, 4, 3]], dtype=np.float32), np.array([[1, 1, 2, 3, 4], [9, 3, 4, -5, 1], [1, 2, 5, -2, 2]], dtype=np.float32))) # different from numpy due to NaNs being replaced by zeros instead of # being replaced by 1 / #input units assert np.allclose( [[4.4000001, 0., 0.60000002]], back_dense_fn( np.array([[2, 1, 0, 3, 1]], dtype=np.float32), np.array([[1, 0, 3]], dtype=np.float32), np.array([[0, 1, 2, 3, 4], [9, 3, 4, -5, 1], [0, 0, 5, -2, 2]], dtype=np.float32))) # Only regression test, values are computed by hand, # so could be incorrect assert np.allclose( [[3.44895196, 3.202142, 1.34890628], [4.04285717, 0., 2.95714283]], back_dense_fn( np.array([[2, 1, 1, 3, 1], [2, 1, 0, 3, 1]], dtype=np.float32), np.array([[1, 4, 3], [1, 0, 3]], dtype=np.float32), np.array([[1, 1, 2, 3, 4], [9, 3, 4, -5, 1], [1, 2, 5, -2, 2]], dtype=np.float32)))
def test_dense_z_b_theano(): back_dense_fn = create_back_dense_fn('z_b', min_in=-2, max_in=4) out_relevances = np.array([[5, 4, 1]], dtype=np.float32) weights = np.array([[1, -2, 4], [0, 1, 2]], dtype=np.float32) in_activations = np.array([[3, 2]], dtype=np.float32) in_relevances = back_dense_fn(out_relevances, in_activations, weights) assert np.allclose([5 + 4 / 3.0 + 5 / 7.0, 8 / 3.0 + 2 / 7.0], in_relevances)
def test_dense_w_sqr_theano(): back_dense_fn = create_back_dense_fn('w_sqr') assert np.allclose([[ 1.57243109, 4.92130327, 0.50626564]], back_dense_fn(np.array([[2,1,0,3,1]], dtype=np.float32), np.array([[0,1,2,3,4], [9,3,4,-5,1],[0,0,5,-2,2]], dtype=np.float32))) assert np.allclose([[1.65684497, 4.97152281, 1.37163186]], back_dense_fn(np.array([[2,1,1,3,1]], dtype=np.float32), np.array([[1,1,2,3,4], [9,3,4,-5,1],[1,2,5,-2,2]], dtype=np.float32))) # only regression test, not handcalculated, result could be false(!) assert np.allclose([[ 1.57243109, 4.92130327, 0.50626564], [ 1.66131997, 5.27685881, 1.06182122]], back_dense_fn(np.array([[2,1,0,3,1], [2,1,1,3,1]], dtype=np.float32), np.array([[0,1,2,3,4], [9,3,4,-5,1],[0,0,5,-2,2]], dtype=np.float32)))
def test_dense_w_sqr_theano(): back_dense_fn = create_back_dense_fn('w_sqr') assert np.allclose( [[1.57243109, 4.92130327, 0.50626564]], back_dense_fn( np.array([[2, 1, 0, 3, 1]], dtype=np.float32), np.array([[0, 1, 2, 3, 4], [9, 3, 4, -5, 1], [0, 0, 5, -2, 2]], dtype=np.float32))) assert np.allclose( [[1.65684497, 4.97152281, 1.37163186]], back_dense_fn( np.array([[2, 1, 1, 3, 1]], dtype=np.float32), np.array([[1, 1, 2, 3, 4], [9, 3, 4, -5, 1], [1, 2, 5, -2, 2]], dtype=np.float32))) # only regression test, not handcalculated, result could be false(!) assert np.allclose( [[1.57243109, 4.92130327, 0.50626564], [1.66131997, 5.27685881, 1.06182122]], back_dense_fn( np.array([[2, 1, 0, 3, 1], [2, 1, 1, 3, 1]], dtype=np.float32), np.array([[0, 1, 2, 3, 4], [9, 3, 4, -5, 1], [0, 0, 5, -2, 2]], dtype=np.float32)))