Exemplo n.º 1
0
    def check_forward(self, x_data, t_data, w_data, sampler):
        batch_size = len(self.t)
        x = chainer.Variable(x_data)
        t = chainer.Variable(t_data)
        w = chainer.Variable(w_data)

        # return_samples=False
        y = functions.negative_sampling(x,
                                        t,
                                        w,
                                        sampler,
                                        self.sample_size,
                                        reduce=self.reduce)
        assert y.dtype == self.dtype

        # return_samples=True
        y_, samples = functions.negative_sampling(x,
                                                  t,
                                                  w,
                                                  sampler,
                                                  self.sample_size,
                                                  reduce=self.reduce,
                                                  return_samples=True)

        xp = chainer.backend.get_array_module(x)
        assert isinstance(samples, xp.ndarray)
        assert samples.dtype == numpy.int32
        assert samples.shape == (batch_size, self.sample_size + 1)

        # Sampler is deterministic, so y and y_ should equal.
        assert y.dtype == y_.dtype
        numpy.testing.assert_array_equal(cuda.to_cpu(y.array),
                                         cuda.to_cpu(y_.array))

        assert y.shape == self.gy.shape

        samples = cuda.to_cpu(samples)

        loss = numpy.empty((len(self.x), ), self.dtype)
        for i in six.moves.range(len(self.x)):
            ix = self.x[i]
            it = self.t[i]
            if it == -1:
                loss[i] = 0
            else:
                iw = self.w[samples[i]]

                f = iw.dot(ix)
                # first one is positive example
                f[0] *= -1
                loss[i] = numpy.logaddexp(f, 0).sum()

        if self.reduce == 'sum':
            loss = loss.sum()

        assert y.dtype == loss.dtype
        testing.assert_allclose(y.data, loss, **self.check_forward_options)
Exemplo n.º 2
0
    def test_forward(self, backend_config):
        sampler = make_sampler(backend_config, self.label_size)
        x_data = backend_config.get_array(self.x)
        t_data = backend_config.get_array(self.t)
        w_data = backend_config.get_array(self.w)
        batch_size = len(self.t)
        x = chainer.Variable(x_data)
        t = chainer.Variable(t_data, requires_grad=False)
        w = chainer.Variable(w_data)

        # return_samples=False
        y = functions.negative_sampling(
            x, t, w, sampler, self.sample_size, reduce=self.reduce)
        assert y.dtype == self.dtype

        # return_samples=True
        y_, samples = functions.negative_sampling(
            x, t, w, sampler, self.sample_size, reduce=self.reduce,
            return_samples=True)

        xp = chainer.backend.get_array_module(x)
        assert isinstance(samples, xp.ndarray)
        assert samples.dtype == numpy.int32
        assert samples.shape == (batch_size, self.sample_size + 1)

        # Sampler is deterministic, so y and y_ should equal.
        assert y.dtype == y_.dtype
        cpu_device = CpuDevice()
        numpy.testing.assert_array_equal(
            cpu_device.send(y.array), cpu_device.send(y_.array))

        assert y.shape == self.gy.shape

        samples = cpu_device.send(samples)

        loss = numpy.empty((len(self.x),), self.dtype)
        for i in six.moves.range(len(self.x)):
            ix = self.x[i]
            it = self.t[i]
            if it == -1:
                loss[i] = 0
            else:
                iw = self.w[samples[i]]

                f = iw.dot(ix)
                # first one is positive example
                f[0] *= -1
                loss[i] = numpy.logaddexp(f, 0).sum()

        if self.reduce == 'sum':
            loss = loss.sum()

        assert y.dtype == loss.dtype
        testing.assert_allclose(y.data, loss, **self.check_forward_options)
 def f(x, w):
     return functions.negative_sampling(x,
                                        t_data,
                                        w,
                                        sampler,
                                        self.sample_size,
                                        reduce=self.reduce)
