python类using_config()的实例源码

vgg.py 文件源码 项目:chainer-visualization 作者: hvy 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def feature_map_activations(self, x):

        """Forward pass through the convolutional layers of the VGG returning
        all of its intermediate feature map activations."""

        hs = []
        pre_pooling_sizes = []

        h = x
        for conv_block, mp in zip(self.conv_blocks, self.mps):
            for conv in conv_block:
                h = F.relu(conv(h))

            pre_pooling_sizes.append(h.data.shape[2:])

            # Disable cuDNN, else pooling indices will not be stored
            with chainer.using_config('use_cudnn', 'never'):
                h = mp.apply((h,))[0]
            hs.append(h)

        return hs, pre_pooling_sizes
pgt.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def act(self, state):

        with chainer.using_config('train', False):
            s = self.batch_states([state], self.xp, self.phi)
            if self.act_deterministically:
                action = self.policy(s).most_probable
            else:
                action = self.policy(s).sample()
            # Q is not needed here, but log it just for information
            q = self.q_function(s, action)

        # Update stats
        self.average_q *= self.average_q_decay
        self.average_q += (1 - self.average_q_decay) * float(q.data)

        self.logger.debug('t:%s a:%s q:%s',
                          self.t, action.data[0], q.data)
        return cuda.to_cpu(action.data[0])
double_dqn.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def _compute_target_values(self, exp_batch, gamma):

        batch_next_state = exp_batch['next_state']

        with chainer.using_config('train', False):
            with state_kept(self.q_function):
                next_qout = self.q_function(batch_next_state)

        target_next_qout = self.target_q_function(batch_next_state)

        next_q_max = target_next_qout.evaluate_actions(
            next_qout.greedy_actions)

        batch_rewards = exp_batch['reward']
        batch_terminal = exp_batch['is_state_terminal']

        return batch_rewards + self.gamma * (1.0 - batch_terminal) * next_q_max
debug.py 文件源码 项目:chainer-qrnn 作者: musyoku 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_rnn():
    np.random.seed(0)
    num_layers = 50
    seq_length = num_layers * 2
    batchsize = 2
    vocab_size = 4
    data = np.random.randint(0, vocab_size, size=(batchsize, seq_length), dtype=np.int32)
    source, target = make_source_target_pair(data)
    model = RNNModel(vocab_size, ndim_embedding=100, num_layers=num_layers, ndim_h=3, kernel_size=3, pooling="fo", zoneout=False, wgain=1, densely_connected=True)

    with chainer.using_config("train", False):
        np.random.seed(0)
        model.reset_state()
        Y = model(source).data

        model.reset_state()
        np.random.seed(0)
        for t in range(source.shape[1]):
            y = model.forward_one_step(source[:, :t+1]).data
            target = np.swapaxes(np.reshape(Y, (batchsize, -1, vocab_size)), 1, 2)
            target = np.reshape(np.swapaxes(target[:, :, t, None], 1, 2), (batchsize, -1))
            assert np.sum((y - target) ** 2) == 0
            print("t = {} OK".format(t))
seq2seq.py 文件源码 项目:convolutional_seq2seq 作者: soskek 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def __call__(self, trainer):
        print('## Calculate BLEU')
        with chainer.no_backprop_mode():
            with chainer.using_config('train', False):
                references = []
                hypotheses = []
                for i in range(0, len(self.test_data), self.batch):
                    sources, targets = zip(*self.test_data[i:i + self.batch])
                    references.extend([[t.tolist()] for t in targets])

                    sources = [
                        chainer.dataset.to_device(self.device, x) for x in sources]
                    ys = [y.tolist()
                          for y in self.model.translate(sources, self.max_length)]
                    hypotheses.extend(ys)

        bleu = bleu_score.corpus_bleu(
            references, hypotheses,
            smoothing_function=bleu_score.SmoothingFunction().method1) * 100
        print('BLEU:', bleu)
        reporter.report({self.key: bleu})
