コード例 #1
0
ファイル: test_vstack.py プロジェクト: asi1024/chainer
 def check_value_check(self):
     if self.valid:
         # Check if it throws nothing
         functions.vstack(self.xs)
     else:
         with pytest.raises(type_check.InvalidType):
             functions.vstack(self.xs)
コード例 #2
0
ファイル: encoders.py プロジェクト: BriansIDP/Espnet_comm
    def __call__(self, xs, ilens):
        """BRNN forward propagation.

        Args:
            xs (chainer.Variable): Batch of padded charactor ids. (B, Tmax)
            ilens (chainer.Variable): Batch of length of each input batch. (B,)

        Returns:
            tuple(chainer.Variable): Tuple of `chainer.Variable` objects.
            chainer.Variable: `ilens` .

        """
        logging.info(self.__class__.__name__ + " input lengths: " + str(ilens))
        # need to move ilens to cpu
        ilens = cuda.to_cpu(ilens)

        if "lstm" in self.typ:
            _, _, ys = self.nbrnn(None, None, xs)
        else:
            _, ys = self.nbrnn(None, xs)
        ys = self.l_last(F.vstack(ys))  # (sum _utt frame_utt) x dim
        xs = F.split_axis(ys, np.cumsum(ilens[:-1]), axis=0)

        # final tanh operation
        xs = F.split_axis(F.tanh(F.vstack(xs)), np.cumsum(ilens[:-1]), axis=0)

        # 1 utterance case, it becomes an array, so need to make a utt tuple
        if not isinstance(xs, tuple):
            xs = [xs]

        return xs, ilens  # x: utt list of frame x dim
コード例 #3
0
    def __call__(self, xs, ilens):
        '''BLSTMP forward

        :param xs:
        :param ilens:
        :return:
        '''
        logging.info(self.__class__.__name__ + ' input lengths: ' + str(ilens))

        for layer in six.moves.range(self.elayers):
            hy, cy, ys = self['bilstm' + str(layer)](None, None, xs)
            # ys: utt list of frame x cdim x 2 (2: means bidirectional)
            # TODO(watanabe) replace subsample and FC layer with CNN
            ys, ilens = _subsamplex(ys, self.subsample[layer + 1])
            # (sum _utt frame_utt) x dim
            ys = self['bt' + str(layer)](F.vstack(ys))
            xs = F.split_axis(ys, _ilens_to_index(ilens), axis=0)
            del hy, cy

        # final tanh operation
        xs = F.split_axis(F.tanh(F.vstack(xs)), _ilens_to_index(ilens), axis=0)

        # 1 utterance case, it becomes an array, so need to make a utt tuple
        if not isinstance(xs, tuple):
            xs = [xs]

        return xs, ilens  # x: utt list of frame x dim
コード例 #4
0
ファイル: encoders.py プロジェクト: BriansIDP/Espnet_comm
    def __call__(self, xs, ilens):
        """RNNP forward.

        Args:
            xs (chainer.Variable): Batch of padded charactor ids. (B, Tmax)
            ilens (chainer.Variable): Batch of length of each input batch. (B,)

        Returns:
            xs (chainer.Variable):subsampled vector of xs.
            chainer.Variable: Subsampled vector of ilens.

        """
        logging.info(self.__class__.__name__ + " input lengths: " + str(ilens))

        for layer in six.moves.range(self.elayers):
            if "lstm" in self.typ:
                _, _, ys = self[self.rnn_label + str(layer)](None, None, xs)
            else:
                _, ys = self[self.rnn_label + str(layer)](None, xs)
            # ys: utt list of frame x cdim x 2 (2: means bidirectional)
            # TODO(watanabe) replace subsample and FC layer with CNN
            ys, ilens = _subsamplex(ys, self.subsample[layer + 1])
            # (sum _utt frame_utt) x dim
            ys = self["bt" + str(layer)](F.vstack(ys))
            xs = F.split_axis(ys, np.cumsum(ilens[:-1]), axis=0)

        # final tanh operation
        xs = F.split_axis(F.tanh(F.vstack(xs)), np.cumsum(ilens[:-1]), axis=0)

        # 1 utterance case, it becomes an array, so need to make a utt tuple
        if not isinstance(xs, tuple):
            xs = [xs]

        return xs, ilens  # x: utt list of frame x dim
