我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用chainer.no_backprop_mode()。
def _compute_y_and_t(self, exp_batch, gamma): batch_size = exp_batch['reward'].shape[0] # Compute Q-values for current states batch_state = exp_batch['state'] 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, gamma), (batch_size, 1)) return batch_q, batch_q_target
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.data[0]) correction_advantage = action_value.q_values.data[0] - v return -F.sum(correction_weight * action_distrib.all_log_prob * correction_advantage, axis=1)
def compute_policy_gradient_sample_correction( action_distrib, action_distrib_mu, action_value, v, truncation_threshold): """Compute off-policy bias correction term wrt a sampled action.""" assert np.isscalar(v) assert truncation_threshold is not None with chainer.no_backprop_mode(): sample_action = action_distrib.sample().data rho_dash_inv = compute_importance( action_distrib_mu, action_distrib, sample_action) if (truncation_threshold > 0 and rho_dash_inv >= 1 / truncation_threshold): return chainer.Variable(np.asarray([0], dtype=np.float32)) correction_weight = max(0, 1 - truncation_threshold * rho_dash_inv) assert correction_weight <= 1 q = float(action_value.evaluate_actions(sample_action).data[0]) correction_advantage = q - v return -(correction_weight * action_distrib.log_prob(sample_action) * correction_advantage)
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.data) 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()
def __call__(self, trainer): with chainer.no_backprop_mode(): references = [] hypotheses = [] for i in range(0, len(self.test_data[0:100]), self.batch): sources, targets = zip(*self.test_data[i:i + self.batch]) references.extend([[t[0].tolist()] for t in targets]) sources = [ chainer.dataset.to_device(self.device, x) for x in sources] ys = self.model.translate(sources, self.max_length) ys = [y.tolist() for y in ys] hypotheses.extend(ys) source, target = zip(*self.test_data[0:100]) loss = self.model.CalculateValLoss(source, target) bleu = bleu_score.corpus_bleu( references, hypotheses, smoothing_function=bleu_score.SmoothingFunction().method1) reporter.report({self.key[0]: bleu}) reporter.report({self.key[1]: loss})
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})
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})
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")
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")
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")
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")
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
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))
def select_action(self, t, greedy_action_func, action_value=None): assert action_value is not None assert isinstance(action_value, chainerrl.action_value.DiscreteActionValue) n_actions = action_value.q_values.shape[1] with chainer.no_backprop_mode(): probs = chainer.cuda.to_cpu( F.softmax(action_value.q_values / self.T).data).ravel() return np.random.choice(np.arange(n_actions), p=probs)
def _compute_y_and_t(self, exp_batch, gamma): batch_state = exp_batch['state'] batch_size = len(exp_batch['reward']) qout = self.q_function(batch_state) batch_actions = exp_batch['action'] # Q(s_t,a_t) batch_q = F.reshape(qout.evaluate_actions( batch_actions), (batch_size, 1)) with chainer.no_backprop_mode(): # Compute target values target_qout = self.target_q_function(batch_state) # Q'(s_t,a_t) target_q = F.reshape(target_qout.evaluate_actions( batch_actions), (batch_size, 1)) # LQ'(s_t,a) target_q_expect = F.reshape( self._l_operator(target_qout), (batch_size, 1)) # r + g * LQ'(s_{t+1},a) batch_q_target = F.reshape( self._compute_target_values(exp_batch, gamma), (batch_size, 1)) # Q'(s_t,a_t) + r + g * LQ'(s_{t+1},a) - LQ'(s_t,a) t = target_q + batch_q_target - target_q_expect return batch_q, t
def act(self, obs): # Use the process-local model for acting with chainer.no_backprop_mode(): statevar = self.batch_states([obs], np, self.phi) pout, _ = self.model.pi_and_v(statevar) if self.act_deterministically: return pout.most_probable.data[0] else: return pout.sample().data[0]
def act(self, obs): # Use the process-local model for acting with chainer.no_backprop_mode(): statevar = self.batch_states([obs], self.xp, self.phi) action_distrib, _ = self.model(statevar) if self.act_deterministically: return chainer.cuda.to_cpu( action_distrib.most_probable.data)[0] else: return chainer.cuda.to_cpu(action_distrib.sample().data)[0]
def _compute_y_and_t(self, exp_batch, gamma): batch_state = exp_batch['state'] batch_size = len(exp_batch['reward']) qout = self.q_function(batch_state) batch_actions = exp_batch['action'] batch_q = qout.evaluate_actions(batch_actions) # Compute target values with chainer.no_backprop_mode(): target_qout = self.target_q_function(batch_state) batch_next_state = exp_batch['next_state'] with state_kept(self.target_q_function): target_next_qout = self.target_q_function( batch_next_state) next_q_max = F.reshape(target_next_qout.max, (batch_size,)) batch_rewards = exp_batch['reward'] batch_terminal = exp_batch['is_state_terminal'] # T Q: Bellman operator t_q = batch_rewards + self.gamma * \ (1.0 - batch_terminal) * next_q_max # T_PAL Q: persistent advantage learning operator cur_advantage = F.reshape( target_qout.compute_advantage(batch_actions), (batch_size,)) next_advantage = F.reshape( target_next_qout.compute_advantage(batch_actions), (batch_size,)) tpal_q = t_q + self.alpha * \ F.maximum(cur_advantage, next_advantage) return batch_q, tpal_q
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]
def update(self): xp = self.xp if self.standardize_advantages: all_advs = xp.array([b['adv'] for b in self.memory]) mean_advs = xp.mean(all_advs) std_advs = xp.std(all_advs) target_model = copy.deepcopy(self.model) dataset_iter = chainer.iterators.SerialIterator( self.memory, self.minibatch_size) dataset_iter.reset() while dataset_iter.epoch < self.epochs: batch = dataset_iter.__next__() states = batch_states([b['state'] for b in batch], xp, self.phi) actions = xp.array([b['action'] for b in batch]) distribs, vs_pred = self.model(states) with chainer.no_backprop_mode(): target_distribs, _ = target_model(states) advs = xp.array([b['adv'] for b in batch], dtype=xp.float32) if self.standardize_advantages: advs = (advs - mean_advs) / std_advs self.optimizer.update( self._lossfun, distribs, vs_pred, distribs.log_prob(actions), vs_pred_old=xp.array( [b['v_pred'] for b in batch], dtype=xp.float32), target_log_probs=target_distribs.log_prob(actions), advs=advs, vs_teacher=xp.array( [b['v_teacher'] for b in batch], dtype=xp.float32), )
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
def act(self, obs): with chainer.no_backprop_mode(): batch_obs = self.batch_states([obs], self.xp, self.phi) action_distrib = self.model(batch_obs) if self.act_deterministically: return chainer.cuda.to_cpu( action_distrib.most_probable.data)[0] else: return chainer.cuda.to_cpu(action_distrib.sample().data)[0]
def CalculateValLoss(self, xs, ys): with chainer.no_backprop_mode(), chainer.using_config('train', False): loss = self.CalcLoss(xs, ys) return loss.data
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
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
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, c, _ = self.encoder(None, 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, c, ys = self.decoder(h, c, 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
def CalculateValLoss(self, xs, ys): with chainer.no_backprop_mode(), chainer.using_config('train', False): loss, n_w, n_c, n_c_a = self.CalcLoss(xs, ys) return loss.data
def generate_text(model, seed, length=512, top_n=10): """ generates text of specified length from trained model with given seed character sequence. """ logger.info("generating %s characters from top %s choices.", length, top_n) logger.info('generating with seed: "%s".', seed) generated = seed encoded = encode_text(seed).astype(np.int32) model.predictor.reset_state() with chainer.using_config("train", False), chainer.no_backprop_mode(): for idx in encoded[:-1]: x = Variable(np.array([idx])) # input shape: [1] # set internal states model.predictor(x) next_index = encoded[-1] for i in range(length): x = Variable(np.array([next_index], dtype=np.int32)) # input shape: [1] probs = F.softmax(model.predictor(x)) # output shape: [1, vocab_size] next_index = sample_from_probs(probs.data.squeeze(), top_n) # append to sequence generated += ID2CHAR[next_index] logger.info("generated text: \n%s\n", generated) return generated
def feature(self, x): with chainer.no_backprop_mode(): return self.fe(x).data
def predict(self, f): with chainer.no_backprop_mode(): h = F.relu(f) h = F.relu(self.fc1(f)) y = self.fc2(h) return y.data
def get_greedy_action(Q, obs, show_f=False): xp = Q.xp obs = xp.expand_dims(xp.asarray(obs, dtype=np.float32), 0) with chainer.no_backprop_mode(): f = Q.feature(obs) q = Q.predict(f)[0] #q = Q(obs).data[0] if show_f: show_feature(f) return int(xp.argmax(q))
def update(Q, target_Q, opt, samples, gamma=0.99, target_type='double_dqn'): xp = Q.xp s = np.ndarray(shape=(minibatch_size, STATE_LENGTH, FRAME_WIDTH, FRAME_HEIGHT), dtype=np.float32) a = np.asarray([sample[1] for sample in samples], dtype=np.int32) r = np.asarray([sample[2] for sample in samples], dtype=np.float32) done = np.asarray([sample[3] for sample in samples], dtype=np.float32) s_next = np.ndarray(shape=(minibatch_size, STATE_LENGTH, FRAME_WIDTH, FRAME_HEIGHT), dtype=np.float32) for i in xrange(minibatch_size): s[i] = samples[i][0] s_next[i] = samples[i][4] # to gpu if available s = xp.asarray(s) a = xp.asarray(a) r = xp.asarray(r) done = xp.asarray(done) s_next = xp.asarray(s_next) # Prediction: Q(s,a) y = F.select_item(Q(s), a) # Target: r + gamma * max Q_b (s',b) with chainer.no_backprop_mode(): if target_type == 'dqn': t = r + gamma * (1 - done) * F.max(target_Q(s_next), axis=1) elif target_type == 'double_dqn': t = r + gamma * (1 - done) * F.select_item( target_Q(s_next), F.argmax(Q(s_next), axis=1)) else: raise ValueError('Unsupported target_type: {}'.format(target_type)) loss = mean_clipped_loss(y, t) Q.cleargrads() loss.backward() opt.update()
def get_greedy_action(Q, obs): xp = Q.xp obs = xp.expand_dims(xp.asarray(obs, dtype=np.float32), 0) with chainer.no_backprop_mode(): q = Q(obs).data[0] return int(xp.argmax(q))
def main(): args = parse_args() gen1 = net.Generator1() chainer.serializers.load_npz(args.model_path, gen1) device_id = None if args.gpu >= 0: device_id = args.gpu cuda.get_device(device_id).use() gen1.to_gpu(device_id) out_vector_path = None np.random.seed(1) if args.vector_file1 and args.vector_index1 >= 0 and args.vector_file2 and args.vector_index2 >= 0: with open(args.vector_file1, 'rb') as f: z = np.load(f) z1 = z[args.vector_index1] with open(args.vector_file2, 'rb') as f: z = np.load(f) z2 = z[args.vector_index2] w = np.arange(10).astype(np.float32).reshape((-1, 1)) / 9 z = (1 - w) * z1 + w * z2 z = z / (np.linalg.norm(z, axis=1, keepdims=True) + 1e-12) else: z = np.random.normal(0, 1, (100, latent_size)).astype(np.float32) out_vector_path = '{}.npy'.format(args.output) z = z / (np.linalg.norm(z, axis=1, keepdims=True) + 1e-12) with chainer.no_backprop_mode(): if device_id is None: x = gen1(z, train=False) else: x = gen1(cuda.to_gpu(z, device_id), train=False) x = cuda.to_cpu(x.data) batch, ch, h, w = x.shape x = x.reshape((-1, 10, ch, h, w)).transpose((0, 3, 1, 4, 2)).reshape((-1, 10 * w, ch)) x = ((x + 1) * 127.5).clip(0, 255).astype(np.uint8) Image.fromarray(x).save('{}.jpg'.format(args.output)) if out_vector_path: with open(out_vector_path, 'wb') as f: np.save(f, z)
def __call__(self, x): if self.g.data is None: if self.V.data is None: kh, kw = _pair(self.ksize) V_shape = (self.out_channels, x.shape[1], kh, kw) self.V.initialize(V_shape) xp = cuda.get_array_module(x) with chainer.no_backprop_mode(): t = convolution_2d(x, self.V, Variable(xp.full((self.out_channels, 1, 1, 1), 1.0).astype(x.dtype)), None, self.stride, self.pad) # compute output with g = 1 and without bias mean_t, std_t = self._initialize_params(t.data) return (t - mean_t) / std_t return convolution_2d(x, self.V, self.g, self.b, self.stride, self.pad)
def translate(self, x_block, max_length=50): # TODO: efficient inference by re-using convolution result with chainer.no_backprop_mode(): with chainer.using_config('train', False): # if isinstance(x_block, list): x_block = source_pad_concat_convert( x_block, device=None) batch, x_length = x_block.shape y_block = self.xp.zeros((batch, 1), dtype=x_block.dtype) eos_flags = self.xp.zeros((batch, ), dtype=x_block.dtype) result = [] for i in range(max_length): log_prob_tail = self(x_block, y_block, y_block, get_prediction=True) ys = self.xp.argmax(log_prob_tail.data, axis=1).astype('i') result.append(ys) y_block = F.concat([y_block, ys[:, None]], axis=1).data eos_flags += (ys == 0) if self.xp.all(eos_flags): break result = cuda.to_cpu(self.xp.stack(result).T) # Remove EOS taggs outs = [] for y in result: inds = np.argwhere(y == 0) if len(inds) > 0: y = y[:inds[0, 0]] if len(y) == 0: y = np.array([1], 'i') outs.append(y) return outs
def translate(self, xs, max_length=100): with chainer.no_backprop_mode(): with chainer.using_config('train', False): xs = [x[::-1] for x in xs] exs = sequence_embed(self.embed_x, xs) # Encode input sequence and send hidden stats to decoder. self.mn_encoder(exs) # Encoder does not return anything. # All evaluation will be done in decoder process. return None
def translate(self, xs, max_length=100): batch = len(xs) with chainer.no_backprop_mode(): with chainer.using_config('train', False): result = [] ys = self.xp.zeros(batch, 'i') eys = self.embed_y(ys) eys = chainer.functions.split_axis( eys, batch, 0, force_tuple=True) # Receive hidden stats from encoder process. h, c, ys, _ = self.mn_decoder(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) # Recursively decode using the previously predicted token. for i in range(1, max_length): eys = self.embed_y(ys) eys = chainer.functions.split_axis( eys, batch, 0, force_tuple=True) # Non-MN RNN link can be accessed via `actual_rnn`. h, c, ys = self.mn_decoder.actual_rnn(h, c, 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 = numpy.argwhere(y == 0) if len(inds) > 0: y = y[:inds[0, 0]] outs.append(y) return outs
def evaluate(self): bt = time.time() with chainer.no_backprop_mode(): references = [] hypotheses = [] observation = {} with reporter.report_scope(observation): for i in range(0, len(self.test_data), self.batch): src, trg = zip(*self.test_data[i:i + self.batch]) references.extend([[t.tolist()] for t in trg]) src = [chainer.dataset.to_device(self.device, x) for x in src] if self.comm.rank == 0: self.model.translate(src, self.max_length) elif self.comm.rank == 1: ys = [y.tolist() for y in self.model.translate( src, self.max_length)] hypotheses.extend(ys) if self.comm.rank == 1: bleu = bleu_score.corpus_bleu( references, hypotheses, smoothing_function=bleu_score. SmoothingFunction().method1) reporter.report({'bleu': bleu}, self.model) et = time.time() if self.comm.rank == 1: print("BleuEvaluator(single)::evaluate(): " "took {:.3f} [s]".format(et - bt)) sys.stdout.flush() return observation
def translate(self, xs, max_length=100): batch = len(xs) with chainer.no_backprop_mode(): with chainer.using_config('train', False): xs = [x[::-1] for x in xs] exs = sequence_embed(self.embed_x, xs) # Initial hidden variable and cell variable # zero = self.xp.zeros((self.n_layers, batch, self.n_units), 'f') # NOQA # h, c, _ = self.encoder(zero, zero, exs, train=False) # NOQA h, c, _ = self.encoder(None, None, exs) ys = self.xp.zeros(batch, 'i') result = [] for i in range(max_length): eys = self.embed_y(ys) eys = chainer.functions.split_axis( eys, batch, 0, force_tuple=True) h, c, ys = self.decoder(h, c, 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 = numpy.argwhere(y == 0) if len(inds) > 0: y = y[:inds[0, 0]] outs.append(y) return outs
def translate_with_beam_search(self, sentence: np.ndarray, max_length: int = 30, beam_width=3) -> 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]) heaps = [[] for _ in range(max_length + 1)] heaps[0].append((0, [EOS], hidden_states, cell_states)) # (score, translation, hidden_states, cell_states) solution = [] solution_score = 1e8 for i in range(max_length): heaps[i] = sorted(heaps[i], key=lambda t: t[0])[:beam_width] for score, translation, i_hidden_states, i_cell_states in heaps[i]: wid = translation[-1] output, new_hidden_states, new_cell_states = \ self._translate_one_word(wid, i_hidden_states, i_cell_states, attentions) for next_wid in np.argsort(output.data)[::-1]: if output.data[next_wid] < 1e-6: break next_score = score - np.log(output.data[next_wid]) if next_score > solution_score: break next_translation = translation + [next_wid] next_item = (next_score, next_translation, new_hidden_states, new_cell_states) if next_wid == EOS: if next_score < solution_score: solution = translation[1:] # [1:] drops first EOS solution_score = next_score else: heaps[i + 1].append(next_item) return solution