def test_unstack(self): tensor = random_uniform([1,2,3,4]) for dim in range(len(tensor.shape)): components = unstack(tensor, dim, keepdims=True) np.testing.assert_equal(tensor, concat(components, axis=dim)) components = unstack(tensor, dim, keepdims=False) np.testing.assert_equal(tensor, stack(components, axis=dim)) # --- TensorFlow --- sess = tf.InteractiveSession() for dim in range(len(tensor.shape)): components = unstack(tf.constant(tensor), dim, keepdims=True) np.testing.assert_equal(tensor, concat(components, axis=dim).eval()) components = unstack(tf.constant(tensor), dim, keepdims=False) np.testing.assert_equal(tensor, stack(components, axis=dim).eval())
def test_pad_circular(self): tf.InteractiveSession() # --- 1D --- a = np.array([1, 2, 3, 4, 5]) a_ = pad(a, [[2, 3]], mode='circular') np.testing.assert_equal(a_, [4, 5, 1, 2, 3, 4, 5, 1, 2, 3]) a = tf.constant(a) a_ = pad(a, [[2, 3]], mode='circular').eval() np.testing.assert_equal(a_, [4, 5, 1, 2, 3, 4, 5, 1, 2, 3]) # --- 2D + batch --- t = [[3, 1, 2, 3, 1], [6, 4, 5, 6, 4], [3, 1, 2, 3, 1]] a = np.array([[1, 2, 3], [4, 5, 6]]).reshape([1, 2, 3, 1]) a_ = pad(a, [[0, 0], [0, 1], [1, 1], [0, 0]], mode='circular') np.testing.assert_equal(a_.shape, [1, 3, 5, 1]) np.testing.assert_equal(a_.reshape([3, 5]), t) a = tf.constant(a) a_ = pad(a, [[0, 0], [0, 1], [1, 1], [0, 0]], mode='circular').eval() np.testing.assert_equal(a_.shape, [1, 3, 5, 1]) np.testing.assert_equal(a_.reshape([3, 5]), t)
def test_multimode_pad(self): a = np.array([[1,2], [3,4]]) print(a) p = pad(a, [[1,1], [1,1]], mode=['symmetric', ['wrap', 'constant']], constant_values=[0, [0, 10]]) np.testing.assert_equal(p[0,1:-1], [1,2]) np.testing.assert_equal(p[3,1:-1], [3,4]) np.testing.assert_equal(p[1:-1,0], [2,4]) np.testing.assert_equal(p[1:-1,3], [10, 10]) print(p) tf.InteractiveSession() a_tf = tf.constant(a, tf.float32, shape=(2,2)) p_tf = pad(a_tf, [[1,1], [1,1]], mode=['symmetric', ['wrap', 'constant']], constant_values=[0, [0, 10]]) np.testing.assert_equal(p, p_tf.eval())
def test_fft(self): tf.InteractiveSession() for dims in range(1, 4): shape = [2] + [4] * dims + [3] x_np = np.random.randn(*shape) + 1j * np.random.randn(*shape) x_np = x_np.astype(np.complex64) x_tf = tf.constant(x_np, tf.complex64) k_np = fft(x_np) k_tf = fft(x_tf) self.assertLess(max(abs(k_np - k_tf.eval())), 1e-3) x_np = ifft(k_np) x_tf = ifft(k_tf) self.assertLess(max(abs(x_np - x_tf.eval())), 1e-3)