コード例 #5
0
ファイル: encoders.py プロジェクト: zw76859420/espnet
    def __call__(self, xs, ilens):
        """BRNN forward

        :param xs:
        :param ilens:
        :return:
        """
        logging.info(self.__class__.__name__ + ' input lengths: ' + str(ilens))
        # need to move ilens to cpu
        ilens = cuda.to_cpu(ilens)

        if "lstm" in self.typ:
            _, _, ys = self.nbrnn(None, None, xs)
        else:
            _, ys = self.nbrnn(None, xs)
        ys = self.l_last(F.vstack(ys))  # (sum _utt frame_utt) x dim
        xs = F.split_axis(ys, np.cumsum(ilens[:-1]), axis=0)

        # final tanh operation
        xs = F.split_axis(F.tanh(F.vstack(xs)), np.cumsum(ilens[:-1]), axis=0)

        # 1 utterance case, it becomes an array, so need to make a utt tuple
        if not isinstance(xs, tuple):
            xs = [xs]

        return xs, ilens  # x: utt list of frame x dim
コード例 #6
0
    def hierarchical_encode(self, ids, xs, xs_embed, position_info, x_spans,
                            shell_spans, x_position_info):

        _, _, ys_l = self.Bilstm(None, None, xs_embed)

        if self.lstm_ac:
            ac_reps = self.get_span_reps(x_spans, ys_l, split_axis=True)
            _, _, ys_l_ac = self.AcBilstm(None, None, ac_reps)

            ac_reps = chaFunc.vstack(ys_l_ac)
        else:
            ac_reps = self.get_span_reps(x_spans, ys_l)

        if self.lstm_shell:
            shell_reps = self.get_span_reps(shell_spans, ys_l, split_axis=True)
            _, _, ys_l_shell = self.ShellBilstm(None, None, shell_reps)
            shell_reps = chaFunc.vstack(ys_l_shell)
        else:
            shell_reps = self.get_span_reps(shell_spans, ys_l)

        ac_shell_reps = chaFunc.concat([ac_reps, shell_reps], axis=-1)

        span_reps_bow = self.get_bow_reps(ids, xs, xs_embed, position_info,
                                          x_spans, shell_spans,
                                          x_position_info)

        ac_shell_reps = chaFunc.concat(
            [ac_shell_reps, position_info, span_reps_bow], -1)

        assert ac_shell_reps.shape[-1] == self.ac_shell_rep_size_in

        return ac_shell_reps
コード例 #7
0
    def entropy_filter(self, x, b, ent_T):
        xp = cuda.get_array_module(b)
        #print "Entropy input pre_data : %s " % b.data
        #a = F.softmax(b)
        #print "a[0] : %s" % (a[0].data)
        #print 'Entropy input : %s' % (a.data)
        eb = entropy(F.softmax(b)) / np.log(b.shape[1])
        print "Entropy result : %s " % (eb.data)
        eb.to_cpu()
        if hasattr(eb.data, 'get'):
            with cuda.get_device(eb.data):
                exited = eb.data < ent_T
            exited = exited.get()
        else:
            exited = eb.data < ent_T
        print "exited : %s" % exited
        y_exit = []
        y_cont = []
        for i, idx in enumerate(exited):
            if idx:
                y_exit.append(b[i:i + 1])
            else:
                y_cont.append(x[i:i + 1])

        if len(y_exit) > 0:
            y_exit = F.vstack(y_exit)
        if len(y_cont) > 0:
            y_cont = F.vstack(y_cont)
        #print "y_exit = %s \n y_cont = %s" % (y_exit.data,y_cont.data.shape)
        return y_exit, y_cont, exited
コード例 #8
0
ファイル: encoders.py プロジェクト: zw76859420/espnet
    def __call__(self, xs, ilens):
        """RNNP forward

        :param xs:
        :param ilens:
        :return:
        """
        logging.info(self.__class__.__name__ + ' input lengths: ' + str(ilens))

        for layer in six.moves.range(self.elayers):
            if "lstm" in self.typ:
                _, _, ys = self[self.rnn_label + str(layer)](None, None, xs)
            else:
                _, ys = self[self.rnn_label + str(layer)](None, xs)
            # ys: utt list of frame x cdim x 2 (2: means bidirectional)
            # TODO(watanabe) replace subsample and FC layer with CNN
            ys, ilens = _subsamplex(ys, self.subsample[layer + 1])
            # (sum _utt frame_utt) x dim
            ys = self['bt' + str(layer)](F.vstack(ys))
            xs = F.split_axis(ys, np.cumsum(ilens[:-1]), axis=0)

        # final tanh operation
        xs = F.split_axis(F.tanh(F.vstack(xs)), np.cumsum(ilens[:-1]), axis=0)

        # 1 utterance case, it becomes an array, so need to make a utt tuple
        if not isinstance(xs, tuple):
            xs = [xs]

        return xs, ilens  # x: utt list of frame x dim
