Exemplo n.º 1
0
    def forward(self, xs, ys, reduce='mean', transpose=False):
        """Computes negative log-likelihood of linear-chain CRF

        Args:
            xs (list of Variable): Input vector for each label
            ys (list of Variable): Expected output labels.
            transpose (bool): If ``True``, input/output sequences
            will be sorted in descending order of length.

        Returns:
            ~chainer.Variable: A variable holding the average negative
            log-likelihood of the input sequences.

        .. seealso:: See :func:`~chainer.frunctions.crf1d` for more detail.
        """

        if transpose:
            indices = argsort_list_descent(xs)
            xs = permutate_list(xs, indices, inv=False)
            ys = permutate_list(ys, indices, inv=False)
            trans_x = transpose_sequence.transpose_sequence(xs)
            trans_y = transpose_sequence.transpose_sequence(ys)
            loss = crf1d.crf1d(self.cost, trans_x, trans_y, reduce)

        else:
            loss = crf1d.crf1d(self.cost, xs, ys, reduce)

        return loss
Exemplo n.º 2
0
    def argmax(self, xs, transpose=False):
        """Computes a state that maximizes a joint probability.

        Args:
            xs (list of Variable): Input vector for each label.
            transpose (bool): If ``True``, input/output sequences
            will be sorted in descending order of length.

        Returns:
            tuple: A tuple of :class:`~chainer.Variable` representing each
            log-likelihood and a list representing the argmax path.

        .. seealso:: See :func:`~chainer.frunctions.crf1d_argmax` for more
           detail.

        """

        if transpose:
            indices = argsort_list_descent(xs)
            xs = permutate_list(xs, indices, inv=False)
            trans_x = transpose_sequence.transpose_sequence(xs)
            score, path = crf1d.argmax_crf1d(self.cost, trans_x)

            path = transpose_sequence.transpose_sequence(path)
            path = [p.array for p in path]
            path = permutate_list(path, indices, inv=True)

        else:
            score, path = crf1d.argmax_crf1d(self.cost, xs)

        return score, path
Exemplo n.º 3
0
    def argmax(self, xs, transpose=False):
        """Computes a state that maximizes a joint probability.

        Args:
            xs (list of Variable): Input vector for each label.
            transpose (bool): If ``True``, input/output sequences
            will be sorted in descending order of length.

        Returns:
            tuple: A tuple of :class:`~chainer.Variable` representing each
            log-likelihood and a list representing the argmax path.

        .. seealso:: See :func:`~chainer.frunctions.crf1d_argmax` for more
           detail.

        """

        if transpose:
            indices = argsort_list_descent(xs)
            xs = permutate_list(xs, indices, inv=False)
            trans_x = transpose_sequence.transpose_sequence(xs)
            score, path = crf1d.argmax_crf1d(self.cost, trans_x)

            path = transpose_sequence.transpose_sequence(path)
            path = [p.array for p in path]
            path = permutate_list(path, indices, inv=True)

        else:
            score, path = crf1d.argmax_crf1d(self.cost, xs)

        return score, path
Exemplo n.º 4
0
    def forward(self, xs, ys, reduce='mean', transpose=False):
        """Computes negative log-likelihood of linear-chain CRF

        Args:
            xs (list of Variable): Input vector for each label
            ys (list of Variable): Expected output labels.
            transpose (bool): If ``True``, input/output sequences
            will be sorted in descending order of length.

        Returns:
            ~chainer.Variable: A variable holding the average negative
            log-likelihood of the input sequences.

        .. seealso:: See :func:`~chainer.frunctions.crf1d` for more detail.
        """

        if transpose:
            indices = argsort_list_descent(xs)
            xs = permutate_list(xs, indices, inv=False)
            ys = permutate_list(ys, indices, inv=False)
            trans_x = transpose_sequence.transpose_sequence(xs)
            trans_y = transpose_sequence.transpose_sequence(ys)
            loss = crf1d.crf1d(self.cost, trans_x, trans_y, reduce)

        else:
            loss = crf1d.crf1d(self.cost, xs, ys, reduce)

        return loss
