當前位置: 首頁>>代碼示例>>Python>>正文


Python chainer.using_config方法代碼示例

本文整理匯總了Python中chainer.using_config方法的典型用法代碼示例。如果您正苦於以下問題:Python chainer.using_config方法的具體用法?Python chainer.using_config怎麽用?Python chainer.using_config使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在chainer的用法示例。


在下文中一共展示了chainer.using_config方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: residual

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
def residual(self, x):
        h = x
        h = self.c1(h)
        if self.bn:
            h = self.b1(h)
        if self.activation:
            h = self.activation(h)
        if self.mode:
            h = self.mode(h)
        if self.dr:
            with chainer.using_config('train', True):
                h = F.dropout(h, self.dr)
        h = self.c2(h)
        if self.bn:
            h = self.b2(h)
        if self.activation:
            h = self.activation(h)
        return h 
開發者ID:pstuvwx,項目名稱:Deep_VoiceChanger,代碼行數:20,代碼來源:block.py

示例2: __call__

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
def __call__(self, x):
        if self.dr:
            with chainer.using_config('train', True):
                x = F.dropout(x, self.dr)
        if self.gap:
            x = F.sum(x, axis=(2,3))
        N = x.shape[0]
        #Below code copyed from https://github.com/pfnet-research/chainer-gan-lib/blob/master/minibatch_discrimination/net.py
        feature = F.reshape(F.leaky_relu(x), (N, -1))
        m = F.reshape(self.md(feature), (N, self.B * self.C, 1))
        m0 = F.broadcast_to(m, (N, self.B * self.C, N))
        m1 = F.transpose(m0, (2, 1, 0))
        d = F.absolute(F.reshape(m0 - m1, (N, self.B, self.C, N)))
        d = F.sum(F.exp(-F.sum(d, axis=2)), axis=2) - 1
        h = F.concat([feature, d])

        h = self.l(h)
        return h 
開發者ID:pstuvwx,項目名稱:Deep_VoiceChanger,代碼行數:20,代碼來源:block.py

示例3: act

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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.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)[0]
            else:
                action = chainer.cuda.to_cpu(
                    action_distrib.sample().array)[0]

        return action 
開發者ID:chainer,項目名稱:chainerrl,代碼行數:23,代碼來源:ppo.py

示例4: batch_act

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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] 
開發者ID:chainer,項目名稱:chainerrl,代碼行數:25,代碼來源:ddpg.py

示例5: sync_target_network

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
def sync_target_network(self):
        """Synchronize target network with current network."""
        if self.target_model is None:
            self.target_model = copy.deepcopy(self.model)
            call_orig = self.target_model.__call__

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

            self.target_model.__call__ = call_test
        else:
            synchronize_parameters(
                src=self.model,
                dst=self.target_model,
                method=self.target_update_method,
                tau=self.soft_update_tau) 
開發者ID:chainer,項目名稱:chainerrl,代碼行數:19,代碼來源:dqn.py

示例6: batch_act_and_train

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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 
開發者ID:chainer,項目名稱:chainerrl,代碼行數:22,代碼來源:dqn.py

示例7: act

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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 
開發者ID:chainer,項目名稱:chainerrl,代碼行數:23,代碼來源:trpo.py

示例8: batch_act

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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 
開發者ID:chainer,項目名稱:chainerrl,代碼行數:22,代碼來源:trpo.py

示例9: main

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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)) 
開發者ID:Pinafore,項目名稱:qb,代碼行數:24,代碼來源:test.py

示例10: compute_test_loss

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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 
開發者ID:fabiencro,項目名稱:knmt,代碼行數:21,代碼來源:encoder_decoder.py

示例11: compute_loss_all

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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 
開發者ID:fabiencro,項目名稱:knmt,代碼行數:20,代碼來源:evaluation.py

示例12: forward

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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) 
開發者ID:dsanno,項目名稱:chainer-image-caption,代碼行數:25,代碼來源:train.py

示例13: predict

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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 
開發者ID:vecto-ai,項目名稱:vecto,代碼行數:18,代碼來源:text_classification.py

示例14: get_vectors

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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 
開發者ID:vecto-ai,項目名稱:vecto,代碼行數:19,代碼來源:text_classification.py

示例15: out_generated_image

# 需要導入模塊: import chainer [as 別名]
# 或者: from chainer import using_config [as 別名]
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.array)
        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 
開發者ID:chainer,項目名稱:chainer,代碼行數:27,代碼來源:visualize.py


注:本文中的chainer.using_config方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。