Пример #1
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]]))
Пример #2
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)
Пример #3
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))
Пример #4
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))
Пример #5
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))
Пример #6
0
    def test_reduce_sum_batched(self):
        x_arr = np.array([[[1], [2]], [[3], [4]], [[5], [6]]])
        y_arr = np.array([9, 12])
        x = graph.Placeholder(shape=(3, 2, 1), batched=True)
        y1 = graph.ReduceSum(x, (0, 2), True)
        y2 = graph.ReduceSum(x, (0, 2), False)

        np.testing.assert_array_equal(
            y_arr * 3, graph.run(y1, {x: np.array([x_arr, 2 * x_arr])}))
        np.testing.assert_array_equal(
            [y_arr, 2 * y_arr], graph.run(y2,
                                          {x: np.array([x_arr, 2 * x_arr])}))
Пример #7
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))
Пример #8
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))
Пример #9
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)
Пример #10
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))
Пример #11
0
    def run(self,
            inputs,
            expected_output,
            *args,
            tf=None,
            tf_session=None,
            **kwargs):
        input_dict = self.get_input_dict(inputs, expected_output)

        if not tf_session or not tf:
            loss, grads = run((self.loss, self.grad), input_dict)

            self._run(grads, *args, **kwargs)

            return loss
        else:
            if kwargs:
                raise NotImplementedError
            input_dict = {
                p.build_tf(tf, tf_session): v
                for p, v in input_dict.items()
            }
            opt_tensor = self.get_tf_opt(tf, tf_session, *args, **kwargs)
            loss, _ = tf_session.run(opt_tensor, input_dict)
            return loss
Пример #12
0
    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])}))
Пример #13
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))
Пример #14
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))
Пример #15
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))
Пример #16
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))
Пример #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))
Пример #18
0
    def test_slice_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))

        np.testing.assert_array_equal([[[2], [5]], [[-2], [-5]]],
                                      graph.run(
                                          y1, {x: np.array([x_arr, -x_arr])}))
Пример #19
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))
Пример #20
0
    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])}))
Пример #21
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)
Пример #22
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)
Пример #23
0
    def __call__(self, *args, tf_session=None, **kwargs):
        assert len(args) + len(kwargs) == len(self.inputs)
        assert not args or not kwargs

        input_dict = kwargs if kwargs else {
            i: v
            for i, v in zip(self.inputs, args)
        }

        return run(self.output, input_dict, tf_session)
Пример #24
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)
Пример #25
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))
Пример #26
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)
Пример #27
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)
Пример #28
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])
Пример #29
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)
Пример #30
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])