CNN.py 文件源码 项目:vsmlib 作者: undertherain 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def __call__(self, xs):

        if self.freeze:
            self.embed.disable_update()
        xs = self.embed(xs)
        batchsize, height, width = xs.shape
        xs = F.reshape(xs, (batchsize, 1, height, width))
        conv3_xs = self.conv3(xs)
        conv4_xs = self.conv4(xs)
        conv5_xs = self.conv5(xs)
        h1 = F.max_pooling_2d(F.relu(conv3_xs), conv3_xs.shape[2])
        h2 = F.max_pooling_2d(F.relu(conv4_xs), conv4_xs.shape[2])
        h3 = F.max_pooling_2d(F.relu(conv5_xs), conv5_xs.shape[2])
        concat_layer = F.concat([h1, h2, h3], axis=1)
        with chainer.using_config('train', True):
            y = self.l1(F.dropout(F.tanh(concat_layer)))
        return y
visualize.py 文件源码 项目:adversarial-autoencoder 作者: musyoku 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def plot_scatter():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", "-m", type=str, default="model.hdf5")
    args = parser.parse_args()

    dataset_train, dataset_test = chainer.datasets.get_mnist()
    images_train, labels_train = dataset_train._datasets
    images_test, labels_test = dataset_test._datasets

    model = Model()
    assert model.load(args.model)

    # normalize
    images_train = (images_train - 0.5) * 2
    images_test = (images_test - 0.5) * 2

    with chainer.no_backprop_mode() and chainer.using_config("train", False):
        z = model.encode_x_yz(images_test)[1].data
    plot.scatter_labeled_z(z, labels_test, "scatter_gen.png")
visualize.py 文件源码 项目:adversarial-autoencoder 作者: musyoku 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def plot_representation():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", "-m", type=str, default="model.hdf5")
    args = parser.parse_args()

    dataset_train, dataset_test = chainer.datasets.get_mnist()
    images_train, labels_train = dataset_train._datasets
    images_test, labels_test = dataset_test._datasets

    model = Model()
    assert model.load(args.model)

    # normalize
    images_train = (images_train - 0.5) * 2
    images_test = (images_test - 0.5) * 2

    with chainer.no_backprop_mode() and chainer.using_config("train", False):
        y_onehot, z = model.encode_x_yz(images_test, apply_softmax_y=True)
        representation = model.encode_yz_representation(y_onehot, z).data
    plot.scatter_labeled_z(representation, labels_test, "scatter_r.png")
visualize.py 文件源码 项目:adversarial-autoencoder 作者: musyoku 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def plot_z():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", "-m", type=str, default="model.hdf5")
    args = parser.parse_args()

    dataset_train, dataset_test = chainer.datasets.get_mnist()
    images_train, labels_train = dataset_train._datasets
    images_test, labels_test = dataset_test._datasets

    model = Model()
    assert model.load(args.model)

    # normalize
    images_train = (images_train - 0.5) * 2
    images_test = (images_test - 0.5) * 2

    with chainer.no_backprop_mode() and chainer.using_config("train", False):
        z = model.encode_x_yz(images_test)[1].data
    plot.scatter_labeled_z(z, labels_test, "scatter_z.png")
visualize.py 文件源码 项目:adversarial-autoencoder 作者: musyoku 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def plot_scatter():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", "-m", type=str, default="model.hdf5")
    args = parser.parse_args()

    dataset_train, dataset_test = chainer.datasets.get_mnist()
    images_train, labels_train = dataset_train._datasets
    images_test, labels_test = dataset_test._datasets

    model = Model()
    assert model.load(args.model)

    # normalize
    images_train = (images_train - 0.5) * 2
    images_test = (images_test - 0.5) * 2

    with chainer.no_backprop_mode() and chainer.using_config("train", False):
        z = model.encode_x_z(images_test).data
    plot.scatter_labeled_z(z, labels_test, "scatter_z.png")
visualize.py 文件源码 项目:adversarial-autoencoder 作者: musyoku 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def plot_scatter():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", "-m", type=str, default="model.hdf5")
    args = parser.parse_args()

    dataset_train, dataset_test = chainer.datasets.get_mnist()
    images_train, labels_train = dataset_train._datasets
    images_test, labels_test = dataset_test._datasets

    model = Model()
    assert model.load(args.model)

    # normalize
    images_train = (images_train - 0.5) * 2
    images_test = (images_test - 0.5) * 2

    with chainer.no_backprop_mode() and chainer.using_config("train", False):
        z = model.encode_x_yz(images_test)[1].data
    plot.scatter_labeled_z(z, labels_test, "scatter_gen.png")