Exemplo n.º 5
0
 def _Transpose(X, indices=None, inv=False):
     if not inv:
         indices = argsort_list_descent(X)
         transpose = list(transpose_sequence.transpose_sequence(permutate_list(X, indices, inv=inv)))
         return indices, transpose
     else:
         transpose = permutate_list(transpose_sequence.transpose_sequence(X), indices, inv=inv)
         return transpose
    def __call__(self, hx, cx, xs, **kwargs):
        """__call__(self, hx, cx, xs)

        Calculate all hidden states and cell states.

        .. warning::

           ``train`` argument is not supported anymore since v2.
           Instead, use ``chainer.using_config('train', train)``.
           See :func:`chainer.using_config`.

        Args:
            hx (~chainer.Variable or None): Initial hidden states. If ``None``
                is specified zero-vector is used.
            cx (~chainer.Variable or None): Initial cell states. If ``None``
                is specified zero-vector is used.
            xs (list of ~chainer.Variable): List of input sequences.
                Each element ``xs[i]`` is a :class:`chainer.Variable` holding
                a sequence.
        """
        argument.check_unexpected_kwargs(
            kwargs,
            train='train argument is not supported anymore. '
            'Use chainer.using_config')
        argument.assert_kwargs_empty(kwargs)

        assert isinstance(xs, (list, tuple))
        xp = cuda.get_array_module(hx, *xs)
        indices = n_step_rnn.argsort_list_descent(xs)
        indices_array = xp.array(indices)

        xs = n_step_rnn.permutate_list(xs, indices, inv=False)
        if hx is None:
            hx = self.init_hx(xs)
        else:
            hx = permutate.permutate(hx, indices_array, axis=1, inv=False)

        if cx is None:
            cx = self.init_hx(xs)
        else:
            cx = permutate.permutate(cx, indices_array, axis=1, inv=False)

        trans_x = transpose_sequence.transpose_sequence(xs)

        ws = [[w.w0, w.w1, w.w2, w.w3, w.w4, w.w5, w.w6, w.w7] for w in self]
        bs = [[w.b0, w.b1, w.b2, w.b3, w.b4, w.b5, w.b6, w.b7] for w in self]

        hy, cy, trans_y = self.rnn(self.n_layers, self.dropout, hx, cx, ws, bs,
                                   trans_x)

        hy = permutate.permutate(hy, indices_array, axis=1, inv=True)
        cy = permutate.permutate(cy, indices_array, axis=1, inv=True)
        ys = transpose_sequence.transpose_sequence(trans_y)
        ys = n_step_rnn.permutate_list(ys, indices, inv=True)

        return hy, cy, ys
Exemplo n.º 7
0
    def __call__(self, hx, cx, xs, **kwargs):
        """__call__(self, hx, cx, xs)

        Calculate all hidden states and cell states.

        .. warning::

           ``train`` argument is not supported anymore since v2.
           Instead, use ``chainer.using_config('train', train)``.
           See :func:`chainer.using_config`.

        Args:
            hx (~chainer.Variable or None): Initial hidden states. If ``None``
                is specified zero-vector is used.
            cx (~chainer.Variable or None): Initial cell states. If ``None``
                is specified zero-vector is used.
            xs (list of ~chianer.Variable): List of input sequences.
                Each element ``xs[i]`` is a :class:`chainer.Variable` holding
                a sequence.
        """
        argument.check_unexpected_kwargs(
            kwargs, train='train argument is not supported anymore. '
            'Use chainer.using_config')
        argument.assert_kwargs_empty(kwargs)

        assert isinstance(xs, (list, tuple))
        indices = n_step_rnn.argsort_list_descent(xs)

        xs = n_step_rnn.permutate_list(xs, indices, inv=False)
        if hx is None:
            hx = self.init_hx(xs)
        else:
            hx = permutate.permutate(hx, indices, axis=1, inv=False)

        if cx is None:
            cx = self.init_hx(xs)
        else:
            cx = permutate.permutate(cx, indices, axis=1, inv=False)

        trans_x = transpose_sequence.transpose_sequence(xs)

        ws = [[w.w0, w.w1, w.w2, w.w3, w.w4, w.w5, w.w6, w.w7] for w in self]
        bs = [[w.b0, w.b1, w.b2, w.b3, w.b4, w.b5, w.b6, w.b7] for w in self]

        hy, cy, trans_y = self.rnn(
            self.n_layers, self.dropout, hx, cx, ws, bs, trans_x)

        hy = permutate.permutate(hy, indices, axis=1, inv=True)
        cy = permutate.permutate(cy, indices, axis=1, inv=True)
        ys = transpose_sequence.transpose_sequence(trans_y)
        ys = n_step_rnn.permutate_list(ys, indices, inv=True)

        return hy, cy, ys
