@@ -0,0 +1,2167 @@
+# -*- coding: utf-8 -*-%0A%22%22%22%0ACreated on Tue Feb 07 19:23:18 2017%0A%0A@author: sakurai%0A%22%22%22%0A%0A%0Aimport unittest%0A%0Aimport numpy as np%0A%0Aimport chainer%0Afrom chainer import cuda%0Afrom chainer import gradient_check%0Afrom chainer import testing%0Afrom chainer.testing import attr%0A%0Afrom n_pair_mc_loss import n_pair_mc_loss%0A%0A%0Aclass TestNPairsMCLoss(unittest.TestCase):%0A%0A    def setUp(self):%0A        self.f_data = np.array(%5B%5B1, 1%5D, %5B-1, 1%5D, %5B-1, -1%5D, %5B1, -1%5D%5D,%0A                               dtype=np.float32)%0A        self.f_p_data = np.array(%5B%5B1, 1%5D, %5B-1, 1%5D, %5B-1, -1%5D, %5B1, -1%5D%5D,%0A                                 dtype=np.float32)%0A        self.l2_reg = 0.001%0A%0A    def check_forward(self, f_data, f_p_data, l2_reg):%0A        xp = cuda.get_array_module(f_data)%0A        num_examples = len(f_data)%0A        f = chainer.Variable(f_data)%0A        f_p = chainer.Variable(f_p_data)%0A%0A        loss = n_pair_mc_loss(f, f_p, l2_reg)%0A%0A        loss_for_each = %5B%5D%0A        for i in range(num_examples):%0A            exps = %5B%5D%0A            for j in set(range(num_examples)) - %7Bi%7D:%0A                exp_ij = xp.exp(f_data%5Bi%5D.dot(f_p_data%5Bj%5D) -%0A                                f_data%5Bi%5D.dot(f_p_data%5Bi%5D))%0A                exps.append(exp_ij)%0A            loss_i = xp.log(1.0 + sum(exps))%0A            loss_for_each.append(loss_i)%0A        loss_expected = xp.asarray(loss_for_each, dtype=np.float32).mean()%0A%0A        testing.assert_allclose(loss.data, loss_expected, atol=1e-2)%0A%0A    def test_forward_cpu(self):%0A        self.check_forward(self.f_data, self.f_p_data, self.l2_reg)%0A%0A    @attr.gpu%0A    def test_forward_gpu(self):%0A        self.check_forward(%0A            cuda.to_gpu(self.f_data), cuda.to_gpu(self.f_p_data), self.l2_reg)%0A%0A    def check_backward(self, f, f_p, l2_reg):%0A        gradient_check.check_backward(%0A            lambda f, f_p: n_pair_mc_loss(f, f_p, l2_reg), (f, f_p),%0A            None, atol=1.e-1)%0A%0A    def test_backward_cpu(self):%0A        self.check_backward(self.f_data, self.f_p_data, self.l2_reg)%0A%0A    @attr.gpu%0A    def test_backward_gpu(self):%0A        self.check_backward(%0A            cuda.to_gpu(self.f_data), cuda.to_gpu(self.f_p_data), self.l2_reg)%0A%0A%0Atesting.run_module(__name__, __file__)%0A

Add tests for n_pair_mc_loss