Python chainer.no_backprop_mode() Examples

The following are 30 code examples of chainer.no_backprop_mode(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer , or try the search function .
Example #1
Source File: acer.py    From chainerrl with MIT License 6 votes vote down vote up
def update_on_policy(self, statevar):
        assert self.t_start < self.t

        if not self.disable_online_update:
            if statevar is None:
                R = 0
            else:
                with chainer.no_backprop_mode():
                    with state_kept(self.model):
                        action_distrib, action_value, v = self.model(statevar)
                R = float(v.array)
            self.update(
                t_start=self.t_start, t_stop=self.t, R=R,
                states=self.past_states,
                actions=self.past_actions,
                rewards=self.past_rewards,
                values=self.past_values,
                action_values=self.past_action_values,
                action_distribs=self.past_action_distrib,
                action_distribs_mu=None,
                avg_action_distribs=self.past_avg_action_distrib)

        self.init_history_data_for_online_update() 
Example #2
Source File: dqn_cartpole.py    From chainer with MIT License 6 votes vote down vote up
def update(Q, target_Q, opt, samples, gamma=0.99, target_type='double_dqn'):
    """Update a Q-function with given samples and a target Q-function."""
    dtype = chainer.get_dtype()
    xp = Q.xp
    obs = xp.asarray([sample[0] for sample in samples], dtype=dtype)
    action = xp.asarray([sample[1] for sample in samples], dtype=np.int32)
    reward = xp.asarray([sample[2] for sample in samples], dtype=dtype)
    done = xp.asarray([sample[3] for sample in samples], dtype=dtype)
    obs_next = xp.asarray([sample[4] for sample in samples], dtype=dtype)
    # Predicted values: Q(s,a)
    y = F.select_item(Q(obs), action)
    # Target values: r + gamma * max_b Q(s',b)
    with chainer.no_backprop_mode():
        if target_type == 'dqn':
            next_q = F.max(target_Q(obs_next), axis=1)
        elif target_type == 'double_dqn':
            next_q = F.select_item(target_Q(obs_next),
                                   F.argmax(Q(obs_next), axis=1))
        else:
            raise ValueError('Unsupported target_type: {}'.format(target_type))
        target = reward + gamma * (1 - done) * next_q
    loss = mean_clipped_loss(y, target)
    Q.cleargrads()
    loss.backward()
    opt.update() 
Example #3
Source File: model.py    From chainer with MIT License 6 votes vote down vote up
def forward(self, imgs, captions):
        """Batch of images to a single loss."""
        imgs = Variable(imgs)
        if self.finetune_feat_extractor:
            img_feats = self.feat_extractor(imgs)
        else:
            # Extract features with the `train` configuration set to `False` in
            # order to basically skip the dropout regularizations. This is how
            # dropout is used during standard inference. Also, since we are not
            # going to optimize the feature extractor, we explicitly set the
            # backpropgation mode to not construct any computational graphs.
            with chainer.using_config('train', False), \
                    chainer.no_backprop_mode():
                img_feats = self.feat_extractor(imgs)

        loss = self.lang_model(img_feats, captions)

        # Report the loss so that it can be printed, logged and plotted by
        # other trainer extensions
        reporter.report({'loss': loss}, self)

        return loss 
Example #4
Source File: run_text_classifier.py    From chainer with MIT License 6 votes vote down vote up
def run_online(device):
    # predict labels online
    print('Enter inputs for Online Predictions')
    for l in sys.stdin:
        l = l.strip()
        if not l:
            print('# blank line')
            continue
        text = nlp_utils.normalize_text(l)
        words = nlp_utils.split_text(text, char_based=setup['char_based'])
        xs = nlp_utils.transform_to_array([words], vocab, with_label=False)
        xs = nlp_utils.convert_seq(xs, device=device, with_label=False)
        with chainer.using_config('train', False), chainer.no_backprop_mode():
            prob = model.predict(xs, softmax=True)[0]
        answer = int(model.xp.argmax(prob))
        score = float(prob[answer])
        print('{}\t{:.4f}\t{}'.format(answer, score, ' '.join(words))) 
Example #5
Source File: ppo.py    From chainerrl with MIT License 6 votes vote down vote up
def batch_act(self, batch_obs):
        xp = self.xp
        b_state = self.batch_states(batch_obs, xp, self.phi)

        if self.obs_normalizer:
            b_state = self.obs_normalizer(b_state, update=False)

        with chainer.using_config('train', False), chainer.no_backprop_mode():
            if self.recurrent:
                (action_distrib, _), self.test_recurrent_states = self.model(
                    b_state, self.test_recurrent_states)
            else:
                action_distrib, _ = self.model(b_state)
            if self.act_deterministically:
                action = chainer.cuda.to_cpu(
                    action_distrib.most_probable.array)
            else:
                action = chainer.cuda.to_cpu(action_distrib.sample().array)

        return action 
Example #6
Source File: seq2seq.py    From chainer with MIT License 6 votes vote down vote up
def __call__(self, trainer):
        with chainer.no_backprop_mode():
            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)
        reporter.report({self.key: bleu}) 