Exemplo n.º 8
0
 def _accuracy(self, ys, ts):
     ys = permutate_list(ys, argsort_list_descent(ys), inv=False)
     ts = permutate_list(ts, argsort_list_descent(ts), inv=False)
     correct = 0
     total = 0
     exact = 0
     for _y, _t in zip(ys, ts):
         y = _y.data
         t = _t.data
         _correct = (y == cuda.to_gpu(t)).sum()
         _total = t.size
         if _correct == _total:
             exact += 1
         correct += _correct
         total += _total
     accuracy = correct / total
     self._eval = {
         'accuracy': accuracy,
         'correct': correct,
         'total': total,
         'exact': exact
     }
     return accuracy
Exemplo n.º 9
0
    def __call__(self, hx, cx, xs, train=True):
        """Calculate all hidden states and cell states.

        Args:
            hx (~chainer.Variable or None): Initial hidden states. If ``None``
                is specified zero-vector is used.
            cx (~chainer.Variable or None): Initial cell states. If ``None``
                is specified zero-vector is used.
            xs (list of ~chianer.Variable): List of input sequences.
                Each element ``xs[i]`` is a :class:`chainer.Variable` holding
                a sequence.
        """
        assert isinstance(xs, (list, tuple))
        indices = argsort_list_descent(xs)

        xs = permutate_list(xs, indices, inv=False)
        if hx is None:
            hx = self.init_hx(xs)
        else:
            hx = permutate.permutate(hx, indices, axis=1, inv=False)

        if cx is None:
            cx = self.init_hx(xs)
        else:
            cx = permutate.permutate(cx, indices, axis=1, inv=False)

        trans_x = transpose_sequence.transpose_sequence(xs)

        ws = [[w.w0, w.w1, w.w2, w.w3, w.w4, w.w5, w.w6, w.w7] for w in self]
        bs = [[w.b0, w.b1, w.b2, w.b3, w.b4, w.b5, w.b6, w.b7] for w in self]

        hy, cy, trans_y = self.rnn(self.n_layers,
                                   self.dropout,
                                   hx,
                                   cx,
                                   ws,
                                   bs,
                                   trans_x,
                                   train=train,
                                   use_cudnn=self.use_cudnn)

        hy = permutate.permutate(hy, indices, axis=1, inv=True)
        cy = permutate.permutate(cy, indices, axis=1, inv=True)
        ys = transpose_sequence.transpose_sequence(trans_y)
        ys = permutate_list(ys, indices, inv=True)

        return hy, cy, ys
Exemplo n.º 10
0
    def predict_crf(self, hs, ls=None, calculate_loss=True):
        indices = argsort_list_descent(hs)
        trans_hs = F.transpose_sequence(permutate_list(hs, indices, inv=False))
        score, trans_ys = self.crf.argmax(trans_hs)
        ys = permutate_list(F.transpose_sequence(trans_ys), indices, inv=True)
        ps = [y.data for y in ys]

        xp = cuda.get_array_module(hs[0])
        if calculate_loss:
            tmp = permutate_list(ls, indices, inv=False)
            trans_ls = F.transpose_sequence(
                permutate_list(ls, indices, inv=False))  # sort by length
            loss = self.crf(trans_hs, trans_ls)
        else:
            loss = chainer.Variable(xp.array(0, dtype='f'))

        return loss, ps
Exemplo n.º 11
0
    def __call__(self, xs):
        """

        Args:
            xs (list or tuple): batch-length list of Variable, each with shape
                (

        Returns:

        """
        assert isinstance(xs, (list, tuple))
        indices = argsort_list_descent(xs)

        xs = permutate_list(xs, indices, inv=False)
        h = self.rnn.init_h(xs)
        c = self.rnn.init_c(xs)
        # list of Variable, each with shape (batch, features)
        trans_x = transpose_sequence.transpose_sequence(xs)

        z_lst = []
        pz_lst = []
        for x in trans_x:
            if len(x) < len(h):
                # a sequence ended
                h = h[:len(x)]
                c = [c_i[:len(x)] for c_i in c]
            pz, z, h, c = self._forward_single(x, h, c)
            z_lst.append(z)
            pz_lst.append(pz)

        # h_lst and c_lst basically have same order as x
        z_lst = transpose_sequence.transpose_sequence(z_lst)
        z_lst = permutate_list(z_lst, indices, inv=True)
        z_lst = [F.squeeze(z, 1).data for z in z_lst]

        # h_lst and c_lst basically have same order as x
        pz_lst = transpose_sequence.transpose_sequence(pz_lst)
        pz_lst = permutate_list(pz_lst, indices, inv=True)
        pz_lst = [F.squeeze(pz, 1) for pz in pz_lst]

        return pz_lst, z_lst