コード例 #9
0
    def _asign_gt_to_anchor(self, anchors, locs, confs, gt_bboxes, gt_labels):
        _anchors, _locs, _confs = [], [], []
        _gt_labels, _gt_bboxes = [], []
        for anchor, loc, conf, gt_bbox, gt_label in zip(
                anchors, locs, confs, gt_bboxes, gt_labels):
            if gt_label.shape[0] > 0:
                iou = bbox_iou(anchor, gt_bbox)
                max_iou = self.xp.max(iou, axis=-1)
                max_iou_indices = self.xp.argmax(iou, axis=-1)
            else:  # guard no annotation
                max_iou = self.xp.zeros(conf.shape[0], self.xp.float32)
                max_iou_indices = self.xp.empty(conf.shape[0], self.xp.float32)

            fg_mask = max_iou > self._fg_thresh
            bg_mask = max_iou < self._bg_thresh
            n_bg = self.xp.where(bg_mask)[0].shape[0]
            max_iou_indices_fg = max_iou_indices[fg_mask]

            _gt_label_fg = self.xp.array(
                [gt_label[i] + 1 for i in max_iou_indices_fg], self.xp.int32)

            _gt_bbox_fg = self.xp.array(
                [gt_bbox[i] for i in max_iou_indices_fg], self.xp.float32)
            if _gt_bbox_fg.shape[0] == 0:  # guard not fg anchor
                _gt_bbox_fg = self.xp.empty((0, 4), self.xp.float32)

            _anchors.append(F.vstack((anchor[fg_mask], anchor[bg_mask])))
            _locs.append(F.vstack((loc[fg_mask], loc[bg_mask])))
            _confs.append(F.vstack((conf[fg_mask], conf[bg_mask])))
            _gt_bboxes.append(
                self.xp.vstack((_gt_bbox_fg, self.xp.zeros((n_bg, 4)))))
            _gt_labels.append(
                self.xp.hstack((_gt_label_fg, self.xp.zeros(n_bg))))

        return _anchors, _locs, _confs, _gt_bboxes, _gt_labels
コード例 #10
0
 def check_value_check(self):
     if self.valid:
         # Check if it throws nothing
         functions.vstack(self.xs)
     else:
         with self.assertRaises(type_check.InvalidType):
             functions.vstack(self.xs)
コード例 #11
0
ファイル: feature.py プロジェクト: chantera/coordparser
    def _forward_spans(hs_flatten,
                       pairs,
                       ckeys,
                       lengths,
                       use_block=True,
                       block_size=128):
        xp = chainer.cuda.get_array_module(hs_flatten)
        begins, ends = pairs.T

        if use_block:

            def _uniq(start, end):
                idxs = defaultdict(lambda: len(idxs))
                offset = np.array([idxs[(s, e)] for s, e in zip(start, end)])
                start, end = np.array(
                    [k for k, v in sorted(idxs.items(), key=lambda x: x[1])]).T
                return start, end, offset

            def _sum(start, end):
                size = len(start)
                lb, ub = min(start), max(end)
                hs = hs_flatten[lb:ub]
                mask = xp.zeros((size, ub - lb, 1), dtype=xp.float32)
                for i, (s, e) in enumerate(zip(start, end)):
                    mask[i, s - lb:e - lb] = 1.0
                return F.sum(hs * mask, axis=1)

            def _extract(start, end):
                spans = []
                start, end, offset = _uniq(start, end)
                ofs, lb, ub = 0, 0, 0
                for k in range(len(start)):
                    lb, ub = min(lb, start[k]), max(ub, end[k])
                    if ub - lb > block_size and k > 0:
                        spans.append(_sum(start[ofs:k], end[ofs:k]))
                        ofs, lb, ub = k, start[k], end[k]
                spans.append(_sum(start[ofs:], end[ofs:]))
                spans = F.vstack(spans) / xp.asarray(end - start)[:, None]
                return F.embed_id(xp.asarray(offset), spans)

            left_spans = _extract(begins, ckeys)
            right_spans = _extract(ckeys + 1, ends + 1)
        else:

            @functools.lru_cache(maxsize=None)
            def _get_span_v(i, j):
                return F.average(hs_flatten[i:j + 1], axis=0)

            left_spans = F.vstack([
                _get_span_v(begin, ckey_pre)
                for begin, ckey_pre in zip(begins, ckeys - 1)
            ])
            right_spans = F.vstack([
                _get_span_v(ckey_post, end)
                for ckey_post, end in zip(ckeys + 1, ends)
            ])

        return left_spans, right_spans
