Beispiel #1
0
    def test_gradient_multiple_with_indices(self, inputs, gc, dc):
        (x1, x2, x3) = inputs

        def f(inputs, outputs):
            for idx in [0, 1, 2]:
                self.assertEqual(type(inputs[idx].shape), tuple)
                outputs[idx].reshape(inputs[idx].shape)
                outputs[idx].data[...] = inputs[idx].data * 2

        def grad_f(inputs, outputs):
            # Ordering is [inputs, outputs, grad_outputs]
            self.assertEqual(len(inputs), 8)
            self.assertEqual(len(outputs), 1)
            for (grad_output_idx, grad_input_idx) in [(6, 0)]:
                grad_output = inputs[grad_output_idx]
                grad_input = outputs[grad_input_idx]
                grad_input.reshape(grad_output.shape)
                grad_input.data[...] = grad_output.data * 2

        op = CreatePythonOperator(
            f, ["x1", "x2", "x3"], ["y1", "y2", "y3"],
            grad_f=grad_f,
            grad_output_indices=[0, 2],  # Receive grad outputs for y1 and y3
            grad_input_indices=[0]       # Produce grad inputs for x1
        )

        self.assertGradientChecks(gc, op, [x1, x2, x3], 0, [0, 2])
        self.assertDeviceChecks(dc, op, [x1, x2, x3], [0, 1, 2])
 def test_exception_builder(self):
     op = CreatePythonOperator(MainOpFunctionThatThrowsCustomErrorInBuilder,
                               [], [])
     with six.assertRaisesRegex(
             self, CustomError,
             "This is an intentional exception in builder."):
         workspace.RunOperatorOnce(op)
Beispiel #3
0
 def test_feed(self, x):
     def f(inputs, _):
         self.assertEqual(x.shape, inputs[0].shape)
         self.assertEqual(type(inputs[0].shape), tuple)
         self.assertEqual(type(inputs[0].data), np.ndarray)
         np.testing.assert_almost_equal(x, inputs[0].data)
     op = CreatePythonOperator(f, ["x"], [])
     workspace.FeedBlob("x", x)
     workspace.RunOperatorOnce(op)
Beispiel #4
0
    def test_caught_exception_doesnt_terminate(self, x):
        def f(inputs, outputs):
            try:
                raise Exception("Exception in handler")
            except Exception:
                pass

        op = CreatePythonOperator(f, ["x"], ["y"])
        workspace.FeedBlob("x", x)
        workspace.RunOperatorOnce(op)
Beispiel #5
0
 def test_feed_with_gc(self, x):
     def f(inputs, _):
         self.assertEqual(x.shape, inputs[0].shape)
         np.testing.assert_almost_equal(x, inputs[0].data)
     op = CreatePythonOperator(f, ["x"], [])
     workspace.FeedBlob("x", x)
     workspace.RunOperatorOnce(op)
     del f
     workspace.FeedBlob("x", x)
     workspace.RunOperatorOnce(op)
Beispiel #6
0
    def test_reshape(self, x):
        def f(inputs, outputs):
            outputs[0].reshape(inputs[0].shape)
            self.assertEqual(x.shape, inputs[0].shape)
            self.assertEqual(x.shape, outputs[0].shape)
            outputs[0].data[...] = inputs[0].data

        op = CreatePythonOperator(f, ["x"], ["y"])
        workspace.FeedBlob("x", x)
        workspace.RunOperatorOnce(op)
        y = workspace.FetchBlob("y")
        np.testing.assert_almost_equal(x, y)
Beispiel #7
0
    def test_gradient(self, x, in_place, gc, dc):
        def f(inputs, outputs):
            outputs[0].reshape(inputs[0].shape)
            outputs[0].data[...] = inputs[0].data * 2

        def grad_f(inputs, outputs):
            # Ordering is [inputs, outputs, grad_outputs]
            grad_output = inputs[2]

            grad_input = outputs[0]
            grad_input.reshape(grad_output.shape)
            grad_input.data[...] = grad_output.data * 2

        op = CreatePythonOperator(
            f, ["x"], ["x" if in_place else "y"], grad_f=grad_f)
        self.assertGradientChecks(gc, op, [x], 0, [0])
        self.assertDeviceChecks(dc, op, [x], [0])
Beispiel #8
0
 def test_multithreaded_evaluation(self, x, n, w):
     def f(inputs, outputs):
         outputs[0].reshape(inputs[0].shape)
         outputs[0].data[...] = inputs[0].data
     ops = [CreatePythonOperator(f, ["x"], [str(i)]) for i in range(n)]
     net = core.Net("net")
     net.Proto().op.extend(ops)
     net.Proto().type = "dag"
     net.Proto().num_workers = w
     iters = 100
     plan = core.Plan("plan")
     plan.AddStep(core.ExecutionStep("test-step", net, iters))
     workspace.FeedBlob("x", x)
     workspace.RunPlan(plan.Proto().SerializeToString())
     for i in range(n):
         y = workspace.FetchBlob(str(i))
         np.testing.assert_almost_equal(x, y)