Exemplo n.º 12
0
    def __call__(self, xs):
        """

        Args:
            xs (list or tuple): batch-length list of Variable, each with shape
                (

        Returns:

        """
        assert isinstance(xs, (list, tuple))
        indices = argsort_list_descent(xs)

        xs = permutate_list(xs, indices, inv=False)
        h = self.init_h(xs)
        c = self.init_c(xs)
        # list of Variable, each with shape (batch, features)
        trans_x = transpose_sequence.transpose_sequence(xs)

        c_lst = []
        h_lst = []
        for x in trans_x:
            if len(x) < len(h):
                # a sequence ended
                h = h[:len(x)]
                c = [c_i[:len(x)] for c_i in c]
            h, c = self.forward_single(x, h, c)
            c_lst.append(F.hstack(c))
            h_lst.append(h)

        # h_lst and c_lst basically have same order as x
        h_lst = transpose_sequence.transpose_sequence(h_lst)
        h_lst = permutate_list(h_lst, indices, inv=True)

        c_lst = transpose_sequence.transpose_sequence(c_lst)
        c_lst = permutate_list(c_lst, indices, inv=True)

        return h_lst, c_lst
Exemplo n.º 13
0
    def __call__(self, h0, hx, xs, **kwargs):
        """__call__(self, hx, xs)

        Calculate all hidden states and cell states.

        .. warning::

           ``train`` argument is not supported anymore since v2.
           Instead, use ``chainer.using_config('train', train)``.
           See :func:`chainer.using_config`.

        Args:
            hx (~chainer.Variable or None): Initial hidden states. If ``None``
                is specified zero-vector is used.
            xs (list of ~chianer.Variable): List of input sequences.
                Each element ``xs[i]`` is a :class:`chainer.Variable` holding
                a sequence.

        """
        argument.check_unexpected_kwargs(
            kwargs,
            train='train argument is not supported anymore. '
            'Use chainer.using_config')
        argument.assert_kwargs_empty(kwargs)

        assert isinstance(
            xs, (list, tuple)), "xs in not a list or tupple: %r" % type(xs)
        indices = argsort_list_descent(xs)

        xs = permutate_list(xs, indices, inv=False)

        if h0 is None:
            h0 = self.init_hx(xs)
        else:
            h0 = permutate.permutate(h0, indices, axis=1, inv=False)

        hx = permutate_list(hx, indices, inv=False)

        trans_x = transpose_sequence.transpose_sequence(xs)

        ws = [[w.w0, w.w1, w.w2, w.w3, w.w4, w.w5] for w in self]
        bs = [[w.b0, w.b1, w.b2, w.b3, w.b4, w.b5] for w in self]
        for w in self:
            w1 = w.w6
            w2 = w.w7
            w3 = w.w8
            b1 = w.b6
            b2 = w.b7
            b3 = w.b8
        W = [w1, w2, w3]
        B = [b1, b2, b3]

        h_list, h_bar_list, c_s_list, z_s_list = self.rnn(
            self.n_layers, self.dropout, h0, hx, ws, bs, trans_x, W, B)
        '''
        print(type(h_list),len(h_list))
        print(type(h_list[0]),len(h_list[0]))
        print(type(h_list[0][0]),len(h_list[0][0]))
        '''

        #batch内で入れ替え
        h_list = transpose_sequence.transpose_sequence(h_list)
        h_list = permutate_list(h_list, indices, inv=True)
        '''
        print(type(h_list),len(h_list))
        print(type(h_list[0]),len(h_list[0]))
        print(type(h_list[0][0]),len(h_list[0][0]))
        '''
        h_bar_list = transpose_sequence.transpose_sequence(h_bar_list)
        h_bar_list = permutate_list(h_bar_list, indices, inv=True)
        '''
        print(type(c_s_list),len(c_s_list))
        print(type(c_s_list[0]),len(c_s_list[0]))
        print(type(c_s_list[0][0]),len(c_s_list[0][0]), c_s_list[0][0])
        
        print(type(z_s_list), len(z_s_list))
        print(type(z_s_list[0]), len(z_s_list[0]), z_s_list[0])
        '''
        c_s_list = transpose_sequence.transpose_sequence(c_s_list)
        c_s_list = permutate_list(c_s_list, indices, inv=True)
        z_s_list = transpose_sequence.transpose_sequence(z_s_list)
        z_s_list = permutate_list(z_s_list, indices, inv=True)

        return h_list, h_bar_list, c_s_list, z_s_list