コード例 #12
0
 def calcLoss(G, X, S):
     GXr = G*xp.real(X).astype(np.float32)
     GXi = G*xp.imag(X).astype(np.float32)
     Sr = xp.real(S).astype(np.float32)
     Si = xp.imag(S).astype(np.float32)
     gxL = [F.expand_dims(iDGTcf.iDGT(GXr[ii],GXi[ii],windowDG,shiftLenG,fftLenG), axis=0) for ii in range(len(G))]
     sL = [F.expand_dims(iDGTcf.iDGT(Sr[ii],Si[ii],windowDG,shiftLenG,fftLenG), axis=0) for ii in range(len(G))]
     gx = F.vstack(gxL)
     s = F.vstack(sL)
     loss = F.mean_absolute_error(gx,s)
     return loss
コード例 #13
0
ファイル: test_inout.py プロジェクト: msakai/onnx-chainer
 def forward(self, *xs, **kwxs):
     if kwxs:
         h = F.vstack(list(kwxs.values()))
     elif len(xs) > 1:
         h = F.vstack(xs)
     else:
         h = xs[0]
     h2 = self.l1(h)
     h3 = F.relu(h2)
     h4 = self.l2(h3)
     return F.relu(h4)
コード例 #14
0
    def merge_representation(self, index, section, xs, ys):
        """
        Merge and average the context representation to prepare the input
        for next layer. If the prediction is 'O', its corresponding row of
        context representation of xs will be used as the input for next  
        layer, otherwise its corresponding row of ys will be seleted as the
        input for next layer.
        
        + index: merge index for predicts         
        + xs: context representation, input of bi_word_tag BiLSTM layer
        + ys: context representation, output of bi_word_tag BiLSTM layer
        e.g. predicts: B-Gene, I-Gene, O,B-protein,B-DNA
          index array:       
          [ 1, -1, -1, -1
            1, -1, -1, -1
           -1,  0, -1, -1
           -1, -1,  1, -1
           -1, -1, -1,  1 ]

          ys_index clip array:
          [ 1,  0,  0,  0
            1,  0,  0,  0
            0,  1,  0,  0
            0,  0,  1,  0
            0,  0,  0,  1 ]

          xs index(1-|index|) array: 
          [ 0,  0,  0,  0
            0,  0,  0,  0
            0,  1,  0,  0
            0,  0,  0,  0
            0,  0,  0,  0 ]
        """
        ys_index = index.copy()
        ys_index = F.clip(ys_index.astype('f'), 0., 1.0)
        ys = F.matmul(ys_index, F.vstack(ys), transa=True)
        xs_index = index.copy()
        xs_index = 1 - Fmat.absolute(xs_index.astype('f'))
        xs = F.matmul(xs_index, F.vstack(xs), transa=True)

        # Sum word vectors
        ys = Fmat.add(xs, ys)

        # Average word vectors for entity representation
        sum_index = F.sum(ys_index, axis=0)
        sum_index = F.clip(sum_index, 1.0, 1000000.0)
        sum_index = F.tile(sum_index, (ys.shape[1], 1))
        sum_index = F.transpose(sum_index)
        ys = Fmat.div(ys, sum_index)
        ys = F.split_axis(ys, section, axis=0)

        return ys
コード例 #15
0
ファイル: Model.py プロジェクト: kurtespinosa/chainer-rnn-ner
    def __call__(self, xs, hx, cx, xxs, train=True):
        forward_char_embeds = [[self.char_embed(item) for item in items]
                               for items in xxs]
        backward_char_embeds = [[item[::-1] for item in items]
                                for items in forward_char_embeds]

        # Encode character sequences
        forward_encodings = []
        backward_encodings = []
        for forward, backward in zip(forward_char_embeds,
                                     backward_char_embeds):
            hhx = chainer.Variable(
                self.xp.zeros((1, len(forward), 25), dtype=self.xp.float32))
            ccx = chainer.Variable(
                self.xp.zeros((1, len(forward), 25), dtype=self.xp.float32))
            _, __, forward_char_encs = self.forward_char(hhx, ccx, forward)
            _, __, backward_char_encs = self.backward_char(hhx, ccx, backward)
            forward_encodings.append([x[-1] for x in forward_char_encs])
            backward_encodings.append([x[-1] for x in backward_char_encs])

        forward_encodings = [F.vstack(x) for x in forward_encodings]
        backward_encodings = [F.vstack(x) for x in backward_encodings]

        # Encode word embeddings
        xs = [self.embed(item) for item in xs]
        xs_forward = [
            F.concat([x, y, z], axis=1)
            for x, y, z in zip(xs, forward_encodings, backward_encodings)
        ]
        xs_backward = [x[::-1] for x in xs_forward]

        if self.dropout and train:
            xs_forward = [F.dropout(item) for item in xs_forward]
            xs_backward = [F.dropout(item) for item in xs_backward]
        forward_hy, forward_cy, forward_ys = self.forward_l1(hx,
                                                             cx,
                                                             xs_forward,
                                                             train=train)
        backward_hy, backward_cy, backward_ys = self.backward_l1(hx,
                                                                 cx,
                                                                 xs_backward,
                                                                 train=train)
        ys = [
            F.concat([forward, backward[::-1]], axis=1)
            for forward, backward in zip(forward_ys, backward_ys)
        ]
        y = [self.l2(item) for item in ys]
        return y
