Ejemplo n.º 1
0
    def test_convolution_with_step(self):
        x = graph.Constant(self.x)
        f = graph.Constant(self.filters)

        conv = graph.Convolution(x, f, step=2)

        np.testing.assert_array_equal(self.expected_step_2, graph.run(conv))
Ejemplo n.º 2
0
    def test_matmul_vec(self):
        x = graph.Constant([1, 2, 3])
        y = graph.Constant([[1, 2], [1, 3], [2, 4]])

        m = graph.Matmul(x, y)

        np.testing.assert_array_equal([9, 20], graph.run(m))
Ejemplo n.º 3
0
    def test_accuracy_with_flags(self):
        classes = graph.Constant([1, 2, 3, 2, 0, 3])
        e_classes = graph.Constant([0, 2, 3, 2, 1, 3])
        flags = graph.Constant([0, 1, 1, 1, 1, 0])

        acc = metrics.accuracy(e_classes, classes, flags)
        self.assertEqual(0.75, graph.run(acc))
Ejemplo n.º 4
0
    def test_multply(self):
        x1 = graph.Constant([[1], [2], [3], [4]])
        x2 = graph.Constant([[1, -1], [2, -2], [3, -3], [4, -4]])
        y = graph.Multiply(x1, x2)

        np.testing.assert_array_equal([[1, -1], [4, -4], [9, -9], [16, -16]],
                                      graph.run(y))
Ejemplo n.º 5
0
    def test_convolution(self):
        x_c = graph.Constant(self.x)
        f_c = graph.Constant(self.filters)

        conv = graph.Convolution(x_c, f_c)

        np.testing.assert_array_equal(graph.run(conv), self.expected)
Ejemplo n.º 6
0
    def test_concatenate(self):
        x1 = graph.Constant([[1, 2, 3], [4, 5, 6]])
        x2 = graph.Constant([[7, 8], [9, 10]])
        y = graph.Concatenate((x1, x2), axis=1)

        np.testing.assert_array_equal([[1, 2, 3, 7, 8], [4, 5, 6, 9, 10]],
                                      graph.run(y))
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
    def test_matmul(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)

        np.testing.assert_array_equal(graph.run(m), np.matmul(x_arr, y_arr))
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    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)
Ejemplo n.º 12
0
    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])
Ejemplo n.º 13
0
    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])
Ejemplo n.º 14
0
    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)
Ejemplo n.º 15
0
    def test_placeholder(self):
        a = graph.Placeholder(shape=(), batched=False)
        b = graph.Constant(2)
        c = graph.Sum(a, b)

        self.assertEqual(graph.run(c, {a: 3}), 5)
        self.assertEqual(graph.run(c, {a: 4}), 6)
Ejemplo n.º 16
0
    def test_batched(self):
        a = graph.Placeholder(shape=(2,), batched=True)
        b = graph.Constant([1, 2])
        c = graph.Sum(a, b)

        np.testing.assert_array_equal(graph.run(c, {a: np.array([[1, 2]])}), np.array([[2, 4]]))
        np.testing.assert_array_equal(graph.run(c, {a: np.array([[1, 2], [3, 4]])}), np.array([[2, 4], [4, 6]]))
Ejemplo n.º 17
0
    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))
Ejemplo n.º 18
0
    def test_relu(self):
        x = graph.Constant([[1, 2, -3, 4], [-1, 2, -3, 0]])
        y = graph.ReLU(x)
        y01 = graph.ReLU(x, 0.1)

        np.testing.assert_array_equal([[1, 2, 0, 4], [0, 2, 0, 0]],
                                      graph.run(y))
        np.testing.assert_array_equal(
            [[1, 2, -3 * 0.1, 4], [-1 * 0.1, 2, -3 * 0.1, 0]], graph.run(y01))
Ejemplo n.º 19
0
    def test_reduce_sum(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)

        np.testing.assert_array_equal([[9], [12]], graph.run(y1))
        np.testing.assert_array_equal([3, 7, 11], graph.run(y2))
        self.assertEqual(21, graph.run(y3))
Ejemplo n.º 20
0
    def test_reduce_mean(self):
        x = graph.Constant([[[1], [2]], [[3], [4]], [[5], [6]]])
        y1 = graph.ReduceMean(x, axis=0)
        y2 = graph.ReduceMean(x, axis=(1, -1))
        y3 = graph.ReduceMean(x)

        np.testing.assert_array_equal([[3], [4]], graph.run(y1))
        np.testing.assert_array_equal([1.5, 3.5, 5.5], graph.run(y2))
        self.assertEqual(3.5, graph.run(y3))
Ejemplo n.º 21
0
    def test_softmax(self):
        x_arr = np.array([[1, 2, 3], [-1, -2, -3]])
        x = graph.Constant(x_arr)
        y = graph.Softmax(x)

        s = np.repeat(np.sum(np.exp(x_arr - [[3], [-1]]), axis=-1),
                      x_arr.shape[-1]).reshape(x_arr.shape)
        np.testing.assert_array_equal(
            np.exp(x_arr - [[3], [-1]]) / s, graph.run(y))
Ejemplo n.º 22
0
    def test_convolution_batched(self):
        x_p = graph.Placeholder(self.x.shape, batched=True)
        f_c = graph.Constant(self.filters)

        conv = graph.Convolution(x_p, f_c)

        x = np.stack([self.x, 2 * self.x, 3 * self.x])
        expected = np.stack(
            [self.expected, 2 * self.expected, 3 * self.expected])

        np.testing.assert_array_equal(graph.run(conv, {x_p: x}), expected)
Ejemplo n.º 23
0
    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)))
Ejemplo n.º 24
0
    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))
Ejemplo n.º 25
0
    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])
Ejemplo n.º 26
0
    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))
Ejemplo n.º 27
0
    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)
Ejemplo n.º 28
0
    def test_accuracy(self):
        classes = graph.Constant([1, 2, 3, 2, 0, 3])
        e_classes = graph.Constant([0, 2, 3, 2, 1, 3])

        acc = metrics.accuracy(e_classes, classes)
        self.assertEqual(4/6, graph.run(acc))
Ejemplo n.º 29
0
    def test_fp_rate(self):
        classes = graph.Constant([1, 0, 0, 1, 1, 1, 1])
        e_classes = graph.Constant([0, 1, 0, 1, 1, 1, 0])

        pr = metrics.fp_rate(e_classes, classes)
        self.assertEqual(2 / 3, graph.run(pr))
Ejemplo n.º 30
0
    def test_divide(self):
        x1 = graph.Constant([1, 2, 3, 4])
        x2 = graph.Constant([4, 3, 2, 1])
        y = graph.Divide(x1, x2)

        np.testing.assert_array_equal([1 / 4, 2 / 3, 3 / 2, 4], graph.run(y))