visualize.py 文件源码 项目:adversarial-autoencoder 作者: musyoku 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def plot_representation():
    parser = argparse.ArgumentParser()
    parser.add_argument("--model", "-m", type=str, default="model.hdf5")
    args = parser.parse_args()

    dataset_train, dataset_test = chainer.datasets.get_mnist()
    images_train, labels_train = dataset_train._datasets
    images_test, labels_test = dataset_test._datasets

    model = Model()
    assert model.load(args.model)

    # normalize
    images_train = (images_train - 0.5) * 2
    images_test = (images_test - 0.5) * 2

    with chainer.no_backprop_mode() and chainer.using_config("train", False):
        y_onehot, z = model.encode_x_yz(images_test, apply_softmax_y=True)
        representation = model.encode_yz_representation(y_onehot, z).data
    plot.scatter_labeled_z(representation, labels_test, "scatter_r.png")
test_conv_2d_bn_activ.py 文件源码 项目:chainercv 作者: chainer 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def check_forward(self, x_data):
        x = chainer.Variable(x_data)
        # Make the batch normalization to be the identity function.
        self.l.bn.avg_var[:] = 1
        self.l.bn.avg_mean[:] = 0
        with chainer.using_config('train', False):
            y = self.l(x)

        self.assertIsInstance(y, chainer.Variable)
        self.assertIsInstance(y.array, self.l.xp.ndarray)

        if self.activ == 'relu':
            np.testing.assert_almost_equal(
                cuda.to_cpu(y.array), np.maximum(cuda.to_cpu(x_data), 0),
                decimal=4
            )
        elif self.activ == 'add_one':
            np.testing.assert_almost_equal(
                cuda.to_cpu(y.array), cuda.to_cpu(x_data) + 1,
                decimal=4
            )
visualize.py 文件源码 项目:chainermn 作者: chainer 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def out_generated_image(gen, dis, rows, cols, seed, dst):
    @chainer.training.make_extension()
    def make_image(trainer):
        np.random.seed(seed)
        n_images = rows * cols
        xp = gen.xp
        z = Variable(xp.asarray(gen.make_hidden(n_images)))
        with chainer.using_config('train', False):
            x = gen(z)
        x = chainer.cuda.to_cpu(x.data)
        np.random.seed()

        x = np.asarray(np.clip(x * 255, 0.0, 255.0), dtype=np.uint8)
        _, _, H, W = x.shape
        x = x.reshape((rows, cols, 3, H, W))
        x = x.transpose(0, 3, 1, 4, 2)
        x = x.reshape((rows * H, cols * W, 3))

        preview_dir = '{}/preview'.format(dst)
        preview_path = preview_dir +\
            '/image{:0>8}.png'.format(trainer.updater.iteration)
        if not os.path.exists(preview_dir):
            os.makedirs(preview_dir)
        Image.fromarray(x).save(preview_path)
    return make_image
encoder_decoder.py 文件源码 项目:workspace 作者: nojima 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def translate(self, sentence: np.ndarray, max_length: int = 30) -> List[int]:
        with chainer.no_backprop_mode(), chainer.using_config('train', False):
            sentence = sentence[::-1]

            embedded_xs = self._embed_input(sentence)
            hidden_states, cell_states, attentions = self._encoder(None, None, [embedded_xs])

            wid = EOS
            result = []

            for i in range(max_length):
                output, hidden_states, cell_states = \
                    self._translate_one_word(wid, hidden_states, cell_states, attentions)

                wid = np.argmax(output.data)
                if wid == EOS:
                    break
                result.append(wid)

            return result
