def forward(self, xs, ys, reduce='mean'): indices = argsort_list_descent(xs) xs = permutate_list(xs, indices, inv=False) xs = F.transpose_sequence(xs) ys = permutate_list(ys, indices, inv=False) ys = F.transpose_sequence(ys) return F.crf1d(self.cost, xs, ys, reduce)
def check_forward(self, cost_data, xs_data, ys_data): cost = chainer.Variable(cost_data) xs = [chainer.Variable(x) for x in xs_data] ys = [chainer.Variable(y) for y in ys_data] log_p = functions.crf1d(cost, xs, ys) z = numpy.zeros((self.batches[0],), numpy.float32) for b, length in enumerate(self.lengths): for ys in itertools.product(range(self.n_label), repeat=length): z[b] += numpy.exp(self._calc_score(b, ys)) score = numpy.zeros((self.batches[0],), numpy.float32) for b, length in enumerate(self.lengths): ys = [self.ys[i][b] for i in range(length)] score[b] = self._calc_score(b, ys) expect = numpy.sum(-(score - numpy.log(z))) / self.batches[0] testing.assert_allclose(log_p.data, expect)
def check_forward(self, cost_data, xs_data, ys_data): cost = chainer.Variable(cost_data) xs = [chainer.Variable(x) for x in xs_data] ys = [chainer.Variable(y) for y in ys_data] log_p = functions.crf1d(cost, xs, ys) z = numpy.zeros((self.batches[0], ), numpy.float32) for b, length in enumerate(self.lengths): for ys in itertools.product(range(self.n_label), repeat=length): z[b] += numpy.exp(self._calc_score(b, ys)) score = numpy.zeros((self.batches[0], ), numpy.float32) for b, length in enumerate(self.lengths): ys = [self.ys[i][b] for i in range(length)] score[b] = self._calc_score(b, ys) expect = numpy.sum(-(score - numpy.log(z))) / self.batches[0] testing.assert_allclose(log_p.data, expect)
def test_forward(self): cost = chainer.Variable(self.cost) xs = [chainer.Variable(x) for x in self.xs] ys = [chainer.Variable(y) for y in self.ys] log_p = functions.crf1d(cost, xs, ys) z = numpy.zeros((self.batch, ), numpy.float32) for y1 in range(self.n_label): for y2 in range(self.n_label): for y3 in range(self.n_label): z += numpy.exp(self.xs[0][range(self.batch), y1] + self.xs[1][range(self.batch), y2] + self.xs[2][range(self.batch), y3] + self.cost[y1, y2] + self.cost[y2, y3]) score = (self.xs[0][range(self.batch), self.ys[0]] + self.xs[1][range(self.batch), self.ys[1]] + self.xs[2][range(self.batch), self.ys[2]] + self.cost[self.ys[0], self.ys[1]] + self.cost[self.ys[1], self.ys[2]]) expect = numpy.sum(-(score - numpy.log(z))) / self.batch testing.assert_allclose(log_p.data, expect)
def test_forward(self): cost = chainer.Variable(self.cost) xs = [chainer.Variable(x) for x in self.xs] ys = [chainer.Variable(y) for y in self.ys] log_p = functions.crf1d(cost, xs, ys) z = numpy.zeros((self.batch,), numpy.float32) for y1 in range(self.n_label): for y2 in range(self.n_label): for y3 in range(self.n_label): z += numpy.exp(self.xs[0][range(self.batch), y1] + self.xs[1][range(self.batch), y2] + self.xs[2][range(self.batch), y3] + self.cost[y1, y2] + self.cost[y2, y3]) score = (self.xs[0][range(self.batch), self.ys[0]] + self.xs[1][range(self.batch), self.ys[1]] + self.xs[2][range(self.batch), self.ys[2]] + self.cost[self.ys[0], self.ys[1]] + self.cost[self.ys[1], self.ys[2]]) expect = numpy.sum(-(score - numpy.log(z))) / self.batch testing.assert_allclose(log_p.data, expect)
def test_backward(self): cost = chainer.Variable(self.cost) xs = [chainer.Variable(self.xs[i]) for i in range(3)] ys = [chainer.Variable(self.ys[i]) for i in range(3)] log_p = functions.crf1d(cost, xs, ys) log_p.backward()
def f(cost, *args): xs = args[:len(args) // 2] ys = args[len(args) // 2:] return functions.crf1d(cost, xs, ys, reduce=self.reduce)
def check_invalid_option(self, cost_data, xs_data, ys_data): with self.assertRaises(ValueError): functions.crf1d(cost_data, xs_data, ys_data, 'invalid_option')
def f(cost, *args): xs = args[:len(args) // 2] ys = args[len(args) // 2:] return functions.crf1d(cost, xs, ys)