Beispiel #9
0
    def test_gradient_multiple(self, inputs, gc, dc):
        (x1, x2) = inputs

        def f(inputs, outputs):
            for idx in [0, 1]:
                self.assertEqual(type(inputs[idx].shape), tuple)
                outputs[idx].reshape(inputs[idx].shape)
                outputs[idx].data[...] = inputs[idx].data * 2

        def grad_f(inputs, outputs):
            # Ordering is [inputs, outputs, grad_outputs]
            self.assertEqual(len(inputs), 6)
            self.assertEqual(len(outputs), 2)
            for (grad_output_idx, grad_input_idx) in [(4, 0), (5, 1)]:
                grad_output = inputs[grad_output_idx]
                grad_input = outputs[grad_input_idx]
                grad_input.reshape(grad_output.shape)
                grad_input.data[...] = grad_output.data * 2

        op = CreatePythonOperator(f, ["x1", "x2"], ["y1", "y2"], grad_f=grad_f)

        for idx in [0, 1]:
            self.assertGradientChecks(gc, op, [x1, x2], idx, [0, 1])
        self.assertDeviceChecks(dc, op, [x1, x2], [0, 1])
Beispiel #10
0
 def test_exception(self):
     op = CreatePythonOperator(MainOpFunctionThatThrowsRuntimeError, [], [])
     with self.assertRaisesRegexp(
         RuntimeError, "This is an intentional exception."
     ):
         workspace.RunOperatorOnce(op)
Beispiel #11
0
    def test_forward_and_gradient(self):
        N = 64
        Ncls = 2
        Y = np.random.randn(N, 4 * Ncls).astype(np.float32)
        Y_hat = np.random.randn(N, 4 * Ncls).astype(np.float32)
        U = np.random.randn(N, 10 * Ncls).astype(np.float32)
        print(np.prod(Y.shape))
        print(np.prod(U.shape))
        inside_weights = np.random.randn(N, 4 * Ncls).astype(np.float32)
        inside_weights[inside_weights < 0] = 0
        #outside_weights = np.ones((N, 4 * Ncls)).astype(np.float32)
        outside_weights_ind = np.random.randint(0, Ncls, N)
        outside_weights = np.zeros((N, 4 * Ncls)).astype(np.float32)
        for i in range(N):
            ind = 4 * outside_weights_ind[i]
            outside_weights[i, ind:ind + 4] = 1
        scale = np.random.random()
        beta = np.random.random()
        bbox_inw = outside_weights * (Y - Y_hat)

        #op = CreatePythonOperator(
        #    CovLossOp().forward, ["x1", "x2", "x3"], ["y1"],
        #    grad_f=CovLossOp().backward,
        #    grad_input_indices=[0, 1]
        #    )

        #op = CreatePythonOperator(
        #    UpperTriangularOp().forward, ["x1"], ["y1"],
        #    grad_f=UpperTriangularOp().backward,
        #    #grad_input_indices=[0, 1]
        #    )
        op = CreatePythonOperator(
            LogDetOp().forward,
            ["x1"],
            ["y1"],
            grad_f=LogDetOp().backward,
            #grad_input_indices=[0, 1]
        )
        #op = CreatePythonOperator(
        #    SimpleOp().forward, ["x1"], ["y1"],
        #    grad_f=SimpleOp().backward,
        #    #grad_input_indices=[0, 1]
        #    )

        gc = gradient_checker.GradientChecker(
            stepsize=0.005,
            threshold=0.005,
            #device_option=core.DeviceOption(caffe2_pb2.CUDA, 0)
        )

        #res, grad, grad_estimated = gc.CheckSimple(
        #    op, [bbox_inw, U, outside_weights], 0, [0]
        #)
        U = U.reshape((N, Ncls, 10))
        U_reshape = np.zeros((N, Ncls, 4, 4), dtype=U.dtype)
        U_reshape[:, :, 0, 0] = np.exp(U[:, :, 0])
        U_reshape[:, :, 1, 1] = np.exp(U[:, :, 4])
        U_reshape[:, :, 2, 2] = np.exp(U[:, :, 7])
        U_reshape[:, :, 3, 3] = np.exp(U[:, :, 9])
        U_reshape[:, :, 0, 1:] = U[:, :, 1:4]
        U_reshape[:, :, 1, 2:] = U[:, :, 5:7]
        U_reshape[:, :, 2, 3] = U[:, :, 8]
        res, grad, grad_estimated = gc.CheckSimple(op, [U_reshape], 0, [0])
        #res, grad, grad_estimated = gc.CheckSimple(
        #    op, [U], 0, [0]
        #)

        self.assertTrue(grad.shape == grad_estimated.shape,
                        'Fail check: grad.shape != grad_estimated.shape')

        ## To inspect the gradient and estimated gradient:
        #np.set_printoptions(precision=3, suppress=True)
        #print('grad:')
        #print(grad)
        #print('grad_estimated:')
        #print(grad_estimated)

        self.assertTrue(res)
Beispiel #12
0
 def test_exception(self):
     op = CreatePythonOperator(MainOpFunctionThatThrowsRuntimeError, [], [])
     with self.assertRaises(RuntimeError):
         workspace.RunOperatorOnce(op)