コード例 #16
0
    def translate(self, hxs, max_length):
        """Generate target sentences given hidden states of source sentences.

        Args:
            hxs: Hidden states for source sequences.

        Returns:
            ys: Generated sequences.

        """
        batch_size, _, _ = hxs.shape
        compute_context = self.attention(hxs)
        c = Variable(self.xp.zeros((batch_size, self.n_units), 'f'))
        h = F.broadcast_to(self.bos_state, ((batch_size, self.n_units)))
        # first character's embedding
        previous_embedding = self.embed_y(
            Variable(self.xp.full((batch_size, ), EOS, 'i')))

        results = []
        for _ in range(max_length):
            context = compute_context(h)
            concatenated = F.concat((previous_embedding, context))

            c, h = self.lstm(c, h, concatenated)
            concatenated = F.concat((concatenated, h))

            logit = self.w(self.maxout(concatenated))
            y = F.reshape(F.argmax(logit, axis=1), (batch_size, ))

            results.append(y)
            previous_embedding = self.embed_y(y)

        results = F.separate(F.transpose(F.vstack(results)), axis=0)
        ys = [get_subsequence_before_eos(result.data) for result in results]
        return ys
コード例 #17
0
    def forward(self, xs, ilens):
        '''BLSTM forward

        :param xs:
        :param ilens:
        :return:
        '''
        logging.info(self.__class__.__name__ + ' input lengths: ' + str(ilens))
        # need to move ilens to cpu
        ilens = cuda.to_cpu(ilens)
        hy, cy, ys = self.nblstm(None, None, xs)
        ys = self.l_last(F.vstack(ys))  # (sum _utt frame_utt) x dim

        # (satos) xs = F.split_axis(ys, np.cumsum(ilens[:-1]), axis=0)
        # (satos) del hy, cy

        # final tanh operation
        # (satos) xs = F.split_axis(F.tanh(F.vstack(xs)), np.cumsum(ilens[:-1]), axis=0)
        # ニュアンス的にtanhがほぼidの挙動しかしないぽいな

        xs = F.split_axis(F.tanh(ys), np.cumsum(ilens[:-1]), axis=0)  # (satos)

        # 1 utterance case, it becomes an array, so need to make a utt tuple

        # print(isinstance(xs,tuple),xs)
        # (satos) たってき無視する(実際tupleなので大丈夫そう)
        # if not isinstance(xs, tuple):
        #    xs = [xs]

        # print(xs[2])
        return xs, ilens  # x: utt list of frame x dim
コード例 #18
0
    def merge_representation(self, index, section, ys):
        """
        Merge and average the context representation to prepare the input
        for next layer. If the prediction is 'O', its corresponding row of
        context representation of xs will be used as the input for next  
        layer, otherwise its corresponding row of ys will be seleted as the
        input for next layer.
        
        + index: merge index for predicts
        + ys: context representation, output of bi_word_tag BiLSTM layer
        e.g. predicts: B-Gene, I-Gene, O,B-protein,B-DNA
          index array:
          [ 1,  0,  0,  0
            1,  0,  0,  0
            0,  1,  0,  0
            0,  0,  1,  0
            0,  0,  0,  1 ]

        """
        ys = F.matmul(index.astype('f'), F.vstack(ys), transa=True)

        # Average word vectors for entity representation
        sum_index = F.sum(index.astype('f'), axis=0)
        sum_index = F.tile(sum_index, (ys.shape[1], 1))
        sum_index = F.transpose(sum_index)
        ys = Fmat.div(ys, sum_index)
        ys = F.split_axis(ys, section, axis=0)

        return ys
コード例 #19
0
ファイル: cuesn.py プロジェクト: imai-laboratory/chainerESN
def fit(model, data_x, data_y, init_len):
    '''
    data_x: input with the shape of (samples, channel)
    data_y: teacher signal with the shape of (samples, channel)
    '''
    train_len = len(data_x)
    X = xp.zeros(
        (1 + model.n_inputs + model.n_reservoir, train_len - init_len),
        dtype=xp.float32)
    Yt = data_y[init_len:train_len + 1]

    # forward computation
    for i in range(train_len):
        u = data_x[i]
        u = F.reshape(u, (-1, 1))
        r = model.update(u)
        if i >= init_len:
            # collect data after initialization
            '''
            s = F.vstack(
                (xp.array([[0]], dtype=xp.float32), u, r
                 ))[:, 0].data
            '''
            X[:, i - init_len] = F.vstack(
                (xp.array([[0]], dtype=xp.float32), u, r))[:, 0].data
    # linear regression: X and Yt
    model.Wo = xp.dot(Yt[:, 0], scipy.linalg.pinv(X))