Exemplo n.º 4
0
    def check_forward(self, x_data, t_data, w_data, sampler):
        x = chainer.Variable(x_data)
        t = chainer.Variable(t_data)
        w = chainer.Variable(w_data)
        y = functions.negative_sampling(
            x, t, w, sampler, self.sample_size, reduce=self.reduce)
        self.assertEqual(y.shape, self.gy.shape)

        samples = cuda.to_cpu(y.creator.samples)

        loss = numpy.empty((len(self.x),), numpy.float32)
        for i in six.moves.range(len(self.x)):
            ix = self.x[i]
            it = self.t[i]
            if it == -1:
                loss[i] = 0
            else:
                iw = self.w[samples[i]]

                f = iw.dot(ix)
                # first one is positive example
                f[0] *= -1
                loss[i] = numpy.logaddexp(f, 0).sum()

        if self.reduce == 'sum':
            loss = loss.sum()

        testing.assert_allclose(y.data, loss)
    def check_forward(self, x_data, t_data, w_data, sampler):
        x = chainer.Variable(x_data)
        t = chainer.Variable(t_data)
        w = chainer.Variable(w_data)
        y = functions.negative_sampling(x,
                                        t,
                                        w,
                                        sampler,
                                        self.sample_size,
                                        reduce=self.reduce)
        self.assertEqual(y.shape, self.gy.shape)

        samples = cuda.to_cpu(y.creator.samples)

        loss = numpy.empty((len(self.x), ), numpy.float32)
        for i in six.moves.range(len(self.x)):
            ix = self.x[i]
            it = self.t[i]
            if it == -1:
                loss[i] = 0
            else:
                iw = self.w[samples[i]]
                f = iw.dot(ix)
                # first one is positive example
                f[0] *= -1
                loss[i] = numpy.logaddexp(f, 0).sum()

        if self.reduce == 'sum':
            loss = loss.sum()

        testing.assert_allclose(y.data, loss)
Exemplo n.º 6
0
 def __call__(self, xb, eb, sampler, ngs):
     loss = None
     for i in range(len(xb)):
         x = Variable(np.array([xb[i]], dtype=np.int32))
         e = eb[i]
         ls = F.negative_sampling(e, x, self.embed.W, sampler, ngs)
         loss = ls if loss is None else loss + ls
     return loss
    def test_forward(self, backend_config):
        # TODO(niboshi): Support it
        if backend_config.use_chainerx and self.dtype == numpy.float16:
            raise unittest.SkipTest('ChainerX does not support float16')
        sampler = make_sampler(backend_config, self.label_size)
        x_data = backend_config.get_array(self.x)
        t_data = backend_config.get_array(self.t)
        w_data = backend_config.get_array(self.w)
        batch_size = len(self.t)
        x = chainer.Variable(x_data)
        t = chainer.Variable(t_data, requires_grad=False)
        w = chainer.Variable(w_data)

        # return_samples=False
        y = functions.negative_sampling(x,
                                        t,
                                        w,
                                        sampler,
                                        self.sample_size,
                                        reduce=self.reduce)
        assert y.dtype == self.dtype

        # return_samples=True
        y_, samples = functions.negative_sampling(x,
                                                  t,
                                                  w,
                                                  sampler,
                                                  self.sample_size,
                                                  reduce=self.reduce,
                                                  return_samples=True)

        xp = chainer.backend.get_array_module(x)
        assert isinstance(samples, xp.ndarray)
        assert samples.dtype == numpy.int32
        assert samples.shape == (batch_size, self.sample_size + 1)

        # Sampler is deterministic, so y and y_ should equal.
        assert y.dtype == y_.dtype
        cpu_device = CpuDevice()
        numpy.testing.assert_array_equal(cpu_device.send(y.array),
                                         cpu_device.send(y_.array))

        assert y.shape == self.gy.shape

        samples = cpu_device.send(samples)

        loss = numpy.empty((len(self.x), ), self.dtype)
        for i in six.moves.range(len(self.x)):
            ix = self.x[i]
            it = self.t[i]
            if it == -1:
                loss[i] = 0
            else:
                iw = self.w[samples[i]]

                f = iw.dot(ix)
                # first one is positive example
                f[0] *= -1
                loss[i] = numpy.logaddexp(f, 0).sum()

        if self.reduce == 'sum':
            loss = loss.sum()

        assert y.dtype == loss.dtype
        testing.assert_allclose(y.data, loss, **self.check_forward_options)
Exemplo n.º 8
0
 def f(x, w):
     return functions.negative_sampling(
         x, t_data, w, sampler, self.sample_size, reduce=self.reduce)