def test_relu_grad(self): x = graph.Constant([[1, 2, -3, 4], [-1, 2, -3, 0]]) y = graph.ReLU(x) y01 = graph.ReLU(x, 0.1) g = graph.Grad(y, x) g01 = graph.Grad(y01, x) np.testing.assert_array_equal([[1, 1, 0, 1], [0, 1, 0, 1]], graph.run(g)) np.testing.assert_array_equal([[1, 1, 0.1, 1], [0.1, 1, 0.1, 1]], graph.run(g01))
def test_reduce_sum_grad(self): x = graph.Constant([[[1], [2]], [[3], [4]], [[5], [6]]]) y1 = graph.ReduceSum(x, axis=0) y2 = graph.ReduceSum(x, axis=(1, -1)) y3 = graph.ReduceSum(x) g1 = graph.Grad(y1, x) g2 = graph.Grad(y2, x) g3 = graph.Grad(y3, x) np.testing.assert_array_equal(np.ones_like(x.value), graph.run(g1)) np.testing.assert_array_equal(np.ones_like(x.value), graph.run(g2)) np.testing.assert_array_equal(np.ones_like(x.value), graph.run(g3))
def test_reduce_sum_grad_batched(self): x_arr = np.array([[[1], [2]], [[3], [4]], [[5], [6]]]) x = graph.Placeholder(shape=(3, 2, 1), batched=True) y1 = graph.ReduceSum(x, (0, 2), True) y2 = graph.ReduceSum(x, (0, 2), False) g1 = graph.Grad(y1, x) g2 = graph.Grad(y2, x) np.testing.assert_array_equal( [np.ones_like(x_arr), np.ones_like(x_arr)], graph.run(g1, {x: np.array([x_arr, 2 * x_arr])})) np.testing.assert_array_equal( [np.ones_like(x_arr), np.ones_like(x_arr)], graph.run(g2, {x: np.array([x_arr, 2 * x_arr])}))
def test_slice_grad(self): x_arr = [[1, 2, 3], [4, 5, 6]] x = graph.Constant(x_arr) y1 = graph.Slice(x, (0, 1), (2, 2)) g1 = graph.Grad(y1, x) np.testing.assert_array_equal([[0, 1, 0], [0, 1, 0]], graph.run(g1))
def test_slice_grad_batched(self): x_arr = np.array([[1, 2, 3], [4, 5, 6]]) x = graph.Placeholder((2, 3), True) y1 = graph.Slice(x, (0, 1), (2, 2)) g1 = graph.Grad(y1, x) np.testing.assert_array_equal( [[[0, 1, 0], [0, 1, 0]], [[0, 1, 0], [0, 1, 0]]], graph.run(g1, {x: np.array([x_arr, -x_arr])}))
def test_divide_grad(self): x1 = graph.Constant([1, 2, 3, 4]) x2 = graph.Constant([4, 3, 2, 1]) y = graph.Divide(x1, x2) g = graph.Grad(y, (x1, x2)) g1, g2 = graph.run(g) np.testing.assert_array_equal([1 / 4, 1 / 3, 1 / 2, 1], g1) np.testing.assert_array_equal([-1 / 16, -2 / 9, -3 / 4, -4], g2)
def test_multply_grad(self): x1 = graph.Constant([[1], [2], [3], [4]]) x2 = graph.Constant([[1, -1], [2, -2], [3, -3], [4, -4]]) y = graph.Multiply(x1, x2) g = graph.Grad(y, (x1, x2)) g1, g2 = graph.run(g) np.testing.assert_array_equal([[0], [0], [0], [0]], g1) np.testing.assert_array_equal([[1, 1], [2, 2], [3, 3], [4, 4]], g2)
def test_concatenate_grad(self): x1 = graph.Constant([[1, 2, 3], [4, 5, 6]]) x2 = graph.Constant([[7, 8], [9, 10]]) y = graph.Concatenate((x1, x2), axis=1) g = graph.Grad(y, (x1, x2)) g1, g2 = graph.run(g) np.testing.assert_array_equal(np.ones_like(x1.value), g1) np.testing.assert_array_equal(np.ones_like(x2.value), g2)
def test_matmul_vec_grad(self): x = graph.Constant([1, 2, 3]) y = graph.Constant([[1, 2], [1, 3], [2, 4]]) m = graph.Matmul(x, y) g = graph.Grad(m, [x, y]) g_x, g_y = graph.run(g) np.testing.assert_array_equal([3, 4, 6], g_x) np.testing.assert_array_equal([[1, 1], [2, 2], [3, 3]], g_y)
def test_grad_simple(self): a = graph.Constant(1) b = graph.Constant(2) c = graph.Constant(4) d = graph.Sum(a, b) e = graph.MultiplyByScalar(d, c) g = graph.Grad(e, [a, b, c]) self.assertSequenceEqual(graph.run(g), [4, 4, 3])
def test_convolution_grad_with_step(self): x_c = graph.Constant(self.x) f_c = graph.Constant(self.filters) conv = graph.Convolution(x_c, f_c, step=2) grad = graph.Grad(conv, [x_c, f_c]) grad_x, grad_f = graph.run(grad) np.testing.assert_array_equal(grad_x, self.grad_x_step_2) np.testing.assert_array_equal(grad_f, self.grad_f_step_2)
def test_grad(self): a = graph.Constant(1) b = graph.Constant(2) c = graph.Constant(4) d = graph.Sum(a, b) e = graph.Sum(b, c) f = graph.MultiplyByScalar(d, e) g = graph.Grad(f, [a, b, c]) self.assertSequenceEqual(graph.run(g), [6, 9, 3])
def test_grad_placeholder_variable(self): a = graph.Constant(1) b = graph.Placeholder(batched=False, shape=()) c = graph.Variable(shape=()) c.value = 4 d = graph.Sum(a, b) e = graph.MultiplyByScalar(d, c) g = graph.Grad(e, [a, b, c]) self.assertSequenceEqual(graph.run(g, {b: 2}), [4, 4, 3])
def test_convolution_grad_batched(self): x_p = graph.Placeholder(self.x.shape, batched=True) f_c = graph.Constant(self.filters) x = np.stack([self.x, 2 * self.x, 3 * self.x]) conv = graph.Convolution(x_p, f_c) grad = graph.Grad(conv, [x_p, f_c]) grad_x, grad_f = graph.run(grad, {x_p: x}) np.testing.assert_array_equal(grad_x, np.stack([self.grad_x] * 3)) np.testing.assert_array_equal(grad_f, self.grad_f * 6)
def test_matmul_grad(self): x_arr = np.array([[1, 2], [2, 3], [3, 4]]) y_arr = np.array([[1, 2, 3, 4], [4, 5, 6, 7]]) x = graph.Constant(x_arr) y = graph.Constant(y_arr) m = graph.Matmul(x, y) g = graph.Grad(m, [x, y]) mv, (g_x, g_y) = graph.run((m, g)) self.assertSequenceEqual(g_x.shape, x.shape) self.assertSequenceEqual(g_y.shape, y.shape) np.testing.assert_array_equal(g_x, np.matmul(np.ones_like(mv), y_arr.T)) np.testing.assert_array_equal(g_y, np.matmul(x_arr.T, np.ones_like(mv)))