Example #7
Source File: ddpg.py    From chainerrl with MIT License 6 votes vote down vote up
def batch_act(self, batch_obs):
        """Select a batch of actions for evaluation.

        Args:
            batch_obs (Sequence of ~object): Observations.

        Returns:
            Sequence of ~object: Actions.
        """

        with chainer.using_config('train', False), chainer.no_backprop_mode():
            batch_xs = self.batch_states(batch_obs, self.xp, self.phi)
            batch_action = self.policy(batch_xs).sample()
            # Q is not needed here, but log it just for information
            q = self.q_function(batch_xs, batch_action)

        # Update stats
        self.average_q *= self.average_q_decay
        self.average_q += (1 - self.average_q_decay) * float(
            q.array.mean(axis=0))
        self.logger.debug('t:%s a:%s q:%s',
                          self.t, batch_action.array[0], q.array)
        return [cuda.to_cpu(action.array) for action in batch_action] 
Example #8
Source File: acer.py    From chainerrl with MIT License 6 votes vote down vote up
def compute_policy_gradient_full_correction(
        action_distrib, action_distrib_mu, action_value, v,
        truncation_threshold):
    """Compute off-policy bias correction term wrt all actions."""
    assert truncation_threshold is not None
    assert np.isscalar(v)
    with chainer.no_backprop_mode():
        rho_all_inv = compute_full_importance(action_distrib_mu,
                                              action_distrib)
        correction_weight = (
            np.maximum(1 - truncation_threshold * rho_all_inv,
                       np.zeros_like(rho_all_inv)) *
            action_distrib.all_prob.array[0])
        correction_advantage = action_value.q_values.array[0] - v
    return -F.sum(correction_weight *
                  action_distrib.all_log_prob *
                  correction_advantage, axis=1) 
