How to use the chainer.functions.concat function in chainer

To help you get started, we’ve selected a few chainer examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Hi-king / chainer_progressive_gan / chainer_progressive_gan / models / progressive_generator.py View on Github external
stage = self.max_stage
        stage = min(stage, self.max_stage)
        alpha = stage - math.floor(stage)
        stage = math.floor(stage)

        z_shape = (len(z), self.n_hidden, 1, 1)
        if (skip_hs is not None) and (skip_hs[-1].shape == z_shape):
            h = skip_hs.pop(-1)
        else:
            h = chainer.functions.reshape(z, z_shape)
        h = chainer.functions.leaky_relu(feature_vector_normalization(self.c0(h)))
        h = chainer.functions.leaky_relu(feature_vector_normalization(self.c1(h)))

        for i in range(1, int(stage // 2 + 1)):
            if skip_hs is not None:  # conditional
                h = chainer.functions.concat([h, skip_hs[- i]], axis=1)
            h = self.bs[i](h)

        if int(stage) % 2 == 0:
            out = self.outs[int(stage // 2)]
            # print(h.shape)
            x = out(h)
        else:
            out_prev = self.outs[stage // 2]
            out_curr = self.outs[stage // 2 + 1]
            b_curr = self.bs[stage // 2 + 1]

            x_0 = out_prev(chainer.functions.unpooling_2d(h, 2, 2, 0, outsize=(2 * h.shape[2], 2 * h.shape[3])))
            if skip_hs is not None:  # conditional
                skip_hs_original = skip_hs[- int(stage // 2 + 1)]
                h = chainer.functions.concat([h, skip_hs_original], axis=1)
            h = b_curr(h)
github ace12358 / WordSegmentation / src / train_feedforward_simple.py View on Github external
def forward_one(x, target, label):
    # make input window vector
    distance = window // 2
    char_vecs = list()
    x = list(x)
    for i in range(distance):
        x.append('')
        x.insert(0,'<s>')
    for i in range(-distance, distance + 1):
        char = x[target + i]
        char_id = char2id[char]
        char_vec = model.embed(get_onehot(char_id))
        char_vecs.append(char_vec)

    concat = F.concat(tuple(char_vecs))
    hidden = model.hidden1(F.sigmoid(concat))
    pred = model.output(hidden)
    correct = get_onehot(label)
    return np.argmax(pred), F.softmax_cross_entropy(pred, correct)
</s>
github soskek / captioning_chainer / nets.py View on Github external
def calculate_loss(self, xs, ys, others):
        h, c = self.prepare(xs)
        input_ys = [y[:-1] for y in ys]
        target_ys = [y[1:] for y in ys]
        es = sequence_embed(self.embed, input_ys, self.dropout)
        h, c, hs = self.rnn(h, c, es)
        concat_h = F.dropout(F.concat(hs, axis=0), self.dropout)
        concat_output = self.output(concat_h)
        concat_target = F.concat(target_ys, axis=0)
        loss = F.softmax_cross_entropy(concat_output, concat_target)
        accuracy = F.accuracy(concat_output, concat_target)
        reporter.report({'loss': loss.data}, self)
        reporter.report({'perp': self.xp.exp(loss.data)}, self)
        reporter.report({'acc': accuracy.data}, self)
        return loss
github hirofumi0810 / neural_sp / src / models / chainer / attention / attention_seq2seq.py View on Github external
if not eos_flag[b]:
                        if y.data[b] == eos:
                            eos_flag[b] = True
                        else:
                            y_lens[b] += 1
                        # NOTE: exclude 

            # Break if  is outputed in all mini-batch
            if sum(y.data == eos)[0] == len(y):
                break

        # Concatenate in T_out dimension
        best_hyps = F.concat(best_hyps, axis=1)
        # aw = F.concat(aw, axis=-1).reshape(
        #     batch_size, -1, max_time, getattr(self, 'num_heads_' + str(task)))
        aw = F.concat(aw, axis=-1).reshape(batch_size, -1, max_time)

        # Convert to numpy
        best_hyps = self.var2np(best_hyps)
        aw = self.var2np(aw)

        # Reverse the order
        if dir == 'bwd':
            for b in range(batch_size):
                best_hyps[b, :y_lens[b]] = best_hyps[b, :y_lens[b]][::-1]

        return best_hyps, aw
github chainer / models / elmo-chainer / bilm / elmo_lstm.py View on Github external
zeros = self.xp.zeros((num_layers,
                                   batch_size - num_valid,
                                   returned_timesteps,
                                   encoder_dim), 'f')
            zeros = chainer.Variable(zeros)
            stacked_sequence_output = F.concat(
                [stacked_sequence_output, zeros], 1)

            # The states also need to have invalid rows added back.
            new_states = []
            for state in final_states:
                state_dim = state.shape[-1]
                zeros = self.xp.zeros(
                    (num_layers, batch_size - num_valid, state_dim), 'f')
                zeros = chainer.Variable(zeros)
                new_states.append(F.concat([state, zeros], 1))
            final_states = new_states

        # It's possible to need to pass sequences which are padded to longer than the
        # max length of the sequence to a Seq2StackEncoder. However, packing and unpacking
        # the sequences mean that the returned tensor won't include these dimensions, because
        # the RNN did not need to process them. We add them back on in the form of zeros here.
        sequence_length_difference = total_sequence_length - returned_timesteps
        if sequence_length_difference > 0:
            zeros = self.xp.zeros((num_layers,
                                   batch_size,
                                   sequence_length_difference,
                                   stacked_sequence_output[0].shape[-1]), 'f')
            zeros = chainer.Variable(zeros)
            stacked_sequence_output = F.concat(
                [stacked_sequence_output, zeros], 2)
github chainer / chainer-chemistry / chainer_chemistry / models / ggnn_gwm.py View on Github external
# ebmbed super node
        h_s = self.embed_super(super_node)

        g_list = []
        for step in range(self.n_layers):
            message_layer_index = 0 if self.weight_tying else step
            h2 = self.update_layers[message_layer_index](h, adj)

            h, h_s = self.gwm(h, h2, h_s, message_layer_index)
            if self.concat_hidden:
                g = self.readout_layers[step](h, h0, is_real_node)
                g_list.append(g)

        if self.concat_hidden:
            return functions.concat(g_list, axis=1)
        else:
            g = self.readout_layers[0](h, h0, is_real_node)
            g2 = functions.concat( (g, h_s), axis=1 )
            out_g = functions.relu(self.linear_for_concat_super(g2))
            return out_g
github amasky / ram / infer.py View on Github external
draw.rectangle(xy=list(xy), outline=color)
    del draw
    plt.imshow(canvas)
    plt.axis('off')

    # glimpse at each scale
    gs = crop(x, center=ls[t:t+1], size=g_size)
    plt.subplot(3+n_scales, n_steps, n_steps + t+1)
    plt.imshow(gs.data[0,0], cmap='gray')
    plt.axis('off')

    for k in range(1, n_scales):
        s = np.power(2,k)
        patch = crop(x, center=ls[t:t+1], size=g_size*s)
        patch = F.average_pooling_2d(patch, ksize=s)
        gs = F.concat((gs, patch), axis=1)
        plt.subplot(3+n_scales, n_steps, n_steps*(k+1) + t+1)
        plt.imshow(gs.data[0,k], cmap='gray')
        plt.axis('off')

    # output probability
    plt.subplot2grid((3+n_scales,n_steps), (1+n_scales,t), rowspan=2)
    plt.barh(np.arange(10), ys[t], align='center')
    plt.xlim(0, 1)
    plt.ylim(-0.5, 9.5)

    if t == 0:
        plt.yticks(np.arange(10))
    else:
        plt.yticks(np.arange(10), ['' for _ in range(10)])
    plt.xticks([])
github knorth55 / chainer-fcis / fcis / models / fcis_resnet101.py View on Github external
rois2 = loc2bbox(rois, roi_locs)
            H, W = img_size
            rois2[:, 0::2] = self.xp.clip(rois2[:, 0::2], 0, H)
            rois2[:, 1::2] = self.xp.clip(rois2[:, 1::2], 0, W)

            # PSROI pooling and regression
            indices_and_rois2 = self.xp.concatenate(
                (roi_indices[:, None], rois2), axis=1)
            roi_seg_scores2, roi_cls_locs2, roi_cls_scores2 = self._pool(
                indices_and_rois2, h_cls_seg, h_locs,
                gt_roi_labels=gt_roi_labels)

            # concat 1st and 2nd iteration results
            rois = self.xp.concatenate((rois, rois2))
            roi_indices = self.xp.concatenate((roi_indices, roi_indices))
            roi_cls_scores = F.concat(
                (roi_cls_scores, roi_cls_scores2), axis=0)
            roi_cls_locs = F.concat(
                (roi_cls_locs, roi_cls_locs2), axis=0)
            roi_seg_scores = F.concat(
                (roi_seg_scores, roi_seg_scores2), axis=0)
        return rois, roi_indices, roi_seg_scores, roi_cls_locs, roi_cls_scores
github hirofumi0810 / neural_sp / src / models / chainer / attention / attention_seq2seq.py View on Github external
else:
                raise ValueError(self.decoding_order)

            # Generate
            logits_step = getattr(self, 'fc_' + str(task) + '_' + dir)(F.tanh(
                getattr(self, 'W_d_' + str(task) + '_' + dir)(dec_out) +
                getattr(self, 'W_c_' + str(task) + '_' + dir)(context_vec)))

            logits.append(logits_step)
            aw.append(aw_step)

        # Concatenate in T_out-dimension
        logits = F.concat(logits, axis=1)
        # aw = F.concat(aw, axis=-1).reshape(
        #     batch_size, -1, max_time, getattr(self, 'num_heads_' + str(task)))
        aw = F.concat(aw, axis=-1).reshape(batch_size, -1, max_time)

        return logits, aw