train.py 文件源码 项目:chainer-ADDA 作者: pfnet-research 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def test_pretrained_on_target(source_cnn, target, args):
    print(":: testing pretrained source CNN on target domain")

    if args.device >= 0:
        source_cnn.to_gpu()

    with chainer.using_config('train', False):
        _, target_test_iterator = data2iterator(target, args.batchsize, multiprocess=False)

        mean_accuracy = 0.0
        n_batches = 0

        for batch in target_test_iterator:
            batch, labels = chainer.dataset.concat_examples(batch, device=args.device)
            encode = source_cnn.encoder(batch)
            classify = source_cnn.classifier(encode)
            acc = accuracy.accuracy(classify, labels)
            mean_accuracy += acc.data
            n_batches += 1
        mean_accuracy /= n_batches

        print(":: classifier trained on only source, evaluated on target: accuracy {}%".format(mean_accuracy))
sklearn_wrapper.py 文件源码 项目:chainer_sklearn 作者: corochann 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def _forward(self, *args, calc_score=False):
        """Forward computation without backward.

        Predicts by the model's output by returning `predictor`'s output
        """
        with chainer.using_config('train', False), chainer.no_backprop_mode():
            if calc_score:
                self(*args)
                return self.y
            else:
                if self.predictor is None:
                    print("[ERROR] predictor is not set or not build yet.")
                    return
                # TODO: it passes all the args, sometimes (x, y) which is too many arguments.
                # Consider how to deal with the number of input
                if hasattr(self.predictor, '_forward'):
                    fn = self.predictor._forward
                else:
                    fn = self.predictor
                return fn(*filter_args(fn, args))
test.py 文件源码 项目:chainer-glu 作者: musyoku 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def test_rnn():
    np.random.seed(0)
    num_blocks = 10
    num_layers_per_block = 5
    seq_length = num_layers_per_block * num_blocks * 2
    batchsize = 2
    vocab_size = 4
    data = np.random.randint(0, vocab_size, size=(batchsize, seq_length), dtype=np.int32)
    source, target = make_source_target_pair(data)
    model = RNNModel(vocab_size, ndim_embedding=3, num_blocks=num_blocks, num_layers_per_block=num_layers_per_block, ndim_h=3, kernel_size=3, wgain=1)

    with chainer.using_config("train", False):
        np.random.seed(0)
        model.reset_state()
        Y = model(source).data

        model.reset_state()
        np.random.seed(0)
        for t in xrange(source.shape[1]):
            y = model.forward_one_step(source[:, :t+1]).data
            target = np.swapaxes(np.reshape(Y, (batchsize, -1, vocab_size)), 1, 2)
            target = np.reshape(np.swapaxes(target[:, :, t, None], 1, 2), (batchsize, -1))
            assert np.sum((y - target) ** 2) == 0
            print("t = {} OK".format(t))
train.py 文件源码 项目:chainer-image-caption 作者: dsanno 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def forward(net, image_batch, sentence_batch, train=True):
    images = xp.asarray(image_batch)
    n, sentence_length = sentence_batch.shape
    net.initialize(images)
    loss = 0
    acc = 0
    size = 0
    for i in range(sentence_length - 1):
        target = xp.where(xp.asarray(sentence_batch[:, i]) != eos, 1, 0).astype(np.float32)
        if (target == 0).all():
            break
        with chainer.using_config('train', train):
            with chainer.using_config('enable_backprop', train):
                x = xp.asarray(sentence_batch[:, i])
                t = xp.asarray(sentence_batch[:, i + 1])
                y = net(x)
                y_max_index = xp.argmax(y.data, axis=1)
                mask = target.reshape((len(target), 1)).repeat(y.data.shape[1], axis=1)
                y = y * mask
                loss += F.softmax_cross_entropy(y, t)
                acc += xp.sum((y_max_index == t) * target)
                size += xp.sum(target)
    return loss / size, float(acc) / size, float(size)
