Python chainer.using_config() Examples
The following are 30
code examples of chainer.using_config().
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: evaluation.py From knmt with GNU General Public License v3.0 | 6 votes |
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 #2
Source File: block.py From Deep_VoiceChanger with MIT License | 6 votes |
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
Example #3
Source File: ppo.py From chainerrl with MIT License | 6 votes |
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
Example #4
Source File: visualize.py From chainer with MIT License | 6 votes |
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.backends.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
Example #5
Source File: block.py From Deep_VoiceChanger with MIT License | 6 votes |
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
Example #6
Source File: ddpg.py From chainerrl with MIT License | 6 votes |
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 #7
Source File: model.py From chainer with MIT License | 6 votes |
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 #8
Source File: visualize.py From chainer with MIT License | 6 votes |
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
Example #9
Source File: dqn.py From chainerrl with MIT License | 6 votes |
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)
Example #10
Source File: dqn.py From chainerrl with MIT License | 6 votes |
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 #11
Source File: text_classification.py From vecto with Mozilla Public License 2.0 | 6 votes |
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 #12
Source File: text_classification.py From vecto with Mozilla Public License 2.0 | 6 votes |
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 #13
Source File: trpo.py From chainerrl with MIT License | 6 votes |
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 #14
Source File: trpo.py From chainerrl with MIT License | 6 votes |
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 #15
Source File: train.py From chainer-image-caption with MIT License | 6 votes |
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)
Example #16
Source File: test.py From qb with MIT License | 6 votes |
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 #17
Source File: encoder_decoder.py From knmt with GNU General Public License v3.0 | 6 votes |
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 #18
Source File: visualize.py From tensorboardX with MIT License | 5 votes |
def out_generated_image(gen, dis, rows, cols, seed, dst, writer): @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) writer.add_image('img', x, trainer.updater.iteration) return make_image
Example #19
Source File: utils.py From imgclsmob with MIT License | 5 votes |
def __call__(self, imgs): imgs = self.model.xp.asarray([self.do_transform(img) for img in imgs]) with using_config("train", False), no_backprop_mode(): imgs = Variable(imgs) predictions = self.model(imgs) output = to_cpu(predictions.array if hasattr(predictions, "array") else cupy.asnumpy(predictions)) return output
Example #20
Source File: resnet50.py From chainer with MIT License | 5 votes |
def __init__(self): super(ResNet50_Nhwc, self).__init__() with self.init_scope(): self.conv1 = L.Convolution2D( 3, 64, 7, 2, 3, initialW=initializers.HeNormal()) self.bn1 = L.BatchNormalization(64) with chainer.using_config('compute_mode', 'cudnn_fast'): self.res2 = Block(3, 64, 64, 256, 1) self.res3 = Block(4, 256, 128, 512) self.res4 = Block(6, 512, 256, 1024) self.res5 = Block(3, 1024, 512, 2048) self.fc = L.Linear(2048, 1000)
Example #21
Source File: cifar1.py From imgclsmob with MIT License | 5 votes |
def predict(self, imgs): imgs = self.xp.asarray([self._preprocess(img) for img in imgs]) with chainer.using_config('train', False), chainer.function.no_backprop_mode(): imgs = chainer.Variable(imgs) predictions = self.model(imgs) output = chainer.backends.cuda.to_cpu(predictions.array) return output
Example #22
Source File: test_init.py From chainer with MIT License | 5 votes |
def test_valid_case_combination(self): with chainer.using_config('use_cudnn', 'always'): self.assertTrue(chainer.should_use_cudnn('==always')) self.assertTrue(chainer.should_use_cudnn('>=auto')) with chainer.using_config('use_cudnn', 'auto'): self.assertFalse(chainer.should_use_cudnn('==always')) self.assertTrue(chainer.should_use_cudnn('>=auto')) with chainer.using_config('use_cudnn', 'never'): self.assertFalse(chainer.should_use_cudnn('==always')) self.assertFalse(chainer.should_use_cudnn('>=auto'))
Example #23
Source File: test_init.py From chainer with MIT License | 5 votes |
def test_no_cudnn_available(self): with chainer.using_config('use_cudnn', 'always'): self.assertFalse(chainer.should_use_cudnn('==always')) self.assertFalse(chainer.should_use_cudnn('>=auto'))
Example #24
Source File: imagenet1k1.py From imgclsmob with MIT License | 5 votes |
def predict(self, imgs): imgs = self.xp.asarray([self._preprocess(img) for img in imgs]) with chainer.using_config('train', False), chainer.function.no_backprop_mode(): imgs = chainer.Variable(imgs) predictions = self.model(imgs) output = chainer.backends.cuda.to_cpu(predictions.array) return output
Example #25
Source File: seg_utils1.py From imgclsmob with MIT License | 5 votes |
def predict(self, imgs): imgs = self.xp.asarray([self._preprocess(img) for img in imgs]) with chainer.using_config("train", False), chainer.function.no_backprop_mode(): imgs = chainer.Variable(imgs) predictions = self.model(imgs) output = chainer.backends.cuda.to_cpu(predictions.array) # output = np.argmax(output, axis=1).astype(np.int32) return output
Example #26
Source File: nets.py From contextual_augmentation with MIT License | 5 votes |
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 #27
Source File: test_init.py From chainer with MIT License | 5 votes |
def test_invalid_config(self): with chainer.using_config('use_cudnn', True): self.assertRaises(ValueError, chainer.should_use_cudnn, '>=auto') with chainer.using_config('use_cudnn', False): self.assertRaises(ValueError, chainer.should_use_cudnn, '>=auto') with chainer.using_config('use_cudnn', 'on'): self.assertRaises(ValueError, chainer.should_use_cudnn, '>=auto')
Example #28
Source File: evaluation.py From knmt with GNU General Public License v3.0 | 5 votes |
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'))
Example #29
Source File: test_init.py From chainer with MIT License | 5 votes |
def test_higher_version_required(self): with chainer.using_config('use_cudnn', 'always'): self.assertFalse(chainer.should_use_cudnn( '>=auto', cuda.cuda.cudnn.getVersion() + 1))
Example #30
Source File: test_init.py From chainer with MIT License | 5 votes |
def test_custom_init(self): with chainer.using_config('dtype', 'float16'): array = self._generate_array(numpy) self.assertEqual('float16', array.dtype)