コード例 #20
0
 def __call__(self, xs, ts):
     hy, cy, ys = self.lstm(None, None, xs)
     ys = F.relu(self.ll1(F.vstack(ys)))
     del hy, cy
     ys = self.ll2(ys)
     loss = F.mean_squared_error(ys, ts)
     return loss
コード例 #21
0
def crop_and_resize(bottom_data, bottom_rois, outh, outw, spatial_scale):
    if isinstance(bottom_rois, chainer.Variable):
        bottom_rois = bottom_rois.data
    bottom_rois = cuda.to_cpu(bottom_rois)

    B, C, H, W = bottom_data.shape
    N = bottom_rois.shape[0]

    ys = collections.defaultdict(list)
    for i_roi in six.moves.range(N):
        i_batch, x1, y1, x2, y2 = bottom_rois[i_roi].tolist()
        i_batch = int(i_batch)
        x1 = int(spatial_scale * x1)
        x2 = max(int(spatial_scale * x2), x1 + 1)
        y1 = int(spatial_scale * y1)
        y2 = max(int(spatial_scale * y2), y1 + 1)
        x_roi = bottom_data[i_batch][:, y1:y2, x1:x2]
        x_roi = x_roi[None, :, :, :]  # N, C, H, W
        y = functions.resize_images(x_roi, (outh, outw))
        ys[i_batch].append(y)

    yss = []
    for i_batch in six.moves.range(B):
        ys = functions.vstack(ys[i_batch])
        yss.append(ys)
    yss = functions.concat(yss, axis=0)
    return yss
コード例 #22
0
def compute_shifts(cell, pbc, cutoff):
    xp = cell.xp
    reciprocal_cell = F.batch_inv(cell)
    inv_distances = F.max(F.sqrt(F.sum(reciprocal_cell**2, axis=1)), axis=0)
    num_repeats = F.ceil(cutoff * inv_distances)
    num_repeats = F.where(pbc, num_repeats, xp.zeros_like(num_repeats.data))
    num_repeats = F.max(num_repeats, axis=0)
    r1 = xp.arange(1, num_repeats.data[0] + 1)
    r2 = xp.arange(1, num_repeats.data[1] + 1)
    r3 = xp.arange(1, num_repeats.data[2] + 1)
    o = xp.zeros(1, dtype=r1.dtype)
    return F.vstack([
        xp.array([[0.0, 0.0, 0.0]]),
        cartesian_prod(r1, r2, r3),
        cartesian_prod(r1, r2, o),
        cartesian_prod(r1, r2, -r3),
        cartesian_prod(r1, o, r3),
        cartesian_prod(r1, o, o),
        cartesian_prod(r1, o, -r3),
        cartesian_prod(r1, -r2, r3),
        cartesian_prod(r1, -r2, o),
        cartesian_prod(r1, -r2, -r3),
        cartesian_prod(o, r2, r3),
        cartesian_prod(o, r2, o),
        cartesian_prod(o, r2, -r3),
        cartesian_prod(o, o, r3),
    ]).data
コード例 #23
0
    def translate(self, hxs, max_length=100):
        batch_size, _, _ = hxs.shape
        compute_context = self.attention(hxs)
        c = Variable(self.xp.zeros((batch_size, self.n_units), 'f'))
        h = Variable(self.xp.zeros((batch_size, self.n_units), 'f'))

        ys = self.xp.full(batch_size, tokens['<SOS>'], np.int32)

        results = []
        for _ in range(max_length):
            eys = self.embed_y(ys)

            context = compute_context(h)
            concatenated = F.concat([eys, context])

            c, h = self.lstm(c, h, concatenated)
            concatenated = F.concat([concatenated, h])

            logit = self.w(self.maxout(concatenated))
            y = F.reshape(F.argmax(logit, axis=1), (batch_size, ))

            results.append(y)

        results = F.separate(F.transpose(F.vstack(results)), axis=0)

        outs = []
        for y in results:
            inds = np.argwhere(y == tokens['<EOS>'])
            if len(inds) > 0:
                y = y[:inds[0, 0]]
            outs.append(y)

        return outs