yolov2_train_class_caltech.py 文件源码 项目:chainer-object-detection 作者: dsanno 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def evaluate(model, dataset, crop_margin, test_size):
    xp = model.xp
    iterator = chainer.iterators.SerialIterator(dataset, 1, repeat=False, shuffle=False)
    acc_sum = 0
    iteration = 0
    for batch in iterator:
        image_batch = []
        label_batch = []
        for image_path, category_id, _ in batch:
            image = load_image(image_path)
            image_width, image_height = image.size
            crop_size = min(image_width, image_height) - crop_margin
            crop_rect = ((image_width - crop_size) // 2, (image_height - crop_size) // 2, crop_size, crop_size)
#            input_size = test_size
            input_size = int(round(crop_size / 32.0) * 32)
            if input_size < 64:
                input_size = 64
            elif input_size > test_size:
                input_size = test_size
            image_batch.append(transform_image(image, crop_rect, input_size))
            label_batch.append(category_id)

        x = xp.asarray(image_batch)
        t = xp.asarray(label_batch)

        with chainer.using_config('enable_backprop', False):
            with chainer.using_config('train', False):
                y = model(x)
        acc = F.accuracy(y, t)
        acc_sum += float(acc.data)
    return acc_sum / len(dataset)
yolov2_train_class.py 文件源码 项目:chainer-object-detection 作者: dsanno 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def evaluate(model, dataset, crop_margin, test_size, batch_size):
    xp = model.xp
    iterator = chainer.iterators.SerialIterator(dataset, batch_size, repeat=False, shuffle=False)
    acc_sum = 0
    iteration = 0
    for batch in iterator:
        image_batch = []
        label_batch = []
        for image_path, category_id, _ in batch:
            image = load_image(image_path)
            image_width, image_height = image.size
            crop_size = min(image_width, image_height) - crop_margin
            crop_rect = ((image_width - crop_size) // 2, (image_height - crop_size) // 2, crop_size, crop_size)
            input_size = test_size
            image_batch.append(transform_image(image, crop_rect, input_size))
            label_batch.append(category_id)

        x = xp.asarray(image_batch)
        t = xp.asarray(label_batch)

        with chainer.using_config('enable_backprop', False):
            with chainer.using_config('train', False):
                y = model(x)
        acc = F.accuracy(y, t)
        acc_sum += float(acc.data) * batch_size
    return acc_sum / len(dataset)
ddpg.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def disable_train(chain):
    call_orig = chain.__call__

    def call_test(self, x):
        with chainer.using_config('train', False):
            return call_orig(self, x)

    chain.__call__ = call_test
ddpg.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def act(self, state):

        with chainer.using_config('train', False):
            s = self.batch_states([state], self.xp, self.phi)
            action = self.policy(s).sample()
            # Q is not needed here, but log it just for information
            q = self.q_function(s, action)

        # Update stats
        self.average_q *= self.average_q_decay
        self.average_q += (1 - self.average_q_decay) * float(q.data)

        self.logger.debug('t:%s a:%s q:%s',
                          self.t, action.data[0], q.data)
        return cuda.to_cpu(action.data[0])
ppo.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def _act(self, state):
        xp = self.xp
        with chainer.using_config('train', False):
            b_state = batch_states([state], xp, self.phi)
            with chainer.no_backprop_mode():
                action_distrib, v = self.model(b_state)
                action = action_distrib.sample()
            return cuda.to_cpu(action.data)[0], cuda.to_cpu(v.data)[0]
dqn.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def compute_q_values(self, states):
        """Compute Q-values

        Args:
          states (list of cupy.ndarray or numpy.ndarray)
        Returns:
          list of numpy.ndarray
        """
        with chainer.using_config('train', False):
            if not states:
                return []
            batch_x = self.batch_states(states, self.xp, self.phi)
            q_values = list(cuda.to_cpu(
                self.model(batch_x).q_values))
            return q_values
dqn.py 文件源码 项目:chainerrl 作者: chainer 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def act(self, state):
        with chainer.using_config('train', False):
            with chainer.no_backprop_mode():
                action_value = self.model(
                    self.batch_states([state], self.xp, self.phi))
                q = float(action_value.max.data)
                action = cuda.to_cpu(action_value.greedy_actions.data)[0]

        # Update stats
        self.average_q *= self.average_q_decay
        self.average_q += (1 - self.average_q_decay) * q

        self.logger.debug('t:%s q:%s action_value:%s', self.t, q, action_value)
        return action
Parallel_BiGRU.py 文件源码 项目:NANHM-for-GEC 作者: shinochin 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def CalculateValLoss(self, xs, ys):
        with chainer.no_backprop_mode(), chainer.using_config('train', False):
            loss = self.CalcLoss(xs, ys)
        return loss.data
GRU.py 文件源码 项目:NANHM-for-GEC 作者: shinochin 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def translate(self, xs, max_length=100):
        batch = len(xs)
        with chainer.no_backprop_mode(), chainer.using_config('train', False):
            xs = [x[::-1] for x in xs]
            exs = sequence_embed(self.embed_x, xs)
            h, _ = self.encoder(None, exs)
            ys = self.xp.full(batch, EOS, 'i')
            result = []
            for i in range(max_length):
                eys = self.embed_y(ys)
                eys = chainer.functions.split_axis(eys, batch, 0)
                h, ys = self.decoder(h, eys)
                cys = chainer.functions.concat(ys, axis=0)
                wy = self.W(cys)
                ys = self.xp.argmax(wy.data, axis=1).astype('i')
                result.append(ys)

        result = cuda.to_cpu(self.xp.stack(result).T)

        # Remove EOS taggs
        outs = []
        for y in result:
            inds = np.argwhere(y == EOS)
            if len(inds) > 0:
                y = y[:inds[0, 0]]
            outs.append(y)
        return outs
Att_BiGRU.py 文件源码 项目:NANHM-for-GEC 作者: shinochin 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def translate(self, xs, max_length=100):
        batch = len(xs)
        with chainer.no_backprop_mode(), chainer.using_config('train', False):
            xs_f = xs
            xs_b = [x[::-1] for x in xs]
            exs_f = sequence_embed(self.embed_x, xs_f)
            exs_b = sequence_embed(self.embed_x, xs_b)
            _, hf = self.encoder_f(None, exs_f)
            _, hb = self.encoder_b(None, exs_b)
            ht = list(map(lambda x,y: F.concat([x, y], axis=1), hf, hb))
            ys = self.xp.full(batch, EOS, 'i')
            result = []
            for i in range(max_length):
                eys = self.embed_y(ys)
                eys = chainer.functions.split_axis(eys, batch, 0)
                h_list, h_bar_list, c_s_list, z_s_list = self.decoder(None, ht, eys)
                cys = chainer.functions.concat(h_list, axis=0)
                wy = self.W(cys)
                ys = self.xp.argmax(wy.data, axis=1).astype('i')
                result.append(ys)

        result = cuda.to_cpu(self.xp.stack(result).T)

        # Remove EOS taggs
        outs = []
        for y in result:
            inds = np.argwhere(y == EOS)
            if len(inds) > 0:
                y = y[:inds[0, 0]]
            outs.append(y)
        return outs
BiGRU.py 文件源码 项目:NANHM-for-GEC 作者: shinochin 项目源码 文件源码 阅读 57 收藏 0 点赞 0 评论 0
def translate(self, xs, max_length=100):
        batch = len(xs)
        with chainer.no_backprop_mode(), chainer.using_config('train', False):
            xs_f = xs
            xs_b = [x[::-1] for x in xs]
            exs_f = sequence_embed(self.embed_x, xs_f)
            exs_b = sequence_embed(self.embed_x, xs_b)
            fx, _ = self.encoder_f(None, exs_f)
            bx, _ = self.encoder_b(None, exs_b)
            h = F.concat([fx, bx], axis=2)
            ys = self.xp.full(batch, EOS, 'i')
            result = []
            for i in range(max_length):
                eys = self.embed_y(ys)
                eys = chainer.functions.split_axis(eys, batch, 0)
                h, ys = self.decoder(h, eys)
                cys = chainer.functions.concat(ys, axis=0)
                wy = self.W(cys)
                ys = self.xp.argmax(wy.data, axis=1).astype('i')
                result.append(ys)

        result = cuda.to_cpu(self.xp.stack(result).T)

        # Remove EOS taggs
        outs = []
        for y in result:
            inds = np.argwhere(y == EOS)
            if len(inds) > 0:
                y = y[:inds[0, 0]]
            outs.append(y)
        return outs


问题


面经


文章

微信
公众号

扫码关注公众号