Example #9
Source File: seq2seq.py    From chainer with MIT License 6 votes vote down vote up
def __call__(self, trainer):
        device = self.device

        with chainer.no_backprop_mode():
            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 = [device.send(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)
        chainer.report({self.key: bleu}) 
Example #10
Source File: dqn.py    From chainerrl with MIT License 6 votes vote down vote up
def _compute_y_and_t(self, exp_batch):
        batch_size = exp_batch['reward'].shape[0]

        # Compute Q-values for current states
        batch_state = exp_batch['state']

        if self.recurrent:
            qout, _ = self.model.n_step_forward(
                batch_state,
                exp_batch['recurrent_state'],
                output_mode='concat',
            )
        else:
            qout = self.model(batch_state)

        batch_actions = exp_batch['action']
        batch_q = F.reshape(qout.evaluate_actions(
            batch_actions), (batch_size, 1))

        with chainer.no_backprop_mode():
            batch_q_target = F.reshape(
                self._compute_target_values(exp_batch),
                (batch_size, 1))

        return batch_q, batch_q_target 
Example #11
Source File: dqn.py    From chainerrl with MIT License 6 votes vote down vote up
def batch_act_and_train(self, batch_obs):
        with chainer.using_config('train', False), chainer.no_backprop_mode():
            batch_av = self._evaluate_model_and_update_recurrent_states(
                batch_obs, test=False)
            batch_maxq = batch_av.max.array
            batch_argmax = cuda.to_cpu(batch_av.greedy_actions.array)
        batch_action = [
            self.explorer.select_action(
                self.t, lambda: batch_argmax[i],
                action_value=batch_av[i:i + 1],
            )
            for i in range(len(batch_obs))]
        self.batch_last_obs = list(batch_obs)
        self.batch_last_action = list(batch_action)

        # Update stats
        self.average_q *= self.average_q_decay
        self.average_q += (1 - self.average_q_decay) * float(batch_maxq.mean())

        return batch_action 
Example #12
Source File: text_classification.py    From vecto with Mozilla Public License 2.0 6 votes vote down vote up
def get_vectors(model, sentences):
    model, vocab, setup = model
    vectors = []
    for sentence in sentences:
        sentence = sentence.strip()
        text = nlp_utils.normalize_text(sentence)
        if setup['char_based']:
            words = list(text)
        else:
            words = word_tokenize_txt(text)
        xs = nlp_utils.transform_to_array([words], vocab, with_label=False)
        xs = nlp_utils.convert_seq(xs, device=-1, with_label=False)  # todo use GPU
        with chainer.using_config('train', False), chainer.no_backprop_mode():
            vector = model.encoder(xs)
            vectors.append(vector.data[0])
    vectors = numpy.asarray(vectors)
    return vectors 
Example #13
Source File: iqn.py    From chainerrl with MIT License 6 votes vote down vote up
def _compute_loss(self, exp_batch, errors_out=None):
        """Compute a loss.

        Returns:
            Returns:
                chainer.Variable: Scalar loss.
        """
        y, taus = self._compute_y_and_taus(exp_batch)
        with chainer.no_backprop_mode():
            t = self._compute_target_values(exp_batch)

        eltwise_loss = compute_eltwise_huber_quantile_loss(y, t, taus)
        if errors_out is not None:
            del errors_out[:]
            delta = F.mean(eltwise_loss, axis=(1, 2))
            errors_out.extend(cuda.to_cpu(delta.array))

        if 'weights' in exp_batch:
            return compute_weighted_value_loss(
                eltwise_loss, exp_batch['weights'],
                batch_accumulator=self.batch_accumulator)
        else:
            return compute_value_loss(
                eltwise_loss, batch_accumulator=self.batch_accumulator) 
Example #14
Source File: text_classification.py    From vecto with Mozilla Public License 2.0 6 votes vote down vote up
def predict(model, sentence):
    model, vocab, setup = model
    sentence = sentence.strip()
    text = nlp_utils.normalize_text(sentence)
    # words = nlp_utils.split_text(text, char_based=setup['char_based'])
    if setup['char_based']:
        words = list(text)
    else:
        words = word_tokenize_txt(text)
    xs = nlp_utils.transform_to_array([words], vocab, with_label=False)
    xs = nlp_utils.convert_seq(xs, device=-1, with_label=False)  # todo use GPU
    with chainer.using_config('train', False), chainer.no_backprop_mode():
        prob = model.predict(xs, softmax=True)[0]
    answer = int(model.xp.argmax(prob))
    score = float(prob[answer])
    return answer, score 
Example #15
Source File: evaluation.py    From knmt with GNU General Public License v3.0 6 votes vote down vote up
def compute_loss_all(encdec, test_data, eos_idx, mb_size, gpu=None, reverse_src=False, reverse_tgt=False,
                        use_chainerx=False):
    with chainer.using_config("train", False), chainer.no_backprop_mode():
        if encdec.encdec_type() == "ff":
            assert not reverse_src and not reverse_tgt
            return encdec.compute_test_loss(test_data, mb_size=mb_size, nb_mb_for_sorting=20)
        
        mb_provider_test = minibatch_provider(test_data, eos_idx, mb_size, nb_mb_for_sorting=-1, loop=False,
                                              gpu=gpu,
                                              reverse_src=reverse_src, reverse_tgt=reverse_tgt, use_chainerx=use_chainerx)
        test_loss = 0
        test_nb_predictions = 0
        for src_batch, tgt_batch, src_mask in mb_provider_test:
            loss, attn = encdec(src_batch, tgt_batch, src_mask, raw_loss_info=True)
            test_loss += loss[0].data
            test_nb_predictions += loss[1]
        test_loss /= test_nb_predictions
        return test_loss 
Example #16
Source File: encoder_decoder.py    From knmt with GNU General Public License v3.0 6 votes vote down vote up
def compute_test_loss(self, test_data, mb_size=64, nb_mb_for_sorting= 20):
        def mb_provider():
            required_data = nb_mb_for_sorting * mb_size
            cursor = 0
            while cursor < len(test_data):
                larger_batch = test_data[cursor:cursor+required_data]
                cursor += required_data
                for minibatch in batch_sort_and_split(larger_batch, size_parts = mb_size):
                    yield six.moves.zip(*minibatch)
        
        with chainer.using_config("train", False), chainer.no_backprop_mode():
            total_loss = 0
            total_nb_predictions = 0.0     
            for src_batch, tgt_batch in mb_provider():
                loss = self.compute_loss(src_batch, tgt_batch, reduce="no")
                nb_tgt_words = sum(len(seq) + 1 for seq in tgt_batch) # +1 for eos
                total_loss += self.xp.sum(loss.data)
                total_nb_predictions += nb_tgt_words
            return total_loss / total_nb_predictions 
Example #17
Source File: trpo.py    From chainerrl with MIT License 6 votes vote down vote up
def act(self, obs):
        xp = self.xp
        b_state = self.batch_states([obs], xp, self.phi)

        if self.obs_normalizer:
            b_state = self.obs_normalizer(b_state, update=False)

        with chainer.using_config('train', False), chainer.no_backprop_mode():
            if self.recurrent:
                action_distrib, self.test_recurrent_states =\
                    self.policy(b_state, self.test_recurrent_states)
            else:
                action_distrib = self.policy(b_state)
            if self.act_deterministically:
                action = chainer.cuda.to_cpu(
                    action_distrib.most_probable.array)[0]
            else:
                action = chainer.cuda.to_cpu(
                    action_distrib.sample().array)[0]

        return action 
Example #18
Source File: trpo.py    From chainerrl with MIT License 6 votes vote down vote up
def batch_act(self, batch_obs):
        xp = self.xp
        b_state = self.batch_states(batch_obs, xp, self.phi)

        if self.obs_normalizer:
            b_state = self.obs_normalizer(b_state, update=False)

        with chainer.using_config('train', False), chainer.no_backprop_mode():
            if self.recurrent:
                (action_distrib, _), self.test_recurrent_states = self.model(
                    b_state, self.test_recurrent_states)
            else:
                action_distrib, _ = self.model(b_state)
            if self.act_deterministically:
                action = chainer.cuda.to_cpu(
                    action_distrib.most_probable.array)
            else:
                action = chainer.cuda.to_cpu(action_distrib.sample().array)

        return action 
Example #19
Source File: test.py    From qb with MIT License 6 votes vote down vote up
def main():
    setup_dir = 'result/nn_guesser/args.json'
    model, vocab, answers, args = setup_model(setup_dir)

    questions = QuestionDatabase().all_questions().values()
    questions = [q for q in questions if q.fold == GUESSER_DEV_FOLD]
    percentages = [0.1, 0.25, 0.5, 0.75, 1.0]
    results = [[] for _ in percentages]
    
    for q in tqdm(questions):
        text = nlp(q.flatten_text())
        for i, per in enumerate(percentages):
            t = text[:int(len(text) * per)]
            t = [w.lower_ for w in t if w.is_alpha or w.is_digit]
            xs = nlp_utils.transform_to_array([t], vocab, with_label=False)
            xs = nlp_utils.convert_seq(xs, device=args.gpu, with_label=False)
            with chainer.using_config('train', False), chainer.no_backprop_mode():
                prob = model.predict(xs, softmax=True)[0]
            guess = answers[int(model.xp.argmax(prob))]
            results[i].append(guess == q.page)
    for i, rs in enumerate(results):
        print(percentages[i], sum(rs) / len(rs)) 
Example #20
Source File: encoder_decoder.py    From knmt with GNU General Public License v3.0 5 votes vote down vote up
def greedy_translate(self, src_seq, nb_steps, cut_eos=True, sample=False):
        with chainer.using_config("train", False), chainer.no_backprop_mode():
            encoded_source, src_mask = self.encoder(src_seq)
            decoding_cell = self.decoder.get_conditionalized_cell(encoded_source, src_mask)
            
            logits, decoder_state = decoding_cell.get_initial_logits()
            
            mb_size = len(src_seq)
            result = [[] for _ in six.moves.range(mb_size)]
            finished = [False for _ in six.moves.range(mb_size)]
            
            num_step = 0
            while 1:
                if sample:
                    logits = logits + self.xp.random.gumbel(size=logits.data.shape).astype(self.xp.float32)
                
    #             print "logits shape", logits.shape
                prev_word = self.xp.argmax(logits.data, axis = 1).reshape(-1, 1).astype(self.xp.int32)
    #             print "prev w shape", prev_word.shape
                assert prev_word.shape == (mb_size, 1)
                for i in six.moves.range(mb_size):
                    if not finished[i]:
                        if cut_eos and prev_word[i, 0] == self.decoder.eos_idx:
                            finished[i] = True
                            continue
                        result[i].append(prev_word[i, 0])
                
                prev_word = self.xp.where(prev_word == self.decoder.eos_idx, 0, prev_word)
                num_step += 1
                if num_step > nb_steps:
                    break
                logits, decoder_state = decoding_cell(decoder_state, prev_word)
                
            result = [[int(x) for x in seq] for seq in result]
            return result 
Example #21
Source File: train_vae.py    From chainer with MIT License 5 votes vote down vote up
def save_images(device, encoder, decoder, train, test, prior, out_dir):

    # Training samples
    train_ind = [1, 3, 5, 10, 2, 0, 13, 15, 17]
    x = device.send(np.asarray(train[train_ind]))
    with chainer.using_config('train', False):
        with chainer.no_backprop_mode():
            z = encoder(x).mean
            y = decoder(z, inference=True).mean
            y = y.array
    save3x3(x, os.path.join(out_dir, 'train'))
    save3x3(y, os.path.join(out_dir, 'train_reconstructed'))

    # Test samples
    test_ind = [3, 2, 1, 18, 4, 8, 11, 17, 61]
    x = device.send(np.asarray(test[test_ind]))
    with chainer.using_config('train', False):
        with chainer.no_backprop_mode():
            z = encoder(x).mean
            y = decoder(z, inference=True).mean
            y = y.array
    save3x3(x, os.path.join(out_dir, 'test'))
    save3x3(y, os.path.join(out_dir, 'test_reconstructed'))

    # Draw images from 9 randomly sampled values of z
    z = prior().sample(9)
    with chainer.using_config('train', False):
        with chainer.no_backprop_mode():
            y = decoder(z, inference=True).mean
            y = y.array
    save3x3(y, os.path.join(out_dir, 'sampled')) 
Example #22
Source File: test_function.py    From chainer with MIT License 5 votes vote down vote up
def test_force_backprop_mode(self):
        with chainer.no_backprop_mode():
            with chainer.force_backprop_mode():
                y = self.x + 1
        self.assertTrue(y.creator_node is not None)

        y = self.x + 1
        self.assertTrue(y.creator_node is not None)

        with chainer.force_backprop_mode():
            y = self.x + 1
        self.assertTrue(y.creator_node is not None) 
Example #23
Source File: test_ctc.py    From chainer with MIT License 5 votes vote down vote up
def test_no_backprop_mode(self):
        xs_data = numpy.random.uniform(-1, 1, (4, 2, 3)).astype(numpy.float32)
        t_data = numpy.array([[0, 1], [1, 0]]).astype(numpy.int32)
        with chainer.no_backprop_mode():
            x = [chainer.Variable(x_data) for x_data in xs_data]
            t = chainer.Variable(t_data)
            functions.connectionist_temporal_classification(x, t, 2) 
Example #24
Source File: trainer.py    From Deep_VoiceChanger with MIT License 5 votes vote down vote up
def preview_convert(iterator_a, iterator_b, g_a, g_b, device, gla, dst):
    @chainer.training.make_extension()
    def make_preview(trainer):
        with chainer.using_config('train', False):
            with chainer.no_backprop_mode():
                x_a = iterator_a.next()
                x_a = convert.concat_examples(x_a, device)
                x_a = chainer.Variable(x_a)

                x_b = iterator_b.next()
                x_b = convert.concat_examples(x_b, device)
                x_b = chainer.Variable(x_b)

                x_ab = g_a(x_a)
                x_ba = g_b(x_b)

                x_bab = g_a(x_ba)
                x_aba = g_b(x_ab)

                preview_dir = '{}/preview'.format(dst)
                if not os.path.exists(preview_dir):
                    os.makedirs(preview_dir)
                image_dir = '{}/image'.format(dst)
                if not os.path.exists(image_dir):
                    os.makedirs(image_dir)

                names = ['a', 'ab', 'aba', 'b', 'ba', 'bab']
                images = [x_a, x_ab, x_aba, x_b, x_ba, x_bab]
                for n, i in zip(names, images):
                    i = cp.asnumpy(i.data)[:,:,padding:-padding,:].reshape(1, -1, 128)
                    image.save(image_dir+'/{}{}.jpg'.format(trainer.updater.epoch,n), i)
                    w = np.concatenate([gla.inverse(_i) for _i in dataset.reverse(i)])
                    dataset.save(preview_dir+'/{}{}.wav'.format(trainer.updater.epoch,n), 16000, w)

    return make_preview 
Example #25
Source File: test_function.py    From chainer with MIT License 5 votes vote down vote up
def test_no_backprop_mode(self):
        y = self.x + 1
        self.assertTrue(y.creator_node is not None)

        with chainer.no_backprop_mode():
            y = self.x + 1
        self.assertTrue(y.creator_node is None)

        y = self.x + 1
        self.assertTrue(y.creator_node is not None) 
Example #26
Source File: inception_resnet_v2.py    From nips17-adversarial-attack with MIT License 5 votes vote down vote up
def load_inception_resnet_v2(checkpoint_path, enable_aux=False):
    model = InceptionResnetV2(enable_aux=enable_aux)
    with chainer.no_backprop_mode():
        model(np.random.randn(2, 3, 299, 299).astype('f'))  # initialize params
    if _tf_import_error is not None:
        raise RuntimeError('could not import tensorflow; the import error as follows:\n' + str(_tf_import_error))
    reader = pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
    model.load_tf_checkpoint(reader, 'InceptionResnetV2')
    return model 
Example #27
Source File: nets.py    From contextual_augmentation with MIT License 5 votes vote down vote up
def predict(self, xs, labels=None):
        with chainer.using_config('train', False), chainer.no_backprop_mode():
            t_out_concat = self.encode(xs, labels=labels, add_original=0.)
            prob_concat = F.softmax(self.output.output(t_out_concat)).data
            x_len = [len(x) for x in xs]
            x_section = np.cumsum(x_len[:-1])
            ps = np.split(cuda.to_cpu(prob_concat), x_section, 0)
        return ps 
Example #28
Source File: test_function_node.py    From chainer with MIT License 5 votes vote down vote up
def test_backprop_mode_affects_chainerx(self):
        # chainer.{no,force}_backprop_mode should affect chainerx's
        # counterpart.

        assert chainerx.is_backprop_required()

        # nobp
        with chainer.no_backprop_mode():
            assert not chainerx.is_backprop_required()

            # nobp > forcebp
            with chainer.force_backprop_mode():
                assert chainerx.is_backprop_required()

            # nobp > nobp
            with chainer.no_backprop_mode():
                assert not chainerx.is_backprop_required()

        assert chainerx.is_backprop_required()

        # forcebp
        with chainer.force_backprop_mode():
            assert chainerx.is_backprop_required()

            # forcebp > forcebp
            with chainer.force_backprop_mode():
                assert chainerx.is_backprop_required()

            # forcebp > nobp
            with chainer.no_backprop_mode():
                assert not chainerx.is_backprop_required()

        assert chainerx.is_backprop_required() 
Example #29
Source File: test_function_node.py    From chainer with MIT License 5 votes vote down vote up
def test_force_backprop_mode(self):
        with chainer.no_backprop_mode():
            with chainer.force_backprop_mode():
                y = self.x + 1
        self.assertTrue(y.creator_node is not None)

        y = self.x + 1
        self.assertTrue(y.creator_node is not None)

        with chainer.force_backprop_mode():
            y = self.x + 1
        self.assertTrue(y.creator_node is not None) 
Example #30
Source File: evaluation.py    From knmt with GNU General Public License v3.0 5 votes vote down vote up
def sample_once(encdec, src_batch, tgt_batch, src_mask, src_indexer, tgt_indexer, eos_idx, max_nb=None,
                s_unk_tag="#S_UNK#", t_unk_tag="#T_UNK#"):
    with chainer.using_config("train", False), chainer.no_backprop_mode():
        print("sample")
        sample_greedy, score, attn_list = encdec(src_batch, 50, src_mask, use_best_for_sample=True, need_score=True)
    
    
    #                 sample, score = encdec(src_batch, 50, src_mask, use_best_for_sample = False)
        assert len(src_batch[0].data) == len(tgt_batch[0].data)
        assert len(sample_greedy[0]) == len(src_batch[0].data)
    
        debatched_src = de_batch(src_batch, mask=src_mask, eos_idx=None, is_variable=True)
        debatched_tgt = de_batch(tgt_batch, eos_idx=eos_idx, is_variable=True)
        debatched_sample = de_batch(sample_greedy, eos_idx=eos_idx)
    
        sample_random, score_random, attn_list_random = encdec(src_batch, 50, src_mask, use_best_for_sample=False, need_score=True)
        
        
        
        debatched_sample_random = de_batch(sample_random, eos_idx=eos_idx)
    
        for sent_num in six.moves.range(len(debatched_src)):
            if max_nb is not None and sent_num > max_nb:
                break
            src_idx_seq = debatched_src[sent_num]
            tgt_idx_seq = debatched_tgt[sent_num]
            sample_idx_seq = debatched_sample[sent_num]
            sample_random_idx_seq = debatched_sample_random[sent_num]
    
            print("sent num", sent_num)
    
            for name, seq, unk_tag, indexer, this_eos_idx in six.moves.zip("src tgt sample sample_random".split(" "),
                                                                 [src_idx_seq, tgt_idx_seq, sample_idx_seq, sample_random_idx_seq],
                                                                 [s_unk_tag, t_unk_tag, t_unk_tag, t_unk_tag],
                                                                 [src_indexer, tgt_indexer, tgt_indexer, tgt_indexer],
                                                                 [None, eos_idx, eos_idx, eos_idx]):
                print(name, "idx:", seq)
                print(name, "raw:", " ".join(indexer.deconvert_swallow(seq, unk_tag=unk_tag, eos_idx=this_eos_idx)).encode('utf-8'))
                print(name, "postp:", indexer.deconvert(seq, unk_tag=unk_tag, eos_idx=this_eos_idx).encode('utf-8'))