コード例 #24
0
    def translate(
            self,
            encoded: Variable,
            max_length: int = 100
    ) -> List[ndarray]:
        sentence_count = encoded.shape[0]

        self.setup(encoded)
        cell, state, previous_words = self.get_initial_states(sentence_count)

        result = []
        for _ in range(max_length):
            cell, state, context, concatenated = \
                self.advance_one_step(cell, state, previous_words)
            logit, state = self.compute_logit(concatenated, state, context)

            output_id = F.reshape(F.argmax(logit, axis=1), (sentence_count,))
            result.append(output_id)

            previous_words = output_id

        # Remove words after <EOS>
        outputs = F.separate(F.transpose(F.vstack(result)), axis=0)
        assert len(outputs) == sentence_count
        output_sentences = []
        for output in outputs:
            assert output.shape == (max_length,)
            indexes = np.argwhere(output.data == EOS)
            if len(indexes) > 0:
                output = output[:indexes[0, 0] + 1]
            output_sentences.append(output.data)

        return output_sentences
コード例 #25
0
ファイル: tagging.py プロジェクト: chantera/svm2017
 def __call__(self, chars):
     """
     Note: we use zero-padding instead of spacial PADDING token.
     """
     if isinstance(chars, (tuple, list)):
         return F.vstack([self.forward_one(_chars) for _chars in chars])
     return self.forward_one(chars)
コード例 #26
0
ファイル: models.py プロジェクト: chantera/mtl-parser
    def parse(self, hs):
        self.hs = self.parser_blstm(hs)
        self.pads = F.tanh(self.pad_linear(self.pad_embed(self.xp.arange(4))))
        states = [(sent_idx, transition.State(h.shape[0]))
                  for sent_idx, h in enumerate(hs)]
        _states = states[:]
        while len(_states) > 0:
            features = []
            fs = []
            for sent_idx, state in _states:
                feature = self.xp.array([self.extract_feature(state)])
                features.append(feature)
                fs.append(self._populate_features(feature, sent_idx))
            fs = F.vstack(fs)
            fs = F.stack(F.split_axis(fs, fs.shape[0] // 4, axis=0), axis=0)
            fs = F.reshape(fs, (fs.shape[0], -1))

            action_scores = self.parser_mlp(fs)
            action_scores.to_cpu()
            action_scores = action_scores.data
            for i in range(len(_states) - 1, -1, -1):
                _, state = _states[i]
                best_action, best_score = -1, -np.inf
                for action, score in enumerate(action_scores[i]):
                    if score > best_score and \
                            self.transition_system.is_allowed(action, state):
                        best_action, best_score = action, score
                self.transition_system.apply(best_action, state)
                if self.transition_system.is_terminal(state):
                    del _states[i]

        heads, labels, _states = zip(*[(state.heads, state.labels, state)
                                       for i, state in states])
        return heads, labels, _states
コード例 #27
0
ファイル: lstm_parser_bi.py プロジェクト: hiroshinoji/depccg
    def forward(self, ws, ss, ps, dep_ts=None):
        batchsize = len(ws)
        xp = chainer.cuda.get_array_module(ws[0])
        split = scanl(lambda x, y: x + y, 0, [w.shape[0] for w in ws])[1:-1]

        wss = self.emb_word(F.hstack(ws))
        sss = F.reshape(self.emb_suf(F.vstack(ss)), (-1, 4 * self.afix_dim))
        pss = F.reshape(self.emb_prf(F.vstack(ps)), (-1, 4 * self.afix_dim))
        ins = F.dropout(F.concat([wss, sss, pss]),
                        self.dropout_ratio,
                        train=self.train)

        xs_f = list(F.split_axis(ins, split, 0))
        xs_b = [x[::-1] for x in xs_f]
        cx_f, hx_f, cx_b, hx_b = self._init_state(xp, batchsize)
        _, _, hs_f = self.lstm_f(hx_f, cx_f, xs_f, train=self.train)
        _, _, hs_b = self.lstm_b(hx_b, cx_b, xs_b, train=self.train)
        hs_b = [x[::-1] for x in hs_b]
        # ys: [(sentence length, number of category)]
        hs = [F.concat([h_f, h_b]) for h_f, h_b in zip(hs_f, hs_b)]

        dep_ys = [
            self.biaffine_arc(
                F.elu(F.dropout(self.arc_dep(h), 0.32, train=self.train)),
                F.elu(F.dropout(self.arc_head(h), 0.32, train=self.train)))
            for h in hs
        ]

        # if dep_ts is not None and random.random >= 0.5:
        if dep_ts is not None:
            heads = dep_ts
        else:
            heads = [F.argmax(y, axis=1) for y in dep_ys]

        heads = F.elu(F.dropout(
            self.rel_head(
                F.vstack([F.embed_id(t, h, ignore_label=IGNORE) \
                        for h, t in zip(hs, heads)])),
            0.32, train=self.train))

        childs = F.elu(
            F.dropout(self.rel_dep(F.vstack(hs)), 0.32, train=self.train))
        cat_ys = self.biaffine_tag(childs, heads)

        cat_ys = list(F.split_axis(cat_ys, split, 0))

        return cat_ys, dep_ys
コード例 #28
0
    def predict_with_mask(self, x, ent_Ts=None, test=True):
        num = x.shape[0]
        bs = []
        exit_i = 0
        for i, link in enumerate(self.links):
            if isinstance(link, Sequential):
                b = link(x, test=test)
                b = b[0]
                y_exit, y_cont, exited = self.entropy_filter(
                    x, b, ent_Ts[min(exit_i,
                                     len(ent_Ts) - 1)])
                exit_i = exit_i + 1
                b = y_exit
                bs.append((b, exited))
                x = y_cont
                if len(x) == 0:
                    break
            elif isinstance(link, function.dropout):
                x = link(x, train=not test)
            elif isinstance(link, chainer.links.BatchNormalization):
                x = link(x, test=test)
            elif hasattr(link, '__call__') and 'train' in inspect.getargspec(
                    link.__call__)[0]:
                x = link(x, train=not test)
            elif hasattr(link, '__call__') and 'test' in inspect.getargspec(
                    link.__call__)[0]:
                x = link(x, test=test)
            else:
                x = link(x)
        #if len(x) > 0:
        #    bs.append((x,[True]*x.shape[0]))
        ys = [None] * num
        exited = [False] * num
        # branch exit
        for b, ex in bs:
            i = 0
            j = 0
            for exit in ex:
                while ys[i] is not None:
                    i = i + 1
                if exit:
                    ys[i] = b[j]
                    exited[i] = True  #only count the branch exited
                    j = j + 1
                i = i + 1
        # main exit
        if len(x) > 0:
            b, ex = (x, [True] * x.shape[0])
            i = 0
            j = 0
            for exit in ex:
                while ys[i] is not None:
                    i = i + 1
                if exit:
                    ys[i] = b[j]
                    j = j + 1
                i = i + 1

        return F.vstack(ys), exited
コード例 #29
0
 def forward(self, x):
     hy, cy, x = self.h(None, None, x)
     x = np.array([_x.data for _x in x])
     x = F.transpose(x, axes=(1, 0, 2))
     x = F.vstack([np.array([_x.data[-1] for _x in x])])
     #x = chainer.Variable(np.array([_h[-1].data for _h in h]))
     x = self.out(x)
     return x
コード例 #30
0
 def __call__(self, h):  #h:batch_size*time*channel
     batch_size = h.shape[0]
     sequence_length = h.shape[1]
     h1 = self.lstm(h[:, 0, :]).reshape(1, batch_size, -1)
     for i in range(1, sequence_length):
         h1 = F.vstack((h1, self.lstm(h[:,
                                        i, :]).reshape(1, batch_size, -1)))
     return h1
コード例 #31
0
    def __call__(self, h):  #h:batch_size*time*channel
        batch_size = h.shape[0]
        sequence_length = h.shape[1]

        h1 = self.lstm_forward(h[:, 0, :]).reshape(1, batch_size, -1)
        for i in range(1, sequence_length):
            h1 = F.vstack(
                (h1, self.lstm_forward(h[:, i, :]).reshape(1, batch_size, -1)))

        h2 = self.lstm_backward(h[:, sequence_length - 1, :]).reshape(
            1, batch_size, -1)
        for i in range(sequence_length - 2, -1, -1):
            h2 = F.vstack(
                (self.lstm_backward(h[:, i, :]).reshape(1, batch_size,
                                                        -1), h2))

        return h1 + h2
コード例 #32
0
ファイル: test_vstack.py プロジェクト: asi1024/chainer
 def forward(self, inputs, device):
     x = list(inputs)
     y = functions.vstack(x)
     return y,
コード例 #33
0
ファイル: test_vstack.py プロジェクト: KotaroSetoyama/chainer
    def check_forward(self, xs_data):
        xs = [chainer.Variable(x) for x in xs_data]
        y = functions.vstack(xs)

        expect = numpy.vstack(self.xs)
        testing.assert_allclose(y.data, expect)
コード例 #34
0
ファイル: test_vstack.py プロジェクト: KotaroSetoyama/chainer
 def func(*xs):
     return functions.vstack(xs)
コード例 #35
0
ファイル: test_vstack.py プロジェクト: Fhrozen/chainer
 def func(*xs):
     y = functions.vstack(xs)
     return y * y